Updated IStatement and IExpression classes to fit the method calls
This commit is contained in:
parent
5fa6664850
commit
ab19751146
@ -4,6 +4,7 @@ import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckHelper;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Datatype.IDatatype;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
|
@ -7,6 +7,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockStatement extends AbstractType implements IStatement {
|
||||
@ -42,20 +43,20 @@ public class BlockStatement extends AbstractType implements IStatement{
|
||||
}
|
||||
result.type = this.returnType;
|
||||
// todo check if the block returns the needed return type in every case
|
||||
// todo ignore unreadchable statements?
|
||||
// todo ignore unreachable statements?
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
// Create a new HashMap for the local variables of the block
|
||||
// It has every variable of the parent block
|
||||
// This Map is discarded at the end of the block
|
||||
//TODO: Do this for every block --> if, while, ...
|
||||
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
|
||||
LinkedHashMap<String, String> blockLocalVars = new LinkedHashMap<>(localVars);
|
||||
|
||||
for (IStatement statement : statements) {
|
||||
statement.codeGen(mv, blockLocalVars);
|
||||
statement.codeGen(mv, blockLocalVars, typeContext);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class EmptyStatement extends AbstractType implements IStatement{
|
||||
@ -18,7 +19,7 @@ public class EmptyStatement extends AbstractType implements IStatement{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
//An empty statement does not generate any code
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,5 @@ public interface IStatement extends Node {
|
||||
|
||||
TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception;
|
||||
|
||||
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars) throws Exception;
|
||||
|
||||
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class IfElseStatement extends AbstractType implements IStatement{
|
||||
@ -43,21 +44,21 @@ public class IfElseStatement extends AbstractType implements IStatement{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
|
||||
LinkedHashMap<String, String> blockLocalVars = new LinkedHashMap<>(localVars);
|
||||
|
||||
Label conditionFalse = new Label();
|
||||
Label statementEnd = new Label();
|
||||
|
||||
condition.codeGen(mv);
|
||||
condition.codeGen(mv, typeContext, localVars);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
|
||||
ifStatement.codeGen(mv, blockLocalVars); //If the condition is true, execute the ifBlock
|
||||
ifStatement.codeGen(mv, blockLocalVars, typeContext); //If the condition is true, execute the ifBlock
|
||||
mv.visitJumpInsn(Opcodes.GOTO, statementEnd); //Jump to the end of the if-else statement
|
||||
|
||||
mv.visitLabel(conditionFalse);
|
||||
elseStatement.codeGen(mv, blockLocalVars); //If the condition is false, execute the elseBlock
|
||||
elseStatement.codeGen(mv, blockLocalVars, typeContext); //If the condition is false, execute the elseBlock
|
||||
|
||||
mv.visitLabel(statementEnd); //End of the if-else statement
|
||||
|
||||
|
@ -7,6 +7,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class IfStatement extends AbstractType implements IStatement{
|
||||
@ -36,16 +37,16 @@ public class IfStatement extends AbstractType implements IStatement{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
|
||||
LinkedHashMap<String, String> blockLocalVars = new LinkedHashMap<>(localVars);
|
||||
|
||||
Label conditionFalse = new Label();
|
||||
|
||||
condition.codeGen(mv);
|
||||
condition.codeGen(mv, typeContext, localVars);
|
||||
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
|
||||
ifStatement.codeGen(mv, blockLocalVars);
|
||||
ifStatement.codeGen(mv, blockLocalVars, typeContext);
|
||||
|
||||
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
|
||||
}
|
||||
|
@ -6,10 +6,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class LocalVarDecl implements IStatement{
|
||||
String type;
|
||||
@ -30,7 +27,7 @@ public class LocalVarDecl implements IStatement{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
localVars.put(identifier, type);
|
||||
|
||||
// Set a default value for the variable --> less problems
|
||||
|
@ -7,6 +7,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class ReturnStatement extends AbstractType implements IStatement{
|
||||
@ -34,10 +35,10 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
// This is a problem at "BinaryExpression" and here because we need to know the type to return
|
||||
// At this point in time we can either return reference types or have an error message
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
if (expression != null) {
|
||||
expression.codeGen(mv);
|
||||
expression.codeGen(mv, typeContext, localVars);
|
||||
//Get the Type of the expression
|
||||
//TODO: Resolve how do we get the type of the expression
|
||||
String type = expression.typeCheck(null, null, null).type;
|
||||
|
@ -7,6 +7,7 @@ import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class WhileStatement extends AbstractType implements IStatement {
|
||||
@ -35,19 +36,19 @@ public class WhileStatement extends AbstractType implements IStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
|
||||
LinkedHashMap<String, String> blockLocalVars = new LinkedHashMap<>(localVars);
|
||||
|
||||
Label conditionFalse = new Label();
|
||||
Label LoopStart = new Label();
|
||||
|
||||
mv.visitLabel(LoopStart);
|
||||
|
||||
condition.codeGen(mv);
|
||||
condition.codeGen(mv, typeContext, localVars);
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0)
|
||||
|
||||
statement.codeGen(mv, blockLocalVars);
|
||||
statement.codeGen(mv, blockLocalVars, typeContext);
|
||||
//statement.codeGen(mv);
|
||||
//TODO: If the block ends with a return statement, we might have to pop it from the stack
|
||||
// So the next iteration starts with a clean stack
|
||||
|
Loading…
Reference in New Issue
Block a user