Add C++17-style if-init blocks

This commit is contained in:
Jason Turner 2016-06-26 22:10:53 -06:00
parent 09bdec4882
commit e3d1741c63
4 changed files with 54 additions and 2 deletions

View File

@ -278,6 +278,18 @@ while (some_condition()) { /* do something */ }
for (x : [1,2,3]) { print(i); }
```
## Conditionals
```
if (expression) { }
```
```
// C++17-style init-if blocks
// Value of 'statement' is scoped for entire `if` block
if (statement; expression) { }
```
## Built in Types
```

View File

@ -823,6 +823,29 @@ namespace chaiscript
};
template<typename T>
struct If_Init_AST_Node final : AST_Node_Impl<T> {
If_Init_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::If, std::move(t_loc), std::move(t_children))
{
assert(this->children.size() == 3 || this->children.size() == 4);
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
this->children[0]->eval(t_ss);
if (this->get_bool_condition(this->children[1]->eval(t_ss))) {
return this->children[2]->eval(t_ss);
} else {
if (this->children.size() == 4) {
return this->children[3]->eval(t_ss);
} else {
return void_var();
}
}
}
};
template<typename T>
struct If_AST_Node final : AST_Node_Impl<T> {
If_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children) :

View File

@ -1557,7 +1557,13 @@ namespace chaiscript
throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
}
if (!(Operator() && Char(')'))) {
if (!Equation()) {
throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
}
const bool is_if_init = Eol() && Equation();
if (!Char(')')) {
throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
}
@ -1585,7 +1591,11 @@ namespace chaiscript
}
}
build_match<eval::If_AST_Node<Tracer>>(prev_stack_top);
if (!is_if_init) {
build_match<eval::If_AST_Node<Tracer>>(prev_stack_top);
} else {
build_match<eval::If_Init_AST_Node<Tracer>>(prev_stack_top);
}
}
return retval;

7
unittests/init_if.chai Normal file
View File

@ -0,0 +1,7 @@
if (var x = 2; x == 3) {
assert_true(false);
} else if (var y = 3; y == 6) {
assert_true(false);
} else {
assert_equal(5, y + x);
}