parser: Add comma
This commit is contained in:
parent
0033c4b814
commit
8e853aaaf2
@ -259,6 +259,10 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
|
||||
|
||||
return memory.get(identifier).type;
|
||||
}
|
||||
case NodeType::Comma: {
|
||||
analyze(node.children[0], memory);
|
||||
return analyze(node.children[1], memory);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw exception();
|
||||
|
@ -47,7 +47,7 @@ ExprStatement ->
|
||||
| Type ParIdentifier // Declaration
|
||||
|
||||
|
||||
Expr -> Comp
|
||||
Expr -> Comp, Expr | Comp
|
||||
|
||||
Comp ->
|
||||
| Sum
|
||||
@ -129,7 +129,8 @@ enum class NodeType {
|
||||
Eq, // -> Sum == Comp
|
||||
Neq, // -> Sum != Comp
|
||||
Land, // -> Sum && Comp
|
||||
Lor // -> Sum || Comp
|
||||
Lor, // -> Sum || Comp
|
||||
Comma // -> Comp, Expr
|
||||
};
|
||||
|
||||
struct InnerNode;
|
||||
|
@ -17,7 +17,7 @@ CodePosition null_pos = {
|
||||
const char* _debug_ast_node_names[] = {
|
||||
"Prog", "Epsilon", "AssignedDeclaration", "Declaration", "Plus", "Minus", "Mult", "Div", "Mod",
|
||||
"UnaryMinus", "UnaryPlus", "Neg", "Assignment", "LIncr", "RIncr", "LDecr", "RDecr", "If", "IfElse",
|
||||
"For", "While", "Bloc", "Lt", "Gt", "Leq", "Geq", "Eq", "Neq", "Land", "Lor"
|
||||
"For", "While", "Bloc", "Lt", "Gt", "Leq", "Geq", "Eq", "Neq", "Land", "Lor", "Comma"
|
||||
};
|
||||
void _debug_print_tree(const Node& node, int depth, const string& prefix) {
|
||||
if (holds_alternative<InnerNode>(node)) {
|
||||
@ -427,7 +427,44 @@ ParseReturn parse_expr_statement(vector<Token> tokens) {
|
||||
}
|
||||
|
||||
ParseReturn parse_expr(vector<Token> tokens) {
|
||||
return parse_comp(tokens);
|
||||
if (tokens.size() == 0)
|
||||
throw ParseException();
|
||||
|
||||
// At least 1 Term
|
||||
ParseReturn ret = parse_comp(tokens);
|
||||
tokens = ret.tokens;
|
||||
|
||||
Node node = ret.node;
|
||||
|
||||
//* We construct a tree
|
||||
while (tokens.size() != 0 && tokens.back().type == TokenType::Comma) {
|
||||
Token last_token;
|
||||
try {
|
||||
last_token = tokens.back();
|
||||
tokens.pop_back();
|
||||
|
||||
ParseReturn ret = parse_comp(tokens);
|
||||
tokens = ret.tokens;
|
||||
|
||||
InnerNode new_node = {
|
||||
.type=NodeType::Comma,
|
||||
.children={node, ret.node},
|
||||
.pos=last_token.pos
|
||||
};
|
||||
node = new_node;
|
||||
} catch (const ParseException& pex) {
|
||||
tokens.emplace_back(last_token);
|
||||
return {
|
||||
.node=node,
|
||||
.tokens=tokens
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
.node=node,
|
||||
.tokens=tokens
|
||||
};
|
||||
}
|
||||
|
||||
ParseReturn parse_comp(vector<Token> tokens) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user