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

59 lines
1.9 KiB
Java
Executable File

package frontend.ast.val;
import java.util.ArrayList;
import error.Errors;
import frontend.ast.Node;
import frontend.ast.SyntaxType;
import frontend.ast.exp.ConstExp;
import frontend.ast.token.TokenNode;
import frontend.lexer.TokenStream;
import frontend.lexer.TokenType;
public class ConstInitVal extends Node {
public ConstInitVal(TokenStream ts) {
super(SyntaxType.CONST_INIT_VAL, ts);
}
public void parse(Errors errors) {
if (getCurrToken().getType() != TokenType.LBRACE) {
ConstExp cep = new ConstExp(this.ts);
cep.parse(errors);
addChild(cep);
} else {
TokenNode lbrace = new TokenNode(this.ts);
addChild(lbrace);
if (getCurrToken().getType() != TokenType.RBRACE) {
while (true) { // judge rbrace or not ??
ConstExp cep = new ConstExp(this.ts);
cep.parse(errors);
addChild(cep);
if (getCurrToken().getType() == TokenType.COMMA) {
TokenNode comma = new TokenNode(this.ts);
addChild(comma);
} else {
break;
}
}
}
TokenNode rbrace = new TokenNode(this.ts);
addChild(rbrace);
}
}
public ArrayList<Integer> getValue() {
ArrayList<Integer> valueList = new ArrayList<>();
if (getChild(0) instanceof ConstExp) {
valueList.add(((ConstExp) getChild(0)).getValue());
return valueList;
} else {
for (int i = 1; i < getChildren().size(); i += 2) {
if (getChild(i) instanceof ConstExp) {
valueList.add(((ConstExp) getChild(i)).getValue());
}
}
return valueList;
}
}
}