From c764b710ea5a59607331724daa304705ee628b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Thu, 20 Jun 2024 13:59:44 +0200 Subject: [PATCH] calling newstatementexpression from localvar --- src/main/java/Input.java | 7 +++---- .../abstractSyntaxTree/Statement/LocalVarDecl.java | 10 +++++++++- .../StatementExpression/NewStatementExpression.java | 1 - 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/Input.java b/src/main/java/Input.java index 9bca38c..d9b2534 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -2,11 +2,10 @@ class Example1 { int i; Example e; int m(int n){ + int x; + x = -3; + int i = 5; Example e = new Example(); - e.m(1); - this.e = new Example(); - this.e.m(2); - this.m(2); return 0; } } diff --git a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java index 6d19b22..fd748c3 100644 --- a/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java +++ b/src/main/java/abstractSyntaxTree/Statement/LocalVarDecl.java @@ -22,12 +22,20 @@ public class LocalVarDecl extends AbstractType implements IStatement{ } @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws TypeCheckException { - TypeCheckHelper.typeExists(this.type, new ArrayList<>(methodContext.keySet())); + // left + TypeCheckHelper.typeExists(this.type, new ArrayList<>(methodContext.keySet())); localVars.put(this.identifier, this.type); + // right part if existing + if(expression != null){ + expression.typeCheck(methodContext, typeContext, localVars); + } + + TypeCheckResult result = new TypeCheckResult(); result.type = "void"; + return result; } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java index 3beba9b..a868c0e 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java @@ -28,7 +28,6 @@ public class NewStatementExpression extends AbstractType implements IExpression, @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws TypeCheckException { - //todo testen wenn in ast vorhanden if(!TypeCheckHelper.typeExists(className, new ArrayList<>(typeContext.keySet()))){ throw new TypeCheckException("An instance of " + className + " is created, but the type does not exist."); }