From 8549799175e6457b062f8ca1dd5de4b287dbcccf Mon Sep 17 00:00:00 2001 From: StefanZ3 Date: Fri, 31 May 2024 10:44:45 +0200 Subject: [PATCH 1/7] fix ifstmt in ASTGenerator --- src/main/java/astGenerator/ASTGenerator.java | 34 ++++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index 183013d..b628494 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -5,6 +5,8 @@ import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Expression.BinaryExpression; import abstractSyntaxTree.Expression.IExpression; +import abstractSyntaxTree.Expression.IntConstantExpression; +import abstractSyntaxTree.Expression.LocalVarIdentifier; import abstractSyntaxTree.Node; import abstractSyntaxTree.Parameter.Parameter; import abstractSyntaxTree.Parameter.ParameterList; @@ -132,12 +134,12 @@ public class ASTGenerator extends DecafBaseVisitor { @Override public Node visitIfElseStmt(DecafParser.IfElseStmtContext ctx) { if (ctx.elseStmt() != null) { - return visitIfStmt(ctx.ifStmt()); - } else { Node expression = visitExpression(ctx.ifStmt().expression()); Node ifStatement = visitStatement(ctx.ifStmt().statement()); Node elseStatement = visitStatement(ctx.elseStmt().statement()); return new IfElseStatement((IExpression) expression, (IStatement) ifStatement, (IStatement) elseStatement); + } else { + return visitIfStmt(ctx.ifStmt()); } } @@ -190,7 +192,7 @@ public class ASTGenerator extends DecafBaseVisitor { @Override public Node visitExpression(DecafParser.ExpressionContext ctx) { if (ctx.subExpression() != null) { - + return visitSubExpression(ctx.subExpression()); } else if (ctx.binaryExpr() != null) { return visitBinaryExpr(ctx.binaryExpr()); } @@ -238,7 +240,15 @@ public class ASTGenerator extends DecafBaseVisitor { //todo @Override public Node visitDotSubExpr(DecafParser.DotSubExprContext ctx) { - return super.visitDotSubExpr(ctx); +// if (ctx.IntValue() != null) { +// int value = Integer.parseInt(ctx.IntValue().getText()); +// return new IntConstantExpression(value); +// } + if(ctx.Identifier() != null) { + String identifier = ctx.Identifier().getText(); + return new LocalVarIdentifier(identifier); + } + return null; } @Override @@ -256,8 +266,20 @@ public class ASTGenerator extends DecafBaseVisitor { @Override public Node visitSubExpression(DecafParser.SubExpressionContext ctx) { - if (ctx.subExpression() != null) { - visitSubExpression(ctx.subExpression()); + if (ctx.assignableExpr() != null) { + return visitAssignableExpr(ctx.assignableExpr()); + } else if (ctx.stmtExpr() != null) { + return visitStmtExpr(ctx.stmtExpr()); + } else if (ctx.subExpression() != null) { + return visitSubExpression(ctx.subExpression()); + } + return null; + } + + @Override + public Node visitAssignableExpr(DecafParser.AssignableExprContext ctx) { + if (ctx.Identifier() != null) { + return new LocalVarIdentifier(ctx.Identifier().getText()); } return null; } From 9d86ab3a9fe82eec8e76c388b47ac05eeae64bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Fri, 31 May 2024 10:59:06 +0200 Subject: [PATCH 2/7] method parameters --- src/main/java/Compiler.java | 25 +++++++++++-------- src/main/java/Input.java | 8 ++++-- .../abstractSyntaxTree/Class/MethodDecl.java | 6 ++--- .../Statement/BlockStatement.java | 24 ++++++++++++++++++ .../Statement/IfElseStatement.java | 19 +++++++++----- .../Statement/IfStatement.java | 8 +++--- .../Statement/WhileStatement.java | 13 ++++++---- .../AssignStatementExpression.java | 22 ++++++++-------- .../MethodCallStatementExpression.java | 13 +++++++--- .../NewStatementExpression.java | 9 ++++--- 10 files changed, 98 insertions(+), 49 deletions(-) diff --git a/src/main/java/Compiler.java b/src/main/java/Compiler.java index b123157..0d29ce8 100644 --- a/src/main/java/Compiler.java +++ b/src/main/java/Compiler.java @@ -27,9 +27,21 @@ public class Compiler { public static void main(String[] args) throws Exception{ - Path filePath = Paths.get("NichtHaskell/src/main/java/Input.java"); + Path filePath = Paths.get("src/main/java/Input.java"); + + + // todo remove this debug info + Path absolutePath = filePath.toAbsolutePath(); + System.out.println("Full path: " + absolutePath); + + String content; + try { + content = Files.readString(filePath); + }catch (java.nio.file.NoSuchFileException e){ + System.out.println("File not found"); + return; + } - String content = Files.readString(filePath); System.out.println("--- print content ---"); System.out.println(content); @@ -91,16 +103,7 @@ 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 a67c0d2..8438b90 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -6,6 +6,10 @@ class Example { } class Example2 { boolean instVarBool; - int m(int n){return 1;} - + int m(int n){ + while(instVarBool){ + return n; + } + return -1; + } } diff --git a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java index fc074c7..1af6f0f 100644 --- a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java @@ -75,7 +75,7 @@ public class MethodDecl implements Node { mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", descriptor, false); mv.visitCode(); - codeBlock.codeGen(mv, localVars); + //codeBlock.codeGen(mv, localVars); mv.visitInsn(Opcodes.RETURN); //automatically computed max stack and max locals @@ -87,7 +87,7 @@ public class MethodDecl implements Node { MethodVisitor mv = cw.visitMethod(access, name, "([Ljava/lang/String;)V", null, null); mv.visitCode(); - codeBlock.codeGen(mv, localVars); + //codeBlock.codeGen(mv, localVars); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); @@ -97,7 +97,7 @@ public class MethodDecl implements Node { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, name, getMethodDescriptor(methodContext), null, null); mv.visitCode(); - codeBlock.codeGen(mv, localVars); + //codeBlock.codeGen(mv, localVars); // We have to check the return type to get the return opcode // For methods which return an actual value, the return opcode is created in the method body to ensure the diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index 33196e6..7cffad6 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -36,6 +36,30 @@ public class BlockStatement extends AbstractType implements IStatement { for (IStatement statement : statements) { TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); + if (typeOfCurrentStatement.type.contains(",")) { + // else if has 2 returns, all code paths must retrun a value. + String[] substrings = typeOfCurrentStatement.type.split(","); + + String firstType = substrings[0]; + String secondType = substrings[1]; + + if(!firstType.equals(this.returnType) || !firstType.equals(this.returnType)){ + if(!firstType.equals("void")){ + throw new Exception("TypeCeck Exception: if paths return wrong type"); + } + if(!secondType.equals("void")){ + throw new Exception("TypeCeck Exception: else paths return wrong type"); + } + boolean firstIsVoid = firstType.equals("void"); + + if(!firstIsVoid){ + typeOfCurrentStatement.type = firstType; + }else{ + typeOfCurrentStatement.type = secondType; + } + } + } + if (!typeOfCurrentStatement.type.equals(this.returnType)) { if (!typeOfCurrentStatement.type.equals("void")) throw new Exception("TypeCheck Exception: Block returns the wrong type."); diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index 4c2c2ed..51b5e7b 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -9,6 +9,7 @@ import org.objectweb.asm.*; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; +import java.util.Objects; public class IfElseStatement extends AbstractType implements IStatement{ IExpression condition; @@ -26,11 +27,11 @@ public class IfElseStatement extends AbstractType implements IStatement{ public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { TypeCheckResult result = new TypeCheckResult(); - //TypeCheckResult conditionType = condition.typeCheck(); + TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars); -// if (!conditionType.equals("bool")) { -// throw new IllegalArgumentException("should be boolean"); -// } + if (!conditionType.equals("bool")) { + throw new IllegalArgumentException("should be boolean"); + } TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars); TypeCheckResult elseStatementType = elseStatement.typeCheck(methodContext, typeContext, localVars); @@ -38,11 +39,17 @@ public class IfElseStatement extends AbstractType implements IStatement{ if (!ifStatementType.equals(elseStatementType)) { throw new IllegalArgumentException("if and else have different types"); } - - result.type = elseStatementType.type; + if(ifStatementType.type != "void" && elseStatementType.type != "void"){ + if(Objects.equals(ifStatementType.type, elseStatementType.type)){ + throw new Exception("TypeCeck Exception: If and else return different not-void types"); + } + } + result.type = ifStatementType.type + "," + elseStatementType.type; return result; } + + @Override public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { diff --git a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java index d53737d..08ddde0 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -25,11 +25,11 @@ public class IfStatement extends AbstractType implements IStatement{ public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { TypeCheckResult result = new TypeCheckResult(); - //TypeCheckResult conditionType = condition.typeCheck(); + TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars); -// if (!conditionType.equals("bool")) { -// throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool."); -// } + if (!conditionType.equals("bool")) { + throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool."); + } TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars); result.type = ifStatementType.type; diff --git a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index 1b7b577..6f8e678 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -23,15 +23,18 @@ public class WhileStatement extends AbstractType implements IStatement { public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { TypeCheckResult result = new TypeCheckResult(); -// TypeCheckResult conditionType = condition.typeCheck(); -// -// if (!conditionType.equals("bool")) { -// throw new IllegalArgumentException("Expected boolean"); -// } + // check condition + TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars); + if (!conditionType.equals("bool")) { + throw new IllegalArgumentException("Expected boolean"); + } + // check code block TypeCheckResult statementType = statement.typeCheck(methodContext, typeContext, localVars); + // set result result.type = statementType.type; + setTypeCheckResult(result); return result; } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index b2f1e53..b56d75e 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -12,6 +12,7 @@ import org.objectweb.asm.*; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.Objects; public class AssignStatementExpression extends AbstractType implements IExpression, IStatement { public String operator; @@ -19,7 +20,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi public IExpression right; private InstVarExpression instVar; - public AssignStatementExpression(String operator, IExpression leftExpression, IExpression rightExpression){ + public AssignStatementExpression(String operator, IExpression leftExpression, IExpression rightExpression) { this.operator = operator; this.left = leftExpression; this.right = rightExpression; @@ -37,7 +38,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi LocalVarIdentifier localVarIdentifier = (LocalVarIdentifier) left; String identifier = localVarIdentifier.getIdentifier(); leftType.type = localVars.get(identifier); - }else{ + } else { leftType = left.typeCheck(methodContext, typeContext, localVars); } TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); @@ -51,13 +52,11 @@ public class AssignStatementExpression extends AbstractType implements IExpressi } @Override - public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { - if (left instanceof VarRefExpression varRef) { - - } + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { } + public TypeCheckResult typeCheck() throws Exception { return null; } @@ -70,22 +69,22 @@ public class AssignStatementExpression extends AbstractType implements IExpressi // Call the codeGen on the right expression which will push the value of the right expression onto the stack right.codeGen(mv, typeContext, localVars); - if (left instanceof LocalVarIdentifier) { + if (left instanceof LocalVarIdentifier) { LocalVarIdentifier localVar = (LocalVarIdentifier) left; String varName = localVar.getIdentifier(); //Get the index of the local variable int index = -1; int counter = 0; - for (String key : localVars.keySet()){ - if (key.equals(varName)){ + for (String key : localVars.keySet()) { + if (key.equals(varName)) { index = counter; break; } counter++; } - if (index == -1){ + if (index == -1) { throw new Exception("Variable " + varName + " not found"); } @@ -100,7 +99,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi break; } - } else if (left instanceof InstVarExpression){ + } else if (left instanceof InstVarExpression) { instVar = (InstVarExpression) left; // Load "this" onto the stack @@ -115,5 +114,6 @@ public class AssignStatementExpression extends AbstractType implements IExpressi // // 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 7a8498c..ef0a91c 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -51,6 +51,11 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr return null; } + @Override + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { + + } + //Errors occur due to the change in parameter in the RefType class // I need the methodContext here to get the method descriptor @@ -66,10 +71,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr for (MethodDecl methodDecl : methodDecls) { if (methodDecl.name.equals(methodName)) { //Get the method descriptor - descriptor = methodDecl.getMethodDescriptor(methodContext); + // descriptor = methodDecl.getMethodDescriptor(methodContext); } } - mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false); + // mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false); } else { // Load this onto the stack @@ -86,9 +91,9 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr for (MethodDecl methodDecl : methodDecls) { if (methodDecl.name.equals(methodName)) { //Get the method descriptor - descriptor = methodDecl.getMethodDescriptor(methodContext); + // descriptor = methodDecl.getMethodDescriptor(methodContext); } } - mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false); + // mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false); } } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java index 7938552..f188cb1 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java @@ -8,6 +8,7 @@ import abstractSyntaxTree.Statement.IStatement; import org.objectweb.asm.MethodVisitor; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; public class NewStatementExpression extends AbstractType implements IExpression, IStatement { @@ -19,12 +20,14 @@ public class NewStatementExpression extends AbstractType implements IExpression, } @Override - public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { } @Override - public void codeGen(MethodVisitor mv) throws Exception { - throw new Exception("CodeGen not implemented for NewStatementExpression"); + public void codeGen(MethodVisitor mv, HashMap> typeContext, LinkedHashMap localVars) throws Exception { + } + + } From 8d6d8d55886c19381ace6b59e646a83d6dc7a371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Fri, 31 May 2024 11:03:16 +0200 Subject: [PATCH 3/7] merged assign statement expression --- src/main/java/Input.java | 2 +- .../AssignStatementExpression.java | 22 +++++-------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/main/java/Input.java b/src/main/java/Input.java index 8438b90..80b66b6 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -7,7 +7,7 @@ class Example { class Example2 { boolean instVarBool; int m(int n){ - while(instVarBool){ + if(instVarBool){ return n; } return -1; diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index c539516..1259900 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -51,12 +51,6 @@ public class AssignStatementExpression extends AbstractType implements IExpressi return result; } - @Override - public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { - - } - - public TypeCheckResult typeCheck() throws Exception { return null; } @@ -112,14 +106,10 @@ public class AssignStatementExpression extends AbstractType implements IExpressi } else if (left instanceof InstVarExpression instVar) { mv.visitInsn(Opcodes.DUP_X1); -// 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); -// } + // 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); + } } -} + */ + } +} \ No newline at end of file From 7458be43d785e33786c52b7bd3c7b8c7d6fcd376 Mon Sep 17 00:00:00 2001 From: StefanZ3 Date: Fri, 31 May 2024 11:05:05 +0200 Subject: [PATCH 4/7] add class intConstant to ast --- .../Expression/IntConstantExpression.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java diff --git a/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java new file mode 100644 index 0000000..4a6a605 --- /dev/null +++ b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java @@ -0,0 +1,11 @@ +package abstractSyntaxTree.Expression; + +import TypeCheck.AbstractType; + +public class IntConstantExpression extends AbstractType implements IExpression{ + public int value; + + public IntConstantExpression(int value) { + this.value = value; + } +} From b20b7adc99c7ea894b159124fa04b08e3a880010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Fri, 31 May 2024 11:06:10 +0200 Subject: [PATCH 5/7] removed unused method --- .../StatementExpression/NewStatementExpression.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java index f188cb1..7937135 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java @@ -24,10 +24,5 @@ public class NewStatementExpression extends AbstractType implements IExpression, } - @Override - public void codeGen(MethodVisitor mv, HashMap> typeContext, LinkedHashMap localVars) throws Exception { - - } - } From abc4e3ff7018335f8643cc390e704040fbc5b493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Fri, 31 May 2024 11:54:57 +0200 Subject: [PATCH 6/7] continued typecheck --- src/main/java/Input.java | 4 +- .../Expression/IntConstantExpression.java | 16 +++++++ .../Expression/LocalVarIdentifier.java | 8 +++- .../Statement/BlockStatement.java | 47 ++++++++++--------- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/main/java/Input.java b/src/main/java/Input.java index 80b66b6..4b8b0f9 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -7,7 +7,9 @@ class Example { class Example2 { boolean instVarBool; int m(int n){ - if(instVarBool){ + boolean localBool; + localBool = true; + if(localBool){ return n; } return -1; diff --git a/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java index 4a6a605..08f04bc 100644 --- a/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java @@ -1,6 +1,12 @@ package abstractSyntaxTree.Expression; import TypeCheck.AbstractType; +import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Parameter.ParameterList; +import org.objectweb.asm.MethodVisitor; + +import java.util.HashMap; +import java.util.LinkedHashMap; public class IntConstantExpression extends AbstractType implements IExpression{ public int value; @@ -8,4 +14,14 @@ public class IntConstantExpression extends AbstractType implements IExpression{ public IntConstantExpression(int value) { this.value = value; } + + @Override + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + + @Override + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { + + } } diff --git a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java index f00ff46..573e917 100644 --- a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java +++ b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java @@ -24,7 +24,13 @@ public class LocalVarIdentifier implements IExpression{ @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { - return null; + TypeCheckResult result = new TypeCheckResult(); + if (localVars.containsKey(identifier)) { + result.type = localVars.get(identifier); + } else { + throw new Exception("TypeCheck Exception: local var does not exist"); + } + return result; } @Override diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index 7cffad6..276f32e 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -34,35 +34,38 @@ public class BlockStatement extends AbstractType implements IStatement { } for (IStatement statement : statements) { - TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); + // todo remove later if there are no null statement any more + if (statement != null) { + TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); - if (typeOfCurrentStatement.type.contains(",")) { - // else if has 2 returns, all code paths must retrun a value. - String[] substrings = typeOfCurrentStatement.type.split(","); + if (typeOfCurrentStatement.type.contains(",")) { + // else if has 2 returns, all code paths must retrun a value. + String[] substrings = typeOfCurrentStatement.type.split(","); - String firstType = substrings[0]; - String secondType = substrings[1]; + String firstType = substrings[0]; + String secondType = substrings[1]; - if(!firstType.equals(this.returnType) || !firstType.equals(this.returnType)){ - if(!firstType.equals("void")){ - throw new Exception("TypeCeck Exception: if paths return wrong type"); - } - if(!secondType.equals("void")){ - throw new Exception("TypeCeck Exception: else paths return wrong type"); - } - boolean firstIsVoid = firstType.equals("void"); + if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) { + if (!firstType.equals("void")) { + throw new Exception("TypeCeck Exception: if paths return wrong type"); + } + if (!secondType.equals("void")) { + throw new Exception("TypeCeck Exception: else paths return wrong type"); + } + boolean firstIsVoid = firstType.equals("void"); - if(!firstIsVoid){ - typeOfCurrentStatement.type = firstType; - }else{ - typeOfCurrentStatement.type = secondType; + if (!firstIsVoid) { + typeOfCurrentStatement.type = firstType; + } else { + typeOfCurrentStatement.type = secondType; + } } } - } - if (!typeOfCurrentStatement.type.equals(this.returnType)) { - if (!typeOfCurrentStatement.type.equals("void")) - throw new Exception("TypeCheck Exception: Block returns the wrong type."); + if (!typeOfCurrentStatement.type.equals(this.returnType)) { + if (!typeOfCurrentStatement.type.equals("void")) + throw new Exception("TypeCheck Exception: Block returns the wrong type."); + } } } result.type = this.returnType; From 25d379876bfe095a765cfca4a8055afbbc591473 Mon Sep 17 00:00:00 2001 From: StefanZ3 Date: Fri, 31 May 2024 11:55:33 +0200 Subject: [PATCH 7/7] add assign --- src/main/java/astGenerator/ASTGenerator.java | 37 ++++++++++++++------ 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index b628494..8ae4d20 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -3,10 +3,7 @@ package astGenerator; import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Class.RefType; -import abstractSyntaxTree.Expression.BinaryExpression; -import abstractSyntaxTree.Expression.IExpression; -import abstractSyntaxTree.Expression.IntConstantExpression; -import abstractSyntaxTree.Expression.LocalVarIdentifier; +import abstractSyntaxTree.Expression.*; import abstractSyntaxTree.Node; import abstractSyntaxTree.Parameter.Parameter; import abstractSyntaxTree.Parameter.ParameterList; @@ -176,9 +173,12 @@ public class ASTGenerator extends DecafBaseVisitor { @Override public Node visitAssign(DecafParser.AssignContext ctx) { - return new AssignStatementExpression("", null, null); + Node right = visitExpression(ctx.expression()); + Node left = visitAssignableExpr(ctx.assignableExpr()); + return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right); } + @Override public Node visitMethodCall(DecafParser.MethodCallContext ctx) { return super.visitMethodCall(ctx); @@ -240,13 +240,14 @@ public class ASTGenerator extends DecafBaseVisitor { //todo @Override public Node visitDotSubExpr(DecafParser.DotSubExprContext ctx) { -// if (ctx.IntValue() != null) { -// int value = Integer.parseInt(ctx.IntValue().getText()); -// return new IntConstantExpression(value); -// } - if(ctx.Identifier() != null) { + if (ctx.IntValue() != null) { + int value = Integer.parseInt(ctx.IntValue().getText()); + return new IntConstantExpression(value); + } else if(ctx.Identifier() != null) { String identifier = ctx.Identifier().getText(); return new LocalVarIdentifier(identifier); + } else if(ctx.instVar() != null) { + return visitInstVar(ctx.instVar()); } return null; } @@ -283,4 +284,20 @@ public class ASTGenerator extends DecafBaseVisitor { } return null; } + + @Override + public Node visitInstVar(DecafParser.InstVarContext ctx) { + return super.visitInstVar(ctx); + } + +// @Override +// public Node visitArgumentList(DecafParser.ArgumentListContext ctx) { +// if (ctx.expression().size() == 1) { +// return visitExpression(ctx.expression(0)); +// } else if (ctx.expression().size() >= 2) { +// for(DecafParser.ExpressionContext expressionContext: ctx.expression()) { +// +// } +// } +// } }