From 6915a97432ba2ad3a064bb24162d13110b6bb654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Thu, 20 Jun 2024 08:00:23 +0200 Subject: [PATCH] instvar typecheck and getTypeCheckresults --- src/main/java/Compiler.java | 3 +++ src/main/java/Input.java | 26 +++---------------- .../Datatype/BoolDatatype.java | 2 +- .../Datatype/CharDatatype.java | 2 +- .../Datatype/IntDatatype.java | 2 +- .../Expression/BinaryExpression.java | 3 +-- .../Expression/BooleanConstantExpression.java | 4 +++ .../Expression/CharConstantExpression.java | 5 ++++ .../Expression/InstVarExpression.java | 5 ++-- .../Expression/IntConstantExpression.java | 7 +++-- .../Expression/LocalVarIdentifier.java | 14 +++++----- .../Expression/UnaryExpression.java | 2 +- .../Statement/BlockStatement.java | 4 +-- .../Statement/EmptyStatement.java | 2 +- .../Statement/IfElseStatement.java | 2 +- .../Statement/IfStatement.java | 2 +- .../Statement/LocalVarDecl.java | 5 ++-- .../Statement/ReturnStatement.java | 8 +++--- .../Statement/WhileStatement.java | 2 +- .../AssignStatementExpression.java | 4 +++ .../MethodCallStatementExpression.java | 5 ++++ .../NewStatementExpression.java | 4 +++ 22 files changed, 63 insertions(+), 50 deletions(-) diff --git a/src/main/java/Compiler.java b/src/main/java/Compiler.java index c351917..e391269 100644 --- a/src/main/java/Compiler.java +++ b/src/main/java/Compiler.java @@ -105,6 +105,9 @@ public class Compiler { //abstractSyntaxTree.classes.get(0).methodDecls.get(0).codeBlock.returnType = "char"; + AssignStatementExpression a = (AssignStatementExpression) abstractSyntaxTree.classes.get(0).methodDecls.get(0).codeBlock.statements.get(2); + InstVarExpression b = (InstVarExpression) a.left; + b.classRef = abstractSyntaxTree.classes.get(0); abstractSyntaxTree.typeCheck(); abstractSyntaxTree.codeGen(); diff --git a/src/main/java/Input.java b/src/main/java/Input.java index 8521bf3..36b02eb 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -1,29 +1,11 @@ -class ExampleEmpty { - -} - class Example { int i; boolean b; char c; - -} -class Example2 { - boolean instVarBool; int m(int n){ int localA; - int localB; - return n; + localA = 5; + this.i = 3; + return localA; } -} - -class Example3 { - char isNice; - char m(int n) { - int test = 12; - this.m(test); - boolean l; - l = false; - return 't'; - } -} +} \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java b/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java index 966746f..03d7f06 100644 --- a/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java +++ b/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java @@ -37,6 +37,6 @@ public class BoolDatatype extends AbstractType implements IDatatype{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java b/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java index 26ce204..a1aa152 100644 --- a/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java +++ b/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java @@ -37,6 +37,6 @@ public class CharDatatype extends AbstractType implements IDatatype{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java b/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java index 0fcd83c..17e58c1 100644 --- a/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java +++ b/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java @@ -44,6 +44,6 @@ public class IntDatatype extends AbstractType implements IDatatype{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java index 2fe5f81..25b5b14 100644 --- a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java @@ -186,9 +186,8 @@ public class BinaryExpression extends AbstractType implements IExpression{ && Objects.equals(right, binaryExpression.right) ); } - @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java index eb12f43..c7ec132 100644 --- a/src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java @@ -27,4 +27,8 @@ public class BooleanConstantExpression extends AbstractType implements IExpressi public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { } + @Override + public TypeCheckResult getTypeCheckResult() { + return super.getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java index 9933484..e3a6430 100644 --- a/src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java @@ -27,4 +27,9 @@ public class CharConstantExpression extends AbstractType implements IExpression{ public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { } + + @Override + public TypeCheckResult getTypeCheckResult() { + return super.getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java index 95842e3..8e47168 100644 --- a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java @@ -1,5 +1,6 @@ package abstractSyntaxTree.Expression; +import TypeCheck.AbstractType; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Parameter.ParameterList; @@ -14,7 +15,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Objects; -public class InstVarExpression implements IExpression{ +public class InstVarExpression extends AbstractType implements IExpression{ public RefType classRef; @@ -85,6 +86,6 @@ public class InstVarExpression implements IExpression{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java index e3e7392..3665fd2 100644 --- a/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java @@ -19,7 +19,10 @@ public class IntConstantExpression extends AbstractType implements IExpression{ @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { - return null; + TypeCheckResult result = new TypeCheckResult(); + result.type = "int"; + setTypeCheckResult(result); + return result; } @Override @@ -38,6 +41,6 @@ public class IntConstantExpression extends AbstractType implements IExpression{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java index fd191a5..5065969 100644 --- a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java +++ b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java @@ -1,5 +1,6 @@ package abstractSyntaxTree.Expression; +import TypeCheck.AbstractType; import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckResult; import org.objectweb.asm.*; @@ -12,7 +13,7 @@ import org.objectweb.asm.MethodVisitor; import java.util.HashMap; import java.util.Objects; -public class LocalVarIdentifier implements IExpression{ +public class LocalVarIdentifier extends AbstractType implements IExpression{ String identifier; public LocalVarIdentifier(String identifier){ @@ -31,6 +32,7 @@ public class LocalVarIdentifier implements IExpression{ } else { throw new Exception("TypeCheck Exception: Local var " + identifier + " does not exist."); } + setTypeCheckResult(result); return result; } @@ -73,6 +75,11 @@ public class LocalVarIdentifier implements IExpression{ } } + @Override + public TypeCheckResult getTypeCheckResult() { + return super.getTypeCheckResult(); + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -81,9 +88,4 @@ public class LocalVarIdentifier implements IExpression{ return (Objects.equals(identifier, localVarIdentifier.identifier) ); } - - @Override - public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); - } } diff --git a/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java index f2bdd20..5be81e7 100644 --- a/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java @@ -83,6 +83,6 @@ public class UnaryExpression extends AbstractType implements IExpression{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index e0eb7e1..5459b52 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -74,7 +74,7 @@ public class BlockStatement extends AbstractType implements IStatement { continue; // set return of block if not known yet - if(this.returnType == null) + if(this.returnType == null|| this.returnType == "not") this.returnType = typeOfCurrentStatement.type; if (!typeOfCurrentStatement.type.equals(this.returnType)) @@ -113,6 +113,6 @@ public class BlockStatement extends AbstractType implements IStatement { @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } \ 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 b14fa67..f5c4e43 100644 --- a/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java @@ -31,6 +31,6 @@ public class EmptyStatement extends AbstractType implements IStatement{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index f373d76..a26c62d 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -81,6 +81,6 @@ public class IfElseStatement extends AbstractType implements IStatement{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java index 33d55f2..8ad8499 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -64,7 +64,7 @@ public class IfStatement extends AbstractType implements IStatement{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java index 6122ab2..21553de 100644 --- a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java +++ b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java @@ -1,5 +1,6 @@ package abstractSyntaxTree.Statement; +import TypeCheck.AbstractType; import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Parameter.ParameterList; @@ -8,7 +9,7 @@ import org.objectweb.asm.Opcodes; import java.util.*; -public class LocalVarDecl implements IStatement{ +public class LocalVarDecl extends AbstractType implements IStatement{ String type; String identifier; public LocalVarDecl(String type, String identifier) { @@ -62,6 +63,6 @@ public class LocalVarDecl implements IStatement{ @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } diff --git a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java index b08226f..c255aee 100644 --- a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java @@ -65,8 +65,8 @@ public class ReturnStatement extends AbstractType implements IStatement{ ); } -// @Override -// public TypeCheckResult getTypeCheckResult() { -// return this. -// } + @Override + public TypeCheckResult getTypeCheckResult() { + return super.getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index c65d6b5..23ef3bf 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -73,6 +73,6 @@ public class WhileStatement extends AbstractType implements IStatement { @Override public TypeCheckResult getTypeCheckResult() { - return getTypeCheckResult(); + return super.getTypeCheckResult(); } } \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index daabedb..6d5cbd8 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -143,4 +143,8 @@ public class AssignStatementExpression extends AbstractType implements IExpressi && Objects.equals(right, assignStatementExpression.right) ); } + @Override + public TypeCheckResult getTypeCheckResult() { + return super.getTypeCheckResult(); + } } \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index 88b3ec3..4fa060c 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -103,4 +103,9 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr && Objects.equals(arguments, methodCallStatementExpression.arguments) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return super.getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java index 61a22be..af9fbe7 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java @@ -84,4 +84,8 @@ public class NewStatementExpression extends AbstractType implements IExpression, descriptor.append(")V"); // Constructors always return void return descriptor.toString(); } + @Override + public TypeCheckResult getTypeCheckResult() { + return super.getTypeCheckResult(); + } }