diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index fdc5f60..2f26dca 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 e7263ba..d02e328 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 2e7064d..370c3c4 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) {