From 56d316a6d0dd7a4b46841ff9ed2641093f40be2d Mon Sep 17 00:00:00 2001 From: Bruder John Date: Sat, 1 Jun 2024 18:37:13 +0200 Subject: [PATCH] Changed to InstVar --- .../ast/expression/ExpresssionOperator.java | 4 +- src/main/java/ast/expression/InstVar.java | 23 +++++++++ .../statement/AssignmentStatementNode.java | 11 +++-- src/main/java/ast/type/This.java | 9 +++- src/main/java/classFileOutput/Example.class | Bin 167 -> 0 bytes src/main/java/parser/ASTBuilder.java | 5 +- src/main/java/semantic/SemanticAnalyzer.java | 44 +++++++++++++----- src/main/java/semantic/SemanticVisitor.java | 3 ++ .../exeptions/NotDeclearedException.java | 9 ++++ src/test/java/semantic/Mocker.java | 19 ++------ 10 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 src/main/java/ast/expression/InstVar.java create mode 100644 src/main/java/semantic/exeptions/NotDeclearedException.java diff --git a/src/main/java/ast/expression/ExpresssionOperator.java b/src/main/java/ast/expression/ExpresssionOperator.java index b1f62a3..42bc137 100644 --- a/src/main/java/ast/expression/ExpresssionOperator.java +++ b/src/main/java/ast/expression/ExpresssionOperator.java @@ -1,13 +1,13 @@ package ast.expression; public enum ExpresssionOperator { - DOT, // . + DOT, // . NICHT MEHR GEBRAUCHT PLUS, // + MINUS, // - MULTIPLY, // * DIVIDE, // / NOT, // ! - ASSIGNMENT, // = + ASSIGNMENT, // = (NICHT MEHR GEBRAUCHT ??) EQUALS, // == UNEQUALS, // != ERROR //TODO: Remove This diff --git a/src/main/java/ast/expression/InstVar.java b/src/main/java/ast/expression/InstVar.java new file mode 100644 index 0000000..51c7629 --- /dev/null +++ b/src/main/java/ast/expression/InstVar.java @@ -0,0 +1,23 @@ +package ast.expression; + +import ast.type.TypeNode; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; +import visitor.Visitable; + +public class InstVar implements ExpressionNode, Visitable { + public String identifier; + public ExpressionNode expression; + public TypeNode type; + + + public InstVar(ExpressionNode expression, String identifier) { + this.identifier = identifier; + this.expression = expression; + } + + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return visitor.analyze(this); + } +} diff --git a/src/main/java/ast/statement/AssignmentStatementNode.java b/src/main/java/ast/statement/AssignmentStatementNode.java index f9fd38e..d76cc85 100644 --- a/src/main/java/ast/statement/AssignmentStatementNode.java +++ b/src/main/java/ast/statement/AssignmentStatementNode.java @@ -1,15 +1,20 @@ package ast.statement; import ast.expression.BinaryExpressionNode; +import ast.expression.ExpressionNode; +import ast.type.TypeNode; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; import visitor.Visitable; public class AssignmentStatementNode extends StatementNode implements Visitable { - public BinaryExpressionNode expression; + public ExpressionNode expressionLeft; + public ExpressionNode expressionRight; + public TypeNode type; - public AssignmentStatementNode(BinaryExpressionNode expression) { - this.expression = expression; + public AssignmentStatementNode(ExpressionNode expressionLeft, ExpressionNode expressionRight) { + this.expressionLeft = expressionLeft; + this.expressionRight = expressionRight; } @Override diff --git a/src/main/java/ast/type/This.java b/src/main/java/ast/type/This.java index 459eea5..4972ac8 100644 --- a/src/main/java/ast/type/This.java +++ b/src/main/java/ast/type/This.java @@ -1,6 +1,13 @@ package ast.type; import ast.ASTNode; +import ast.expression.ExpressionNode; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; -public class This implements ASTNode, TypeNode{ +public class This implements ASTNode, ExpressionNode { + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } diff --git a/src/main/java/classFileOutput/Example.class b/src/main/java/classFileOutput/Example.class index 522ae9edd25429b756d2dd98d755bdedb11f81c3..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 GIT binary patch literal 0 HcmV?d00001 literal 167 zcmW-Z%?`m(5QV>?KU4{C(b_cjVj~t^h@Jh#4Q;BGxDl^qC9&`T9!g9t=FB(eoXqF@ zdIK{?Lb$q{^QE#OB8*FOGg)QoW476qcJD&ELNG7t!Yzbul5S&sf(VaL5Tla|k+WB~ uw(is%1V731R84Q$94tadyXY4BL`oeceW5zkYw}OPpFZh5)M%>%;ue1AIvEN8 diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java index 0f3b09c..c8dec84 100644 --- a/src/main/java/parser/ASTBuilder.java +++ b/src/main/java/parser/ASTBuilder.java @@ -138,9 +138,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { - - BinaryExpressionNode expression = (BinaryExpressionNode) visit(ctx.expression()); - return new AssignmentStatementNode(expression); + ExpressionNode expression = (ExpressionNode) visit(ctx.expression()); + return null; } @Override diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index a7b1704..ab1051b 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -2,11 +2,7 @@ package semantic; import ast.*; -import ast.expression.BinaryExpressionNode; -import ast.expression.ExpressionNode; -import ast.expression.IdentifierExpressionNode; -import ast.expression.UnaryExpressionNode; -import ast.member.ConstructorNode; +import ast.expression.*; import ast.member.FieldNode; import ast.member.MemberNode; @@ -14,8 +10,6 @@ import ast.member.MethodNode; import ast.parameter.ParameterListNode; import ast.parameter.ParameterNode; import ast.statement.*; - -import java.beans.Expression; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -26,6 +20,7 @@ import ast.type.EnumTypeNode; import ast.type.This; import ast.type.TypeNode; import semantic.exeptions.AlreadyDeclearedException; +import semantic.exeptions.NotDeclearedException; import semantic.exeptions.TypeMismatchException; import typechecker.TypeCheckResult; @@ -155,9 +150,20 @@ public class SemanticAnalyzer implements SemanticVisitor { @Override public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) { boolean valid = true; - BinaryExpressionNode binaryExpressionNode = assignmentStatementNode.expression; - var result = binaryExpressionNode.accept(this); - valid = valid && result.isValid(); + ExpressionNode expressionNodeLeft = assignmentStatementNode.expressionLeft; + var resultLeft = expressionNodeLeft.accept(this); + valid = valid && resultLeft.isValid(); + ExpressionNode expressionNodeRight = assignmentStatementNode.expressionRight; + var resultRight = expressionNodeRight.accept(this); + valid = valid && resultRight.isValid(); + + if(Objects.equals(resultLeft.getType(), resultRight.getType())){ + System.out.println("SAME TYPE"); + } else { + errors.add(new TypeMismatchException("Type mismatch")); + valid = false; + } + return new TypeCheckResult(valid, null); } @@ -190,7 +196,7 @@ public class SemanticAnalyzer implements SemanticVisitor { @Override public TypeCheckResult analyze(IdentifierExpressionNode toCheck) { if(toCheck.name == "this"){ - return new TypeCheckResult(true, new This()); + return new TypeCheckResult(true, null); } else if (currentFields.get(toCheck.name) == null) { errors.add(new AlreadyDeclearedException("Not declared " + toCheck.name + " in this scope")); return new TypeCheckResult(false, null); @@ -235,4 +241,20 @@ public class SemanticAnalyzer implements SemanticVisitor { return new TypeCheckResult(true, new BaseTypeNode(EnumTypeNode.INT)); } + @Override + public TypeCheckResult analyze(InstVar toCheck) { + boolean valid = true; + + if(toCheck.expression instanceof This){ + if(currentFields.get(toCheck.identifier) == null){ + errors.add(new NotDeclearedException("Not declared " + toCheck.identifier + " in this scope")); + valid = false; + } else { + TypeNode typeNode = currentFields.get(toCheck.identifier); + return new TypeCheckResult(valid, typeNode); + } + } + return new TypeCheckResult(valid, null); + } + } \ No newline at end of file diff --git a/src/main/java/semantic/SemanticVisitor.java b/src/main/java/semantic/SemanticVisitor.java index 9a8c6b1..fd69518 100644 --- a/src/main/java/semantic/SemanticVisitor.java +++ b/src/main/java/semantic/SemanticVisitor.java @@ -6,6 +6,7 @@ import ast.LiteralNode; import ast.ProgramNode; import ast.expression.BinaryExpressionNode; import ast.expression.IdentifierExpressionNode; +import ast.expression.InstVar; import ast.expression.UnaryExpressionNode; import ast.member.FieldNode; import ast.member.MethodNode; @@ -39,4 +40,6 @@ public interface SemanticVisitor { TypeCheckResult analyze(WhileStatementNode toCheck); TypeCheckResult analyze(LiteralNode toCheck); + + TypeCheckResult analyze(InstVar toCheck); } \ No newline at end of file diff --git a/src/main/java/semantic/exeptions/NotDeclearedException.java b/src/main/java/semantic/exeptions/NotDeclearedException.java new file mode 100644 index 0000000..aa223a0 --- /dev/null +++ b/src/main/java/semantic/exeptions/NotDeclearedException.java @@ -0,0 +1,9 @@ +package semantic.exeptions; + +public class NotDeclearedException extends RuntimeException { + + public NotDeclearedException(String message) { + super(message); + } + +} diff --git a/src/test/java/semantic/Mocker.java b/src/test/java/semantic/Mocker.java index 782ac7e..433d0ab 100644 --- a/src/test/java/semantic/Mocker.java +++ b/src/test/java/semantic/Mocker.java @@ -3,10 +3,7 @@ package semantic; import ast.ClassNode; import ast.LiteralNode; import ast.ProgramNode; -import ast.expression.BinaryExpressionNode; -import ast.expression.ExpressionNode; -import ast.expression.ExpresssionOperator; -import ast.expression.IdentifierExpressionNode; +import ast.expression.*; import ast.member.FieldNode; import ast.member.MemberNode; import ast.member.MethodNode; @@ -15,10 +12,7 @@ import ast.parameter.ParameterNode; import ast.statement.AssignmentStatementNode; import ast.statement.StatementNode; import ast.statement.VariableDeclarationStatementNode; -import ast.type.AccessTypeNode; -import ast.type.BaseTypeNode; -import ast.type.EnumAccessTypeNode; -import ast.type.EnumTypeNode; +import ast.type.*; import java.util.ArrayList; import java.util.List; @@ -45,16 +39,11 @@ public class Mocker { List statementNodeList = new ArrayList(); - ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this"); - ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar"); - - ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT); + ExpressionNode expressionNodeLeft = new InstVar(new This(), "objectVar"); ExpressionNode expressionNodeRight = new LiteralNode(1); - BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT); - - StatementNode statementNode1 = new AssignmentStatementNode(expressionNode); + StatementNode statementNode1 = new AssignmentStatementNode(expressionNodeLeft, expressionNodeRight); statementNodeList.add(statementNode1); StatementNode statementNode2 = new VariableDeclarationStatementNode(new BaseTypeNode(EnumTypeNode.CHAR), "objectVar", new LiteralNode(1));