Compare commits
3 Commits
b5b6f763e0
...
e99b7adcd4
Author | SHA1 | Date | |
---|---|---|---|
e99b7adcd4 | |||
3783a1d61b | |||
d0f3f3b938 |
@ -18,7 +18,6 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
|||||||
public Node visitProgram(DecafParser.ProgramContext ctx) {
|
public Node visitProgram(DecafParser.ProgramContext ctx) {
|
||||||
List<RefType> classes = new ArrayList<>();
|
List<RefType> classes = new ArrayList<>();
|
||||||
for (DecafParser.ClassdeclContext classDecl : ctx.classdecl()) {
|
for (DecafParser.ClassdeclContext classDecl : ctx.classdecl()) {
|
||||||
// Node refType = new RefType(classDecl.Identifier().getText(), new ArrayList<>(), new ArrayList<>());
|
|
||||||
classes.add((RefType) visit(classDecl));
|
classes.add((RefType) visit(classDecl));
|
||||||
}
|
}
|
||||||
return new Program(classes);
|
return new Program(classes);
|
||||||
@ -28,13 +27,22 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
|||||||
public Node visitClassdecl(DecafParser.ClassdeclContext ctx) {
|
public Node visitClassdecl(DecafParser.ClassdeclContext ctx) {
|
||||||
List<FieldDecl> fieldDecls = new ArrayList<>();
|
List<FieldDecl> fieldDecls = new ArrayList<>();
|
||||||
List<MethodDecl> methodDecls = new ArrayList<>();
|
List<MethodDecl> methodDecls = new ArrayList<>();
|
||||||
|
boolean hasMain;
|
||||||
|
if(ctx.MainMethodDecl() != null) {
|
||||||
|
hasMain = true;
|
||||||
|
} else {
|
||||||
|
hasMain = false;
|
||||||
|
}
|
||||||
for (DecafParser.LocalVarDeclContext fieldDecl: ctx.localVarDecl()) {
|
for (DecafParser.LocalVarDeclContext fieldDecl: ctx.localVarDecl()) {
|
||||||
fieldDecls.add((FieldDecl) visit(fieldDecl));
|
fieldDecls.add((FieldDecl) visit(fieldDecl));
|
||||||
}
|
}
|
||||||
|
for (DecafParser.ConstuctorDeclContext constDecl: ctx.constuctorDecl()) {
|
||||||
|
methodDecls.add((MethodDecl) visit(constDecl));
|
||||||
|
}
|
||||||
for (DecafParser.MethodDeclContext methodDecl: ctx.methodDecl()) {
|
for (DecafParser.MethodDeclContext methodDecl: ctx.methodDecl()) {
|
||||||
methodDecls.add((MethodDecl) visit(methodDecl));
|
methodDecls.add((MethodDecl) visit(methodDecl));
|
||||||
}
|
}
|
||||||
return new RefType(ctx.Identifier().getText(),fieldDecls, methodDecls);
|
return new RefType(ctx.Identifier().getText(),fieldDecls, methodDecls, hasMain);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -42,25 +50,37 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
|||||||
return new FieldDecl(ctx.type().getText(), ctx.Identifier().getText());
|
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
|
@Override
|
||||||
public Node visitMethodDecl(DecafParser.MethodDeclContext ctx) {
|
public Node visitMethodDecl(DecafParser.MethodDeclContext ctx) {
|
||||||
String type;
|
String type;
|
||||||
String name = ctx.Identifier().getText();
|
String name = ctx.Identifier().getText();
|
||||||
List<Parameter> parameterList = new ArrayList<>();
|
ParameterList parameterList = (ParameterList) visit(ctx.parameterList());
|
||||||
if (ctx.Void() != null) {
|
if (ctx.Void() != null) {
|
||||||
type = "void";
|
type = "void";
|
||||||
} else {
|
} else {
|
||||||
type = ctx.type().getText();
|
type = ctx.type().getText();
|
||||||
}
|
}
|
||||||
DecafParser.ParameterListContext parameterListContext = ctx.parameterList();
|
return new MethodDecl("", type , name, parameterList, new BlockStatement(new ArrayList<>(), "void"));
|
||||||
for (DecafParser.ParameterContext parameter: parameterListContext.parameter()) {
|
|
||||||
parameterList.add((Parameter) visit(parameter));
|
|
||||||
}
|
|
||||||
return new MethodDecl("", type , name, new ParameterList(parameterList), new BlockStatement(new ArrayList<>(), "void"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node visitParameter(DecafParser.ParameterContext ctx) {
|
public Node visitParameter(DecafParser.ParameterContext ctx) {
|
||||||
return new Parameter(ctx.type().getText(), ctx.Identifier().getText());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,14 +16,17 @@ public class RefType extends AbstractType implements IClass, Node {
|
|||||||
public String name;
|
public String name;
|
||||||
public List<FieldDecl> fieldDecls;
|
public List<FieldDecl> fieldDecls;
|
||||||
public List<MethodDecl> methodDecls;
|
public List<MethodDecl> methodDecls;
|
||||||
|
boolean hasMain;
|
||||||
|
|
||||||
public RefType(String name,
|
public RefType(String name,
|
||||||
List<FieldDecl> fieldDecls,
|
List<FieldDecl> fieldDecls,
|
||||||
List<MethodDecl> methodDecls
|
List<MethodDecl> methodDecls,
|
||||||
|
boolean hasMain
|
||||||
){
|
){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.fieldDecls = fieldDecls;
|
this.fieldDecls = fieldDecls;
|
||||||
this.methodDecls = methodDecls;
|
this.methodDecls = methodDecls;
|
||||||
|
this.hasMain = hasMain;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext,
|
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext,
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package abstractSyntaxTree.Statement;
|
package abstractSyntaxTree.Statement;
|
||||||
|
|
||||||
import TypeCheck.TypeCheckResult;
|
import TypeCheck.TypeCheckResult;
|
||||||
|
import abstractSyntaxTree.Node;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface IStatement {
|
public interface IStatement extends Node {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user