Updated IStatement and IExpression classes to fit the method calls

This commit is contained in:
Jochen Seyfried 2024-05-31 10:04:27 +02:00
parent 5fa6664850
commit ab19751146
9 changed files with 35 additions and 33 deletions

View File

@ -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;

View File

@ -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<String, String > localVars;
HashMap<String, String> localVars;
public String returnType;
public List<IStatement> statements;
// do we need expression, statementexpression
public BlockStatement(List<IStatement> statements, String returnType){
public BlockStatement(List<IStatement> statements, String returnType) {
this.statements = statements;
this.returnType = returnType;
}
@ -28,7 +29,7 @@ public class BlockStatement extends AbstractType implements IStatement{
HashMap<String, String> 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<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);
}
}
}
}

View File

@ -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
}
}

View File

@ -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;
}

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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;

View File

@ -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