From 783bab85800aa1a59376865a23044c66ce4954c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Wed, 8 May 2024 14:28:51 +0200 Subject: [PATCH] context tables --- Source/.idea/Source.iml | 1 + Source/.idea/libraries/asm_9_7.xml | 9 +++++++ .../abstractSyntaxTree/Class/MethodDecl.java | 16 +++++-------- .../abstractSyntaxTree/Datatype/RefType.java | 4 ++-- Source/abstractSyntaxTree/Program.java | 24 ++++++++++++++----- .../MethodCallStatementExpression.java | 8 +++++-- 6 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 Source/.idea/libraries/asm_9_7.xml diff --git a/Source/.idea/Source.iml b/Source/.idea/Source.iml index b107a2d..f9bd605 100644 --- a/Source/.idea/Source.iml +++ b/Source/.idea/Source.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/Source/.idea/libraries/asm_9_7.xml b/Source/.idea/libraries/asm_9_7.xml new file mode 100644 index 0000000..1d812dc --- /dev/null +++ b/Source/.idea/libraries/asm_9_7.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Source/abstractSyntaxTree/Class/MethodDecl.java b/Source/abstractSyntaxTree/Class/MethodDecl.java index 10b2fa1..ec150be 100644 --- a/Source/abstractSyntaxTree/Class/MethodDecl.java +++ b/Source/abstractSyntaxTree/Class/MethodDecl.java @@ -1,30 +1,26 @@ package abstractSyntaxTree.Class; import TypeCheck.TypeCheckResult; -import abstractSyntaxTree.Program; import java.util.HashMap; import java.util.List; public class MethodDecl implements IClass { - // name + public String classThatContainsMethod; + public String name; + public List parameters; + public String returnType; - private HashMap>> methodContext; - private HashMap> typeContext; //TODO: Move this into the typeCheck private HashMap localVars; // (type, identifier) // add content here - public MethodDecl(HashMap>> methodContext, HashMap> typeContext){ - this.methodContext = methodContext; - this.typeContext = typeContext; - } - public TypeCheckResult typeCheck(List fieldsOrMethods) throws Exception { + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, List fieldsOrMethods) throws Exception { // write localvars + // jede methode als block statement aufrufen return null; } - } diff --git a/Source/abstractSyntaxTree/Datatype/RefType.java b/Source/abstractSyntaxTree/Datatype/RefType.java index 1c3d0ab..11438ee 100644 --- a/Source/abstractSyntaxTree/Datatype/RefType.java +++ b/Source/abstractSyntaxTree/Datatype/RefType.java @@ -13,7 +13,7 @@ import java.util.List; public class RefType extends AbstractType implements IDatatype { - String name; + public String name; public List fieldDecls; public List methodDecls; private HashMap> typeContext; // (class, (type, identifier)) @@ -40,7 +40,7 @@ public class RefType extends AbstractType implements IDatatype { } for (MethodDecl methodDecl : methodDecls) { - methodDecl.typeCheck(methodDecls); + methodDecl.typeCheck(methodContext, typeContext, methodDecls); } result.type = "class"; diff --git a/Source/abstractSyntaxTree/Program.java b/Source/abstractSyntaxTree/Program.java index 0c9aa5d..415a256 100644 --- a/Source/abstractSyntaxTree/Program.java +++ b/Source/abstractSyntaxTree/Program.java @@ -2,23 +2,35 @@ package abstractSyntaxTree; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Class.FieldDecl; +import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Datatype.RefType; import org.objectweb.asm.MethodVisitor; -import java.util.HashMap; -import java.util.List; +import java.util.*; public class Program { public List classes; public HashMap> typeContext; // (class, (type, identifier)) - public HashMap>> methodContext; // (class, (returntype, (identifier, parameter))) + 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); + for (FieldDecl fieldDecl: oneClass.fieldDecls){ + classVars.put(fieldDecl.type, fieldDecl.identifier); + } + typeContext.put(oneClass.name, classVars); + + HashMap> methodIdentifierAndParameter = new HashMap<>(); + HashMap>> returnTypeAndMethod = new HashMap<>(); + for (MethodDecl methodDecl : oneClass.methodDecls){ + methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters); + returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter); + } + + methodContext.put(oneClass.name, returnTypeAndMethod); + oneClass.typeCheck(); } return null; @@ -26,7 +38,7 @@ public class Program { public void codeGen() throws Exception{ for(RefType oneClass : classes){ - oneClass.codeGen(); + //oneClass.codeGen(); } } } diff --git a/Source/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/Source/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index bffaa94..659ea6b 100644 --- a/Source/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/Source/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -43,16 +43,20 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr } @Override + public void codeGen(MethodVisitor mv) throws Exception { + + } + public void CodeGen(MethodVisitor mv) throws Exception { //Generate Bytecode for the receiver if(classThatHasTheMethodIfNotThis != null){ - classThatHasTheMethodIfNotThis.CodeGen(mv); + classThatHasTheMethodIfNotThis.codeGen(mv); } else { mv.visitVarInsn(Opcodes.ALOAD, 0); } for (IExpression argument : arguments) { - argument.CodeGen(mv); + argument.codeGen(mv); } //We need the class reference and the return type of the method