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;