Even more scope / block simplification
This commit is contained in:
parent
5373bbd52e
commit
7b3f06b269
@ -34,7 +34,7 @@ namespace chaiscript
|
|||||||
/// Types of AST nodes available to the parser and eval
|
/// Types of AST nodes available to the parser and eval
|
||||||
enum class AST_Node_Type { Id, Fun_Call, Arg_List, Equation, Var_Decl,
|
enum class AST_Node_Type { Id, Fun_Call, Arg_List, Equation, Var_Decl,
|
||||||
Array_Call, Dot_Access,
|
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,
|
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
|
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) {
|
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",
|
static const char * const ast_node_types[] = { "Id", "Fun_Call", "Arg_List", "Equation", "Var_Decl",
|
||||||
"Array_Call", "Dot_Access",
|
"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",
|
"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"};
|
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary", "Arg", "Global_Decl", "Constant", "Compiled"};
|
||||||
|
|
||||||
|
@ -635,6 +635,20 @@ namespace chaiscript
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Scopeless_Block_AST_Node final : AST_Node_Impl<T> {
|
||||||
|
Scopeless_Block_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children) :
|
||||||
|
AST_Node_Impl<T>(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<typename T>
|
template<typename T>
|
||||||
struct Block_AST_Node final : AST_Node_Impl<T> {
|
struct Block_AST_Node final : AST_Node_Impl<T> {
|
||||||
Block_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children) :
|
Block_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children) :
|
||||||
|
@ -31,11 +31,20 @@ namespace chaiscript {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto child_at(const eval::AST_Node_Impl_Ptr<T> &node, const size_t offset) {
|
auto child_at(const eval::AST_Node_Impl_Ptr<T> &node, const size_t offset) {
|
||||||
|
if (node->children[offset]->identifier == AST_Node_Type::Compiled) {
|
||||||
|
return dynamic_cast<const eval::Compiled_AST_Node<T>&>(*node->children[offset]).m_original_node;
|
||||||
|
} else {
|
||||||
|
return node->children[offset];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
if (node->identifier == AST_Node_Type::Compiled) {
|
if (node->identifier == AST_Node_Type::Compiled) {
|
||||||
return dynamic_cast<const eval::Compiled_AST_Node<T>&>(*node).m_original_node->children[offset];
|
return dynamic_cast<const eval::Compiled_AST_Node<T>&>(*node).m_original_node->children[offset];
|
||||||
} else {
|
} else {
|
||||||
return node->children[offset];
|
return node->children[offset];
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -88,6 +97,7 @@ namespace chaiscript {
|
|||||||
for (size_t i = 0; i < num; ++i) {
|
for (size_t i = 0; i < num; ++i) {
|
||||||
const auto &child = child_at(node, i);
|
const auto &child = child_at(node, i);
|
||||||
if (child->identifier != AST_Node_Type::Block
|
if (child->identifier != AST_Node_Type::Block
|
||||||
|
&& child->identifier != AST_Node_Type::For
|
||||||
&& contains_var_decl_in_scope(child)) {
|
&& contains_var_decl_in_scope(child)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -99,15 +109,20 @@ namespace chaiscript {
|
|||||||
struct Block {
|
struct Block {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
if (node->identifier == AST_Node_Type::Block
|
if (node->identifier == AST_Node_Type::Block)
|
||||||
&& node->children.size() == 1
|
|
||||||
&& !contains_var_decl_in_scope(node))
|
|
||||||
{
|
{
|
||||||
|
if (!contains_var_decl_in_scope(node))
|
||||||
|
{
|
||||||
|
if (node->children.size() == 1) {
|
||||||
return node->children[0];
|
return node->children[0];
|
||||||
} else {
|
} else {
|
||||||
return node;
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Scopeless_Block_AST_Node<T>>(node->text, node->location, node->children);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct If {
|
struct If {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user