From f6c69f28265e8b5ae195102418893a1b5c4621b7 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 25 Apr 2016 21:32:48 -0600 Subject: [PATCH] Allow folding of if blocks --- .../chaiscript/language/chaiscript_engine.hpp | 4 ++-- .../chaiscript/language/chaiscript_eval.hpp | 2 +- .../language/chaiscript_optimizer.hpp | 20 +++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index fdc5f609..2f26dca8 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_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index e7263ba0..d02e328b 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -653,7 +653,7 @@ namespace chaiscript struct Ternary_Cond_AST_Node final : AST_Node { Ternary_Cond_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::Ternary_Cond, std::move(t_loc), std::move(t_children)) { assert(children.size() == 3); } Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { diff --git a/include/chaiscript/language/chaiscript_optimizer.hpp b/include/chaiscript/language/chaiscript_optimizer.hpp index 2e7064d2..370c3c49 100644 --- a/include/chaiscript/language/chaiscript_optimizer.hpp +++ b/include/chaiscript/language/chaiscript_optimizer.hpp @@ -107,6 +107,26 @@ namespace chaiscript { } }; + struct If { + AST_NodePtr optimize(const AST_NodePtr &node) { + if ((node->identifier == AST_Node_Type::If || node->identifier == AST_Node_Type::Ternary_Cond) + && node->children.size() >= 2 + && node->children[0]->identifier == AST_Node_Type::Constant) + { + const auto condition = std::dynamic_pointer_cast(node->children[0])->m_value; + if (condition.get_type_info().bare_equal_type_info(typeid(bool))) { + if (boxed_cast(condition)) { + return node->children[1]; + } else if (node->children.size() == 3) { + return node->children[2]; + } + } + } + + return node; + } + }; + struct Constant_Fold { AST_NodePtr optimize(const AST_NodePtr &node) {