Changed to InstVar
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
Bruder John 2024-06-01 18:37:13 +02:00
parent 22a30d5956
commit 56d316a6d0
10 changed files with 92 additions and 35 deletions

View File

@ -1,13 +1,13 @@
package ast.expression; package ast.expression;
public enum ExpresssionOperator { public enum ExpresssionOperator {
DOT, // . DOT, // . NICHT MEHR GEBRAUCHT
PLUS, // + PLUS, // +
MINUS, // - MINUS, // -
MULTIPLY, // * MULTIPLY, // *
DIVIDE, // / DIVIDE, // /
NOT, // ! NOT, // !
ASSIGNMENT, // = ASSIGNMENT, // = (NICHT MEHR GEBRAUCHT ??)
EQUALS, // == EQUALS, // ==
UNEQUALS, // != UNEQUALS, // !=
ERROR //TODO: Remove This ERROR //TODO: Remove This

View File

@ -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);
}
}

View File

@ -1,15 +1,20 @@
package ast.statement; package ast.statement;
import ast.expression.BinaryExpressionNode; import ast.expression.BinaryExpressionNode;
import ast.expression.ExpressionNode;
import ast.type.TypeNode;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
import visitor.Visitable; import visitor.Visitable;
public class AssignmentStatementNode extends StatementNode implements Visitable { public class AssignmentStatementNode extends StatementNode implements Visitable {
public BinaryExpressionNode expression; public ExpressionNode expressionLeft;
public ExpressionNode expressionRight;
public TypeNode type;
public AssignmentStatementNode(BinaryExpressionNode expression) { public AssignmentStatementNode(ExpressionNode expressionLeft, ExpressionNode expressionRight) {
this.expression = expression; this.expressionLeft = expressionLeft;
this.expressionRight = expressionRight;
} }
@Override @Override

View File

@ -1,6 +1,13 @@
package ast.type; package ast.type;
import ast.ASTNode; 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;
}
} }

View File

@ -138,9 +138,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
BinaryExpressionNode expression = (BinaryExpressionNode) visit(ctx.expression()); return null;
return new AssignmentStatementNode(expression);
} }
@Override @Override

View File

@ -2,11 +2,7 @@ package semantic;
import ast.*; import ast.*;
import ast.expression.BinaryExpressionNode; import ast.expression.*;
import ast.expression.ExpressionNode;
import ast.expression.IdentifierExpressionNode;
import ast.expression.UnaryExpressionNode;
import ast.member.ConstructorNode;
import ast.member.FieldNode; import ast.member.FieldNode;
import ast.member.MemberNode; import ast.member.MemberNode;
@ -14,8 +10,6 @@ import ast.member.MethodNode;
import ast.parameter.ParameterListNode; import ast.parameter.ParameterListNode;
import ast.parameter.ParameterNode; import ast.parameter.ParameterNode;
import ast.statement.*; import ast.statement.*;
import java.beans.Expression;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -26,6 +20,7 @@ import ast.type.EnumTypeNode;
import ast.type.This; import ast.type.This;
import ast.type.TypeNode; import ast.type.TypeNode;
import semantic.exeptions.AlreadyDeclearedException; import semantic.exeptions.AlreadyDeclearedException;
import semantic.exeptions.NotDeclearedException;
import semantic.exeptions.TypeMismatchException; import semantic.exeptions.TypeMismatchException;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
@ -155,9 +150,20 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) { public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
boolean valid = true; boolean valid = true;
BinaryExpressionNode binaryExpressionNode = assignmentStatementNode.expression; ExpressionNode expressionNodeLeft = assignmentStatementNode.expressionLeft;
var result = binaryExpressionNode.accept(this); var resultLeft = expressionNodeLeft.accept(this);
valid = valid && result.isValid(); 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); return new TypeCheckResult(valid, null);
} }
@ -190,7 +196,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(IdentifierExpressionNode toCheck) { public TypeCheckResult analyze(IdentifierExpressionNode toCheck) {
if(toCheck.name == "this"){ if(toCheck.name == "this"){
return new TypeCheckResult(true, new This()); return new TypeCheckResult(true, null);
} else if (currentFields.get(toCheck.name) == null) { } else if (currentFields.get(toCheck.name) == null) {
errors.add(new AlreadyDeclearedException("Not declared " + toCheck.name + " in this scope")); errors.add(new AlreadyDeclearedException("Not declared " + toCheck.name + " in this scope"));
return new TypeCheckResult(false, null); return new TypeCheckResult(false, null);
@ -235,4 +241,20 @@ public class SemanticAnalyzer implements SemanticVisitor {
return new TypeCheckResult(true, new BaseTypeNode(EnumTypeNode.INT)); 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);
}
} }

View File

@ -6,6 +6,7 @@ import ast.LiteralNode;
import ast.ProgramNode; import ast.ProgramNode;
import ast.expression.BinaryExpressionNode; import ast.expression.BinaryExpressionNode;
import ast.expression.IdentifierExpressionNode; import ast.expression.IdentifierExpressionNode;
import ast.expression.InstVar;
import ast.expression.UnaryExpressionNode; import ast.expression.UnaryExpressionNode;
import ast.member.FieldNode; import ast.member.FieldNode;
import ast.member.MethodNode; import ast.member.MethodNode;
@ -39,4 +40,6 @@ public interface SemanticVisitor {
TypeCheckResult analyze(WhileStatementNode toCheck); TypeCheckResult analyze(WhileStatementNode toCheck);
TypeCheckResult analyze(LiteralNode toCheck); TypeCheckResult analyze(LiteralNode toCheck);
TypeCheckResult analyze(InstVar toCheck);
} }

View File

@ -0,0 +1,9 @@
package semantic.exeptions;
public class NotDeclearedException extends RuntimeException {
public NotDeclearedException(String message) {
super(message);
}
}

View File

@ -3,10 +3,7 @@ package semantic;
import ast.ClassNode; import ast.ClassNode;
import ast.LiteralNode; import ast.LiteralNode;
import ast.ProgramNode; import ast.ProgramNode;
import ast.expression.BinaryExpressionNode; import ast.expression.*;
import ast.expression.ExpressionNode;
import ast.expression.ExpresssionOperator;
import ast.expression.IdentifierExpressionNode;
import ast.member.FieldNode; import ast.member.FieldNode;
import ast.member.MemberNode; import ast.member.MemberNode;
import ast.member.MethodNode; import ast.member.MethodNode;
@ -15,10 +12,7 @@ import ast.parameter.ParameterNode;
import ast.statement.AssignmentStatementNode; import ast.statement.AssignmentStatementNode;
import ast.statement.StatementNode; import ast.statement.StatementNode;
import ast.statement.VariableDeclarationStatementNode; import ast.statement.VariableDeclarationStatementNode;
import ast.type.AccessTypeNode; import ast.type.*;
import ast.type.BaseTypeNode;
import ast.type.EnumAccessTypeNode;
import ast.type.EnumTypeNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -45,16 +39,11 @@ public class Mocker {
List<StatementNode> statementNodeList = new ArrayList<StatementNode>(); List<StatementNode> statementNodeList = new ArrayList<StatementNode>();
ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this"); ExpressionNode expressionNodeLeft = new InstVar(new This(), "objectVar");
ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
ExpressionNode expressionNodeRight = new LiteralNode(1); ExpressionNode expressionNodeRight = new LiteralNode(1);
BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT); StatementNode statementNode1 = new AssignmentStatementNode(expressionNodeLeft, expressionNodeRight);
StatementNode statementNode1 = new AssignmentStatementNode(expressionNode);
statementNodeList.add(statementNode1); statementNodeList.add(statementNode1);
StatementNode statementNode2 = new VariableDeclarationStatementNode(new BaseTypeNode(EnumTypeNode.CHAR), "objectVar", new LiteralNode(1)); StatementNode statementNode2 = new VariableDeclarationStatementNode(new BaseTypeNode(EnumTypeNode.CHAR), "objectVar", new LiteralNode(1));