change methods in ASTGenerator

This commit is contained in:
StefanZ3 2024-05-13 22:04:02 +02:00
parent cc05c58159
commit d0f3f3b938
3 changed files with 34 additions and 11 deletions

View File

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

View File

@ -19,11 +19,13 @@ public class RefType extends AbstractType implements IClass, Node {
public RefType(String name,
List<FieldDecl> fieldDecls,
List<MethodDecl> methodDecls
List<MethodDecl> methodDecls,
boolean hasMain
){
this.name = name;
this.fieldDecls = fieldDecls;
this.methodDecls = methodDecls;
this.hasMain = hasMain;
}
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext,

View File

@ -1,12 +1,13 @@
package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Node;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.List;
public interface IStatement {
public interface IStatement extends Node {