From a9101d2c1227aac63d25dc96e66c603b7f90198e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Wed, 8 May 2024 12:48:56 +0200 Subject: [PATCH] working on class/method tables --- Source/abstractSyntaxTree/Class/FieldDecl.java | 11 +++++++---- .../abstractSyntaxTree/Class/MethodDecl.java | 15 +++++++++------ .../abstractSyntaxTree/Datatype/RefType.java | 18 +++++++++--------- Source/abstractSyntaxTree/Program.java | 7 ++++++- .../Statement/BlockStatement.java | 4 ++-- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Source/abstractSyntaxTree/Class/FieldDecl.java b/Source/abstractSyntaxTree/Class/FieldDecl.java index f22e2f6..98e2225 100644 --- a/Source/abstractSyntaxTree/Class/FieldDecl.java +++ b/Source/abstractSyntaxTree/Class/FieldDecl.java @@ -6,14 +6,17 @@ import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Program; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; public class FieldDecl extends AbstractType implements IClass{ - private Program program; - private String identifier; - public FieldDecl(Program program){ - this.program = program; + private HashMap> typeContext; // form class from program + public String type; // from parser + public String identifier; // from parser + + public FieldDecl(HashMap> typeContext){ + this.typeContext = typeContext; } public TypeCheckResult typeCheck(List classFieldsIdentifier) throws Exception { TypeCheckResult result = new TypeCheckResult(); diff --git a/Source/abstractSyntaxTree/Class/MethodDecl.java b/Source/abstractSyntaxTree/Class/MethodDecl.java index 89f9fad..fce967a 100644 --- a/Source/abstractSyntaxTree/Class/MethodDecl.java +++ b/Source/abstractSyntaxTree/Class/MethodDecl.java @@ -8,17 +8,20 @@ import java.util.List; public class MethodDecl implements IClass { - private Program program; + private HashMap>> methodContext; + private HashMap> typeContext; - private HashMap localVars; + private HashMap localVars; // (type, identifier) // add content here - public MethodDecl(Program program){ - this.program = program; + public MethodDecl(HashMap>> methodContext, HashMap> typeContext){ + this.methodContext = methodContext; + this.typeContext = typeContext; } public TypeCheckResult typeCheck(List fieldsOrMethods) throws Exception { + // write localvars + + // jede methode als block statement aufrufen return null; -// write localvars - // write method table } } diff --git a/Source/abstractSyntaxTree/Datatype/RefType.java b/Source/abstractSyntaxTree/Datatype/RefType.java index 59781a6..42f9876 100644 --- a/Source/abstractSyntaxTree/Datatype/RefType.java +++ b/Source/abstractSyntaxTree/Datatype/RefType.java @@ -14,22 +14,22 @@ public class RefType extends AbstractType implements IDatatype { String name; public List fieldDecls; - - private HashMap fieldsTable; // type, identifier public List methodDecls; - private HashMap methodsTable; // return type, method name without parameter - private Program program; + private HashMap> typeContext; // (class, (type, identifier)) + private HashMap>> methodContext; // (class, (returntype, (identifier, parameter) private boolean hasMain; - - public RefType(Program program){ - this.program = program; - } - public RefType(List fieldDecls, List methodDecls){ + public RefType(List fieldDecls, + List methodDecls, + HashMap> typeContext, + HashMap>> methodContext){ this.fieldDecls = fieldDecls; this.methodDecls = methodDecls; + this.typeContext = typeContext; + this.methodContext = methodContext; } + @Override public TypeCheckResult typeCheck() throws Exception { TypeCheckResult result = new TypeCheckResult(); diff --git a/Source/abstractSyntaxTree/Program.java b/Source/abstractSyntaxTree/Program.java index c7c8469..8d06220 100644 --- a/Source/abstractSyntaxTree/Program.java +++ b/Source/abstractSyntaxTree/Program.java @@ -1,6 +1,7 @@ package abstractSyntaxTree; import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Datatype.RefType; import java.util.HashMap; @@ -9,10 +10,14 @@ import java.util.List; public class Program { public List classes; - public HashMap> classTypeIndentifierTable; // (class, (type, identifier)) + public HashMap> typeContext; // (class, (type, identifier)) + public HashMap>> methodContext; // (class, (returntype, (identifier, parameter))) public TypeCheckResult typeCheck() throws Exception{ for(RefType oneClass : classes){ + HashMap classVars = new HashMap<>(); + for (FieldDecl fielsDecl: oneClass.fieldDecls) + classVars.put(fielsDecl.type, fielsDecl.identifier); oneClass.typeCheck(); } return null; diff --git a/Source/abstractSyntaxTree/Statement/BlockStatement.java b/Source/abstractSyntaxTree/Statement/BlockStatement.java index be04bbc..84cb0d7 100644 --- a/Source/abstractSyntaxTree/Statement/BlockStatement.java +++ b/Source/abstractSyntaxTree/Statement/BlockStatement.java @@ -2,8 +2,6 @@ package abstractSyntaxTree.Statement; import TypeCheck.TypeCheckResult; import TypeCheck.AbstractType; -import abstractSyntaxTree.Expression.IExpression; -import org.objectweb.asm.MethodVisitor; import java.util.HashMap; import java.util.List; @@ -14,6 +12,8 @@ public class BlockStatement extends AbstractType implements IStatement{ HashMap localVars; HashMap typeIndentifierTable; // from program List statements; + // do we need expression, statementexpression + public BlockStatement(List statements, HashMap localVars, HashMap typeIndentifierTable){ this.statements = statements;