mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
Clean up if block parsing and eval
This commit is contained in:
parent
83c6df11f0
commit
7d5a97aa2f
@ -653,33 +653,22 @@ namespace chaiscript
|
||||
|
||||
struct If_AST_Node final : AST_Node {
|
||||
If_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> t_children) :
|
||||
AST_Node(std::move(t_ast_node_text), AST_Node_Type::If, std::move(t_loc), std::move(t_children)) { }
|
||||
AST_Node(std::move(t_ast_node_text), AST_Node_Type::If, std::move(t_loc), std::move(t_children))
|
||||
{
|
||||
assert(children.size() == 2 || children.size() == 3);
|
||||
}
|
||||
|
||||
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
|
||||
|
||||
if (get_bool_condition(children[0]->eval(t_ss))) {
|
||||
return children[1]->eval(t_ss);
|
||||
} else {
|
||||
if (children.size() > 2) {
|
||||
size_t i = 2;
|
||||
/// \todo these string comparisons are clunky
|
||||
while (i < children.size()) {
|
||||
if (children[i]->text == "else") {
|
||||
return children[i+1]->eval(t_ss);
|
||||
}
|
||||
else if (children[i]->text == "else if") {
|
||||
if (get_bool_condition(children[i+1]->eval(t_ss))) {
|
||||
return children[i+2]->eval(t_ss);
|
||||
}
|
||||
}
|
||||
i += 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (children.size() == 3) {
|
||||
return children[2]->eval(t_ss);
|
||||
} else {
|
||||
return void_var();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct For_AST_Node final : AST_Node {
|
||||
|
||||
@ -1579,27 +1579,10 @@ namespace chaiscript
|
||||
while (has_matches) {
|
||||
while (Eol()) {}
|
||||
has_matches = false;
|
||||
const auto line = m_position.line;
|
||||
const auto col = m_position.col;
|
||||
if (Keyword("else")) {
|
||||
if (Keyword("if")) {
|
||||
m_match_stack.emplace_back(make_node<eval::If_AST_Node>("else if", line, col, std::vector<AST_NodePtr>()));
|
||||
if (!Char('(')) {
|
||||
throw exception::eval_error("Incomplete 'else if' expression", File_Position(m_position.line, m_position.col), *m_filename);
|
||||
}
|
||||
|
||||
if (!(Operator() && Char(')'))) {
|
||||
throw exception::eval_error("Incomplete 'else if' expression", File_Position(m_position.line, m_position.col), *m_filename);
|
||||
}
|
||||
|
||||
while (Eol()) {}
|
||||
|
||||
if (!Block()) {
|
||||
throw exception::eval_error("Incomplete 'else if' block", File_Position(m_position.line, m_position.col), *m_filename);
|
||||
}
|
||||
if (If()) {
|
||||
has_matches = true;
|
||||
} else {
|
||||
m_match_stack.emplace_back(make_node<eval::If_AST_Node>("else", line, col, std::vector<AST_NodePtr>()));
|
||||
while (Eol()) {}
|
||||
|
||||
if (!Block()) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user