From 866db4ee8b95463055153253adccb0a2d65c27a0 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 10 Apr 2016 21:38:44 -0600 Subject: [PATCH] Reduce instances of Str_AST_Node --- .../chaiscript/language/chaiscript_eval.hpp | 16 +++---- .../chaiscript/language/chaiscript_parser.hpp | 46 +++++++++---------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 63e4e85..7debbec 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -327,13 +327,13 @@ namespace chaiscript struct Equation_AST_Node final : AST_Node { Equation_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::Equation, std::move(t_loc), std::move(t_children)), - m_oper(Operators::to_operator(children[1]->text)) - { assert(children.size() == 3); } + m_oper(Operators::to_operator(text)) + { assert(children.size() == 2); } Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); - Boxed_Value rhs = this->children[2]->eval(t_ss); + Boxed_Value rhs = this->children[1]->eval(t_ss); Boxed_Value lhs = this->children[0]->eval(t_ss); if (m_oper != Operators::Opers::invalid && lhs.get_type_info().is_arithmetic() && @@ -371,17 +371,17 @@ namespace chaiscript } try { - return t_ss->call_function(this->children[1]->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions()); + return t_ss->call_function(this->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions()); } catch(const exception::dispatch_error &e){ - throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, *t_ss); + throw exception::eval_error("Unable to find appropriate'" + this->text + "' operator.", e.parameters, e.functions, false, *t_ss); } } catch(const exception::dispatch_error &e){ throw exception::eval_error("Missing clone or copy constructor for right hand side of equation", e.parameters, e.functions, false, *t_ss); } } - else if (this->children[1]->text == ":=") { + else if (this->text == ":=") { if (lhs.is_undef() || Boxed_Value::type_match(lhs, rhs)) { lhs.assign(rhs); lhs.reset_return_value(); @@ -391,9 +391,9 @@ namespace chaiscript } else { try { - return t_ss->call_function(this->children[1]->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions()); + return t_ss->call_function(this->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions()); } catch(const exception::dispatch_error &e){ - throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, *t_ss); + throw exception::eval_error("Unable to find appropriate'" + this->text + "' operator.", e.parameters, e.functions, false, *t_ss); } } diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 1141351..9b4370f 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1272,7 +1272,7 @@ namespace chaiscript } /// Reads (and potentially captures) a string from input if it matches the parameter - bool Keyword(const char *t_s, bool t_capture = false) { + bool Keyword(const char *t_s) { SkipWS(); const auto start = m_position; bool retval = Keyword_(t_s); @@ -1282,9 +1282,6 @@ namespace chaiscript retval = false; } - if ( t_capture && retval ) { - m_match_stack.push_back(make_node(Position::str(start, m_position), start.line, start.col)); - } return retval; } @@ -1553,7 +1550,7 @@ namespace chaiscript bool is_method = false; - if (Symbol("::", false)) { + if (Symbol("::")) { //We're now a method is_method = true; @@ -1682,12 +1679,11 @@ namespace chaiscript while (has_matches) { while (Eol()) {} has_matches = false; - if (Keyword("else", true)) { + const auto line = m_position.line; + const auto col = m_position.col; + if (Keyword("else")) { if (Keyword("if")) { - const AST_NodePtr back(m_match_stack.back()); - m_match_stack.back() = - chaiscript::make_shared("else if", back->location, back->children); - m_match_stack.back()->annotation = back->annotation; + 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); } @@ -1703,6 +1699,7 @@ namespace chaiscript } has_matches = true; } else { + m_match_stack.emplace_back(make_node("else", line, col, std::vector())); while (Eol()) {} if (!Block()) { @@ -2105,7 +2102,7 @@ namespace chaiscript if (!Id()) { throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename); } - if (!Symbol("::", false)) { + if (!Symbol("::")) { throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename); } if (!Id()) { @@ -2169,7 +2166,7 @@ namespace chaiscript bool Reference() { const auto prev_stack_top = m_match_stack.size(); - if (Symbol("&", false)) { + if (Symbol("&")) { if (!Id()) { throw exception::eval_error("Incomplete '&' expression", File_Position(m_position.line, m_position.col), *m_filename); } @@ -2345,26 +2342,25 @@ namespace chaiscript /// Parses a string of binary equation operators bool Equation() { - bool retval = false; - const auto prev_stack_top = m_match_stack.size(); if (Operator()) { - retval = true; - if (Symbol("=", true, true) || Symbol(":=", true, true) || Symbol("+=", true, true) || - Symbol("-=", true, true) || Symbol("*=", true, true) || Symbol("/=", true, true) || - Symbol("%=", true, true) || Symbol("<<=", true, true) || Symbol(">>=", true, true) || - Symbol("&=", true, true) || Symbol("^=", true, true) || Symbol("|=", true, true)) { - SkipWS(true); - if (!Equation()) { - throw exception::eval_error("Incomplete equation", File_Position(m_position.line, m_position.col), *m_filename); - } + for (const auto sym : {"=", ":=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", "&=", "^=", "|="}) + { + if (Symbol(sym, false, true)) { + SkipWS(true); + if (!Equation()) { + throw exception::eval_error("Incomplete equation", File_Position(m_position.line, m_position.col), *m_filename); + } - build_match(prev_stack_top); + build_match(prev_stack_top, sym); + return true; + } } + return true; } - return retval; + return false; } /// Parses statements allowed inside of a class block