Allow block expressions.

This commit is contained in:
Stephen Chung 2020-03-07 10:39:00 +08:00
parent 473e40e8a4
commit 22cb69a16b
2 changed files with 15 additions and 0 deletions

View File

@ -544,10 +544,15 @@ impl Engine<'_> {
Expr::Identifier(id, pos) => {
Self::search_scope(scope, id, Ok, *pos).map(|(_, val)| val)
}
// lhs[idx_expr]
Expr::Index(lhs, idx_expr, idx_pos) => self
.eval_index_expr(scope, lhs, idx_expr, *idx_pos)
.map(|(_, _, _, x)| x),
// Statement block
Expr::Block(block, _) => self.eval_stmt(scope, block),
// lhs = rhs
Expr::Assignment(lhs, rhs, _) => {
let rhs_val = self.eval_expr(scope, rhs)?;

View File

@ -129,6 +129,7 @@ pub enum Expr {
Identifier(String, Position),
CharConstant(char, Position),
StringConstant(String, Position),
Block(Box<Stmt>, Position),
FunctionCall(String, Vec<Expr>, Option<Dynamic>, Position),
Assignment(Box<Expr>, Box<Expr>, Position),
Dot(Box<Expr>, Box<Expr>, Position),
@ -150,6 +151,7 @@ impl Expr {
| Expr::CharConstant(_, pos)
| Expr::StringConstant(_, pos)
| Expr::FunctionCall(_, _, _, pos)
| Expr::Block(_, pos)
| Expr::Array(_, pos)
| Expr::True(pos)
| Expr::False(pos)
@ -1185,6 +1187,14 @@ fn parse_array_expr<'a>(
}
fn parse_primary<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
// Block statement as expression
match input.peek() {
Some(&(Token::LeftBrace, pos)) => {
return parse_block(input).map(|block| Expr::Block(Box::new(block), pos))
}
_ => (),
}
let token = input.next();
let mut follow_on = false;