Compare commits

...

3 Commits

Author SHA1 Message Date
e99b7adcd4 change methods in ASTGenerator 2024-05-13 22:09:24 +02:00
3783a1d61b Merge remote-tracking branch 'origin/master' 2024-05-13 22:08:05 +02:00
d0f3f3b938 change methods in ASTGenerator 2024-05-13 22:04:02 +02:00
3 changed files with 35 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

@ -16,14 +16,17 @@ public class RefType extends AbstractType implements IClass, Node {
public String name;
public List<FieldDecl> fieldDecls;
public List<MethodDecl> methodDecls;
boolean hasMain;
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 {