johns-branch #12

Merged
i22005 merged 23 commits from johns-branch into main 2024-06-21 16:30:56 +00:00
4 changed files with 10 additions and 57 deletions
Showing only changes of commit 7b41c45cd5 - Show all commits

View File

@ -254,7 +254,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
currentNullType = lResult.getType(); currentNullType = lResult.getType();
var rResult = rExpression.accept(this); 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( errors.add(new TypeMismatchException(
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \"" "Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
+ rResult.getType() + "\"")); + rResult.getType() + "\""));
@ -265,7 +265,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
// } // }
valid = valid && lResult.isValid() && rResult.isValid(); valid = valid && lResult.isValid() && rResult.isValid();
currentNullType = null; 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 @Override
@ -352,12 +352,14 @@ public class SemanticAnalyzer implements SemanticVisitor {
public TypeCheckResult analyze(UnaryExpressionNode unary) { public TypeCheckResult analyze(UnaryExpressionNode unary) {
var valid = true; var valid = true;
if (!currentScope.contains(unary.identifier)) { if (currentScope.contains(unary.identifier)) {
errors.add(new NotDeclearedException("Not declared " + unary.identifier)); return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
return new TypeCheckResult(false, null); } 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, null);
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
} }
} }

View File

@ -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 @Test
public void typeMismatchRefType(){ public void typeMismatchRefType(){

View File

@ -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;
}

View File

@ -3,16 +3,9 @@ public class Example {
public int a; public int a;
public static int testMethod(int b){ public static int testMethod(int b){
a = b; a = b;
return a;
} }
}
public class Test {
public static int testMethod(char x, int a){
return x;
}
} }