From b95bc75a1ef86540a73af2f804278dfa668b96a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Tue, 25 Jun 2024 08:16:29 +0200 Subject: [PATCH] made upperbound method static --- src/main/java/Input.java | 1 + src/main/java/TypeCheck/TypeCheckHelper.java | 2 +- .../abstractSyntaxTree/Expression/BinaryExpression.java | 7 +++---- .../StatementExpression/AssignStatementExpression.java | 3 +-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/Input.java b/src/main/java/Input.java index d9b2534..b047c82 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -1,5 +1,6 @@ class Example1 { int i; + int a = 12345; Example e; int m(int n){ int x; diff --git a/src/main/java/TypeCheck/TypeCheckHelper.java b/src/main/java/TypeCheck/TypeCheckHelper.java index 6e7542a..173a21e 100644 --- a/src/main/java/TypeCheck/TypeCheckHelper.java +++ b/src/main/java/TypeCheck/TypeCheckHelper.java @@ -4,7 +4,7 @@ import java.util.List; import java.util.Objects; public class TypeCheckHelper { - public String upperBound(String type1, String type2) throws TypeCheckException{ + public static String upperBound(String type1, String type2) throws TypeCheckException{ boolean type1Primitiv = Objects.equals(type1, "boolean") || Objects.equals(type1, "int") || Objects.equals(type1, "char"); boolean type2Primitiv = Objects.equals(type2, "boolean") || Objects.equals(type2, "int") || Objects.equals(type2, "char"); diff --git a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java index 4cec259..ba0041d 100644 --- a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java @@ -26,7 +26,6 @@ public class BinaryExpression extends AbstractType implements IExpression{ @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws TypeCheckException { - TypeCheckHelper helper = new TypeCheckHelper(); TypeCheckResult result = new TypeCheckResult(); TypeCheckResult leftType = left.typeCheck(methodContext, typeContext, localVars); @@ -36,7 +35,7 @@ public class BinaryExpression extends AbstractType implements IExpression{ case "&&": case "||" :{ - if (Objects.equals(helper.upperBound(leftType.type, rightType.type), "bool")){ + if (Objects.equals(TypeCheckHelper.upperBound(leftType.type, rightType.type), "bool")){ result.type = "bool"; } break; @@ -47,14 +46,14 @@ public class BinaryExpression extends AbstractType implements IExpression{ case "<=": case ">=": case "!=": - result.type = helper.upperBound(leftType.type, rightType.type); + result.type = TypeCheckHelper.upperBound(leftType.type, rightType.type); break; case "-": case "+": case "*": case "/": - if (Objects.equals(helper.upperBound(leftType.type, rightType.type), "int")){ + if (Objects.equals(TypeCheckHelper.upperBound(leftType.type, rightType.type), "int")){ result.type = "int"; } break; diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index eb31a57..e8975f8 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -30,7 +30,6 @@ public class AssignStatementExpression extends AbstractType implements IExpressi @Override public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws TypeCheckException { - TypeCheckHelper helper = new TypeCheckHelper(); TypeCheckResult result = new TypeCheckResult(); TypeCheckResult leftType; @@ -45,7 +44,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi } TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); - String upperbound = helper.upperBound(leftType.type, rightType.type); + String upperbound = TypeCheckHelper.upperBound(leftType.type, rightType.type); if(Objects.equals(leftType.type, "boolean")) leftType.type = "bool"; if (!Objects.equals(upperbound, leftType.type)) {