diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-08-12 16:00:09 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-08-12 16:07:42 +0200 |
| commit | d002cdf451e1c6efbf7cd7f2303264526b6f8a92 (patch) | |
| tree | 31b8446728a93550cab57d50bba92eb32f280678 /src/syntax | |
| parent | ccb4be4da4e8aeda115b22f2a0b586a86f5e31bd (diff) | |
Named arguments for user defined functions
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/expr.rs | 11 | ||||
| -rw-r--r-- | src/syntax/visit.rs | 24 |
2 files changed, 27 insertions, 8 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index cf9aff4a..9292d5b6 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -437,11 +437,20 @@ pub struct ClosureExpr { /// This only exists if you use the function syntax sugar: `let f(x) = y`. pub name: Option<Ident>, /// The parameter bindings. - pub params: Rc<Vec<Ident>>, + pub params: Vec<ClosureParam>, /// The body of the closure. pub body: Rc<Expr>, } +/// An parameter to a closure: `x` or `draw: false`. +#[derive(Debug, Clone, PartialEq)] +pub enum ClosureParam { + /// A positional parameter. + Pos(Ident), + /// A named parameter with a default value. + Named(Named), +} + /// A with expression: `f with (x, y: 1)`. /// /// Applies arguments to a function. diff --git a/src/syntax/visit.rs b/src/syntax/visit.rs index 81cba5c7..bc9359b3 100644 --- a/src/syntax/visit.rs +++ b/src/syntax/visit.rs @@ -192,13 +192,6 @@ impl_visitors! { v.visit_args(r!(call.args)); } - visit_closure(v, closure: ClosureExpr) { - for param in r!(rc: closure.params) { - v.visit_binding(param); - } - v.visit_expr(r!(rc: closure.body)); - } - visit_args(v, args: CallArgs) { for arg in r!(args.items) { v.visit_arg(arg); @@ -212,6 +205,23 @@ impl_visitors! { } } + visit_closure(v, closure: ClosureExpr) { + for param in r!(closure.params) { + v.visit_param(param); + } + v.visit_expr(r!(rc: closure.body)); + } + + visit_param(v, param: ClosureParam) { + match param { + ClosureParam::Pos(binding) => v.visit_binding(binding), + ClosureParam::Named(named) => { + v.visit_binding(r!(named.name)); + v.visit_expr(r!(named.expr)); + } + } + } + visit_with(v, with_expr: WithExpr) { v.visit_expr(r!(with_expr.callee)); v.visit_args(r!(with_expr.args)); |
