diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index eafa01c9..2977043c 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -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 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(); } } - - return void_var(); } - }; struct For_AST_Node final : AST_Node { diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 6422e3b5..2bdddc12 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -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("else if", line, col, std::vector())); - 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("else", line, col, std::vector())); while (Eol()) {} if (!Block()) {