Files
MY_COMPILER/frontend/ast/decl/ConstDecl.java
2025-12-12 20:14:00 +08:00

52 lines
1.5 KiB
Java
Executable File

package frontend.ast.decl;
import error.Errors;
import error.ErrorType;
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 ConstDecl extends Node {
public ConstDecl(TokenStream ts) {
super(SyntaxType.CONST_DECL, ts);
}
public void parse(Errors errors) {
TokenNode constkk = new TokenNode(this.ts);
addChild(constkk);
TokenNode intkk = new TokenNode(this.ts);
addChild(intkk);
while (true) {
ConstDef cdef = new ConstDef(this.ts);
cdef.parse(errors);
addChild(cdef);
if (getCurrToken().getType() != TokenType.COMMA) {
break;
} else {
TokenNode comma = new TokenNode(this.ts);
addChild(comma);
}
}
if (getCurrToken().getType() != TokenType.SEMICN) {
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.i));
} else {
TokenNode semicoln = new TokenNode(this.ts);
addChild(semicoln);
}
}
public ArrayList<ConstDef> GetConstDefs() {
ArrayList<ConstDef> constDefs = new ArrayList<>();
for (int i = 2; i < getChildren().size(); i += 2) {
constDefs.add((ConstDef) getChild(i));
}
return constDefs;
}
}