From 161652b5d9fdd24c2e071a9cc788e03c29df15ca Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 16 Nov 2014 20:14:47 -0700 Subject: [PATCH 1/4] Reworking of binary operators - Eliminates re-parsing of operator strings - Reduces much redundant code Results 1. smaller binaries 2. less runtime memory usage 4. faster runtime --- .../chaiscript/language/chaiscript_common.hpp | 4 +- .../chaiscript/language/chaiscript_eval.hpp | 135 +++--------------- .../chaiscript/language/chaiscript_parser.hpp | 66 +++------ 3 files changed, 38 insertions(+), 167 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index d15c2d9..a4d41ff 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -37,7 +37,7 @@ namespace chaiscript Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String, Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range, Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or, - Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class + Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class, Binary }; }; @@ -50,7 +50,7 @@ namespace chaiscript "Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String", "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range", "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or", - "Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop"}; + "Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary"}; return ast_node_types[ast_node_type]; } diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index f87b87f..be9aabc 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -63,25 +63,21 @@ namespace chaiscript struct Binary_Operator_AST_Node : public AST_Node { public: - Binary_Operator_AST_Node(std::string t_ast_node_text, int t_id, const std::shared_ptr &t_fname, int t_start_line, int t_start_col, int t_end_line, int t_end_col) : - AST_Node(std::move(t_ast_node_text), t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) + Binary_Operator_AST_Node(const std::string &t_oper) : + AST_Node(t_oper, AST_Node_Type::Binary, std::make_shared(""), 0, 0, 0, 0), + m_oper(Operators::to_operator(t_oper)) { } virtual ~Binary_Operator_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { - return do_oper(t_ss, Operators::to_operator(children[1]->text), children[1]->text, + return do_oper(t_ss, m_oper, text, this->children[0]->eval(t_ss), - this->children[2]->eval(t_ss)); + this->children[1]->eval(t_ss)); } virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE { - if (children.size() == 3) - { - return "(" + this->children[0]->pretty_print() + " " + this->children[1]->text + " " + this->children[2]->pretty_print() + ")"; - } else { - return "(" + this->children[0]->pretty_print() + " " + text + " " + this->children[1]->pretty_print() + ")"; - } + return "(" + this->children[0]->pretty_print() + " " + text + " " + this->children[1]->pretty_print() + ")"; } protected: @@ -110,6 +106,9 @@ namespace chaiscript throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, e.functions, false, t_ss); } } + + private: + Operators::Opers m_oper; }; struct Int_AST_Node : public AST_Node { @@ -316,8 +315,6 @@ namespace chaiscript } catch(const exception::bad_boxed_cast &){ // handle the case where there is only 1 function to try to call and dispatch fails on it - std::vector funcs; - funcs.push_back(fn); throw exception::eval_error("Error calling function '" + this->children[0]->text + "'", params, {fn}, false, t_ss); } catch(const exception::arity_error &e){ @@ -455,7 +452,7 @@ namespace chaiscript { return this->children[0]->eval(t_ss); } else { - std::string idname = this->children[0]->text; + const std::string &idname = this->children[0]->text; Boxed_Value bv; try { @@ -478,72 +475,6 @@ namespace chaiscript }; - struct Comparison_AST_Node : public Binary_Operator_AST_Node { - public: - Comparison_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Comparison, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Comparison_AST_Node() {} - }; - - struct Addition_AST_Node : public Binary_Operator_AST_Node { - public: - Addition_AST_Node(std::string t_ast_node_text = "+", - const std::shared_ptr &t_fname=std::shared_ptr(), - int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Addition, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Addition_AST_Node() {} - virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { - return do_oper(t_ss, Operators::sum, "+", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); - } - }; - - struct Subtraction_AST_Node : public Binary_Operator_AST_Node { - public: - Subtraction_AST_Node(std::string t_ast_node_text = "-", - const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, - int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Subtraction, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Subtraction_AST_Node() {} - virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { - return do_oper(t_ss, Operators::difference, "-", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); - } - }; - - struct Multiplication_AST_Node : public Binary_Operator_AST_Node { - public: - Multiplication_AST_Node(std::string t_ast_node_text = "*", - const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, - int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Multiplication, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Multiplication_AST_Node() {} - virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { - return do_oper(t_ss, Operators::product, "*", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); - } - }; - - struct Division_AST_Node : public Binary_Operator_AST_Node { - public: - Division_AST_Node(std::string t_ast_node_text = "/", - const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, - int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Division, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Division_AST_Node() {} - virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { - return do_oper(t_ss, Operators::quotient, "/", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); - } - }; - - struct Modulus_AST_Node : public Binary_Operator_AST_Node { - public: - Modulus_AST_Node(std::string t_ast_node_text = "%", - const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, - int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Modulus, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Modulus_AST_Node() {} - virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE { - return do_oper(t_ss, Operators::remainder, "%", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); - } - }; struct Array_Call_AST_Node : public AST_Node { public: @@ -1145,8 +1076,9 @@ namespace chaiscript struct Prefix_AST_Node : public AST_Node { public: - Prefix_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(std::move(t_ast_node_text), AST_Node_Type::Prefix, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) + Prefix_AST_Node(Operators::Opers t_oper) : + AST_Node("", AST_Node_Type::Prefix, std::make_shared(""), 0, 0, 0, 0), + m_oper(t_oper) { } virtual ~Prefix_AST_Node() {} @@ -1154,12 +1086,11 @@ namespace chaiscript chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); Boxed_Value bv(this->children[1]->eval(t_ss)); - Operators::Opers oper = Operators::to_operator(children[0]->text, true); try { // short circuit arithmetic operations - if (bv.get_type_info().is_arithmetic() && oper != Operators::invalid) + if (m_oper != Operators::invalid && bv.get_type_info().is_arithmetic()) { - return Boxed_Number::do_oper(oper, std::move(bv)); + return Boxed_Number::do_oper(m_oper, std::move(bv)); } else { chaiscript::eval::detail::Stack_Push_Pop spp(t_ss); fpp.save_params({bv}); @@ -1170,6 +1101,8 @@ namespace chaiscript } } + private: + Operators::Opers m_oper; }; struct Break_AST_Node : public AST_Node { @@ -1513,40 +1446,6 @@ namespace chaiscript }; - struct Shift_AST_Node : public Binary_Operator_AST_Node { - public: - Shift_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Shift, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Shift_AST_Node() {} - }; - - struct Equality_AST_Node : public Binary_Operator_AST_Node { - public: - Equality_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Equality, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Equality_AST_Node() {} - }; - - struct Bitwise_And_AST_Node : public Binary_Operator_AST_Node { - public: - Bitwise_And_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_And, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Bitwise_And_AST_Node() {} - }; - - struct Bitwise_Xor_AST_Node : public Binary_Operator_AST_Node { - public: - Bitwise_Xor_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_Xor, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Bitwise_Xor_AST_Node() {} - }; - - struct Bitwise_Or_AST_Node : public Binary_Operator_AST_Node { - public: - Bitwise_Or_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr &t_fname=std::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_Or, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Bitwise_Or_AST_Node() {} - }; struct Logical_And_AST_Node : public AST_Node { public: diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 482d653..2b4728e 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -787,7 +787,7 @@ namespace chaiscript //If we've seen previous interpolation, add on instead of making a new one m_match_stack.push_back(std::make_shared(match, m_filename, prev_line, prev_col, m_line, m_col)); - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared("+"), prev_stack_top); } else { m_match_stack.push_back(std::make_shared(match, m_filename, prev_line, prev_col, m_line, m_col)); } @@ -824,7 +824,7 @@ namespace chaiscript build_match(std::make_shared(), ev_stack_top); build_match(std::make_shared(), ev_stack_top); build_match(std::make_shared(), tostr_stack_top); - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared("+"), prev_stack_top); } else { throw exception::eval_error("Unclosed in-string eval", File_Position(prev_line, prev_col), *m_filename); } @@ -867,7 +867,7 @@ namespace chaiscript if (is_interpolated) { m_match_stack.push_back(std::make_shared(match, m_filename, prev_line, prev_col, m_line, m_col)); - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared("+"), prev_stack_top); } else { m_match_stack.push_back(std::make_shared(match, m_filename, prev_line, prev_col, m_line, m_col)); } @@ -1846,7 +1846,7 @@ namespace chaiscript throw exception::eval_error("Incomplete '++' expression", File_Position(m_line, m_col), *m_filename); } - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared(Operators::to_operator("++")), prev_stack_top); } else if (Symbol("--", true)) { retval = true; @@ -1854,7 +1854,7 @@ namespace chaiscript throw exception::eval_error("Incomplete '--' expression", File_Position(m_line, m_col), *m_filename); } - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared(Operators::to_operator("--")), prev_stack_top); } else if (Char('-', true)) { retval = true; @@ -1862,7 +1862,7 @@ namespace chaiscript throw exception::eval_error("Incomplete unary '-' expression", File_Position(m_line, m_col), *m_filename); } - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared(Operators::to_operator("-", true)), prev_stack_top); } else if (Char('+', true)) { retval = true; @@ -1870,7 +1870,7 @@ namespace chaiscript throw exception::eval_error("Incomplete unary '+' expression", File_Position(m_line, m_col), *m_filename); } - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared(Operators::to_operator("+", true)), prev_stack_top); } else if (Char('!', true)) { retval = true; @@ -1879,7 +1879,7 @@ namespace chaiscript throw exception::eval_error("Incomplete '!' expression", File_Position(m_line, m_col), *m_filename); } - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared(Operators::to_operator("!")), prev_stack_top); } else if (Char('~', true)) { retval = true; @@ -1888,16 +1888,16 @@ namespace chaiscript throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename); } - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared(Operators::to_operator("~")), prev_stack_top); } else if (Char('&', true)) { retval = true; if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename); + throw exception::eval_error("Incomplete '&' expression", File_Position(m_line, m_col), *m_filename); } - build_match(std::make_shared(), prev_stack_top); + build_match(std::make_shared(Operators::to_operator("&")), prev_stack_top); } return retval; @@ -1923,7 +1923,6 @@ namespace chaiscript bool Operator(const size_t t_precedence = 0) { bool retval = false; - AST_NodePtr oper; const auto prev_stack_top = m_match_stack.size(); if (t_precedence < m_operators.size()) { @@ -1938,10 +1937,9 @@ namespace chaiscript File_Position(m_line, m_col), *m_filename); } + AST_NodePtr oper = m_match_stack.at(m_match_stack.size()-2); + switch (m_operators[t_precedence]) { - case(AST_Node_Type::Comparison) : - build_match(std::make_shared(), prev_stack_top); - break; case(AST_Node_Type::Ternary_Cond) : m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2, m_match_stack.begin() + m_match_stack.size() - 1); @@ -1959,52 +1957,26 @@ namespace chaiscript File_Position(m_line, m_col), *m_filename); } break; + case(AST_Node_Type::Addition) : - oper = m_match_stack.at(m_match_stack.size()-2); - m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2, - m_match_stack.begin() + m_match_stack.size() - 1); - if (oper->text == "+") { - build_match(std::make_shared(), prev_stack_top); - } - else if (oper->text == "-") { - build_match(std::make_shared(), prev_stack_top); - } - break; case(AST_Node_Type::Multiplication) : - oper = m_match_stack.at(m_match_stack.size()-2); - m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2, - m_match_stack.begin() + m_match_stack.size() - 1); - if (oper->text == "*") { - build_match(std::make_shared(), prev_stack_top); - } - else if (oper->text == "/") { - build_match(std::make_shared(), prev_stack_top); - } - else if (oper->text == "%") { - build_match(std::make_shared(), prev_stack_top); - } - break; case(AST_Node_Type::Shift) : - build_match(std::make_shared(), prev_stack_top); - break; case(AST_Node_Type::Equality) : - build_match(std::make_shared(), prev_stack_top); - break; case(AST_Node_Type::Bitwise_And) : - build_match(std::make_shared(), prev_stack_top); - break; case(AST_Node_Type::Bitwise_Xor) : - build_match(std::make_shared(), prev_stack_top); - break; case(AST_Node_Type::Bitwise_Or) : - build_match(std::make_shared(), prev_stack_top); + case(AST_Node_Type::Comparison) : + m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2, m_match_stack.begin() + m_match_stack.size() - 1); + build_match(std::make_shared(oper->text), prev_stack_top); break; + case(AST_Node_Type::Logical_And) : build_match(std::make_shared(), prev_stack_top); break; case(AST_Node_Type::Logical_Or) : build_match(std::make_shared(), prev_stack_top); break; + default: throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_line, m_col), *m_filename); } From c90fe16858e724960255f7aa0a9589208d5649cc Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 16 Nov 2014 21:02:28 -0700 Subject: [PATCH 2/4] Clean up Prefix() implementation Reducing redundant code --- .../chaiscript/language/chaiscript_parser.hpp | 72 ++++--------------- 1 file changed, 12 insertions(+), 60 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 2b4728e..fbbf880 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1835,72 +1835,24 @@ namespace chaiscript /// Reads a unary prefixed expression from input bool Prefix() { - bool retval = false; - const auto prev_stack_top = m_match_stack.size(); +// const std::array prefix_opers; - if (Symbol("++", true)) { - retval = true; + for (const auto &oper : std::initializer_list{"++", "--", "-", "+", "!", "~", "&"}) + { + bool is_char = oper.size() == 1; + if ((is_char && Char(oper[0], true)) || (!is_char && Symbol(oper.c_str(), true))) + { + if (!Operator(m_operators.size()-1)) { + throw exception::eval_error("Incomplete prefix '" + oper + "' expression", File_Position(m_line, m_col), *m_filename); + } - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '++' expression", File_Position(m_line, m_col), *m_filename); + build_match(std::make_shared(Operators::to_operator(oper, true)), prev_stack_top); + return true; } - - build_match(std::make_shared(Operators::to_operator("++")), prev_stack_top); - } else if (Symbol("--", true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '--' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("--")), prev_stack_top); - } else if (Char('-', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete unary '-' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("-", true)), prev_stack_top); - } else if (Char('+', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete unary '+' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("+", true)), prev_stack_top); - } - else if (Char('!', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '!' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("!")), prev_stack_top); - } - else if (Char('~', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("~")), prev_stack_top); - } - else if (Char('&', true)) { - retval = true; - - if (!Operator(m_operators.size()-1)) { - throw exception::eval_error("Incomplete '&' expression", File_Position(m_line, m_col), *m_filename); - } - - build_match(std::make_shared(Operators::to_operator("&")), prev_stack_top); } - return retval; + return false; } /// Parses any of a group of 'value' style ast_node groups from input From 1e8c0ab93e06e30970a85548cc945d70d7060575 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 16 Nov 2014 21:22:55 -0700 Subject: [PATCH 3/4] Reduce redundant parsing / error code --- .../chaiscript/language/chaiscript_parser.hpp | 92 +------------------ 1 file changed, 4 insertions(+), 88 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index fbbf880..d5bfb5f 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -2035,14 +2035,7 @@ namespace chaiscript while (has_more) { const auto prev_line = m_line; const auto prev_col = m_col; - if (Def(true)) { - if (!saw_eol) { - throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = true; - } else if (Var_Decl(true)) { + if (Def(true) || Var_Decl(true)) { if (!saw_eol) { throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); } @@ -2071,7 +2064,7 @@ namespace chaiscript while (has_more) { int prev_line = m_line; int prev_col = m_col; - if (Def()) { + if (Def() || Try() || If() || While() || Class() || For() || Switch()) { if (!saw_eol) { throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); } @@ -2079,55 +2072,7 @@ namespace chaiscript retval = true; saw_eol = true; } - else if (Try()) { - if (!saw_eol) { - throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = true; - } - else if (If()) { - if (!saw_eol) { - throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = true; - } - else if (While()) { - if (!saw_eol) { - throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = true; - } - else if (Class()) { - if (!saw_eol) { - throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = true; - } - else if (For()) { - if (!saw_eol) { - throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = true; - } - else if (Switch()) { - if (!saw_eol) { - throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = true; - } - else if (Return()) { + else if (Return() || Break() || Continue() || Equation()) { if (!saw_eol) { throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename); } @@ -2135,36 +2080,7 @@ namespace chaiscript retval = true; saw_eol = false; } - else if (Break()) { - if (!saw_eol) { - throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = false; - } - else if (Continue()) { - if (!saw_eol) { - throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = false; - } - else if (Block()) { - has_more = true; - retval = true; - saw_eol = true; - } - else if (Equation()) { - if (!saw_eol) { - throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename); - } - has_more = true; - retval = true; - saw_eol = false; - } - else if (Eol()) { + else if (Block() || Eol()) { has_more = true; retval = true; saw_eol = true; From 423e872720ca93f47866dbc912223bde621ceb3e Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 17 Nov 2014 07:02:52 -0700 Subject: [PATCH 4/4] Work around bug in gcc 4.6 for initializer_list Hopefully also fixes MSVC 2012 --- include/chaiscript/language/chaiscript_parser.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index d5bfb5f..08e4034 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1836,9 +1836,9 @@ namespace chaiscript /// Reads a unary prefixed expression from input bool Prefix() { const auto prev_stack_top = m_match_stack.size(); -// const std::array prefix_opers; + const std::vector prefix_opers{"++", "--", "-", "+", "!", "~", "&"}; - for (const auto &oper : std::initializer_list{"++", "--", "-", "+", "!", "~", "&"}) + for (const auto &oper : prefix_opers) { bool is_char = oper.size() == 1; if ((is_char && Char(oper[0], true)) || (!is_char && Symbol(oper.c_str(), true)))