Allow folding of if blocks
This commit is contained in:
parent
184ca7f7b2
commit
f6c69f2826
@ -219,7 +219,7 @@ namespace chaiscript
|
|||||||
std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
||||||
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
||||||
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)),
|
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)),
|
||||||
m_parser(std::make_unique<parser::ChaiScript_Parser<optimizer::Optimizer<optimizer::Block, optimizer::Constant_Fold, optimizer::Return, optimizer::For_Loop>>>())
|
m_parser(std::make_unique<parser::ChaiScript_Parser<optimizer::Optimizer<optimizer::Block, optimizer::Constant_Fold, optimizer::If, optimizer::Return, optimizer::For_Loop>>>())
|
||||||
{
|
{
|
||||||
if (m_module_paths.empty())
|
if (m_module_paths.empty())
|
||||||
{
|
{
|
||||||
@ -244,7 +244,7 @@ namespace chaiscript
|
|||||||
ChaiScript( std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
ChaiScript( std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
||||||
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
||||||
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)),
|
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)),
|
||||||
m_parser(std::make_unique<parser::ChaiScript_Parser<optimizer::Optimizer<optimizer::Block, optimizer::Constant_Fold, optimizer::Return, optimizer::For_Loop>>>())
|
m_parser(std::make_unique<parser::ChaiScript_Parser<optimizer::Optimizer<optimizer::Block, optimizer::Constant_Fold, optimizer::If, optimizer::Return, optimizer::For_Loop>>>())
|
||||||
{
|
{
|
||||||
if (m_module_paths.empty())
|
if (m_module_paths.empty())
|
||||||
{
|
{
|
||||||
|
@ -653,7 +653,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
struct Ternary_Cond_AST_Node final : AST_Node {
|
struct Ternary_Cond_AST_Node final : AST_Node {
|
||||||
Ternary_Cond_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> t_children) :
|
Ternary_Cond_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> 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); }
|
{ assert(children.size() == 3); }
|
||||||
|
|
||||||
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
|
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
|
||||||
|
@ -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<eval::Constant_AST_Node>(node->children[0])->m_value;
|
||||||
|
if (condition.get_type_info().bare_equal_type_info(typeid(bool))) {
|
||||||
|
if (boxed_cast<bool>(condition)) {
|
||||||
|
return node->children[1];
|
||||||
|
} else if (node->children.size() == 3) {
|
||||||
|
return node->children[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct Constant_Fold {
|
struct Constant_Fold {
|
||||||
AST_NodePtr optimize(const AST_NodePtr &node) {
|
AST_NodePtr optimize(const AST_NodePtr &node) {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user