diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 779e5d1..0551ed9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,7 +1,7 @@ -import oldAst.ASTNode; +import ast.ASTNode; import org.antlr.v4.runtime.*; -import oldAst.ProgramNode; -import bytecode.ByteCodeGenerator; +import ast.ProgramNode; +//import bytecode.ByteCodeGenerator; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.tree.ParseTree; @@ -9,7 +9,7 @@ import org.antlr.v4.runtime.CommonTokenStream; import parser.astBuilder.ASTBuilder; import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; -import semantic.SemanticAnalyzer; +//import semantic.SemanticAnalyzer; import java.io.IOException; import java.nio.file.Paths; @@ -60,7 +60,7 @@ public class Main { /* ------------------------- AST builder -> AST ------------------------- */ ASTBuilder astBuilder = new ASTBuilder(); ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree); - + System.out.println(abstractSyntaxTree); // Printing the AST // System.out.println("-------------------- AST builder -> AST --------------------"); // // System.out.println("AST: " + ast.toString()); @@ -70,8 +70,8 @@ public class Main { /* * ------------------------- Semantic Analyzer -> Tast ------------------------- */ - SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); - ProgramNode typedAst = (ProgramNode) semanticAnalyzer.generateTast(abstractSyntaxTree); + //SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); + //ProgramNode typedAst = (ProgramNode) semanticAnalyzer.generateTast(abstractSyntaxTree); // Printing the Tast System.out.println("Tast generated"); @@ -80,9 +80,9 @@ public class Main { * ------------------------- Bytecode Generator -> Bytecode * ------------------------- */ - ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); + //ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); //byteCodeGenerator.generateByteCode(abstractSyntaxTree); - byteCodeGenerator.visit(typedAst); + //byteCodeGenerator.visit(typedAst); System.out.println("Bytecode generated"); } diff --git a/src/main/java/ast/ClassNode.java b/src/main/java/ast/ClassNode.java index a21044a..783d4f8 100644 --- a/src/main/java/ast/ClassNode.java +++ b/src/main/java/ast/ClassNode.java @@ -1,19 +1,14 @@ package ast; import ast.type.AccessModifierNode; -import ast.type.EnumAccessModifierNode; -import bytecode.visitor.ClassVisitor; import ast.member.ConstructorNode; import ast.member.MemberNode; import ast.member.MethodNode; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; import java.util.ArrayList; import java.util.List; -public class ClassNode implements ASTNode, Visitable { +public class ClassNode implements ASTNode { public AccessModifierNode accessType; public String identifier; public List members = new ArrayList<>(); @@ -50,14 +45,4 @@ public class ClassNode implements ASTNode, Visitable { } return methods; } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } - - @Override - public void accept(ClassVisitor classVisitor) { - classVisitor.visit(this); - } } diff --git a/src/main/java/ast/ProgramNode.java b/src/main/java/ast/ProgramNode.java index f8ad19a..9174692 100644 --- a/src/main/java/ast/ProgramNode.java +++ b/src/main/java/ast/ProgramNode.java @@ -1,27 +1,13 @@ package ast; -import bytecode.visitor.ProgramVisitor; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; - import java.util.ArrayList; import java.util.List; -public class ProgramNode implements ASTNode, Visitable { +public class ProgramNode implements ASTNode { public List classes = new ArrayList<>(); public void addClass(ClassNode classNode) { classes.add(classNode); } - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } - - @Override - public void accept(ProgramVisitor programVisitor) { - programVisitor.visit(this); - } } diff --git a/src/main/java/ast/block/BlockNode.java b/src/main/java/ast/block/BlockNode.java index a5ff3f7..9de2e5c 100644 --- a/src/main/java/ast/block/BlockNode.java +++ b/src/main/java/ast/block/BlockNode.java @@ -1,17 +1,17 @@ package ast.block; import ast.ASTNode; -import ast.statement.StatementNode; +import ast.statement.IStatementNode; import java.util.ArrayList; import java.util.List; public class BlockNode implements ASTNode { - public List statements = new ArrayList<>(); + public List statements = new ArrayList<>(); public BlockNode() {} - public void addStatement(StatementNode statement) { + public void addStatement(IStatementNode statement) { statements.add(statement); } } diff --git a/src/main/java/ast/expression/ExpressionNode.java b/src/main/java/ast/expression/ExpressionNode.java deleted file mode 100644 index 1566f3b..0000000 --- a/src/main/java/ast/expression/ExpressionNode.java +++ /dev/null @@ -1,4 +0,0 @@ -package ast.expression; - -public class ExpressionNode { -} diff --git a/src/main/java/ast/expression/IExpressionNode.java b/src/main/java/ast/expression/IExpressionNode.java new file mode 100644 index 0000000..775ac7a --- /dev/null +++ b/src/main/java/ast/expression/IExpressionNode.java @@ -0,0 +1,5 @@ +package ast.expression; + +import ast.ASTNode; + +public interface IExpressionNode extends ASTNode {} diff --git a/src/main/java/ast/expression/binaryexpression/BinaryExpression.java b/src/main/java/ast/expression/binaryexpression/BinaryExpression.java deleted file mode 100644 index fb161f3..0000000 --- a/src/main/java/ast/expression/binaryexpression/BinaryExpression.java +++ /dev/null @@ -1,4 +0,0 @@ -package ast.expression.binaryexpression; - -public class BinaryExpression { -} diff --git a/src/main/java/ast/expression/binaryexpression/BinaryExpressionNode.java b/src/main/java/ast/expression/binaryexpression/BinaryExpressionNode.java new file mode 100644 index 0000000..4afdff4 --- /dev/null +++ b/src/main/java/ast/expression/binaryexpression/BinaryExpressionNode.java @@ -0,0 +1,7 @@ +package ast.expression.binaryexpression; + +import ast.expression.IExpressionNode; + +import java.beans.Expression; + +public class BinaryExpressionNode implements IExpressionNode {} diff --git a/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java b/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java index 4d29cf5..442d44e 100644 --- a/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java +++ b/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java @@ -2,7 +2,7 @@ package ast.expression.binaryexpression; import ast.ASTNode; -public class CalculationExpressionNode implements ASTNode { +public class CalculationExpressionNode extends BinaryExpressionNode { public CalculationExpressionNode calculationExpression; public String operator; public DotExpressionNode dotExpression; diff --git a/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java b/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java index 5a0f5bb..c335d52 100644 --- a/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java +++ b/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java @@ -2,7 +2,7 @@ package ast.expression.binaryexpression; import ast.ASTNode; -public class DotExpressionNode implements ASTNode { +public class DotExpressionNode extends BinaryExpressionNode { public DotExpressionNode dotExpression; public String operator; public DotSubstractionExpressionNode dotSubstractionExpression; diff --git a/src/main/java/ast/expression/binaryexpression/DotSubstractionExpressionNode.java b/src/main/java/ast/expression/binaryexpression/DotSubstractionExpressionNode.java index 848ea14..69d467d 100644 --- a/src/main/java/ast/expression/binaryexpression/DotSubstractionExpressionNode.java +++ b/src/main/java/ast/expression/binaryexpression/DotSubstractionExpressionNode.java @@ -5,7 +5,7 @@ import ast.expression.unaryexpression.MemberAccessNode; import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode; import ast.type.ValueNode; -public class DotSubstractionExpressionNode implements ASTNode { +public class DotSubstractionExpressionNode extends BinaryExpressionNode { public ValueNode value; public String identifier; public MemberAccessNode memberAccess; diff --git a/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java b/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java index ab3f3bd..84f96c8 100644 --- a/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java +++ b/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java @@ -1,15 +1,15 @@ package ast.expression.binaryexpression; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; import ast.expression.unaryexpression.UnaryExpressionNode; -public class NonCalculationExpressionNode implements ASTNode { +public class NonCalculationExpressionNode extends BinaryExpressionNode { public UnaryExpressionNode unaryExpression; public String operator; - public ExpressionNode expression; + public IExpressionNode expression; - public NonCalculationExpressionNode(UnaryExpressionNode unaryExpression, String operator, ExpressionNode expression) { + public NonCalculationExpressionNode(UnaryExpressionNode unaryExpression, String operator, IExpressionNode expression) { this.unaryExpression = unaryExpression; this.operator = operator; this.expression = expression; diff --git a/src/main/java/ast/expression/unaryexpression/NotExpressionNode.java b/src/main/java/ast/expression/unaryexpression/NotExpressionNode.java index 9f3192a..e1e269d 100644 --- a/src/main/java/ast/expression/unaryexpression/NotExpressionNode.java +++ b/src/main/java/ast/expression/unaryexpression/NotExpressionNode.java @@ -1,12 +1,12 @@ package ast.expression.unaryexpression; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; public class NotExpressionNode implements ASTNode { - public ExpressionNode expression; + public IExpressionNode expression; - public NotExpressionNode(ExpressionNode expression) { + public NotExpressionNode(IExpressionNode expression) { this.expression = expression; } } diff --git a/src/main/java/ast/expression/unaryexpression/UnaryExpressionNode.java b/src/main/java/ast/expression/unaryexpression/UnaryExpressionNode.java index db9ab53..6c23545 100644 --- a/src/main/java/ast/expression/unaryexpression/UnaryExpressionNode.java +++ b/src/main/java/ast/expression/unaryexpression/UnaryExpressionNode.java @@ -1,20 +1,19 @@ package ast.expression.unaryexpression; -import ast.ASTNode; -import ast.expression.ExpressionNode; -import ast.statement.StatementNode; +import ast.expression.IExpressionNode; +import ast.statement.IStatementNode; import ast.type.ValueNode; import java.util.Objects; -public class UnaryExpressionNode implements ASTNode { +public class UnaryExpressionNode implements IExpressionNode { public String thisExp; public String identifier; public MemberAccessNode memberAccess; public ValueNode value; public NotExpressionNode notExpression; - public StatementNode statement; - public ExpressionNode expression; + public IStatementNode statement; + public IExpressionNode expression; public UnaryExpressionNode(String value) { if(Objects.equals(value, "this")) { @@ -36,11 +35,11 @@ public class UnaryExpressionNode implements ASTNode { this.notExpression = notExpression; } - public UnaryExpressionNode(StatementNode statement) { + public UnaryExpressionNode(IStatementNode statement) { this.statement = statement; } - public UnaryExpressionNode(ExpressionNode expression) { + public UnaryExpressionNode(IExpressionNode expression) { this.expression = expression; } } diff --git a/src/main/java/ast/member/ConstructorNode.java b/src/main/java/ast/member/ConstructorNode.java index c78f8d4..0f7a200 100644 --- a/src/main/java/ast/member/ConstructorNode.java +++ b/src/main/java/ast/member/ConstructorNode.java @@ -3,8 +3,6 @@ package ast.member; import ast.block.BlockNode; import ast.parameter.ParameterNode; import ast.type.AccessModifierNode; -import bytecode.visitor.MethodVisitor; -import visitor.Visitable; import java.util.ArrayList; import java.util.List; @@ -20,8 +18,8 @@ public class ConstructorNode extends MethodNode { this.identifier = identifier; } - public ConstructorNode(AccessModifierNode accessType, String identifier, BlockNode body) { - this.accessType = accessType; + public ConstructorNode(String accessType, String identifier, BlockNode body) { + this.accessType = new AccessModifierNode(accessType); this.identifier = identifier; this.body = body; } @@ -30,10 +28,4 @@ public class ConstructorNode extends MethodNode { parameters.add(parameterNode); } - /* - @Override - public void accept(MethodVisitor methodVisitor) { - methodVisitor.visit(this); - } - */ } diff --git a/src/main/java/ast/member/FieldNode.java b/src/main/java/ast/member/FieldNode.java index 2518d58..aaaa0fe 100644 --- a/src/main/java/ast/member/FieldNode.java +++ b/src/main/java/ast/member/FieldNode.java @@ -2,12 +2,8 @@ package ast.member; import ast.type.AccessModifierNode; import ast.type.TypeNode; -import bytecode.visitor.ClassVisitor; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; -public class FieldNode implements MemberNode, Visitable { +public class FieldNode implements MemberNode { public AccessModifierNode accessTypeNode; public TypeNode type; public String identifier; @@ -17,14 +13,4 @@ public class FieldNode implements MemberNode, Visitable { this.type = type; this.identifier = name; } - - @Override - public TypeCheckResult accept(SemanticVisitor visitor) { - return visitor.analyze(this); - } - - @Override - public void accept(ClassVisitor classVisitor) { - classVisitor.visit(this); - } } diff --git a/src/main/java/ast/member/MethodNode.java b/src/main/java/ast/member/MethodNode.java index 18932af..b435dca 100644 --- a/src/main/java/ast/member/MethodNode.java +++ b/src/main/java/ast/member/MethodNode.java @@ -4,10 +4,6 @@ import ast.block.BlockNode; import ast.parameter.ParameterNode; import ast.type.AccessModifierNode; import ast.type.TypeNode; -import bytecode.visitor.MethodVisitor; -import semantic.SemanticVisitor; -import typechecker.TypeCheckResult; -import visitor.Visitable; import java.util.ArrayList; import java.util.List; @@ -17,7 +13,7 @@ public class MethodNode implements MemberNode { public TypeNode type; public Boolean voidType; public String identifier; - public List parameters; + public List parameters = new ArrayList<>(); public BlockNode block; public MethodNode() {} @@ -26,9 +22,9 @@ public class MethodNode implements MemberNode { this.block = block; } - public MethodNode(AccessModifierNode accessModifier, TypeNode type, Boolean voidType, String identifier, BlockNode block){ - this.accesModifier = accessModifier; - this.type = type; + public MethodNode(String accessModifier, String type, Boolean voidType, String identifier, BlockNode block){ + this.accesModifier = new AccessModifierNode(accessModifier); + this.type = new TypeNode(type); this.voidType = voidType; this.identifier = identifier; this.block = block; diff --git a/src/main/java/ast/statement/ForStatementNode.java b/src/main/java/ast/statement/ForStatementNode.java index 7b3219e..e71534d 100644 --- a/src/main/java/ast/statement/ForStatementNode.java +++ b/src/main/java/ast/statement/ForStatementNode.java @@ -1,21 +1,21 @@ package ast.statement; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; -public class ForStatementNode implements ASTNode { - public ExpressionNode statementExpressionInit; - public StatementNode localVariableDeclarationInit; - public ExpressionNode expression; - public ExpressionNode statementExpression; +public class ForStatementNode implements IStatementNode { + public IExpressionNode statementExpressionInit; + public IStatementNode localVariableDeclarationInit; + public IExpressionNode expression; + public IExpressionNode statementExpression; - public ForStatementNode(ExpressionNode statementExpressionInit, ExpressionNode expression, ExpressionNode statementExpression) { + public ForStatementNode(IExpressionNode statementExpressionInit, IExpressionNode expression, IExpressionNode statementExpression) { this.statementExpressionInit = statementExpressionInit; this.expression = expression; this.statementExpression = statementExpression; } - public ForStatementNode(StatementNode localVariableDeclarationInit, ExpressionNode expression, ExpressionNode statementExpression) { + public ForStatementNode(IStatementNode localVariableDeclarationInit, IExpressionNode expression, IExpressionNode statementExpression) { this.localVariableDeclarationInit = localVariableDeclarationInit; this.expression = expression; this.statementExpression = statementExpression; diff --git a/src/main/java/ast/statement/IStatementNode.java b/src/main/java/ast/statement/IStatementNode.java new file mode 100644 index 0000000..d920bb9 --- /dev/null +++ b/src/main/java/ast/statement/IStatementNode.java @@ -0,0 +1,6 @@ +package ast.statement; + +import ast.ASTNode; + +public interface IStatementNode extends ASTNode { +} diff --git a/src/main/java/ast/statement/LocalVariableDeclarationNode.java b/src/main/java/ast/statement/LocalVariableDeclarationNode.java index 7c0ddaf..21c6cc3 100644 --- a/src/main/java/ast/statement/LocalVariableDeclarationNode.java +++ b/src/main/java/ast/statement/LocalVariableDeclarationNode.java @@ -1,16 +1,16 @@ package ast.statement; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; import ast.type.TypeNode; -public class LocalVariableDeclarationNode implements ASTNode { +public class LocalVariableDeclarationNode implements IStatementNode { public TypeNode type; public String identifier; public String assign; - public ExpressionNode expression; + public IExpressionNode expression; - public LocalVariableDeclarationNode(TypeNode type, String identifier, String assign, ExpressionNode expression) { + public LocalVariableDeclarationNode(TypeNode type, String identifier, String assign, IExpressionNode expression) { this.type = type; this.identifier = identifier; this.assign = assign; diff --git a/src/main/java/ast/statement/ReturnStatementNode.java b/src/main/java/ast/statement/ReturnStatementNode.java index ab111c5..4da2ecb 100644 --- a/src/main/java/ast/statement/ReturnStatementNode.java +++ b/src/main/java/ast/statement/ReturnStatementNode.java @@ -1,16 +1,12 @@ package ast.statement; import ast.ASTNode; -import ast.expression.ExpressionNode; -import ast.type.TypeNode; +import ast.expression.IExpressionNode; -import java.util.ArrayList; -import java.util.List; +public class ReturnStatementNode implements IStatementNode { + public IExpressionNode expression; -public class ReturnStatementNode implements ASTNode { - public ExpressionNode expression; - - public ReturnStatementNode(ExpressionNode expression) { + public ReturnStatementNode(IExpressionNode expression) { this.expression = expression; } } diff --git a/src/main/java/ast/statement/StatementNode.java b/src/main/java/ast/statement/StatementNode.java deleted file mode 100644 index 544b8b8..0000000 --- a/src/main/java/ast/statement/StatementNode.java +++ /dev/null @@ -1,4 +0,0 @@ -package ast.statement; - -public class StatementNode { -} diff --git a/src/main/java/ast/statement/WhileStatementNode.java b/src/main/java/ast/statement/WhileStatementNode.java index 8a34d66..b1c1c13 100644 --- a/src/main/java/ast/statement/WhileStatementNode.java +++ b/src/main/java/ast/statement/WhileStatementNode.java @@ -2,13 +2,13 @@ package ast.statement; import ast.ASTNode; import ast.block.BlockNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; -public class WhileStatementNode implements ASTNode { - public ExpressionNode expression; +public class WhileStatementNode implements IStatementNode { + public IExpressionNode expression; public BlockNode block; - public WhileStatementNode(ExpressionNode expression, BlockNode block) { + public WhileStatementNode(IExpressionNode expression, BlockNode block) { this.expression = expression; this.block = block; } diff --git a/src/main/java/ast/statement/ifstatement/ElseStatementNode.java b/src/main/java/ast/statement/ifstatement/ElseStatementNode.java index 18f4e0e..74bc6c2 100644 --- a/src/main/java/ast/statement/ifstatement/ElseStatementNode.java +++ b/src/main/java/ast/statement/ifstatement/ElseStatementNode.java @@ -2,8 +2,9 @@ package ast.statement.ifstatement; import ast.ASTNode; import ast.block.BlockNode; +import ast.statement.IStatementNode; -public class ElseStatementNode implements ASTNode { +public class ElseStatementNode implements IStatementNode { public BlockNode block; public ElseStatementNode(BlockNode block) { diff --git a/src/main/java/ast/statement/ifstatement/IfElseStatementNode.java b/src/main/java/ast/statement/ifstatement/IfElseStatementNode.java index 9b7ac8e..75827fd 100644 --- a/src/main/java/ast/statement/ifstatement/IfElseStatementNode.java +++ b/src/main/java/ast/statement/ifstatement/IfElseStatementNode.java @@ -1,11 +1,12 @@ package ast.statement.ifstatement; import ast.ASTNode; +import ast.statement.IStatementNode; import java.util.ArrayList; import java.util.List; -public class IfElseStatementNode implements ASTNode { +public class IfElseStatementNode implements IStatementNode { public IfStatementNode ifStatement; public List elseStatements = new ArrayList<>(); diff --git a/src/main/java/ast/statement/ifstatement/IfStatementNode.java b/src/main/java/ast/statement/ifstatement/IfStatementNode.java index 2b383fc..d0815e3 100644 --- a/src/main/java/ast/statement/ifstatement/IfStatementNode.java +++ b/src/main/java/ast/statement/ifstatement/IfStatementNode.java @@ -2,13 +2,14 @@ package ast.statement.ifstatement; import ast.ASTNode; import ast.block.BlockNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; +import ast.statement.IStatementNode; -public class IfStatementNode implements ASTNode { - public ExpressionNode expression; +public class IfStatementNode implements IStatementNode { + public IExpressionNode expression; public BlockNode block; - public IfStatementNode(ExpressionNode expression, BlockNode block) { + public IfStatementNode(IExpressionNode expression, BlockNode block) { this.expression = expression; this.block = block; } diff --git a/src/main/java/ast/statement/statementexpression/AssignStatementExpressionNode.java b/src/main/java/ast/statement/statementexpression/AssignStatementExpressionNode.java index 8dbad08..286d180 100644 --- a/src/main/java/ast/statement/statementexpression/AssignStatementExpressionNode.java +++ b/src/main/java/ast/statement/statementexpression/AssignStatementExpressionNode.java @@ -1,13 +1,13 @@ package ast.statement.statementexpression; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; -public class AssignStatementExpressionNode implements ASTNode { +public class AssignStatementExpressionNode implements IStatementExpressionNode { public AssignableExpressionNode assignable; - public ExpressionNode expression; + public IExpressionNode expression; - public AssignStatementExpressionNode(AssignableExpressionNode assignable, ExpressionNode expression) { + public AssignStatementExpressionNode(AssignableExpressionNode assignable, IExpressionNode expression) { this.assignable = assignable; this.expression = expression; } diff --git a/src/main/java/ast/statement/statementexpression/AssignableExpressionNode.java b/src/main/java/ast/statement/statementexpression/AssignableExpressionNode.java index e9d0d30..d669fb4 100644 --- a/src/main/java/ast/statement/statementexpression/AssignableExpressionNode.java +++ b/src/main/java/ast/statement/statementexpression/AssignableExpressionNode.java @@ -3,7 +3,7 @@ package ast.statement.statementexpression; import ast.ASTNode; import ast.expression.unaryexpression.MemberAccessNode; -public class AssignableExpressionNode implements ASTNode { +public class AssignableExpressionNode implements IStatementExpressionNode { public String identifier; public MemberAccessNode memberAccess; diff --git a/src/main/java/ast/statement/statementexpression/IStatementExpressionNode.java b/src/main/java/ast/statement/statementexpression/IStatementExpressionNode.java new file mode 100644 index 0000000..30998f6 --- /dev/null +++ b/src/main/java/ast/statement/statementexpression/IStatementExpressionNode.java @@ -0,0 +1,5 @@ +package ast.statement.statementexpression; + +import ast.statement.IStatementNode; + +public interface IStatementExpressionNode extends IStatementNode {} diff --git a/src/main/java/ast/statement/statementexpression/NewDeclarationStatementExpressionNode.java b/src/main/java/ast/statement/statementexpression/NewDeclarationStatementExpressionNode.java index 69a61c2..fa3f86d 100644 --- a/src/main/java/ast/statement/statementexpression/NewDeclarationStatementExpressionNode.java +++ b/src/main/java/ast/statement/statementexpression/NewDeclarationStatementExpressionNode.java @@ -1,20 +1,20 @@ package ast.statement.statementexpression; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; import java.util.ArrayList; import java.util.List; -public class NewDeclarationStatementExpressionNode implements ASTNode { +public class NewDeclarationStatementExpressionNode implements IStatementExpressionNode { public String identifier; - public List expressions = new ArrayList<>(); + public List expressions = new ArrayList<>(); public NewDeclarationStatementExpressionNode(String identifier) { this.identifier = identifier; } - public void addExpression(ExpressionNode expression) { + public void addExpression(IExpressionNode expression) { expressions.add(expression); } } diff --git a/src/main/java/ast/statement/statementexpression/crementExpression/DecrementExpressionNode.java b/src/main/java/ast/statement/statementexpression/crementExpression/DecrementExpressionNode.java index 0d09c5d..d14107d 100644 --- a/src/main/java/ast/statement/statementexpression/crementExpression/DecrementExpressionNode.java +++ b/src/main/java/ast/statement/statementexpression/crementExpression/DecrementExpressionNode.java @@ -2,8 +2,9 @@ package ast.statement.statementexpression.crementExpression; import ast.ASTNode; import ast.statement.statementexpression.AssignableExpressionNode; +import ast.statement.statementexpression.IStatementExpressionNode; -public class DecrementExpressionNode implements ASTNode { +public class DecrementExpressionNode implements IStatementExpressionNode { public CrementType crementType; public AssignableExpressionNode assignableExpression; diff --git a/src/main/java/ast/statement/statementexpression/crementExpression/IncrementExpressionNode.java b/src/main/java/ast/statement/statementexpression/crementExpression/IncrementExpressionNode.java index 815482d..8bf8c16 100644 --- a/src/main/java/ast/statement/statementexpression/crementExpression/IncrementExpressionNode.java +++ b/src/main/java/ast/statement/statementexpression/crementExpression/IncrementExpressionNode.java @@ -2,8 +2,9 @@ package ast.statement.statementexpression.crementExpression; import ast.ASTNode; import ast.statement.statementexpression.AssignableExpressionNode; +import ast.statement.statementexpression.IStatementExpressionNode; -public class IncrementExpressionNode implements ASTNode { +public class IncrementExpressionNode implements IStatementExpressionNode { public CrementType crementType; public AssignableExpressionNode assignableExpression; diff --git a/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/ChainedMethodNode.java b/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/ChainedMethodNode.java index a196f73..c20e9bc 100644 --- a/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/ChainedMethodNode.java +++ b/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/ChainedMethodNode.java @@ -1,20 +1,20 @@ package ast.statement.statementexpression.methodcallstatementnexpression; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; import java.util.ArrayList; import java.util.List; public class ChainedMethodNode implements ASTNode { public String identifier; - public List expressions = new ArrayList<>(); + public List expressions = new ArrayList<>(); public ChainedMethodNode(String identifier) { this.identifier = identifier; } - public void addExpression(ExpressionNode expression) { + public void addExpression(IExpressionNode expression) { expressions.add(expression); } } diff --git a/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/MethodCallStatementExpressionNode.java b/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/MethodCallStatementExpressionNode.java index 1dc6a07..5646f07 100644 --- a/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/MethodCallStatementExpressionNode.java +++ b/src/main/java/ast/statement/statementexpression/methodcallstatementnexpression/MethodCallStatementExpressionNode.java @@ -1,16 +1,17 @@ package ast.statement.statementexpression.methodcallstatementnexpression; import ast.ASTNode; -import ast.expression.ExpressionNode; +import ast.expression.IExpressionNode; +import ast.statement.statementexpression.IStatementExpressionNode; import java.util.ArrayList; import java.util.List; -public class MethodCallStatementExpressionNode implements ASTNode { +public class MethodCallStatementExpressionNode implements IStatementExpressionNode { public TargetNode target; public List chainedMethods = new ArrayList<>(); public String identifier; - public List expressions = new ArrayList<>(); + public List expressions = new ArrayList<>(); public MethodCallStatementExpressionNode(TargetNode target, String identifier) { this.target = target; @@ -21,7 +22,7 @@ public class MethodCallStatementExpressionNode implements ASTNode { chainedMethods.add(chainedMethode); } - public void addExpression(ExpressionNode expression) { + public void addExpression(IExpressionNode expression) { expressions.add(expression); } diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index 7760b5c..eae8647 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -53,7 +53,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { - ConstructorNode constructorNode = new ConstructorNode((AccessModifierNode) visit(ctx.AccessModifier()), ctx.Identifier().getText(), (BlockNode) visit(ctx.block())); + ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.block())); for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { constructorNode.addParameter((ParameterNode) visit(parameter)); } @@ -66,13 +66,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { return new MethodNode((BlockNode) visit(ctx.block())); } else { if(ctx.type() != null) { - MethodNode methodNode = new MethodNode((AccessModifierNode) visit(ctx.AccessModifier()), (TypeNode) visit(ctx.type()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.block())); + MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), ctx.type().getText(), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.block())); for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { methodNode.addParameter((ParameterNode) visit(parameter)); } return methodNode; } else { - MethodNode methodNode = new MethodNode((AccessModifierNode) visit(ctx.AccessModifier()), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.block())); + MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.block())); for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { methodNode.addParameter((ParameterNode) visit(parameter)); } @@ -108,34 +108,34 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) { - return new ReturnStatementNode((ExpressionNode) visit(ctx.expression())); + return new ReturnStatementNode((IExpressionNode) visit(ctx.expression())); } @Override public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) { - return new LocalVariableDeclarationNode(new TypeNode(ctx.type().getText()), ctx.Identifier().getText(), ctx.Assign().getText(), (ExpressionNode) visit(ctx.expression())); + return new LocalVariableDeclarationNode(new TypeNode(ctx.type().getText()), ctx.Identifier().getText(), ctx.Assign().getText(), (IExpressionNode) visit(ctx.expression())); } @Override public ASTNode visitBlock(SimpleJavaParser.BlockContext ctx) { BlockNode blockNode = new BlockNode(); for(SimpleJavaParser.StatementContext statement : ctx.statement()) { - blockNode.addStatement((StatementNode) visit(statement)); + blockNode.addStatement((IStatementNode) visit(statement)); } return blockNode; } @Override public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) { - return new WhileStatementNode((ExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block())); + return new WhileStatementNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block())); } @Override public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) { if(ctx.statementExpression(0) != null) { - return new ForStatementNode((ExpressionNode) visit(ctx.statementExpression(0)), (ExpressionNode) visit(ctx.expression()), (ExpressionNode) visit(ctx.statementExpression(1))); + return new ForStatementNode((IExpressionNode) visit(ctx.statementExpression(0)), (IExpressionNode) visit(ctx.expression()), (IExpressionNode) visit(ctx.statementExpression(1))); } else if(ctx.localVariableDeclaration() != null) { - return new ForStatementNode((StatementNode) visit(ctx.localVariableDeclaration()), (ExpressionNode) visit(ctx.expression()), (ExpressionNode) visit(ctx.statementExpression(1))); + return new ForStatementNode((IStatementNode) visit(ctx.localVariableDeclaration()), (IExpressionNode) visit(ctx.expression()), (IExpressionNode) visit(ctx.statementExpression(1))); } return null; } @@ -151,7 +151,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) { - return new IfStatementNode((ExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block())); + return new IfStatementNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block())); } @Override @@ -175,14 +175,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitAssign(SimpleJavaParser.AssignContext ctx) { - return new AssignStatementExpressionNode((AssignableExpressionNode) visit(ctx.assignableExpression()), (ExpressionNode) visit(ctx.expression())); + return new AssignStatementExpressionNode((AssignableExpressionNode) visit(ctx.assignableExpression()), (IExpressionNode) visit(ctx.expression())); } @Override public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) { NewDeclarationStatementExpressionNode newDeclarationStatementExpressionNode = new NewDeclarationStatementExpressionNode(ctx.Identifier().getText()); for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) { - newDeclarationStatementExpressionNode.addExpression((ExpressionNode) visit(expression)); + newDeclarationStatementExpressionNode.addExpression((IExpressionNode) visit(expression)); } return newDeclarationStatementExpressionNode; } @@ -194,7 +194,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod)); } for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) { - methodCallStatementExpressionNode.addExpression((ExpressionNode) visit(expression)); + methodCallStatementExpressionNode.addExpression((IExpressionNode) visit(expression)); } return methodCallStatementExpressionNode; } @@ -217,7 +217,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { public ASTNode visitChainedMethod(SimpleJavaParser.ChainedMethodContext ctx) { ChainedMethodNode chainedMethodNode = new ChainedMethodNode(ctx.Identifier().getText()); for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) { - chainedMethodNode.addExpression((ExpressionNode) visit(expression)); + chainedMethodNode.addExpression((IExpressionNode) visit(expression)); } return chainedMethodNode; } @@ -295,9 +295,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { } else if(ctx.notExpression() != null) { return new UnaryExpressionNode((NotExpressionNode) visitNotExpression(ctx.notExpression())); } else if(ctx.statementExpression() != null) { - return new UnaryExpressionNode((StatementNode) visitStatementExpression(ctx.statementExpression())); + return new UnaryExpressionNode((IStatementNode) visitStatementExpression(ctx.statementExpression())); } else if(ctx.expression() != null) { - return new UnaryExpressionNode((ExpressionNode) visitExpression(ctx.expression())); + return new UnaryExpressionNode((IExpressionNode) visitExpression(ctx.expression())); } return null; } @@ -332,7 +332,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitNotExpression(SimpleJavaParser.NotExpressionContext ctx) { - return new NotExpressionNode((ExpressionNode) visitExpression(ctx.expression())); + return new NotExpressionNode((IExpressionNode) visitExpression(ctx.expression())); } @@ -382,7 +382,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitNonCalculationExpression(SimpleJavaParser.NonCalculationExpressionContext ctx) { - return new NonCalculationExpressionNode((UnaryExpressionNode) visit(ctx.unaryExpression()), ctx.nonCalculationOperator().getText(), (ExpressionNode) visit(ctx.expression())); + return new NonCalculationExpressionNode((UnaryExpressionNode) visit(ctx.unaryExpression()), ctx.nonCalculationOperator().getText(), (IExpressionNode) visit(ctx.expression())); } @Override diff --git a/src/main/resources/CompilerInput.java b/src/main/resources/CompilerInput.java index 1cbe5ca..ba8a8e5 100644 --- a/src/main/resources/CompilerInput.java +++ b/src/main/resources/CompilerInput.java @@ -11,8 +11,7 @@ public class Example { public class Test { public static int testMethod(char x, int a){ - - - + x = x + a; + return x; } } \ No newline at end of file