From 71caf5006f0b2eb6c3ad77657ea6693dbe63df67 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 23 Apr 2016 22:27:34 -0600 Subject: [PATCH] Pull constant folding optimizer out --- .../chaiscript/language/chaiscript_engine.hpp | 4 +-- .../language/chaiscript_optimizer.hpp | 27 ++++++++++++++-- .../chaiscript/language/chaiscript_parser.hpp | 32 +------------------ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 27161273..1449b2ff 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -219,7 +219,7 @@ namespace chaiscript std::vector t_modulepaths = std::vector(), std::vector t_usepaths = std::vector()) : m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)), - m_parser(std::make_unique>>()) + m_parser(std::make_unique>>()) { if (m_module_paths.empty()) { @@ -244,7 +244,7 @@ namespace chaiscript ChaiScript( std::vector t_modulepaths = std::vector(), std::vector t_usepaths = std::vector()) : m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)), - m_parser(std::make_unique>>()) + m_parser(std::make_unique>>()) { if (m_module_paths.empty()) { diff --git a/include/chaiscript/language/chaiscript_optimizer.hpp b/include/chaiscript/language/chaiscript_optimizer.hpp index 8fc10d83..3e82edd0 100644 --- a/include/chaiscript/language/chaiscript_optimizer.hpp +++ b/include/chaiscript/language/chaiscript_optimizer.hpp @@ -53,7 +53,7 @@ namespace chaiscript { } - struct Return_Optimizer { + struct Return { AST_NodePtr optimize(const AST_NodePtr &p) { if (p->identifier == AST_Node_Type::Def @@ -74,8 +74,31 @@ namespace chaiscript { } }; + struct Constant_Fold { + AST_NodePtr optimize(const AST_NodePtr &node) { + if (node->identifier == AST_Node_Type::Binary + && node->children.size() == 2 + && node->children[0]->identifier == AST_Node_Type::Constant + && node->children[1]->identifier == AST_Node_Type::Constant) + { + const auto oper = node->text; + const auto parsed = Operators::to_operator(oper); + if (parsed != Operators::Opers::invalid) { + const auto lhs = std::dynamic_pointer_cast(node->children[0])->m_value; + const auto rhs = std::dynamic_pointer_cast(node->children[1])->m_value; + if (lhs.get_type_info().is_arithmetic() && rhs.get_type_info().is_arithmetic()) { + const auto val = Boxed_Number::do_oper(parsed, lhs, rhs); + const auto match = node->children[0]->text + " " + oper + " " + node->children[1]->text; + return chaiscript::make_shared(std::move(match), node->location, std::move(val)); + } + } + } - struct For_Loop_Optimizer { + return node; + } + }; + + struct For_Loop { AST_NodePtr optimize(const AST_NodePtr &for_node) { if (for_node->identifier != AST_Node_Type::For) { diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 0b5af4ba..deae9410 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -2129,37 +2129,7 @@ namespace chaiscript case(Operator_Precidence::Bitwise_Xor) : case(Operator_Precidence::Bitwise_Or) : case(Operator_Precidence::Comparison) : - { - bool folded = false; - const auto size = m_match_stack.size(); - - try { - if (m_match_stack[size - 1]->identifier == AST_Node_Type::Constant - && m_match_stack[size - 2]->identifier == AST_Node_Type::Constant) { - const auto parsed = Operators::to_operator(oper); - if (parsed != Operators::Opers::invalid) { - const auto lhs = std::dynamic_pointer_cast(m_match_stack[size-2])->m_value; - const auto rhs = std::dynamic_pointer_cast(m_match_stack[size-1])->m_value; - if (lhs.get_type_info().is_arithmetic() && rhs.get_type_info().is_arithmetic()) { - const auto val = Boxed_Number::do_oper(parsed, lhs, rhs); - const auto start = m_match_stack[size-2]->location; - const auto match = m_match_stack[size-2]->text + " " + oper + " " + m_match_stack[size-1]->text; - m_match_stack.resize(size-2); - m_match_stack.push_back( - make_node(std::move(match), start.start.line, start.start.column, std::move(val))); - folded = true; - } - } - } - } catch (const std::exception &) { - //failure to fold - } - - if (!folded) { - build_match(prev_stack_top, oper); - } - } - + build_match(prev_stack_top, oper); break; case(Operator_Precidence::Logical_And) :