|
|
|
@ -18,7 +18,6 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
|
|
|
|
public Node visitProgram(DecafParser.ProgramContext ctx) {
|
|
|
|
|
List<RefType> classes = new ArrayList<>();
|
|
|
|
|
for (DecafParser.ClassdeclContext classDecl : ctx.classdecl()) {
|
|
|
|
|
// Node refType = new RefType(classDecl.Identifier().getText(), new ArrayList<>(), new ArrayList<>());
|
|
|
|
|
classes.add((RefType) visit(classDecl));
|
|
|
|
|
}
|
|
|
|
|
return new Program(classes);
|
|
|
|
@ -28,13 +27,22 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
|
|
|
|
public Node visitClassdecl(DecafParser.ClassdeclContext ctx) {
|
|
|
|
|
List<FieldDecl> fieldDecls = new ArrayList<>();
|
|
|
|
|
List<MethodDecl> methodDecls = new ArrayList<>();
|
|
|
|
|
boolean hasMain;
|
|
|
|
|
if(ctx.MainMethodDecl() != null) {
|
|
|
|
|
hasMain = true;
|
|
|
|
|
} else {
|
|
|
|
|
hasMain = false;
|
|
|
|
|
}
|
|
|
|
|
for (DecafParser.LocalVarDeclContext fieldDecl: ctx.localVarDecl()) {
|
|
|
|
|
fieldDecls.add((FieldDecl) visit(fieldDecl));
|
|
|
|
|
}
|
|
|
|
|
for (DecafParser.ConstuctorDeclContext constDecl: ctx.constuctorDecl()) {
|
|
|
|
|
methodDecls.add((MethodDecl) visit(constDecl));
|
|
|
|
|
}
|
|
|
|
|
for (DecafParser.MethodDeclContext methodDecl: ctx.methodDecl()) {
|
|
|
|
|
methodDecls.add((MethodDecl) visit(methodDecl));
|
|
|
|
|
}
|
|
|
|
|
return new RefType(ctx.Identifier().getText(),fieldDecls, methodDecls);
|
|
|
|
|
return new RefType(ctx.Identifier().getText(),fieldDecls, methodDecls, hasMain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -42,25 +50,37 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
|
|
|
|
return new FieldDecl(ctx.type().getText(), ctx.Identifier().getText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Node visitConstuctorDecl(DecafParser.ConstuctorDeclContext ctx) {
|
|
|
|
|
String name = ctx.Identifier().getText();
|
|
|
|
|
ParameterList parameterList = (ParameterList) visit(ctx.parameterList());
|
|
|
|
|
return new MethodDecl("", "", name, parameterList, new BlockStatement(new ArrayList<>(), "void"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Node visitMethodDecl(DecafParser.MethodDeclContext ctx) {
|
|
|
|
|
String type;
|
|
|
|
|
String name = ctx.Identifier().getText();
|
|
|
|
|
List<Parameter> parameterList = new ArrayList<>();
|
|
|
|
|
if (ctx.Void() != null) {
|
|
|
|
|
ParameterList parameterList = (ParameterList) visit(ctx.parameterList());
|
|
|
|
|
if (ctx.Void() != null) {
|
|
|
|
|
type = "void";
|
|
|
|
|
} else {
|
|
|
|
|
type = ctx.type().getText();
|
|
|
|
|
}
|
|
|
|
|
DecafParser.ParameterListContext parameterListContext = ctx.parameterList();
|
|
|
|
|
for (DecafParser.ParameterContext parameter: parameterListContext.parameter()) {
|
|
|
|
|
parameterList.add((Parameter) visit(parameter));
|
|
|
|
|
}
|
|
|
|
|
return new MethodDecl("", type , name, new ParameterList(parameterList), new BlockStatement(new ArrayList<>(), "void"));
|
|
|
|
|
return new MethodDecl("", type , name, parameterList, new BlockStatement(new ArrayList<>(), "void"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Node visitParameter(DecafParser.ParameterContext ctx) {
|
|
|
|
|
return new Parameter(ctx.type().getText(), ctx.Identifier().getText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Node visitParameterList(DecafParser.ParameterListContext ctx) {
|
|
|
|
|
List<Parameter> parameters = new ArrayList<>();
|
|
|
|
|
for (DecafParser.ParameterContext parameter: ctx.parameter()) {
|
|
|
|
|
parameters.add((Parameter) visit(parameter));
|
|
|
|
|
}
|
|
|
|
|
return new ParameterList(parameters);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|