From 40694c798c4f4a488aa14568b5da0ed9173ea3f4 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 10 Apr 2016 23:12:35 -0600 Subject: [PATCH] Eliminate Str_AST_Node --- .../chaiscript/language/chaiscript_common.hpp | 4 +-- .../chaiscript/language/chaiscript_eval.hpp | 13 +++------ .../chaiscript/language/chaiscript_parser.hpp | 29 +++++++------------ 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index 7d0a6df..cb1266d 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -32,7 +32,7 @@ namespace chaiscript /// Types of AST nodes available to the parser and eval - enum class AST_Node_Type { Error, Id, Str, Eol, Fun_Call, Arg_List, Variable, Equation, Var_Decl, + enum class AST_Node_Type { Error, Id, Eol, Fun_Call, Arg_List, Variable, Equation, Var_Decl, Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, 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, @@ -44,7 +44,7 @@ namespace chaiscript /// Helper lookup to get the name of each node type const char *ast_node_type_to_string(AST_Node_Type ast_node_type) { - static const char * const ast_node_types[] = { "Internal Parser Error", "Id", "Str", "Eol", "Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", + static const char * const ast_node_types[] = { "Internal Parser Error", "Id", "Eol", "Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", "Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "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", diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index e1a2e19..ba36c7a 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -166,11 +166,6 @@ namespace chaiscript }; - struct Str_AST_Node final : AST_Node { - Str_AST_Node(std::string t_ast_node_text, Parse_Location t_loc) : - AST_Node(std::move(t_ast_node_text), AST_Node_Type::Str, std::move(t_loc)) { } - }; - struct Fun_Call_AST_Node final : AST_Node { Fun_Call_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector t_children) : @@ -1323,12 +1318,12 @@ namespace chaiscript struct Logical_And_AST_Node final : AST_Node { Logical_And_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::Logical_And, std::move(t_loc), std::move(t_children)) - { assert(children.size() == 3); } + { assert(children.size() == 2); } Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { return const_var(get_bool_condition(children[0]->eval(t_ss)) - && get_bool_condition(children[2]->eval(t_ss))); + && get_bool_condition(children[1]->eval(t_ss))); } std::string pretty_print() const override @@ -1340,12 +1335,12 @@ namespace chaiscript struct Logical_Or_AST_Node final : AST_Node { Logical_Or_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::Logical_Or, std::move(t_loc), std::move(t_children)) - { assert(children.size() == 3); } + { assert(children.size() == 2); } Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { return const_var(get_bool_condition(children[0]->eval(t_ss)) - || get_bool_condition(children[2]->eval(t_ss))); + || get_bool_condition(children[1]->eval(t_ss))); } std::string pretty_print() const override diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 879ab60..4980014 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1315,7 +1315,7 @@ namespace chaiscript } /// Reads (and potentially captures) a symbol group from input if it matches the parameter - bool Symbol(const char *t_s, const bool t_capture = false, const bool t_disallow_prevention=false) { + bool Symbol(const char *t_s, const bool t_disallow_prevention=false) { SkipWS(); const auto start = m_position; bool retval = Symbol_(t_s); @@ -1330,10 +1330,6 @@ namespace chaiscript } } - if ( t_capture && retval ) { - m_match_stack.push_back(make_node(Position::str(start, m_position), start.line, start.col)); - } - return retval; } @@ -2205,9 +2201,10 @@ namespace chaiscript return Var_Decl() || Dot_Fun_Array() || Prefix(); } - bool Operator_Helper(const size_t t_precedence) { + bool Operator_Helper(const size_t t_precedence, std::string &oper) { for (auto & elem : m_operator_matches[t_precedence]) { - if (Symbol(elem.c_str(), true)) { + if (Symbol(elem.c_str())) { + oper = elem; return true; } } @@ -2221,7 +2218,8 @@ namespace chaiscript if (t_precedence < m_operators.size()) { if (Operator(t_precedence+1)) { retval = true; - while (Operator_Helper(t_precedence)) { + std::string oper; + while (Operator_Helper(t_precedence, oper)) { while (Eol()) {} if (!Operator(t_precedence+1)) { throw exception::eval_error("Incomplete " @@ -2229,12 +2227,8 @@ namespace chaiscript File_Position(m_position.line, m_position.col), *m_filename); } - AST_NodePtr oper = m_match_stack.at(m_match_stack.size()-2); - switch (m_operators[t_precedence]) { case(AST_Node_Type::Ternary_Cond) : - m_match_stack.erase(advance_copy(m_match_stack.begin(), m_match_stack.size() - 2), - advance_copy(m_match_stack.begin(), m_match_stack.size() - 1)); if (Symbol(":")) { if (!Operator(t_precedence+1)) { throw exception::eval_error("Incomplete " @@ -2258,17 +2252,14 @@ namespace chaiscript case(AST_Node_Type::Bitwise_Xor) : case(AST_Node_Type::Bitwise_Or) : case(AST_Node_Type::Comparison) : - assert(m_match_stack.size() > 1); - m_match_stack.erase(advance_copy(m_match_stack.begin(), m_match_stack.size() - 2), - advance_copy(m_match_stack.begin(), m_match_stack.size() - 1)); - build_match(prev_stack_top, oper->text); + build_match(prev_stack_top, oper); break; case(AST_Node_Type::Logical_And) : - build_match(prev_stack_top); + build_match(prev_stack_top, oper); break; case(AST_Node_Type::Logical_Or) : - build_match(prev_stack_top); + build_match(prev_stack_top, oper); break; default: @@ -2345,7 +2336,7 @@ namespace chaiscript if (Operator()) { for (const auto sym : {"=", ":=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", "&=", "^=", "|="}) { - if (Symbol(sym, false, true)) { + if (Symbol(sym, true)) { SkipWS(true); if (!Equation()) { throw exception::eval_error("Incomplete equation", File_Position(m_position.line, m_position.col), *m_filename);