johns-branch #12

Merged
i22005 merged 23 commits from johns-branch into main 2024-06-21 16:30:56 +00:00
43 changed files with 183 additions and 238 deletions
Showing only changes of commit b5738034b0 - Show all commits

View File

@ -1,6 +1,7 @@
import ast.*; import ast.ASTNode;
import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.*;
import bytecode.ByteCodeGenerator; import ast.ProgramNode;
//import bytecode.ByteCodeGenerator;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTree;
@ -8,7 +9,7 @@ import org.antlr.v4.runtime.CommonTokenStream;
import parser.astBuilder.ASTBuilder; import parser.astBuilder.ASTBuilder;
import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser; import parser.generated.SimpleJavaParser;
import semantic.SemanticAnalyzer; //import semantic.SemanticAnalyzer;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -59,7 +60,7 @@ public class Main {
/* ------------------------- AST builder -> AST ------------------------- */ /* ------------------------- AST builder -> AST ------------------------- */
ASTBuilder astBuilder = new ASTBuilder(); ASTBuilder astBuilder = new ASTBuilder();
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree); ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
System.out.println(abstractSyntaxTree);
// Printing the AST // Printing the AST
// System.out.println("-------------------- AST builder -> AST --------------------"); // System.out.println("-------------------- AST builder -> AST --------------------");
// // System.out.println("AST: " + ast.toString()); // // System.out.println("AST: " + ast.toString());
@ -69,8 +70,8 @@ public class Main {
/* /*
* ------------------------- Semantic Analyzer -> Tast ------------------------- * ------------------------- Semantic Analyzer -> Tast -------------------------
*/ */
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); //SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
ProgramNode typedAst = (ProgramNode) semanticAnalyzer.generateTast(abstractSyntaxTree); //ProgramNode typedAst = (ProgramNode) semanticAnalyzer.generateTast(abstractSyntaxTree);
// Printing the Tast // Printing the Tast
System.out.println("Tast generated"); System.out.println("Tast generated");
@ -79,9 +80,9 @@ public class Main {
* ------------------------- Bytecode Generator -> Bytecode * ------------------------- Bytecode Generator -> Bytecode
* ------------------------- * -------------------------
*/ */
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); //ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
//byteCodeGenerator.generateByteCode(abstractSyntaxTree); //byteCodeGenerator.generateByteCode(abstractSyntaxTree);
byteCodeGenerator.visit(typedAst); //byteCodeGenerator.visit(typedAst);
System.out.println("Bytecode generated"); System.out.println("Bytecode generated");
} }

View File

@ -1,19 +1,14 @@
package ast; package ast;
import ast.type.AccessModifierNode; import ast.type.AccessModifierNode;
import ast.type.EnumAccessModifierNode;
import bytecode.visitor.ClassVisitor;
import ast.member.ConstructorNode; import ast.member.ConstructorNode;
import ast.member.MemberNode; import ast.member.MemberNode;
import ast.member.MethodNode; import ast.member.MethodNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ClassNode implements ASTNode, Visitable { public class ClassNode implements ASTNode {
public AccessModifierNode accessType; public AccessModifierNode accessType;
public String identifier; public String identifier;
public List<MemberNode> members = new ArrayList<>(); public List<MemberNode> members = new ArrayList<>();
@ -50,14 +45,4 @@ public class ClassNode implements ASTNode, Visitable {
} }
return methods; return methods;
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.analyze(this);
}
@Override
public void accept(ClassVisitor classVisitor) {
classVisitor.visit(this);
}
} }

View File

@ -1,4 +0,0 @@
package ast;
public class IdentifierNode {
}

View File

@ -1,27 +1,13 @@
package ast; package ast;
import bytecode.visitor.ProgramVisitor;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ProgramNode implements ASTNode, Visitable { public class ProgramNode implements ASTNode {
public List<ClassNode> classes = new ArrayList<>(); public List<ClassNode> classes = new ArrayList<>();
public void addClass(ClassNode classNode) { public void addClass(ClassNode classNode) {
classes.add(classNode); classes.add(classNode);
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.analyze(this);
}
@Override
public void accept(ProgramVisitor programVisitor) {
programVisitor.visit(this);
}
} }

View File

@ -1,20 +1,17 @@
package ast.block; package ast.block;
import ast.ASTNode; import ast.ASTNode;
import ast.statement.StatementNode; import ast.statement.IStatementNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class BlockNode implements ASTNode, Visitable { public class BlockNode implements ASTNode {
public List<StatementNode> statements = new ArrayList<>(); public List<IStatementNode> statements = new ArrayList<>();
public BlockNode() {} public BlockNode() {}
public void addStatement(StatementNode statement) { public void addStatement(IStatementNode statement) {
statements.add(statement); statements.add(statement);
} }

View File

@ -1,6 +0,0 @@
package ast.expression;
import ast.ASTNode;
public class ExpressionNode implements ASTNode {
}

View File

@ -0,0 +1,5 @@
package ast.expression;
import ast.ASTNode;
public interface IExpressionNode extends ASTNode {}

View File

@ -1,4 +0,0 @@
package ast.expression.binaryexpression;
public class BinaryExpression {
}

View File

@ -0,0 +1,7 @@
package ast.expression.binaryexpression;
import ast.expression.IExpressionNode;
import java.beans.Expression;
public class BinaryExpressionNode implements IExpressionNode {}

View File

@ -2,10 +2,10 @@ package ast.expression.binaryexpression;
import ast.ASTNode; import ast.ASTNode;
public class CalculationExpressionNode implements ASTNode { public class CalculationExpressionNode extends BinaryExpressionNode {
CalculationExpressionNode calculationExpression; public CalculationExpressionNode calculationExpression;
String operator; public String operator;
DotExpressionNode dotExpression; public DotExpressionNode dotExpression;
public CalculationExpressionNode(CalculationExpressionNode calculationExpression, String operator, DotExpressionNode dotExpression) { public CalculationExpressionNode(CalculationExpressionNode calculationExpression, String operator, DotExpressionNode dotExpression) {
this.calculationExpression = calculationExpression; this.calculationExpression = calculationExpression;

View File

@ -2,10 +2,10 @@ package ast.expression.binaryexpression;
import ast.ASTNode; import ast.ASTNode;
public class DotExpressionNode implements ASTNode { public class DotExpressionNode extends BinaryExpressionNode {
DotExpressionNode dotExpression; public DotExpressionNode dotExpression;
String operator; public String operator;
DotSubstractionExpressionNode dotSubstractionExpression; public DotSubstractionExpressionNode dotSubstractionExpression;
public DotExpressionNode(DotExpressionNode dotExpression, String operator, DotSubstractionExpressionNode dotSubstractionExpression) { public DotExpressionNode(DotExpressionNode dotExpression, String operator, DotSubstractionExpressionNode dotSubstractionExpression) {
this.dotExpression = dotExpression; this.dotExpression = dotExpression;

View File

@ -5,12 +5,12 @@ import ast.expression.unaryexpression.MemberAccessNode;
import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode; import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode;
import ast.type.ValueNode; import ast.type.ValueNode;
public class DotSubstractionExpressionNode implements ASTNode { public class DotSubstractionExpressionNode extends BinaryExpressionNode {
ValueNode value; public ValueNode value;
String identifier; public String identifier;
MemberAccessNode memberAccess; public MemberAccessNode memberAccess;
MethodCallStatementExpressionNode methodCall; public MethodCallStatementExpressionNode methodCall;
CalculationExpressionNode calculationExpression; public CalculationExpressionNode calculationExpression;
public DotSubstractionExpressionNode(ValueNode value) { public DotSubstractionExpressionNode(ValueNode value) {
this.value = value; this.value = value;

View File

@ -1,15 +1,15 @@
package ast.expression.binaryexpression; package ast.expression.binaryexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
import ast.expression.unaryexpression.UnaryExpressionNode; import ast.expression.unaryexpression.UnaryExpressionNode;
public class NonCalculationExpressionNode implements ASTNode { public class NonCalculationExpressionNode extends BinaryExpressionNode {
UnaryExpressionNode unaryExpression; public UnaryExpressionNode unaryExpression;
String operator; public String operator;
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.unaryExpression = unaryExpression;
this.operator = operator; this.operator = operator;
this.expression = expression; this.expression = expression;

View File

@ -6,8 +6,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MemberAccessNode implements ASTNode { public class MemberAccessNode implements ASTNode {
Boolean thisExpr; public Boolean thisExpr;
List<String> identifiers = new ArrayList<>(); public List<String> identifiers = new ArrayList<>();
public MemberAccessNode(Boolean thisExpr) { public MemberAccessNode(Boolean thisExpr) {
this.thisExpr = thisExpr; this.thisExpr = thisExpr;

View File

@ -1,12 +1,12 @@
package ast.expression.unaryexpression; package ast.expression.unaryexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
public class NotExpressionNode implements ASTNode { public class NotExpressionNode implements ASTNode {
ExpressionNode expression; public IExpressionNode expression;
public NotExpressionNode(ExpressionNode expression) { public NotExpressionNode(IExpressionNode expression) {
this.expression = expression; this.expression = expression;
} }
} }

View File

@ -1,20 +1,19 @@
package ast.expression.unaryexpression; package ast.expression.unaryexpression;
import ast.ASTNode; import ast.expression.IExpressionNode;
import ast.expression.ExpressionNode; import ast.statement.IStatementNode;
import ast.statement.StatementNode;
import ast.type.ValueNode; import ast.type.ValueNode;
import java.util.Objects; import java.util.Objects;
public class UnaryExpressionNode extends ExpressionNode { public class UnaryExpressionNode implements IExpressionNode {
String thisExp; public String thisExp;
String identifier; public String identifier;
MemberAccessNode memberAccess; public MemberAccessNode memberAccess;
ValueNode value; public ValueNode value;
NotExpressionNode notExpression; public NotExpressionNode notExpression;
StatementNode statement; public IStatementNode statement;
ExpressionNode expression; public IExpressionNode expression;
public UnaryExpressionNode(String value) { public UnaryExpressionNode(String value) {
if(Objects.equals(value, "this")) { if(Objects.equals(value, "this")) {
@ -36,11 +35,11 @@ public class UnaryExpressionNode extends ExpressionNode {
this.notExpression = notExpression; this.notExpression = notExpression;
} }
public UnaryExpressionNode(StatementNode statement) { public UnaryExpressionNode(IStatementNode statement) {
this.statement = statement; this.statement = statement;
} }
public UnaryExpressionNode(ExpressionNode expression) { public UnaryExpressionNode(IExpressionNode expression) {
this.expression = expression; this.expression = expression;
} }
} }

View File

@ -3,8 +3,6 @@ package ast.member;
import ast.block.BlockNode; import ast.block.BlockNode;
import ast.parameter.ParameterNode; import ast.parameter.ParameterNode;
import ast.type.AccessModifierNode; import ast.type.AccessModifierNode;
import bytecode.visitor.MethodVisitor;
import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -20,8 +18,8 @@ public class ConstructorNode extends MethodNode {
this.identifier = identifier; this.identifier = identifier;
} }
public ConstructorNode(AccessModifierNode accessType, String identifier, BlockNode body) { public ConstructorNode(String accessType, String identifier, BlockNode body) {
this.accessType = accessType; this.accessType = new AccessModifierNode(accessType);
this.identifier = identifier; this.identifier = identifier;
this.body = body; this.body = body;
} }
@ -30,10 +28,4 @@ public class ConstructorNode extends MethodNode {
parameters.add(parameterNode); parameters.add(parameterNode);
} }
/*
@Override
public void accept(MethodVisitor methodVisitor) {
methodVisitor.visit(this);
}
*/
} }

View File

@ -2,12 +2,8 @@ package ast.member;
import ast.type.AccessModifierNode; import ast.type.AccessModifierNode;
import ast.type.TypeNode; 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 AccessModifierNode accessTypeNode;
public TypeNode type; public TypeNode type;
public String identifier; public String identifier;
@ -17,14 +13,4 @@ public class FieldNode implements MemberNode, Visitable {
this.type = type; this.type = type;
this.identifier = name; this.identifier = name;
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.analyze(this);
}
@Override
public void accept(ClassVisitor classVisitor) {
classVisitor.visit(this);
}
} }

View File

@ -4,20 +4,16 @@ import ast.block.BlockNode;
import ast.parameter.ParameterNode; import ast.parameter.ParameterNode;
import ast.type.AccessModifierNode; import ast.type.AccessModifierNode;
import ast.type.TypeNode; import ast.type.TypeNode;
import bytecode.visitor.MethodVisitor;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MethodNode implements MemberNode, Visitable { public class MethodNode implements MemberNode {
AccessModifierNode accesModifier; public AccessModifierNode accesModifier;
public TypeNode type; public TypeNode type;
Boolean voidType; public Boolean voidType;
public String identifier; public String identifier;
private List<ParameterNode> parameters = new ArrayList<>(); public List<ParameterNode> parameters = new ArrayList<>();
public BlockNode block; public BlockNode block;
public MethodNode() {} public MethodNode() {}
@ -26,9 +22,9 @@ public class MethodNode implements MemberNode, Visitable {
this.block = block; this.block = block;
} }
public MethodNode(AccessModifierNode accessModifier, TypeNode type, Boolean voidType, String identifier, BlockNode block){ public MethodNode(String accessModifier, String type, Boolean voidType, String identifier, BlockNode block){
this.accesModifier = accessModifier; this.accesModifier = new AccessModifierNode(accessModifier);
this.type = type; this.type = new TypeNode(type);
this.voidType = voidType; this.voidType = voidType;
this.identifier = identifier; this.identifier = identifier;
this.block = block; this.block = block;

View File

@ -1,21 +1,21 @@
package ast.statement; package ast.statement;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
public class ForStatementNode implements ASTNode { public class ForStatementNode implements IStatementNode {
ExpressionNode statementExpressionInit; public IExpressionNode statementExpressionInit;
StatementNode localVariableDeclarationInit; public IStatementNode localVariableDeclarationInit;
ExpressionNode expression; public IExpressionNode expression;
ExpressionNode statementExpression; public IExpressionNode statementExpression;
public ForStatementNode(ExpressionNode statementExpressionInit, ExpressionNode expression, ExpressionNode statementExpression) { public ForStatementNode(IExpressionNode statementExpressionInit, IExpressionNode expression, IExpressionNode statementExpression) {
this.statementExpressionInit = statementExpressionInit; this.statementExpressionInit = statementExpressionInit;
this.expression = expression; this.expression = expression;
this.statementExpression = statementExpression; this.statementExpression = statementExpression;
} }
public ForStatementNode(StatementNode localVariableDeclarationInit, ExpressionNode expression, ExpressionNode statementExpression) { public ForStatementNode(IStatementNode localVariableDeclarationInit, IExpressionNode expression, IExpressionNode statementExpression) {
this.localVariableDeclarationInit = localVariableDeclarationInit; this.localVariableDeclarationInit = localVariableDeclarationInit;
this.expression = expression; this.expression = expression;
this.statementExpression = statementExpression; this.statementExpression = statementExpression;

View File

@ -0,0 +1,6 @@
package ast.statement;
import ast.ASTNode;
public interface IStatementNode extends ASTNode {
}

View File

@ -1,16 +1,16 @@
package ast.statement; package ast.statement;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
import ast.type.TypeNode; import ast.type.TypeNode;
public class LocalVariableDeclarationNode extends StatementNode { public class LocalVariableDeclarationNode implements IStatementNode {
TypeNode type; public TypeNode type;
String identifier; public String identifier;
String assign; public String assign;
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.type = type;
this.identifier = identifier; this.identifier = identifier;
this.assign = assign; this.assign = assign;

View File

@ -1,12 +1,12 @@
package ast.statement; package ast.statement;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
public class ReturnStatementNode implements ASTNode { public class ReturnStatementNode implements IStatementNode {
public ExpressionNode expression; public IExpressionNode expression;
public ReturnStatementNode(ExpressionNode expression) { public ReturnStatementNode(IExpressionNode expression) {
this.expression = expression; this.expression = expression;
} }
} }

View File

@ -1,6 +0,0 @@
package ast.statement;
import ast.ASTNode;
public class StatementNode implements ASTNode {
}

View File

@ -2,13 +2,13 @@ package ast.statement;
import ast.ASTNode; import ast.ASTNode;
import ast.block.BlockNode; import ast.block.BlockNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
public class WhileStatementNode implements ASTNode { public class WhileStatementNode implements IStatementNode {
ExpressionNode expression; public IExpressionNode expression;
BlockNode block; public BlockNode block;
public WhileStatementNode(ExpressionNode expression, BlockNode block) { public WhileStatementNode(IExpressionNode expression, BlockNode block) {
this.expression = expression; this.expression = expression;
this.block = block; this.block = block;
} }

View File

@ -2,9 +2,10 @@ package ast.statement.ifstatement;
import ast.ASTNode; import ast.ASTNode;
import ast.block.BlockNode; import ast.block.BlockNode;
import ast.statement.IStatementNode;
public class ElseStatementNode implements ASTNode { public class ElseStatementNode implements IStatementNode {
BlockNode block; public BlockNode block;
public ElseStatementNode(BlockNode block) { public ElseStatementNode(BlockNode block) {
this.block = block; this.block = block;

View File

@ -1,13 +1,14 @@
package ast.statement.ifstatement; package ast.statement.ifstatement;
import ast.ASTNode; import ast.ASTNode;
import ast.statement.IStatementNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class IfElseStatementNode implements ASTNode { public class IfElseStatementNode implements IStatementNode {
IfStatementNode ifStatement; public IfStatementNode ifStatement;
List<ElseStatementNode> elseStatements = new ArrayList<>(); public List<ElseStatementNode> elseStatements = new ArrayList<>();
public IfElseStatementNode(IfStatementNode ifStatement) { public IfElseStatementNode(IfStatementNode ifStatement) {
this.ifStatement = ifStatement; this.ifStatement = ifStatement;

View File

@ -2,13 +2,14 @@ package ast.statement.ifstatement;
import ast.ASTNode; import ast.ASTNode;
import ast.block.BlockNode; import ast.block.BlockNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
import ast.statement.IStatementNode;
public class IfStatementNode implements ASTNode { public class IfStatementNode implements IStatementNode {
ExpressionNode expression; public IExpressionNode expression;
BlockNode block; public BlockNode block;
public IfStatementNode(ExpressionNode expression, BlockNode block) { public IfStatementNode(IExpressionNode expression, BlockNode block) {
this.expression = expression; this.expression = expression;
this.block = block; this.block = block;
} }

View File

@ -1,13 +1,13 @@
package ast.statement.statementexpression; package ast.statement.statementexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
public class AssignStatementExpressionNode implements ASTNode { public class AssignStatementExpressionNode implements IStatementExpressionNode {
AssignableExpressionNode assignable; public AssignableExpressionNode assignable;
ExpressionNode expression; public IExpressionNode expression;
public AssignStatementExpressionNode(AssignableExpressionNode assignable, ExpressionNode expression) { public AssignStatementExpressionNode(AssignableExpressionNode assignable, IExpressionNode expression) {
this.assignable = assignable; this.assignable = assignable;
this.expression = expression; this.expression = expression;
} }

View File

@ -3,9 +3,9 @@ package ast.statement.statementexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.unaryexpression.MemberAccessNode; import ast.expression.unaryexpression.MemberAccessNode;
public class AssignableExpressionNode implements ASTNode { public class AssignableExpressionNode implements IStatementExpressionNode {
String identifier; public String identifier;
MemberAccessNode memberAccess; public MemberAccessNode memberAccess;
public AssignableExpressionNode(String identifier) { public AssignableExpressionNode(String identifier) {
this.identifier = identifier; this.identifier = identifier;

View File

@ -0,0 +1,5 @@
package ast.statement.statementexpression;
import ast.statement.IStatementNode;
public interface IStatementExpressionNode extends IStatementNode {}

View File

@ -1,5 +0,0 @@
package ast.statement.statementexpression;
public class IncrementExpression {
}

View File

@ -1,20 +1,20 @@
package ast.statement.statementexpression; package ast.statement.statementexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class NewDeclarationStatementExpressionNode implements ASTNode { public class NewDeclarationStatementExpressionNode implements IStatementExpressionNode {
String identifier; public String identifier;
List<ExpressionNode> expressions = new ArrayList<>(); public List<IExpressionNode> expressions = new ArrayList<>();
public NewDeclarationStatementExpressionNode(String identifier) { public NewDeclarationStatementExpressionNode(String identifier) {
this.identifier = identifier; this.identifier = identifier;
} }
public void addExpression(ExpressionNode expression) { public void addExpression(IExpressionNode expression) {
expressions.add(expression); expressions.add(expression);
} }
} }

View File

@ -2,10 +2,11 @@ package ast.statement.statementexpression.crementExpression;
import ast.ASTNode; import ast.ASTNode;
import ast.statement.statementexpression.AssignableExpressionNode; import ast.statement.statementexpression.AssignableExpressionNode;
import ast.statement.statementexpression.IStatementExpressionNode;
public class DecrementExpressionNode implements ASTNode { public class DecrementExpressionNode implements IStatementExpressionNode {
CrementType crementType; public CrementType crementType;
AssignableExpressionNode assignableExpression; public AssignableExpressionNode assignableExpression;
public DecrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) { public DecrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) {
this.assignableExpression = assignableExpression; this.assignableExpression = assignableExpression;

View File

@ -2,10 +2,11 @@ package ast.statement.statementexpression.crementExpression;
import ast.ASTNode; import ast.ASTNode;
import ast.statement.statementexpression.AssignableExpressionNode; import ast.statement.statementexpression.AssignableExpressionNode;
import ast.statement.statementexpression.IStatementExpressionNode;
public class IncrementExpressionNode implements ASTNode { public class IncrementExpressionNode implements IStatementExpressionNode {
CrementType crementType; public CrementType crementType;
AssignableExpressionNode assignableExpression; public AssignableExpressionNode assignableExpression;
public IncrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) { public IncrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) {
this.assignableExpression = assignableExpression; this.assignableExpression = assignableExpression;

View File

@ -1,20 +1,20 @@
package ast.statement.statementexpression.methodcallstatementnexpression; package ast.statement.statementexpression.methodcallstatementnexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ChainedMethodNode implements ASTNode { public class ChainedMethodNode implements ASTNode {
String identifier; public String identifier;
List<ExpressionNode> expressions = new ArrayList<>(); public List<IExpressionNode> expressions = new ArrayList<>();
public ChainedMethodNode(String identifier) { public ChainedMethodNode(String identifier) {
this.identifier = identifier; this.identifier = identifier;
} }
public void addExpression(ExpressionNode expression) { public void addExpression(IExpressionNode expression) {
expressions.add(expression); expressions.add(expression);
} }
} }

View File

@ -1,16 +1,17 @@
package ast.statement.statementexpression.methodcallstatementnexpression; package ast.statement.statementexpression.methodcallstatementnexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.ExpressionNode; import ast.expression.IExpressionNode;
import ast.statement.statementexpression.IStatementExpressionNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MethodCallStatementExpressionNode implements ASTNode { public class MethodCallStatementExpressionNode implements IStatementExpressionNode {
TargetNode target; public TargetNode target;
List<ChainedMethodNode> chainedMethods = new ArrayList<>(); public List<ChainedMethodNode> chainedMethods = new ArrayList<>();
String identifier; public String identifier;
List<ExpressionNode> expressions = new ArrayList<>(); public List<IExpressionNode> expressions = new ArrayList<>();
public MethodCallStatementExpressionNode(TargetNode target, String identifier) { public MethodCallStatementExpressionNode(TargetNode target, String identifier) {
this.target = target; this.target = target;
@ -21,7 +22,7 @@ public class MethodCallStatementExpressionNode implements ASTNode {
chainedMethods.add(chainedMethode); chainedMethods.add(chainedMethode);
} }
public void addExpression(ExpressionNode expression) { public void addExpression(IExpressionNode expression) {
expressions.add(expression); expressions.add(expression);
} }

View File

@ -5,10 +5,10 @@ import ast.expression.unaryexpression.MemberAccessNode;
import ast.statement.statementexpression.NewDeclarationStatementExpressionNode; import ast.statement.statementexpression.NewDeclarationStatementExpressionNode;
public class TargetNode implements ASTNode { public class TargetNode implements ASTNode {
Boolean thisTar; public Boolean thisTar;
MemberAccessNode memberAccess; public MemberAccessNode memberAccess;
NewDeclarationStatementExpressionNode newDeclaration; public NewDeclarationStatementExpressionNode newDeclaration;
String identifier; public String identifier;
public TargetNode(Boolean thisTar) { public TargetNode(Boolean thisTar) {
this.thisTar = thisTar; this.thisTar = thisTar;

View File

@ -1,7 +1,7 @@
package ast.type; package ast.type;
public class AccessModifierNode { public class AccessModifierNode {
EnumAccessModifierNode accessType; public EnumAccessModifierNode accessType;
public AccessModifierNode(String accessModifier) { public AccessModifierNode(String accessModifier) {
setModifier(accessModifier); setModifier(accessModifier);

View File

@ -1,7 +1,7 @@
package ast.type; package ast.type;
public class TypeNode { public class TypeNode {
EnumTypeNode type; public EnumTypeNode type;
public TypeNode(String type) { public TypeNode(String type) {
setType(type); setType(type);

View File

@ -3,8 +3,8 @@ package ast.type;
import ast.ASTNode; import ast.ASTNode;
public class ValueNode implements ASTNode { public class ValueNode implements ASTNode {
EnumValueNode valueType; public EnumValueNode valueType;
String value; public String value;
public ValueNode(EnumValueNode valueType, String value) { public ValueNode(EnumValueNode valueType, String value) {
this.valueType = valueType; this.valueType = valueType;

View File

@ -53,7 +53,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { 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()) { for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
constructorNode.addParameter((ParameterNode) visit(parameter)); constructorNode.addParameter((ParameterNode) visit(parameter));
} }
@ -66,13 +66,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return new MethodNode((BlockNode) visit(ctx.block())); return new MethodNode((BlockNode) visit(ctx.block()));
} else { } else {
if(ctx.type() != null) { 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()) { for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
methodNode.addParameter((ParameterNode) visit(parameter)); methodNode.addParameter((ParameterNode) visit(parameter));
} }
return methodNode; return methodNode;
} else { } 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()) { for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
// methodNode.addParameter((ParameterNode) visit(parameter)); // methodNode.addParameter((ParameterNode) visit(parameter));
} }
@ -108,34 +108,34 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) { public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) {
return new ReturnStatementNode((ExpressionNode) visit(ctx.expression())); return new ReturnStatementNode((IExpressionNode) visit(ctx.expression()));
} }
@Override @Override
public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) { 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 @Override
public ASTNode visitBlock(SimpleJavaParser.BlockContext ctx) { public ASTNode visitBlock(SimpleJavaParser.BlockContext ctx) {
BlockNode blockNode = new BlockNode(); BlockNode blockNode = new BlockNode();
for(SimpleJavaParser.StatementContext statement : ctx.statement()) { for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
blockNode.addStatement((StatementNode) visit(statement)); blockNode.addStatement((IStatementNode) visit(statement));
} }
return blockNode; return blockNode;
} }
@Override @Override
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) { 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 @Override
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) { public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
if(ctx.statementExpression(0) != null) { 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) { } 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; return null;
} }
@ -151,7 +151,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) { 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 @Override
@ -175,14 +175,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitAssign(SimpleJavaParser.AssignContext ctx) { 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 @Override
public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) { public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) {
NewDeclarationStatementExpressionNode newDeclarationStatementExpressionNode = new NewDeclarationStatementExpressionNode(ctx.Identifier().getText()); NewDeclarationStatementExpressionNode newDeclarationStatementExpressionNode = new NewDeclarationStatementExpressionNode(ctx.Identifier().getText());
for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) { for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) {
newDeclarationStatementExpressionNode.addExpression((ExpressionNode) visit(expression)); newDeclarationStatementExpressionNode.addExpression((IExpressionNode) visit(expression));
} }
return newDeclarationStatementExpressionNode; return newDeclarationStatementExpressionNode;
} }
@ -194,7 +194,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod)); methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod));
} }
for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) { for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) {
methodCallStatementExpressionNode.addExpression((ExpressionNode) visit(expression)); methodCallStatementExpressionNode.addExpression((IExpressionNode) visit(expression));
} }
return methodCallStatementExpressionNode; return methodCallStatementExpressionNode;
} }
@ -217,7 +217,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
public ASTNode visitChainedMethod(SimpleJavaParser.ChainedMethodContext ctx) { public ASTNode visitChainedMethod(SimpleJavaParser.ChainedMethodContext ctx) {
ChainedMethodNode chainedMethodNode = new ChainedMethodNode(ctx.Identifier().getText()); ChainedMethodNode chainedMethodNode = new ChainedMethodNode(ctx.Identifier().getText());
for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) { for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) {
chainedMethodNode.addExpression((ExpressionNode) visit(expression)); chainedMethodNode.addExpression((IExpressionNode) visit(expression));
} }
return chainedMethodNode; return chainedMethodNode;
} }
@ -295,9 +295,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} else if(ctx.notExpression() != null) { } else if(ctx.notExpression() != null) {
return new UnaryExpressionNode((NotExpressionNode) visitNotExpression(ctx.notExpression())); return new UnaryExpressionNode((NotExpressionNode) visitNotExpression(ctx.notExpression()));
} else if(ctx.statementExpression() != null) { } else if(ctx.statementExpression() != null) {
return new UnaryExpressionNode((StatementNode) visitStatementExpression(ctx.statementExpression())); return new UnaryExpressionNode((IStatementNode) visitStatementExpression(ctx.statementExpression()));
} else if(ctx.expression() != null) { } else if(ctx.expression() != null) {
return new UnaryExpressionNode((ExpressionNode) visitExpression(ctx.expression())); return new UnaryExpressionNode((IExpressionNode) visitExpression(ctx.expression()));
} }
return null; return null;
} }
@ -332,7 +332,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitNotExpression(SimpleJavaParser.NotExpressionContext ctx) { 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<ASTNode> {
@Override @Override
public ASTNode visitNonCalculationExpression(SimpleJavaParser.NonCalculationExpressionContext ctx) { 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 @Override

View File

@ -11,8 +11,7 @@ public class Example {
public class Test { public class Test {
public static int testMethod(char x, int a){ public static int testMethod(char x, int a){
x = x + a;
return x;
} }
} }