From 08d13a0df7460131a46b9b405d6fb2484510476f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Fri, 31 May 2024 09:39:17 +0200 Subject: [PATCH] method parameters --- .../abstractSyntaxTree/Class/FieldDecl.java | 10 ++++++++- .../Expression/BinaryExpression.java | 12 +++------- .../Expression/InstVarExpression.java | 7 ++++-- .../Expression/LocalVarIdentifier.java | 5 ++++- .../Expression/UnaryExpression.java | 5 ++++- src/main/java/abstractSyntaxTree/Program.java | 4 +++- .../Statement/ReturnStatement.java | 4 ++-- .../AssignStatementExpression.java | 22 +++++++++---------- 8 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/Class/FieldDecl.java b/src/main/java/abstractSyntaxTree/Class/FieldDecl.java index 54c0951..baa0f78 100644 --- a/src/main/java/abstractSyntaxTree/Class/FieldDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/FieldDecl.java @@ -26,7 +26,15 @@ public class FieldDecl extends AbstractType implements Node { TypeCheckResult result = new TypeCheckResult(); - TypeCheckHelper.typeExists(this.type, typeContext.keySet().stream().toList()); + List typesList= new ArrayList<>(); + + for (HashMap innerMap : typeContext.values()) { + for (String value : innerMap.values()) { + typesList.add(value); + } + } + + TypeCheckHelper.typeExists(this.type, typesList); result.type = this.type; setTypeCheckResult(result); diff --git a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java index 7e8f8a8..7b0b2f8 100644 --- a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java @@ -17,12 +17,12 @@ public class BinaryExpression extends AbstractType implements IExpression{ public IExpression right; @Override - public TypeCheckResult typeCheck() throws Exception { + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { TypeCheckHelper helper = new TypeCheckHelper(); TypeCheckResult result = new TypeCheckResult(); - TypeCheckResult leftType = left.typeCheck(); - TypeCheckResult rightType = right.typeCheck(); + TypeCheckResult leftType = left.typeCheck(methodContext, typeContext, localVars); + TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); switch (operator) { @@ -59,12 +59,6 @@ public class BinaryExpression extends AbstractType implements IExpression{ return result; } - - @Override - public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { - return null; - } - @Override public void codeGen(MethodVisitor mv) throws Exception { // Label for the jump instruction diff --git a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java index fed4bec..8a63a44 100644 --- a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java @@ -24,8 +24,11 @@ public class InstVarExpression implements IExpression{ @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { - //todo - return null; + //todo /////////////////// + String varType = typeContext.get(classRef.name).get(fieldName); + TypeCheckResult result = new TypeCheckResult(); + result.type = varType; + return result; } @Override diff --git a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java index 7c1888a..cc8ef3c 100644 --- a/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java +++ b/src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java @@ -2,8 +2,11 @@ package abstractSyntaxTree.Expression; import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Parameter.ParameterList; import org.objectweb.asm.MethodVisitor; +import java.util.HashMap; + public class LocalVarIdentifier implements IExpression{ String identifier; @@ -16,7 +19,7 @@ public class LocalVarIdentifier implements IExpression{ } @Override - public TypeCheckResult typeCheck() throws Exception { + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { return null; } diff --git a/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java index 6fb57f9..69149c4 100644 --- a/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java @@ -4,9 +4,11 @@ import TypeCheck.AbstractType; import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Datatype.IDatatype; +import abstractSyntaxTree.Parameter.ParameterList; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +import java.util.HashMap; import java.util.Objects; public class UnaryExpression extends AbstractType implements IExpression{ @@ -16,8 +18,9 @@ public class UnaryExpression extends AbstractType implements IExpression{ this.operator = operator; this.operand = operand; } + @Override - public TypeCheckResult typeCheck() throws Exception { + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { TypeCheckResult result = new TypeCheckResult(); TypeCheckResult operandTypeCheckResult = operand.typeCheck(); diff --git a/src/main/java/abstractSyntaxTree/Program.java b/src/main/java/abstractSyntaxTree/Program.java index fa9397b..aa6d76b 100644 --- a/src/main/java/abstractSyntaxTree/Program.java +++ b/src/main/java/abstractSyntaxTree/Program.java @@ -36,7 +36,9 @@ public class Program implements Node { // build type context HashMap classVars = new HashMap<>(); for (FieldDecl fieldDecl: oneClass.fieldDecls){ - classVars.put(fieldDecl.type, fieldDecl.identifier); + if(fieldDecl.type == "boolean") + fieldDecl.type = "bool"; + classVars.put(fieldDecl.identifier, fieldDecl.type); } typeContext.put(oneClass.name, classVars); diff --git a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java index 358ee25..e4a2530 100644 --- a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java @@ -23,7 +23,7 @@ public class ReturnStatement extends AbstractType implements IStatement{ if (expression == null) { result.type = "void"; } else { - TypeCheckResult typedExpression = expression.typeCheck(); + TypeCheckResult typedExpression = expression.typeCheck(methodContext, typeContext, localVars); result.type = typedExpression.type; } @@ -39,7 +39,7 @@ public class ReturnStatement extends AbstractType implements IStatement{ if (expression != null) { expression.codeGen(mv); //Get the Type of the expression - String type = expression.typeCheck().type; + String type = expression.typeCheck(null, null, null).type; if (type.equals("int") || type.equals("bool") || type.equals("char")) { mv.visitInsn(Opcodes.IRETURN); diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index 3f289dc..43045ba 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -37,23 +37,23 @@ public class AssignStatementExpression extends AbstractType implements IExpressi String identifier = localVarIdentifier.getIdentifier(); leftType.type = localVars.get(identifier); }else{ - //leftType = left.typeCheck(); + leftType = left.typeCheck(methodContext, typeContext, localVars); } -// TypeCheckResult rightType = right.typeCheck(); -// -// String upperbound = helper.upperBound(leftType.type, rightType.type); -// if (Objects.equals(upperbound, leftType.type)) { -// result.type = leftType.type; -// } -// setTypeCheckResult(result); + TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); + + String upperbound = helper.upperBound(leftType.type, rightType.type); + if (Objects.equals(upperbound, leftType.type)) { + result.type = leftType.type; + } + setTypeCheckResult(result); return result; } @Override public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { - if (left instanceof VarRefExpression varRef) { - - } +// if (left instanceof VarRefExpression varRef) { +// +// } }