64 lines
1.9 KiB
Java
64 lines
1.9 KiB
Java
package frontend.ast;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import error.Errors;
|
|
import frontend.ast.decl.Decl;
|
|
import frontend.ast.func.FuncDef;
|
|
import frontend.ast.func.MainFuncDef;
|
|
import frontend.lexer.TokenStream;
|
|
import frontend.lexer.TokenType;
|
|
|
|
public class CompUnit extends Node {
|
|
public CompUnit(TokenStream ts) {
|
|
super(SyntaxType.COMP_UNIT, ts);
|
|
}
|
|
|
|
public void parse(Errors errors) {
|
|
while (getCurrToken() != null) {
|
|
if (this.ts.peek(1).getType() == TokenType.MAINTK) {
|
|
MainFuncDef mainFuncDef = new MainFuncDef(this.ts);
|
|
mainFuncDef.parse(errors);
|
|
addChild(mainFuncDef);
|
|
} else if (this.ts.peek(2).getType() == TokenType.LPARENT) {
|
|
FuncDef funcdef = new FuncDef(this.ts);
|
|
funcdef.parse(errors);
|
|
addChild(funcdef);
|
|
} else {
|
|
Decl decl = new Decl(this.ts);
|
|
decl.parse(errors);
|
|
addChild(decl);
|
|
}
|
|
}
|
|
}
|
|
|
|
public ArrayList<Decl> GetDecls() {
|
|
ArrayList<Decl> decls = new ArrayList<Decl>();
|
|
for (int i = 0; i < this.getChildren().size(); i++) {
|
|
if (this.getChild(i) instanceof Decl) {
|
|
decls.add((Decl) this.getChild(i));
|
|
}
|
|
}
|
|
return decls;
|
|
}
|
|
|
|
public ArrayList<FuncDef> GetFuncDefs() {
|
|
ArrayList<FuncDef> funcDefs = new ArrayList<FuncDef>();
|
|
for (int i = 0; i < this.getChildren().size(); i++) {
|
|
if (this.getChild(i) instanceof FuncDef) {
|
|
funcDefs.add((FuncDef) this.getChild(i));
|
|
}
|
|
}
|
|
return funcDefs;
|
|
}
|
|
|
|
public MainFuncDef GetMainFuncDef() {
|
|
for (int i = 0; i < this.getChildren().size(); i++) {
|
|
if (this.getChild(i) instanceof MainFuncDef) {
|
|
return (MainFuncDef) this.getChild(i);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|