From 8182e1ee7aba1359d209d0a079158cca923af973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Tue, 25 Jun 2024 17:01:00 +0200 Subject: [PATCH] added thisClass to some other classes --- src/main/java/Compiler.java | 12 +------ src/main/java/Input.java | 1 + .../abstractSyntaxTree/Class/MethodDecl.java | 3 +- .../abstractSyntaxTree/Class/RefType.java | 1 + src/main/java/abstractSyntaxTree/Program.java | 11 +++---- .../Statement/BlockStatement.java | 25 ++++++++++++--- .../Statement/IfElseStatement.java | 9 +++++- .../Statement/IfStatement.java | 4 +++ .../Statement/WhileStatement.java | 1 + .../AssignStatementExpression.java | 12 +++---- .../MethodCallStatementExpression.java | 32 ++++++++++++------- 11 files changed, 71 insertions(+), 40 deletions(-) diff --git a/src/main/java/Compiler.java b/src/main/java/Compiler.java index ae45c5e..599c4cf 100644 --- a/src/main/java/Compiler.java +++ b/src/main/java/Compiler.java @@ -1,15 +1,5 @@ import abstractSyntaxTree.Class.RefType; -import abstractSyntaxTree.Datatype.BoolDatatype; -import abstractSyntaxTree.Datatype.IntDatatype; -import abstractSyntaxTree.Expression.InstVarExpression; -import abstractSyntaxTree.Expression.LocalVarIdentifier; -import abstractSyntaxTree.Expression.UnaryExpression; import abstractSyntaxTree.Program; -import abstractSyntaxTree.Statement.IStatement; -import abstractSyntaxTree.Statement.IfElseStatement; -import abstractSyntaxTree.Statement.LocalVarDecl; -import abstractSyntaxTree.Statement.ReturnStatement; -import abstractSyntaxTree.StatementExpression.AssignStatementExpression; import astGenerator.ASTGenerator; import gen.DecafLexer; import gen.DecafParser; @@ -81,6 +71,6 @@ public class Compiler { abstractSyntaxTree.typeCheck(); - abstractSyntaxTree.codeGen(); + //abstractSyntaxTree.codeGen(); } } diff --git a/src/main/java/Input.java b/src/main/java/Input.java index c28e11e..20d6133 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -9,6 +9,7 @@ class Example1 { x = -3; int i = 5; Example e = new Example(); + e.m(1); return 0; } } diff --git a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java index b4cfcab..6d31059 100644 --- a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java @@ -42,9 +42,10 @@ public class MethodDecl implements Node { } TypeCheckResult result = new TypeCheckResult(); + codeBlock.thisClass = classThatContainsMethod; String CodeBlockType = codeBlock.typeCheck(methodContext, typeContext, localVars).type; if(!Objects.equals(this.returnType, CodeBlockType)) - throw new TypeCheckException("Method returns " + CodeBlockType + ", but should retrun " + this.returnType + ". "); + throw new TypeCheckException("Method returns " + CodeBlockType + ", but should return " + this.returnType + ". "); result.type = codeBlock.returnType; return result; } diff --git a/src/main/java/abstractSyntaxTree/Class/RefType.java b/src/main/java/abstractSyntaxTree/Class/RefType.java index 4c255bc..93c4927 100644 --- a/src/main/java/abstractSyntaxTree/Class/RefType.java +++ b/src/main/java/abstractSyntaxTree/Class/RefType.java @@ -67,6 +67,7 @@ public class RefType extends AbstractType implements Node { // type check each method for (MethodDecl methodDecl : methodDecls) { +// methodDecl.classThatContainsMethod = this.name; methodDecl.typeCheck(methodContext, typeContext); } diff --git a/src/main/java/abstractSyntaxTree/Program.java b/src/main/java/abstractSyntaxTree/Program.java index 2a0c919..eb6fa31 100644 --- a/src/main/java/abstractSyntaxTree/Program.java +++ b/src/main/java/abstractSyntaxTree/Program.java @@ -42,14 +42,13 @@ public class Program implements Node { typeContext.put(oneClass.name, classVars); // build method context - HashMap methodIdentifierAndParameter = new HashMap<>(); - HashMap> returnTypeAndMethod = new HashMap<>(); + HashMap returnTypeAndParameter = new HashMap<>(); + HashMap> identifierAndMethod = new HashMap<>(); for (MethodDecl methodDecl : oneClass.methodDecls){ - - methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters); - returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter); + returnTypeAndParameter.put(methodDecl.returnType, methodDecl.parameters); + identifierAndMethod.put(methodDecl.name, returnTypeAndParameter); } - methodContext.put(oneClass.name, returnTypeAndMethod); + methodContext.put(oneClass.name, identifierAndMethod); } // typecheck each class diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index af76b02..3c5eefe 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -6,6 +6,8 @@ import TypeCheck.AbstractType; import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Parameter.Parameter; import abstractSyntaxTree.Parameter.ParameterList; +import abstractSyntaxTree.StatementExpression.AssignStatementExpression; +import abstractSyntaxTree.StatementExpression.MethodCallStatementExpression; import org.objectweb.asm.*; import java.util.HashMap; @@ -19,6 +21,7 @@ public class BlockStatement extends AbstractType implements IStatement { HashMap localVars; public String returnType; // "not" --> not void public List statements; + public String thisClass; // do we need expression, statementexpression public BlockStatement(List statements, String returnType) { @@ -33,15 +36,30 @@ public class BlockStatement extends AbstractType implements IStatement { TypeCheckResult result = new TypeCheckResult(); this.localVars = localVars; - if (statements.size() == 0) { + if (statements.isEmpty()) { result.type = "void"; } for (IStatement statement : statements) { + if(statement instanceof IfStatement ifStatement){ + ifStatement.thisClass = this.thisClass; + } + if(statement instanceof IfElseStatement ifElseStatement){ + ifElseStatement.thisClass = this.thisClass; + } + if(statement instanceof WhileStatement whileStatement){ + whileStatement.thisClass = this.thisClass; + } + if(statement instanceof MethodCallStatementExpression methodCall){ + methodCall.thisClass = thisClass; + } + if(statement instanceof AssignStatementExpression assignStatementExpression){ + assignStatementExpression.thisClass = thisClass; + } + TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); - if(statement instanceof LocalVarDecl){ - LocalVarDecl localVarDecl = (LocalVarDecl) statement; + if(statement instanceof LocalVarDecl localVarDecl){ localVars.put(localVarDecl.identifier, localVarDecl.type); } @@ -70,7 +88,6 @@ public class BlockStatement extends AbstractType implements IStatement { } } - if(typeOfCurrentStatement.type.equals("void")) continue; // set return of block if not known yet diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index d34270e..af055c4 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -16,6 +16,7 @@ public class IfElseStatement extends AbstractType implements IStatement{ public IExpression condition; IStatement ifStatement; IStatement elseStatement; + public String thisClass; public IfElseStatement(IExpression condition, IStatement ifStatement, IStatement elseStatement) { this.condition = condition; @@ -23,11 +24,17 @@ public class IfElseStatement extends AbstractType implements IStatement{ this.elseStatement = elseStatement; } - @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws TypeCheckException { TypeCheckResult result = new TypeCheckResult(); + if(ifStatement instanceof BlockStatement blockStatement){ + blockStatement.thisClass = thisClass; + } + if(elseStatement instanceof BlockStatement blockStatement){ + blockStatement.thisClass = thisClass; + } + TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars); if (!conditionType.type.equals("boolean")) { diff --git a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java index c54fe62..0084276 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -17,6 +17,7 @@ public class IfStatement extends AbstractType implements IStatement{ //Do we need a block statement here? IStatement ifStatement; + public String thisClass; public IfStatement(IExpression condition, IStatement ifStatement) { this.condition = condition; @@ -33,6 +34,9 @@ public class IfStatement extends AbstractType implements IStatement{ throw new TypeCheckException("Condition of If-Statement should is " + conditionType.type + ", but should be boolean."); } + if(ifStatement instanceof BlockStatement blockStatement){ + blockStatement.thisClass = thisClass; + } TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars); result.type = ifStatementType.type; return result; diff --git a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index e063cdd..bf61905 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -15,6 +15,7 @@ import java.util.Objects; public class WhileStatement extends AbstractType implements IStatement { IExpression condition; IStatement statement; + public String thisClass; public WhileStatement(IExpression condition, IStatement statement) { this.condition = condition; diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index a445ccc..fe4db08 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -21,6 +21,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi public IExpression left; public IExpression right; private InstVarExpression instVar; + public String thisClass; public AssignStatementExpression(String operator, IExpression leftExpression, IExpression rightExpression){ this.operator = operator; @@ -34,16 +35,15 @@ public class AssignStatementExpression extends AbstractType implements IExpressi TypeCheckResult leftType; - if (left instanceof LocalVarIdentifier) { + if (left instanceof LocalVarIdentifier localVarIdentifier) { leftType = new TypeCheckResult(); - LocalVarIdentifier localVarIdentifier = (LocalVarIdentifier) left; String identifier = localVarIdentifier.getIdentifier(); leftType.type = localVars.get(identifier); - // actually instavar of this -// if(leftType == null){ -// localVarIdentifier. -// } + // local var may be actually instvar of this + if(leftType.type == null){ + leftType.type = typeContext.get(thisClass).get(identifier); + } }else{ leftType = left.typeCheck(methodContext, typeContext, localVars); } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index 92646c0..1d33317 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -20,8 +20,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr List arguments; Receiver receiver; List receivingMethods; - RefType classThatHasTheMethodIfNotThis; - RefType thisClass; + public String thisClass; public MethodCallStatementExpression(String methodName, Receiver receiver, List receivingMethods, List arguments) { this.methodName = methodName; @@ -33,8 +32,18 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws TypeCheckException { //todo i really dont get this. waiting for tuesday meeting - throw new TypeCheckException("NOT IMPLEMENTED"); + + String classToSearchMethodIn = thisClass; + + //method is called on something that is not this + if(!receiver.thisExpression){ + classToSearchMethodIn = localVars.get(receiver.identifier); + } + //if classToSearchMethodIn does not conatin method, throw exception. go through list and check each + + + throw new TypeCheckException("NOT IMPLEMENTED"); // TypeCheckResult result = new TypeCheckResult(); // String receivingField; // String classContainingMethod; @@ -58,6 +67,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr // } // // return result; + //return null; } //Errors occur due to the change in parameter in the RefType class @@ -94,15 +104,15 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr String descriptor; - List methodDecls = thisClass.methodDecls; - for (MethodDecl methodDecl : methodDecls) { - if (methodDecl.name.equals(methodName)) { - //Get the method descriptor - //descriptor = methodDecl.getMethodDescriptor(methodContext); //methodContext is missing - } - } + // List methodDecls = thisClass.methodDecls; +// for (MethodDecl methodDecl : methodDecls) { +// if (methodDecl.name.equals(methodName)) { +// //Get the method descriptor +// //descriptor = methodDecl.getMethodDescriptor(methodContext); //methodContext is missing +// } +// } // Invoke the method - String className = classThatHasTheMethodIfNotThis != null ? classThatHasTheMethodIfNotThis.name : thisClass.name; +// String className = classThatHasTheMethodIfNotThis != null ? classThatHasTheMethodIfNotThis.name : thisClass.name; //mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, methodName, descriptor, false); }