diff --git a/src/main/java/Input.java b/src/main/java/Input.java index 4b8b0f9..213cea7 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -1,17 +1,21 @@ +class ExampleEmpty { + +} + class Example { int i; boolean b; char c; + void callM(){ + Example2 example2 = new Example2(); + example2.m(1); + } } class Example2 { boolean instVarBool; int m(int n){ - boolean localBool; - localBool = true; - if(localBool){ - return n; - } - return -1; + boolean a = this.instVarBool; + return n; } } diff --git a/src/main/java/abstractSyntaxTree/Class/RefType.java b/src/main/java/abstractSyntaxTree/Class/RefType.java index 5c858fa..4ce323a 100644 --- a/src/main/java/abstractSyntaxTree/Class/RefType.java +++ b/src/main/java/abstractSyntaxTree/Class/RefType.java @@ -15,8 +15,8 @@ import java.util.Objects; public class RefType extends AbstractType implements Node { - //Class Name - public String name; + + public String name; // Class Name public List fieldDecls; public List methodDecls; boolean hasMain; diff --git a/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java b/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java index f09e9dc..966746f 100644 --- a/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java +++ b/src/main/java/abstractSyntaxTree/Datatype/BoolDatatype.java @@ -35,5 +35,8 @@ public class BoolDatatype extends AbstractType implements IDatatype{ } } - + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java b/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java index 025abc4..26ce204 100644 --- a/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java +++ b/src/main/java/abstractSyntaxTree/Datatype/CharDatatype.java @@ -34,4 +34,9 @@ public class CharDatatype extends AbstractType implements IDatatype{ CharDatatype charDatatype = (CharDatatype) o; return (Objects.equals(value, charDatatype.value)); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Datatype/IDatatype.java b/src/main/java/abstractSyntaxTree/Datatype/IDatatype.java index d729f96..9f57666 100644 --- a/src/main/java/abstractSyntaxTree/Datatype/IDatatype.java +++ b/src/main/java/abstractSyntaxTree/Datatype/IDatatype.java @@ -10,6 +10,8 @@ public interface IDatatype { // visit method for code generation void codeGen(MethodVisitor mv) throws Exception; + + TypeCheckResult getTypeCheckResult(); } //TODO: Check if we need to differentiate between primitive types and reference types --> for example in "==" \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java b/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java index 16e9256..0fcd83c 100644 --- a/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java +++ b/src/main/java/abstractSyntaxTree/Datatype/IntDatatype.java @@ -41,4 +41,9 @@ public class IntDatatype extends AbstractType implements IDatatype{ IntDatatype intDatatype = (IntDatatype) o; return (Objects.equals(value, intDatatype.value)); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java index 6352c80..2fe5f81 100644 --- a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java @@ -3,7 +3,6 @@ package abstractSyntaxTree.Expression; import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckHelper; import TypeCheck.AbstractType; -import abstractSyntaxTree.Datatype.IntDatatype; import abstractSyntaxTree.Parameter.ParameterList; import org.objectweb.asm.*; @@ -187,4 +186,9 @@ public class BinaryExpression extends AbstractType implements IExpression{ && Objects.equals(right, binaryExpression.right) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } \ 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 9730648..db3db58 100644 --- a/src/main/java/abstractSyntaxTree/Expression/IExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/IExpression.java @@ -15,4 +15,6 @@ public interface IExpression extends Node { // visit method for code generation void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception; + + TypeCheckResult getTypeCheckResult(); } diff --git a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java index b95d99e..11e42dc 100644 --- a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java @@ -9,7 +9,6 @@ import org.objectweb.asm.Opcodes; import java.util.HashMap; import java.util.LinkedHashMap; -import java.util.Objects; public class InstVarExpression implements IExpression{ @@ -74,4 +73,9 @@ public class InstVarExpression implements IExpression{ && Objects.equals(fieldName, instVarExpression.fieldName) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java index b1a1d88..e3e7392 100644 --- a/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/IntConstantExpression.java @@ -35,4 +35,9 @@ public class IntConstantExpression extends AbstractType implements IExpression{ return (Objects.equals(value, intConstantExpression.value) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java index 2e3ceb9..3ca68be 100644 --- a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java +++ b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java @@ -81,4 +81,9 @@ 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 6ff3aee..f2bdd20 100644 --- a/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java @@ -80,4 +80,9 @@ public class UnaryExpression extends AbstractType implements IExpression{ && Objects.equals(operand, unaryExpression.operand) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index b573fbc..8b4353b 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -99,4 +99,9 @@ public class BlockStatement extends AbstractType implements IStatement { && Objects.equals(statements, blockStatement.statements) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return 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 b5f1790..b14fa67 100644 --- a/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java @@ -28,4 +28,9 @@ public class EmptyStatement extends AbstractType implements IStatement{ public boolean equals(Object o) { return true; } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Statement/IStatement.java b/src/main/java/abstractSyntaxTree/Statement/IStatement.java index 5c9eee8..7e5c2b6 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IStatement.java @@ -14,4 +14,5 @@ public interface IStatement extends Node { TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception; void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception; + TypeCheckResult getTypeCheckResult(); } diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index 2f66e96..928c935 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -81,4 +81,9 @@ public class IfElseStatement extends AbstractType implements IStatement{ && Objects.equals(elseStatement, ifElseStatement.elseStatement) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java index 2e233a7..33d55f2 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -61,6 +61,11 @@ public class IfStatement extends AbstractType implements IStatement{ && Objects.equals(ifStatement, ifStatementObj.ifStatement) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java index 325e04a..6122ab2 100644 --- a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java +++ b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java @@ -59,4 +59,9 @@ public class LocalVarDecl implements IStatement{ && Objects.equals(identifier, localVarDecl.identifier) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java index 9845fde..a2018b1 100644 --- a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java @@ -63,4 +63,9 @@ public class ReturnStatement extends AbstractType implements IStatement{ return (Objects.equals(expression, returnStatement.expression) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return getTypeCheckResult(); + } } diff --git a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index a753df6..c65d6b5 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -70,4 +70,9 @@ public class WhileStatement extends AbstractType implements IStatement { && Objects.equals(statement, whileStatement.statement) ); } + + @Override + public TypeCheckResult getTypeCheckResult() { + return 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 36c9bc3..a1de06a 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -67,10 +67,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,10 +86,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, 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 6342503..1b7ab7e 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java @@ -1,6 +1,7 @@ package abstractSyntaxTree.StatementExpression; import TypeCheck.AbstractType; +import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Expression.IExpression; import abstractSyntaxTree.Parameter.ParameterList; @@ -8,6 +9,7 @@ import abstractSyntaxTree.Statement.IStatement; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -22,7 +24,13 @@ public class NewStatementExpression extends AbstractType implements IExpression, @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { - return null; + if(!TypeCheckHelper.typeExists(className, new ArrayList<>(typeContext.keySet()))){ + throw new Exception("TypeCheck Exception: An instance of " + className + " is created, but the type does not exist."); + } + TypeCheckResult result = new TypeCheckResult(); + result.type = className; + setTypeCheckResult(result); + return result; } @Override