From c94a18833f23d2b57de1b87971458fd54b56d088 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 3 Mar 2021 17:53:40 +0100 Subject: =?UTF-8?q?Closures=20and=20function=20definitions=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Supports: - Closure syntax: `(x, y) => z` - Shorthand for a single argument: `x => y` - Function syntax: `let f(x) = y` - Capturing of variables from the environment - Error messages for too few / many passed arguments Does not support: - Named arguments - Variadic arguments with `..` --- src/syntax/expr.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/syntax/expr.rs') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 638d9dd3..d76ada69 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -25,8 +25,10 @@ pub enum Expr { Unary(ExprUnary), /// A binary operation: `a + b`. Binary(ExprBinary), - /// An invocation of a function: `foo(...)`. + /// An invocation of a function: `f(x, y)`. Call(ExprCall), + /// A closure expression: `(x, y) => { z }`. + Closure(ExprClosure), /// A let expression: `let x = 1`. Let(ExprLet), /// An if expression: `if x { y } else { z }`. @@ -51,6 +53,7 @@ impl Expr { Self::Unary(v) => v.span, Self::Binary(v) => v.span, Self::Call(v) => v.span, + Self::Closure(v) => v.span, Self::Let(v) => v.span, Self::If(v) => v.span, Self::While(v) => v.span, @@ -58,7 +61,7 @@ impl Expr { } } - /// Whether the expression can be shorten in markup with a hashtag. + /// Whether the expression can be shortened in markup with a hashtag. pub fn has_short_form(&self) -> bool { matches!(self, Expr::Ident(_) @@ -411,6 +414,17 @@ impl ExprArg { } } +/// A closure expression: `(x, y) => { z }`. +#[derive(Debug, Clone, PartialEq)] +pub struct ExprClosure { + /// The source code location. + pub span: Span, + /// The parameter bindings. + pub params: Rc>, + /// The body of the closure. + pub body: Rc, +} + /// A let expression: `let x = 1`. #[derive(Debug, Clone, PartialEq)] pub struct ExprLet { @@ -418,7 +432,7 @@ pub struct ExprLet { pub span: Span, /// The binding to assign to. pub binding: Ident, - /// The expression the pattern is initialized with. + /// The expression the binding is initialized with. pub init: Option>, } -- cgit v1.2.3