From ab19751146fc63992b555da83f56dc09e8e93b6e Mon Sep 17 00:00:00 2001 From: Jochen Seyfried Date: Fri, 31 May 2024 10:04:27 +0200 Subject: [PATCH] Updated IStatement and IExpression classes to fit the method calls --- .../Expression/UnaryExpression.java | 1 + .../Statement/BlockStatement.java | 21 ++++++++++--------- .../Statement/EmptyStatement.java | 3 ++- .../Statement/IStatement.java | 2 -- .../Statement/IfElseStatement.java | 11 +++++----- .../Statement/IfStatement.java | 9 ++++---- .../Statement/LocalVarDecl.java | 7 ++----- .../Statement/ReturnStatement.java | 5 +++-- .../Statement/WhileStatement.java | 9 ++++---- 9 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java index fed80fe..be1367c 100644 --- a/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java @@ -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; diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index f0e3645..33196e6 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -7,17 +7,18 @@ 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{ +public class BlockStatement extends AbstractType implements IStatement { //We will need a parameter which holds the symbol table - HashMap localVars; + HashMap localVars; public String returnType; public List statements; // do we need expression, statementexpression - public BlockStatement(List statements, String returnType){ + public BlockStatement(List statements, String returnType) { this.statements = statements; this.returnType = returnType; } @@ -28,7 +29,7 @@ public class BlockStatement extends AbstractType implements IStatement{ HashMap localVars) throws Exception { TypeCheckResult result = new TypeCheckResult(); - if(statements.size() == 0){ + if (statements.size() == 0) { result.type = "void"; } @@ -36,26 +37,26 @@ public class BlockStatement extends AbstractType implements IStatement{ TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); if (!typeOfCurrentStatement.type.equals(this.returnType)) { - if(!typeOfCurrentStatement.type.equals("void")) + if (!typeOfCurrentStatement.type.equals("void")) throw new Exception("TypeCheck Exception: Block returns the wrong type."); } } 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 localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> 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 blockLocalVars = new HashMap<>(localVars); + LinkedHashMap blockLocalVars = new LinkedHashMap<>(localVars); for (IStatement statement : statements) { - statement.codeGen(mv, blockLocalVars); + statement.codeGen(mv, blockLocalVars, typeContext); } } -} +} \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java b/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java index a7ad544..481c27c 100644 --- a/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java @@ -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 localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { //An empty statement does not generate any code } } diff --git a/src/main/java/abstractSyntaxTree/Statement/IStatement.java b/src/main/java/abstractSyntaxTree/Statement/IStatement.java index 1ee7271..5c9eee8 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IStatement.java @@ -13,7 +13,5 @@ public interface IStatement extends Node { TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception; - void codeGen(MethodVisitor mv, LinkedHashMap localVars) throws Exception; - void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception; } diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index b61d42c..4c2c2ed 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -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 localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { - HashMap blockLocalVars = new HashMap<>(localVars); + LinkedHashMap 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 diff --git a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java index 1a05387..d53737d 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -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 localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { - HashMap blockLocalVars = new HashMap<>(localVars); + LinkedHashMap 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 } diff --git a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java index c53b3af..71df469 100644 --- a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java +++ b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java @@ -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 localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { localVars.put(identifier, type); // Set a default value for the variable --> less problems diff --git a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java index c19beb7..87782c4 100644 --- a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java @@ -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 localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> 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; diff --git a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index 046a4e6..1b7b577 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -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 localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { - HashMap blockLocalVars = new HashMap<>(localVars); + LinkedHashMap 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