From 7b3f06b269809f9be10fa41c1ce5cf761dd1b8fb Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 3 May 2016 14:41:16 -0600 Subject: [PATCH] Even more scope / block simplification --- .../chaiscript/language/chaiscript_common.hpp | 4 +-- .../chaiscript/language/chaiscript_eval.hpp | 14 ++++++++++ .../language/chaiscript_optimizer.hpp | 27 ++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index 2f06f21..17a7492 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -34,7 +34,7 @@ namespace chaiscript /// Types of AST nodes available to the parser and eval enum class AST_Node_Type { Id, Fun_Call, Arg_List, Equation, Var_Decl, Array_Call, Dot_Access, - Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range, + Lambda, Block, Scopeless_Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range, Inline_Range, Try, Catch, Finally, Method, Attr_Decl, Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class, Binary, Arg, Global_Decl, Constant, Compiled }; @@ -47,7 +47,7 @@ namespace chaiscript const char *ast_node_type_to_string(AST_Node_Type ast_node_type) { static const char * const ast_node_types[] = { "Id", "Fun_Call", "Arg_List", "Equation", "Var_Decl", "Array_Call", "Dot_Access", - "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range", + "Lambda", "Block", "Scopeless_Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range", "Inline_Range", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary", "Arg", "Global_Decl", "Constant", "Compiled"}; diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index e4d6031..c3c473b 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -635,6 +635,20 @@ namespace chaiscript }; + template + struct Scopeless_Block_AST_Node final : AST_Node_Impl { + Scopeless_Block_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector> t_children) : + AST_Node_Impl(std::move(t_ast_node_text), AST_Node_Type::Scopeless_Block, std::move(t_loc), std::move(t_children)) { } + + Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { + const auto num_children = this->children.size(); + for (size_t i = 0; i < num_children-1; ++i) { + this->children[i]->eval(t_ss); + } + return this->children.back()->eval(t_ss); + } + }; + template struct Block_AST_Node final : AST_Node_Impl { Block_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector> t_children) : diff --git a/include/chaiscript/language/chaiscript_optimizer.hpp b/include/chaiscript/language/chaiscript_optimizer.hpp index 2ff4ad0..92f4ad1 100644 --- a/include/chaiscript/language/chaiscript_optimizer.hpp +++ b/include/chaiscript/language/chaiscript_optimizer.hpp @@ -31,11 +31,20 @@ namespace chaiscript { template auto child_at(const eval::AST_Node_Impl_Ptr &node, const size_t offset) { + if (node->children[offset]->identifier == AST_Node_Type::Compiled) { + return dynamic_cast&>(*node->children[offset]).m_original_node; + } else { + return node->children[offset]; + } + + + /* if (node->identifier == AST_Node_Type::Compiled) { return dynamic_cast&>(*node).m_original_node->children[offset]; } else { return node->children[offset]; } + */ } template @@ -88,6 +97,7 @@ namespace chaiscript { for (size_t i = 0; i < num; ++i) { const auto &child = child_at(node, i); if (child->identifier != AST_Node_Type::Block + && child->identifier != AST_Node_Type::For && contains_var_decl_in_scope(child)) { return true; } @@ -99,14 +109,19 @@ namespace chaiscript { struct Block { template auto optimize(const eval::AST_Node_Impl_Ptr &node) { - if (node->identifier == AST_Node_Type::Block - && node->children.size() == 1 - && !contains_var_decl_in_scope(node)) + if (node->identifier == AST_Node_Type::Block) { - return node->children[0]; - } else { - return node; + if (!contains_var_decl_in_scope(node)) + { + if (node->children.size() == 1) { + return node->children[0]; + } else { + return chaiscript::make_shared, eval::Scopeless_Block_AST_Node>(node->text, node->location, node->children); + } + } } + + return node; } };