diff --git a/src/engine.rs b/src/engine.rs index c0ccad18..5418a382 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -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)?; diff --git a/src/parser.rs b/src/parser.rs index d01ecdce..328b496e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -129,6 +129,7 @@ pub enum Expr { Identifier(String, Position), CharConstant(char, Position), StringConstant(String, Position), + Block(Box, Position), FunctionCall(String, Vec, Option, Position), Assignment(Box, Box, Position), Dot(Box, Box, 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>) -> Result { + // 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;