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,51 +331,77 @@ ParseReturn parse_expr(vector<Token> tokens) {
} }
ParseReturn parse_comp(vector<Token> tokens) { ParseReturn parse_comp(vector<Token> tokens) {
ParseReturn ret = parse_sum(tokens);
if (tokens.size() == 0) if (tokens.size() == 0)
return ret; throw ParseException();
// At least 1 Sum
ParseReturn ret = parse_sum(tokens);
tokens = ret.tokens; tokens = ret.tokens;
NodeType type;
switch (tokens.back().type) { Node node = ret.node;
case TokenType::Equal:
type = NodeType::Eq; //* We construct a tree
break; while (tokens.size() != 0) {
case TokenType::NotEqual: NodeType type;
type = NodeType::Neq;
break; switch (tokens.back().type) {
case TokenType::Lt: case TokenType::DoubleEqual:
type = NodeType::Lt; type = NodeType::Eq;
break; break;
case TokenType::Gt: case TokenType::NotEqual:
type = NodeType::Gt; type = NodeType::Neq;
break; break;
case TokenType::Leq: case TokenType::Lt:
type = NodeType::Leq; type = NodeType::Lt;
break; break;
case TokenType::Geq: case TokenType::Gt:
type = NodeType::Geq; type = NodeType::Gt;
break; break;
case TokenType::Land: case TokenType::Leq:
type = NodeType::Land; type = NodeType::Leq;
break; break;
case TokenType::Lor: case TokenType::Geq:
type = NodeType::Lor; type = NodeType::Geq;
break; break;
default: case TokenType::Land:
return ret; type = NodeType::Land;
break;
case TokenType::Lor:
type = NodeType::Lor;
break;
default:
return {
.node=node,
.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
};
}
} }
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 { return {
.node=node, .node=node,
.tokens=ret_comp.tokens .tokens=tokens
}; };
} }