summaryrefslogtreecommitdiff
path: root/src/syntax/expr.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-03-03 17:53:40 +0100
committerLaurenz <laurmaedje@gmail.com>2021-03-03 17:53:40 +0100
commitc94a18833f23d2b57de1b87971458fd54b56d088 (patch)
tree9e1ed55cfca15aef6d39ced50a3a5b14d2800aae /src/syntax/expr.rs
parent4d90a066f197264341eff6bf67e8c06cae434eb4 (diff)
Closures and function definitions 🚀
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 `..`
Diffstat (limited to 'src/syntax/expr.rs')
-rw-r--r--src/syntax/expr.rs20
1 files changed, 17 insertions, 3 deletions
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<Vec<Ident>>,
+ /// The body of the closure.
+ pub body: Rc<Expr>,
+}
+
/// 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<Box<Expr>>,
}