Antlr + Visitors
This commit is contained in:
parent
993bd8fc60
commit
20052e619d
4
src/main/java/ast/ExpressionNode.java
Normal file
4
src/main/java/ast/ExpressionNode.java
Normal file
@ -0,0 +1,4 @@
|
||||
package ast;
|
||||
|
||||
public class ExpressionNode {
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package ast;
|
||||
|
||||
public class FieldNode {
|
||||
String type;
|
||||
public class FieldNode extends MemberNode {
|
||||
TypeNode type;
|
||||
String name;
|
||||
|
||||
public FieldNode(String type, String name){
|
||||
public FieldNode(TypeNode type, String name){
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
}
|
||||
|
@ -1,11 +1,17 @@
|
||||
package ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodNode extends MemberNode{
|
||||
public String visibility;
|
||||
public TypeNode visibility;
|
||||
public String name;
|
||||
|
||||
public MethodNode(String visibility, String name){
|
||||
public ParameterListNode parameters;
|
||||
|
||||
public MethodNode(TypeNode visibility, String name, ParameterListNode parameters){
|
||||
this.visibility = visibility;
|
||||
this.name = name;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
}
|
||||
|
12
src/main/java/ast/ParameterListNode.java
Normal file
12
src/main/java/ast/ParameterListNode.java
Normal file
@ -0,0 +1,12 @@
|
||||
package ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ParameterListNode {
|
||||
List<ParameterNode> parameters = new ArrayList<>();
|
||||
|
||||
public ParameterListNode(){
|
||||
parameters
|
||||
}
|
||||
}
|
11
src/main/java/ast/ParameterNode.java
Normal file
11
src/main/java/ast/ParameterNode.java
Normal file
@ -0,0 +1,11 @@
|
||||
package ast;
|
||||
|
||||
public class ParameterNode extends ASTNode {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
|
||||
public ParameterNode(TypeNode type, String identifier) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
}
|
@ -1,12 +1,9 @@
|
||||
package ast;
|
||||
public class ParameterNode extends ASTNode {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgramNode extends ASTNode {
|
||||
public List<ClassNode> classes = new ArrayList<>();
|
||||
|
||||
public void addClass(ClassNode classNode) {
|
||||
classes.add(classNode);
|
||||
public ParameterNode(TypeNode type, String identifier) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
}
|
7
src/main/java/ast/StatementNode.java
Normal file
7
src/main/java/ast/StatementNode.java
Normal file
@ -0,0 +1,7 @@
|
||||
package ast;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public abstract class StatementNode extends ASTNode {
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
package ast;
|
||||
|
||||
public class TypeNode {
|
||||
String type;
|
||||
public class TypeNode extends ASTNode {
|
||||
public String typeName;
|
||||
|
||||
public TypeNode(String typeName) {
|
||||
this.typeName = typeName;
|
||||
}
|
||||
}
|
||||
|
13
src/main/java/ast/VariableDeclarationStatementNode.java
Normal file
13
src/main/java/ast/VariableDeclarationStatementNode.java
Normal file
@ -0,0 +1,13 @@
|
||||
package ast;
|
||||
|
||||
public class VariableDeclarationStatementNode extends StatementNode {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
public ExpressionNode expression; // Nullable for cases without initialization
|
||||
|
||||
public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
@ -4,6 +4,9 @@ import ast.*;
|
||||
import parser.generated.SimpleJavaBaseVisitor;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
@Override
|
||||
public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) {
|
||||
@ -32,5 +35,58 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
return new FieldNode(type, identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
|
||||
TypeNode returnType = (TypeNode) visit(ctx.type());
|
||||
String methodName = ctx.IDENTIFIER().getText();
|
||||
ParameterListNode = (ParameterListNode) visit(ctx.parameterList());
|
||||
List<ParameterNode> parameters = new ArrayList<>();
|
||||
for (SimpleJavaParser.ParameterContext parameter : ctx.parameterList()) {
|
||||
parameters.add(parameter);
|
||||
}
|
||||
MethodNode method = new MethodNode(returnType, methodName);
|
||||
|
||||
for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameterList().parameter()) {
|
||||
method.parameters.add((ParameterNode) visit(paramCtx));
|
||||
}
|
||||
|
||||
for (SimpleJavaParser.StatementContext stmtCtx : ctx.statement()) {
|
||||
method.statements.add((StatementNode) visit(stmtCtx));
|
||||
}
|
||||
|
||||
return method;
|
||||
}
|
||||
@Override
|
||||
public ASTNode visitType(SimpleJavaParser.TypeContext ctx) {
|
||||
return new TypeNode(ctx.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) {
|
||||
if (ctx.variableDeclarationStatement() != null) {
|
||||
return visitVariableDeclarationStatement(ctx.variableDeclarationStatement());
|
||||
} else if (ctx.assignmentStatement() != null) {
|
||||
return visitAssignmentStatement(ctx.assignmentStatement());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitParameterList(SimpleJavaParser.ParameterListContext ctx) {
|
||||
List<ParameterNode> parameters = new ArrayList<>();
|
||||
for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) {
|
||||
parameters.add((ParameterNode) visitParameter(paramCtx));
|
||||
}
|
||||
return new ParameterListNode(parameters); // Assuming you want to keep a dedicated node for the list
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) {
|
||||
TypeNode type = (TypeNode) visit(ctx.type()); // Assuming visitType returns a TypeNode
|
||||
String identifier = ctx.IDENTIFIER().getText();
|
||||
return new ParameterNode(type, identifier);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user