diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 1718d01..191e3a9 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -254,7 +254,7 @@ public class SemanticAnalyzer implements SemanticVisitor { currentNullType = lResult.getType(); var rResult = rExpression.accept(this); - if (!Objects.equals(currentFields.get(toCheck.assignable.identifier), rExpression.getType())) { + if (!Objects.equals(currentScope.getLocalVar(toCheck.assignable.identifier), rExpression.getType())) { errors.add(new TypeMismatchException( "Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \"" + rResult.getType() + "\"")); @@ -265,7 +265,7 @@ public class SemanticAnalyzer implements SemanticVisitor { // } valid = valid && lResult.isValid() && rResult.isValid(); currentNullType = null; - return new TypeCheckResult(valid, new BaseType(TypeEnum.VOID)); // return type is null to get the return type sufficently + return new TypeCheckResult(valid, null); // return type is null to get the return type sufficently } @Override @@ -352,12 +352,14 @@ public class SemanticAnalyzer implements SemanticVisitor { public TypeCheckResult analyze(UnaryExpressionNode unary) { var valid = true; - if (!currentScope.contains(unary.identifier)) { - errors.add(new NotDeclearedException("Not declared " + unary.identifier)); - return new TypeCheckResult(false, null); + if (currentScope.contains(unary.identifier)) { + return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier)); + } else if(currentFields.get(unary.identifier) != null) { + return new TypeCheckResult(valid, currentFields.get(unary.identifier)); + } else { + errors.add(new NotDeclearedException("Var is not Decleared")); } - - return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier)); + return new TypeCheckResult(valid, null); } } \ No newline at end of file diff --git a/src/test/java/semantic/EndToTAST.java b/src/test/java/semantic/EndToTAST.java index 3da4043..dc30e0b 100644 --- a/src/test/java/semantic/EndToTAST.java +++ b/src/test/java/semantic/EndToTAST.java @@ -155,30 +155,6 @@ public class EndToTAST { } - @Test - public void correctInstVarTest(){ - - CharStream codeCharStream = null; - try { - codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/semantic/endToTAST/CorrectInstVar.java")); - } catch (IOException e) { - throw new RuntimeException(e); - } - SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); - CommonTokenStream tokenStream = new CommonTokenStream(lexer); - - SimpleJavaParser parser = new SimpleJavaParser(tokenStream); - ParseTree parseTree = parser.program(); - - ASTBuilder astBuilder = new ASTBuilder(); - ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree); - - ASTNode tast = SemanticAnalyzer.generateTast(abstractSyntaxTree); - - assertTrue(SemanticAnalyzer.errors.isEmpty()); - - } - @Test public void typeMismatchRefType(){ diff --git a/src/test/resources/semantic/endToTAST/CorrectInstVar.java b/src/test/resources/semantic/endToTAST/CorrectInstVar.java deleted file mode 100644 index 5c20dab..0000000 --- a/src/test/resources/semantic/endToTAST/CorrectInstVar.java +++ /dev/null @@ -1,18 +0,0 @@ -public class Test { - - public Example ex; - - public static int testMethod(char x){ - - int b = this.ex.a; - - return x; - } - -} - -public class Example { - - public int a; - -} \ No newline at end of file diff --git a/src/test/resources/semantic/endToTAST/CorrectTest.java b/src/test/resources/semantic/endToTAST/CorrectTest.java index ee71670..f0cdb30 100644 --- a/src/test/resources/semantic/endToTAST/CorrectTest.java +++ b/src/test/resources/semantic/endToTAST/CorrectTest.java @@ -3,16 +3,9 @@ public class Example { public int a; public static int testMethod(int b){ - a = b; + return a; } -} - -public class Test { - - public static int testMethod(char x, int a){ - return x; - } } \ No newline at end of file