From f084165eabbb8ad1b8e8969078fce89070ab4d96 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 24 Feb 2021 21:29:32 +0100 Subject: =?UTF-8?q?While=20loops=20=F0=9F=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/expr.rs | 39 +++++++++++++++++++++++++++------------ src/syntax/visit.rs | 6 ++++++ 2 files changed, 33 insertions(+), 12 deletions(-) (limited to 'src/syntax') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 5b37bb56..638d9dd3 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -19,19 +19,21 @@ pub enum Expr { Template(ExprTemplate), /// A grouped expression: `(1 + 2)`. Group(ExprGroup), - /// A block expression: `{ #let x = 1; x + 2 }`. + /// A block expression: `{ let x = 1; x + 2 }`. Block(ExprBlock), /// A unary operation: `-x`. Unary(ExprUnary), /// A binary operation: `a + b`. Binary(ExprBinary), - /// An invocation of a function: `foo(...)`, `#[foo ...]`. + /// An invocation of a function: `foo(...)`. Call(ExprCall), - /// A let expression: `#let x = 1`. + /// A let expression: `let x = 1`. Let(ExprLet), - /// An if expression: `#if x { y } #else { z }`. + /// An if expression: `if x { y } else { z }`. If(ExprIf), - /// A for expression: `#for x #in y { z }`. + /// A while expression: `while x { y }`. + While(ExprWhile), + /// A for expression: `for x in y { z }`. For(ExprFor), } @@ -51,6 +53,7 @@ impl Expr { Self::Call(v) => v.span, Self::Let(v) => v.span, Self::If(v) => v.span, + Self::While(v) => v.span, Self::For(v) => v.span, } } @@ -62,6 +65,7 @@ impl Expr { | Expr::Call(_) | Expr::Let(_) | Expr::If(_) + | Expr::While(_) | Expr::For(_) ) } @@ -154,7 +158,7 @@ pub struct ExprGroup { pub expr: Box, } -/// A block expression: `{ #let x = 1; x + 2 }`. +/// A block expression: `{ let x = 1; x + 2 }`. #[derive(Debug, Clone, PartialEq)] pub struct ExprBlock { /// The source code location. @@ -365,7 +369,7 @@ pub enum Associativity { Right, } -/// An invocation of a function: `foo(...)`, `#[foo ...]`. +/// An invocation of a function: `foo(...)`. #[derive(Debug, Clone, PartialEq)] pub struct ExprCall { /// The source code location. @@ -407,7 +411,7 @@ impl ExprArg { } } -/// A let expression: `#let x = 1`. +/// A let expression: `let x = 1`. #[derive(Debug, Clone, PartialEq)] pub struct ExprLet { /// The source code location. @@ -418,7 +422,7 @@ pub struct ExprLet { pub init: Option>, } -/// An if expression: `#if x { y } #else { z }`. +/// An if expression: `if x { y } else { z }`. #[derive(Debug, Clone, PartialEq)] pub struct ExprIf { /// The source code location. @@ -431,7 +435,18 @@ pub struct ExprIf { pub else_body: Option>, } -/// A for expression: `#for x #in y { z }`. +/// A while expression: `while x { y }`. +#[derive(Debug, Clone, PartialEq)] +pub struct ExprWhile { + /// The source code location. + pub span: Span, + /// The condition which selects whether to evaluate the body. + pub condition: Box, + /// The expression to evaluate while the condition is true. + pub body: Box, +} + +/// A for expression: `for x in y { z }`. #[derive(Debug, Clone, PartialEq)] pub struct ExprFor { /// The source code location. @@ -447,9 +462,9 @@ pub struct ExprFor { /// A pattern in a for loop. #[derive(Debug, Clone, PartialEq)] pub enum ForPattern { - /// A value pattern: `#for v #in array`. + /// A value pattern: `for v in array`. Value(Ident), - /// A key-value pattern: `#for k, v #in dict`. + /// A key-value pattern: `for k, v in dict`. KeyValue(Ident, Ident), } diff --git a/src/syntax/visit.rs b/src/syntax/visit.rs index 2d3c683f..1bf260c7 100644 --- a/src/syntax/visit.rs +++ b/src/syntax/visit.rs @@ -61,6 +61,7 @@ visit! { Expr::Call(e) => v.visit_call(e), Expr::Let(e) => v.visit_let(e), Expr::If(e) => v.visit_if(e), + Expr::While(e) => v.visit_while(e), Expr::For(e) => v.visit_for(e), } } @@ -132,6 +133,11 @@ visit! { } } + fn visit_while(v, node: &ExprWhile) { + v.visit_expr(&node.condition); + v.visit_expr(&node.body); + } + fn visit_for(v, node: &ExprFor) { v.visit_expr(&node.iter); v.visit_expr(&node.body); -- cgit v1.2.3