From 94b3830561ee66d42b565247356fe0eee4e309f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Tue, 2 Jul 2024 15:36:27 +0200 Subject: [PATCH] instvar special case --- src/main/java/TestClass.java | 1 + src/main/java/TypeCheck/TypeCheckHelper.java | 7 ++++--- .../StatementExpression/AssignStatementExpression.java | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/TestClass.java b/src/main/java/TestClass.java index 55a283e..827e3c9 100644 --- a/src/main/java/TestClass.java +++ b/src/main/java/TestClass.java @@ -5,6 +5,7 @@ class TestClass { public TestClass(){i = 3456;} int meth(int n){ Example e = new Example(); + e.example1 = new TestClass(); int abc = e.example1.m1(2).m2().m3(); return 0; diff --git a/src/main/java/TypeCheck/TypeCheckHelper.java b/src/main/java/TypeCheck/TypeCheckHelper.java index 2ffee4b..dd3ef96 100644 --- a/src/main/java/TypeCheck/TypeCheckHelper.java +++ b/src/main/java/TypeCheck/TypeCheckHelper.java @@ -8,17 +8,18 @@ public class TypeCheckHelper { 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"); - String result; + String result = "class"; if(type1Primitiv && type2Primitiv){ if(Objects.equals(type1, type2)){ result = type1; }else{ throw new TypeCheckException("There is no upper bound between " + type1 + " and " + type2 + "."); } - }else if(type1Primitiv || type2Primitiv){ + }else if(type1Primitiv ^ type2Primitiv){ throw new TypeCheckException("There is no upper bound between " + type1 + " and " + type2 + "."); }else{ - result = "class"; + if(Objects.equals(type1, type2)) + result = type1; } return result; } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index 5556dcb..e970e61 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -45,6 +45,8 @@ public class AssignStatementExpression extends AbstractType implements IExpressi leftType.type = typeContext.get(thisClass).get(identifier); } }else{ + if(left instanceof InstVarExpression instVarExpression) + instVarExpression.thisClass = this.thisClass; leftType = left.typeCheck(methodContext, typeContext, localVars); } TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars);