From a59950e186933cc8e68e4ce65fe8063a662eecf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Tue, 14 May 2024 15:15:58 +0200 Subject: [PATCH] parameterlist, typecheck --- src/main/java/Compiler.java | 10 ++++++ src/main/java/Input.java | 4 +-- .../abstractSyntaxTree/Class/MethodDecl.java | 2 +- .../abstractSyntaxTree/Class/RefType.java | 5 +-- .../Expression/BinaryExpression.java | 12 +++---- .../Expression/IExpression.java | 6 +++- .../Expression/InstVarExpression.java | 13 ++++--- src/main/java/abstractSyntaxTree/Program.java | 12 +++---- .../Statement/IfElseStatement.java | 4 +-- .../Statement/IfStatement.java | 2 +- .../Statement/WhileStatement.java | 2 +- .../AssignStatementExpression.java | 34 +++++++++---------- .../MethodCallStatementExpression.java | 2 +- src/main/java/astGenerator/ASTGenerator.java | 2 +- 14 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/main/java/Compiler.java b/src/main/java/Compiler.java index 56fb082..b123157 100644 --- a/src/main/java/Compiler.java +++ b/src/main/java/Compiler.java @@ -91,6 +91,16 @@ public class Compiler { // System.out.println(refType.name); // } + abstractSyntaxTree.classes.get(1).methodDecls.get(0).codeBlock.returnType = "int"; + List statementsList = abstractSyntaxTree.classes.get(1).methodDecls.get(0).codeBlock.statements; + statementsList.remove(0); + statementsList.add(new LocalVarDecl("int", "localInt")); + statementsList.add(new LocalVarDecl("bool", "localBool")); + statementsList.add(new LocalVarDecl("char", "localChar")); + statementsList.add(new AssignStatementExpression("=", new LocalVarIdentifier("localInt"), new UnaryExpression("", new IntDatatype()))); + statementsList.add(new AssignStatementExpression("=", new InstVarExpression(abstractSyntaxTree.classes.get(1), "instVarBool"), new UnaryExpression("instVarBool", new BoolDatatype()))); + + abstractSyntaxTree.classes.get(1).methodDecls.get(0).codeBlock.statements.add(new ReturnStatement(new UnaryExpression("", new IntDatatype()))); abstractSyntaxTree.typeCheck(); abstractSyntaxTree.codeGen(); diff --git a/src/main/java/Input.java b/src/main/java/Input.java index ea16d77..a67c0d2 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -6,8 +6,6 @@ class Example { } class Example2 { boolean instVarBool; - void m(int i){ - return; - } + int m(int n){return 1;} } diff --git a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java index 3517283..2707e9f 100644 --- a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java @@ -14,7 +14,7 @@ import java.util.List; import java.util.Map; import java.util.Stack; -public class MethodDecl implements IClass, Node { +public class MethodDecl implements Node { //Class Name public String classThatContainsMethod; diff --git a/src/main/java/abstractSyntaxTree/Class/RefType.java b/src/main/java/abstractSyntaxTree/Class/RefType.java index 68b139c..c50c817 100644 --- a/src/main/java/abstractSyntaxTree/Class/RefType.java +++ b/src/main/java/abstractSyntaxTree/Class/RefType.java @@ -3,6 +3,7 @@ package abstractSyntaxTree.Class; import TypeCheck.AbstractType; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Node; +import abstractSyntaxTree.Parameter.ParameterList; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; @@ -29,7 +30,7 @@ public class RefType extends AbstractType implements Node { this.hasMain = hasMain; } - public TypeCheckResult typeCheck(HashMap>>> methodContext, + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext) throws Exception { TypeCheckResult result = new TypeCheckResult(); @@ -83,7 +84,7 @@ public class RefType extends AbstractType implements Node { } for (MethodDecl method : methodDecls) { - method.codeGen(cw); + // method.codeGen(cw); } cw.visitEnd(); } diff --git a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java index 72af1be..ad619f9 100644 --- a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java @@ -37,7 +37,7 @@ public class BinaryExpression extends AbstractType implements IExpression{ case "<=": case ">=": case "!=": - result.type = helper.upperBound(leftType.type, rightType.type); + result.type = helper.upperBound(leftType.type, rightType.type); break; case "-": @@ -86,11 +86,11 @@ public class BinaryExpression extends AbstractType implements IExpression{ mv.visitJumpInsn(Opcodes.IFNE, operationTrue); break; - case "==": - // Keep in mind that only primitive types are allowed in this case (at this time) + case "==": + // Keep in mind that only primitive types are allowed in this case (at this time) - left.codeGen(mv); - right.codeGen(mv); + left.codeGen(mv); + right.codeGen(mv); mv.visitJumpInsn(Opcodes.IF_ICMPEQ, operationTrue); // If the two values are equal, jump to the end of the expression break; @@ -167,4 +167,4 @@ public class BinaryExpression extends AbstractType implements IExpression{ mv.visitLabel(expressionEnd); } -} +} \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/Expression/IExpression.java b/src/main/java/abstractSyntaxTree/Expression/IExpression.java index a679711..1245400 100644 --- a/src/main/java/abstractSyntaxTree/Expression/IExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/IExpression.java @@ -1,11 +1,15 @@ package abstractSyntaxTree.Expression; import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Parameter.ParameterList; import org.objectweb.asm.MethodVisitor; +import java.util.HashMap; + public interface IExpression { // typeCheck method - TypeCheckResult typeCheck() throws Exception; + //TypeCheckResult typeCheck() throws Exception; + TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception; // visit method for code generation void codeGen(MethodVisitor mv) throws Exception; diff --git a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java index f7d58e3..6b9a261 100644 --- a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java @@ -2,9 +2,12 @@ package abstractSyntaxTree.Expression; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Class.RefType; +import abstractSyntaxTree.Parameter.ParameterList; import jdk.jshell.spi.ExecutionControl; import org.objectweb.asm.MethodVisitor; +import java.util.HashMap; + public class InstVarExpression implements IExpression{ //TODO: We have to decide upon more parameters and where they come from, for @@ -12,13 +15,15 @@ public class InstVarExpression implements IExpression{ private RefType classRef; private String fieldName; -/* public InstVarExpression(RefType classRef, String fieldName){ + public InstVarExpression(RefType classRef, String fieldName){ this.classRef = classRef; this.fieldName = fieldName; - }*/ + } + + @Override - public TypeCheckResult typeCheck() throws Exception { - return null; + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + } @Override diff --git a/src/main/java/abstractSyntaxTree/Program.java b/src/main/java/abstractSyntaxTree/Program.java index e09bac6..fa9397b 100644 --- a/src/main/java/abstractSyntaxTree/Program.java +++ b/src/main/java/abstractSyntaxTree/Program.java @@ -20,7 +20,7 @@ import java.util.jar.JarOutputStream; public class Program implements Node { public List classes; public HashMap> typeContext; // (class, (type, identifier)) - public HashMap>> methodContext; // (class, (returntype, (identifier, parameterList))) + public HashMap>> methodContext; // (class, (returntype, (identifier, parameter))) public Program(List classes){ this.classes = classes; @@ -41,11 +41,11 @@ public class Program implements Node { typeContext.put(oneClass.name, classVars); // build method context - HashMap> methodIdentifierAndParameter = new HashMap<>(); - HashMap>> returnTypeAndMethod = new HashMap<>(); + HashMap methodIdentifierAndParameter = new HashMap<>(); + HashMap> returnTypeAndMethod = new HashMap<>(); for (MethodDecl methodDecl : oneClass.methodDecls){ - methodIdentifierAndParameter.put(methodDecl.name, (List) methodDecl.parameters); + methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters); returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter); } methodContext.put(oneClass.name, returnTypeAndMethod); @@ -66,7 +66,7 @@ public class Program implements Node { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, oneClass.name, null, "java/lang/Object", null); - oneClass.codeGen(cw, methodContext); + // oneClass.codeGen(cw); cw.visitEnd(); byte[] bytecode = cw.toByteArray(); @@ -102,4 +102,4 @@ public class Program implements Node { } */ } -} +} \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index 0a07c93..650dfa1 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -51,11 +51,11 @@ public class IfElseStatement extends AbstractType implements IStatement{ condition.codeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0) - ifStatement.codeGen(mv); //If the condition is true, execute the ifBlock + //ifStatement.codeGen(mv); //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); //If the condition is false, execute the elseBlock + //elseStatement.codeGen(mv); //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 fc8c22d..e38902e 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -43,7 +43,7 @@ public class IfStatement extends AbstractType implements IStatement{ condition.codeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0) - ifStatement.codeGen(mv); + //ifStatement.codeGen(mv); 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/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index 02d19e3..9155fb5 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -44,7 +44,7 @@ public class WhileStatement extends AbstractType implements IStatement { condition.codeGen(mv); mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0) - statement.codeGen(mv); + //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 mv.visitJumpInsn(Opcodes.GOTO, LoopStart); // Jump to the start of the while loop diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index 369557f..013799e 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -29,15 +29,15 @@ public class AssignStatementExpression extends AbstractType implements IExpressi TypeCheckHelper helper = new TypeCheckHelper(); TypeCheckResult result = new TypeCheckResult(); - TypeCheckResult leftType = left.typeCheck(); - if(leftType == null){ //left expression is the identifier of a var + TypeCheckResult leftType; + + if (left instanceof LocalVarIdentifier) { leftType = new TypeCheckResult(); - if (left instanceof LocalVarIdentifier) { - LocalVarIdentifier localVarIdentifier = (LocalVarIdentifier) left; - String identifier = localVarIdentifier.getIdentifier(); - leftType.type = localVars.get(identifier); - } - // not local var + LocalVarIdentifier localVarIdentifier = (LocalVarIdentifier) left; + String identifier = localVarIdentifier.getIdentifier(); + leftType.type = localVars.get(identifier); + }else{ + leftType = left.typeCheck(); } TypeCheckResult rightType = right.typeCheck(); @@ -64,14 +64,14 @@ public class AssignStatementExpression extends AbstractType implements IExpressi left.codeGen(mv); right.codeGen(mv); - if (left instanceof VarRefExpression varRef) { - //TODO: Implement the handling of a variable reference --> I need a list of local variables - // for that to determine if the variable is a local or field variable - } else if (left instanceof InstVarExpression instVar) { - mv.visitInsn(Opcodes.DUP_X1); - - // We now again need the owner (class reference), name (of the Field in the owner) and type of the field - //mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type); - } +// if (left instanceof VarRefExpression varRef) { +// //TODO: Implement the handling of a variable reference --> I need a list of local variables +// // for that to determine if the variable is a local or field variable +// } else if (left instanceof InstVarExpression instVar) { +// mv.visitInsn(Opcodes.DUP_X1); +// +// // We now again need the owner (class reference), name (of the Field in the owner) and type of the field +// //mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type); +// } } } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index 0540422..a78b77e 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -61,7 +61,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr public void codeGen(MethodVisitor mv) throws Exception { //Generate Bytecode for the receiver if(classThatHasTheMethodIfNotThis != null){ - classThatHasTheMethodIfNotThis.codeGen(new ClassWriter(ClassWriter.COMPUTE_FRAMES)); + // classThatHasTheMethodIfNotThis.codeGen(new ClassWriter(ClassWriter.COMPUTE_FRAMES)); } else { mv.visitVarInsn(Opcodes.ALOAD, 0); } diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index 24e582f..90797df 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -170,7 +170,7 @@ public class ASTGenerator extends DecafBaseVisitor { @Override public Node visitAssign(DecafParser.AssignContext ctx) { - return new AssignStatementExpression(); + return new AssignStatementExpression("", null, null); } @Override