60 lines
1.7 KiB
Java
60 lines
1.7 KiB
Java
package frontend.ast.decl;
|
|
|
|
import error.ErrorType;
|
|
import error.Errors;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import error.Error;
|
|
import frontend.ast.Node;
|
|
import frontend.ast.SyntaxType;
|
|
import frontend.ast.token.TokenNode;
|
|
import frontend.lexer.TokenStream;
|
|
import frontend.lexer.TokenType;
|
|
|
|
public class VarDecl extends Node {
|
|
public VarDecl(TokenStream ts) {
|
|
super(SyntaxType.VAR_DECL, ts);
|
|
}
|
|
|
|
public void parse(Errors errors) {
|
|
if (getCurrToken().getType() == TokenType.STATICTK) {
|
|
TokenNode staitckk = new TokenNode(ts);
|
|
addChild(staitckk);
|
|
}
|
|
TokenNode intkk = new TokenNode(ts);
|
|
addChild(intkk);
|
|
while (true) {
|
|
VarDef vdf = new VarDef(ts);
|
|
vdf.parse(errors);
|
|
addChild(vdf);
|
|
if (getCurrToken().getType() == TokenType.COMMA) {
|
|
TokenNode comma = new TokenNode(ts);
|
|
addChild(comma);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if (getCurrToken().getType() == TokenType.SEMICN) {
|
|
TokenNode semicn = new TokenNode(ts);
|
|
addChild(semicn);
|
|
} else {
|
|
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.i));
|
|
}
|
|
}
|
|
|
|
public ArrayList<VarDef> GetVarDefs() {
|
|
ArrayList<VarDef> varDefs = new ArrayList<VarDef>();
|
|
if (getChild(1) instanceof VarDef) {
|
|
for (int i = 1; i < getChildren().size(); i += 2) {
|
|
varDefs.add((VarDef) getChild(i));
|
|
}
|
|
} else {
|
|
for (int i = 2; i < getChildren().size(); i += 2) {
|
|
varDefs.add((VarDef) getChild(i));
|
|
}
|
|
}
|
|
return varDefs;
|
|
}
|
|
}
|