Fix comp parse order

This commit is contained in:
augustin64 2023-11-24 11:15:17 +01:00
parent 76770b6ecc
commit 285c054d37

View File

@ -331,14 +331,21 @@ ParseReturn parse_expr(vector<Token> tokens) {
}
ParseReturn parse_comp(vector<Token> tokens) {
ParseReturn ret = parse_sum(tokens);
if (tokens.size() == 0)
return ret;
throw ParseException();
// At least 1 Sum
ParseReturn ret = parse_sum(tokens);
tokens = ret.tokens;
Node node = ret.node;
//* We construct a tree
while (tokens.size() != 0) {
NodeType type;
switch (tokens.back().type) {
case TokenType::Equal:
case TokenType::DoubleEqual:
type = NodeType::Eq;
break;
case TokenType::NotEqual:
@ -363,19 +370,38 @@ ParseReturn parse_comp(vector<Token> tokens) {
type = NodeType::Lor;
break;
default:
return ret;
}
tokens.pop_back();
ParseReturn ret_comp = parse_comp(tokens);
InnerNode node = {
.type=type,
.children={ ret.node, ret_comp.node },
.pos=get_node_pos(ret_comp.node)
};
return {
.node=node,
.tokens=ret_comp.tokens
.tokens=tokens
};
}
Token last_token;
try {
last_token = tokens.back();
tokens.pop_back();
ParseReturn ret = parse_sum(tokens);
tokens = ret.tokens;
InnerNode new_node = {
.type=type,
.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
};
}