johns-branch #12

Merged
i22005 merged 23 commits from johns-branch into main 2024-06-21 16:30:56 +00:00
36 changed files with 131 additions and 175 deletions
Showing only changes of commit 50a52a1e87 - Show all commits

View File

@ -1,7 +1,7 @@
import oldAst.ASTNode; import ast.ASTNode;
import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.*;
import oldAst.ProgramNode; import ast.ProgramNode;
import bytecode.ByteCodeGenerator; //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;
@ -9,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;
@ -60,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());
@ -70,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");
@ -80,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,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,17 +1,17 @@
package ast.block; package ast.block;
import ast.ASTNode; import ast.ASTNode;
import ast.statement.StatementNode; import ast.statement.IStatementNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class BlockNode implements ASTNode { 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,4 +0,0 @@
package ast.expression;
public class ExpressionNode {
}

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,7 +2,7 @@ package ast.expression.binaryexpression;
import ast.ASTNode; import ast.ASTNode;
public class CalculationExpressionNode implements ASTNode { public class CalculationExpressionNode extends BinaryExpressionNode {
public CalculationExpressionNode calculationExpression; public CalculationExpressionNode calculationExpression;
public String operator; public String operator;
public DotExpressionNode dotExpression; public DotExpressionNode dotExpression;

View File

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

View File

@ -5,7 +5,7 @@ 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 {
public ValueNode value; public ValueNode value;
public String identifier; public String identifier;
public MemberAccessNode memberAccess; public MemberAccessNode memberAccess;

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 {
public UnaryExpressionNode unaryExpression; public UnaryExpressionNode unaryExpression;
public String operator; 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.unaryExpression = unaryExpression;
this.operator = operator; this.operator = operator;
this.expression = expression; this.expression = expression;

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 {
public 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 implements ASTNode { public class UnaryExpressionNode implements IExpressionNode {
public String thisExp; public String thisExp;
public String identifier; public String identifier;
public MemberAccessNode memberAccess; public MemberAccessNode memberAccess;
public ValueNode value; public ValueNode value;
public NotExpressionNode notExpression; public NotExpressionNode notExpression;
public StatementNode statement; public IStatementNode statement;
public 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 implements ASTNode {
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,10 +4,6 @@ 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;
@ -17,7 +13,7 @@ public class MethodNode implements MemberNode {
public TypeNode type; public TypeNode type;
public Boolean voidType; public Boolean voidType;
public String identifier; public String identifier;
public List<ParameterNode> parameters; 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 {
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 {
public ExpressionNode statementExpressionInit; public IExpressionNode statementExpressionInit;
public StatementNode localVariableDeclarationInit; public IStatementNode localVariableDeclarationInit;
public ExpressionNode expression; public IExpressionNode expression;
public 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 implements ASTNode { public class LocalVariableDeclarationNode implements IStatementNode {
public TypeNode type; public TypeNode type;
public String identifier; public String identifier;
public String assign; 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.type = type;
this.identifier = identifier; this.identifier = identifier;
this.assign = assign; this.assign = assign;

View File

@ -1,16 +1,12 @@
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 java.util.ArrayList; public class ReturnStatementNode implements IStatementNode {
import java.util.List; public IExpressionNode expression;
public class ReturnStatementNode implements ASTNode { public ReturnStatementNode(IExpressionNode expression) {
public ExpressionNode expression;
public ReturnStatementNode(ExpressionNode expression) {
this.expression = expression; this.expression = expression;
} }
} }

View File

@ -1,4 +0,0 @@
package ast.statement;
public class StatementNode {
}

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 {
public ExpressionNode expression; public IExpressionNode expression;
public 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,8 +2,9 @@ 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 {
public BlockNode block; public BlockNode block;
public ElseStatementNode(BlockNode block) { public ElseStatementNode(BlockNode block) {

View File

@ -1,11 +1,12 @@
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 {
public IfStatementNode ifStatement; public IfStatementNode ifStatement;
public List<ElseStatementNode> elseStatements = new ArrayList<>(); public List<ElseStatementNode> elseStatements = new ArrayList<>();

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 {
public ExpressionNode expression; public IExpressionNode expression;
public 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 {
public AssignableExpressionNode assignable; 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.assignable = assignable;
this.expression = expression; this.expression = expression;
} }

View File

@ -3,7 +3,7 @@ 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 {
public String identifier; public String identifier;
public MemberAccessNode memberAccess; public MemberAccessNode memberAccess;

View File

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

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 {
public String identifier; public String identifier;
public 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,8 +2,9 @@ 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 {
public CrementType crementType; public CrementType crementType;
public AssignableExpressionNode assignableExpression; public AssignableExpressionNode assignableExpression;

View File

@ -2,8 +2,9 @@ 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 {
public CrementType crementType; public CrementType crementType;
public AssignableExpressionNode assignableExpression; public AssignableExpressionNode 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 {
public String identifier; public String identifier;
public 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 {
public TargetNode target; public TargetNode target;
public List<ChainedMethodNode> chainedMethods = new ArrayList<>(); public List<ChainedMethodNode> chainedMethods = new ArrayList<>();
public String identifier; public String identifier;
public 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

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