diff --git a/src/ast/call.rs b/src/ast/call.rs index 547e1f1..adbbf68 100644 --- a/src/ast/call.rs +++ b/src/ast/call.rs @@ -46,3 +46,9 @@ impl HasSpan for Call { } } } + +impl Call { + pub fn expr(self) -> Expr { + Expr::Call(self) + } +} diff --git a/src/ast/field.rs b/src/ast/field.rs index 1972686..44561b4 100644 --- a/src/ast/field.rs +++ b/src/ast/field.rs @@ -67,3 +67,9 @@ impl HasSpan for Field { } } } + +impl Field { + pub fn expr(self) -> Expr { + Expr::Field(self) + } +} diff --git a/src/ast/func_def.rs b/src/ast/func_def.rs index e1e89ad..373fa4d 100644 --- a/src/ast/func_def.rs +++ b/src/ast/func_def.rs @@ -99,3 +99,9 @@ impl HasSpan for FuncDef { } } } + +impl FuncDef { + pub fn expr(self) -> Expr { + Expr::FuncDef(self) + } +} diff --git a/src/ast/lit.rs b/src/ast/lit.rs index 16c7263..091c82a 100644 --- a/src/ast/lit.rs +++ b/src/ast/lit.rs @@ -198,3 +198,9 @@ impl HasSpan for Lit { } } } + +impl Lit { + pub fn expr(self) -> Expr { + Expr::Lit(self) + } +} diff --git a/src/ast/table_constr.rs b/src/ast/table_constr.rs index 0e16ae6..679dd74 100644 --- a/src/ast/table_constr.rs +++ b/src/ast/table_constr.rs @@ -39,3 +39,9 @@ impl HasSpan for TableConstr { self.0.span() } } + +impl TableConstr { + pub fn expr(self) -> Expr { + Expr::TableConstr(self) + } +} diff --git a/src/ast/table_destr.rs b/src/ast/table_destr.rs index 6b24d8f..0aa4b5e 100644 --- a/src/ast/table_destr.rs +++ b/src/ast/table_destr.rs @@ -61,3 +61,9 @@ impl HasSpan for TableDestr { self.span } } + +impl TableDestr { + pub fn expr(self) -> Expr { + Expr::TableDestr(self) + } +} diff --git a/src/ast/var.rs b/src/ast/var.rs index 6ec6026..ce53d63 100644 --- a/src/ast/var.rs +++ b/src/ast/var.rs @@ -56,3 +56,9 @@ impl HasSpan for Var { } } } + +impl Var { + pub fn expr(self) -> Expr { + Expr::Var(self) + } +} diff --git a/src/desugar/call.rs b/src/desugar/call.rs index ec6ab89..162036b 100644 --- a/src/desugar/call.rs +++ b/src/desugar/call.rs @@ -27,22 +27,21 @@ impl Call { value: arg, span, }; - let new = Expr::Lit(Lit::Table( - BoundedSeparated::new(span).then(call).then(arg).table_lit(), - )); + let new = + Lit::Table(BoundedSeparated::new(span).then(call).then(arg).table_lit()).expr(); (new, true) } Self::NoArg { expr, s0, s1, span } => { - let new = Expr::Call(Self::Arg { + let new = Self::Arg { expr, s0, s1, - arg: Expr::Lit(Lit::Nil(span)).boxed(), + arg: Lit::Nil(span).expr().boxed(), s2: Space::empty(span), span, - }); - (new, true) + }; + (new.expr(), true) } Self::Constr { @@ -51,15 +50,15 @@ impl Call { constr, span, } => { - let new = Expr::Call(Self::Arg { + let new = Self::Arg { expr, s0, s1: Space::empty(span), - arg: Expr::TableConstr(constr).boxed(), + arg: constr.expr().boxed(), s2: Space::empty(span), span, - }); - (new, true) + }; + (new.expr(), true) } } } diff --git a/src/desugar/field.rs b/src/desugar/field.rs index 7bbba62..cc7f601 100644 --- a/src/desugar/field.rs +++ b/src/desugar/field.rs @@ -18,13 +18,13 @@ impl Field { .then(TableConstrElem::Lit(TableLitElem::Positional(expr))) .then(TableConstrElem::Lit(TableLitElem::Positional(index))) .table_constr(); - let new = Expr::Call(Call::Constr { - expr: Expr::Lit(Lit::Builtin(Builtin::Get, span)).boxed(), + let new = Call::Constr { + expr: Lit::Builtin(Builtin::Get, span).expr().boxed(), s0: Space::empty(span), constr, span, - }); - (new, true) + }; + (new.expr(), true) } Self::Assign { @@ -43,13 +43,13 @@ impl Field { .then(TableConstrElem::Lit(TableLitElem::Positional(index))) .then(TableConstrElem::Lit(TableLitElem::Positional(value))) .table_constr(); - let new = Expr::Call(Call::Constr { - expr: Expr::Lit(Lit::Builtin(Builtin::Set, span)).boxed(), + let new = Call::Constr { + expr: Lit::Builtin(Builtin::Set, span).expr().boxed(), s0: Space::empty(span), constr, span, - }); - (new, true) + }; + (new.expr(), true) } Self::AccessIdent { @@ -61,15 +61,15 @@ impl Field { } => { // `expr s0 . s1 ident´ // -> `expr s0 [ s1 ident_str ]` - let new = Expr::Field(Self::Access { + let new = Self::Access { expr, s0, s1, - index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), + index: Lit::String(StringLit::from_ident(ident)).expr().boxed(), s2: Space::empty(span), span, - }); - (new, true) + }; + (new.expr(), true) } Self::AssignIdent { @@ -84,18 +84,18 @@ impl Field { } => { // `expr s0 . s1 ident s2 = s3 value` // -> `expr s0 [ s1 ident_str ] s2 = s3 value` - let new = Expr::Field(Self::Assign { + let new = Self::Assign { expr, s0, s1, - index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), + index: Lit::String(StringLit::from_ident(ident)).expr().boxed(), s2: Space::empty(span), s3: s2, s4: s3, value, span, - }); - (new, true) + }; + (new.expr(), true) } } } diff --git a/src/desugar/func_def.rs b/src/desugar/func_def.rs index a40c88d..fd2a6bc 100644 --- a/src/desugar/func_def.rs +++ b/src/desugar/func_def.rs @@ -22,25 +22,24 @@ impl FuncDef { span, }) .table_lit(); - let quote = Expr::Lit(Lit::Table(quote)).boxed(); - let scope = Expr::Call(Call::NoArg { - expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), + let quote = Lit::Table(quote).expr().boxed(); + let scope = Call::NoArg { + expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(), s0: Space::empty(span), s1: Space::empty(span), span, - }); - let new = Expr::TableConstr( - BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional(quote))) - .then(TableConstrElem::Lit(TableLitElem::Named { - name: Ident::new("scope", span), - s0: Space::empty(span), - s1: Space::empty(span), - value: scope.boxed(), - span, - })) - .table_constr(), - ); + }; + let new = BoundedSeparated::new(span) + .then(TableConstrElem::Lit(TableLitElem::Positional(quote))) + .then(TableConstrElem::Lit(TableLitElem::Named { + name: Ident::new("scope", span), + s0: Space::empty(span), + s1: Space::empty(span), + value: scope.expr().boxed(), + span, + })) + .table_constr() + .expr(); (new, true) } @@ -55,32 +54,32 @@ impl FuncDef { } => { // `function s0 ( s1 arg s2 ) s3 body` // -> `function ( ) '{ local arg = 'arg(), body }` - let arg_call = Expr::Call(Call::NoArg { - expr: Expr::Lit(Lit::Builtin(Builtin::Arg, span)).boxed(), + let arg_call = Call::NoArg { + expr: Lit::Builtin(Builtin::Arg, span).expr().boxed(), s0: Space::empty(span), s1: Space::empty(span), span, - }); - let arg_assign = Expr::Var(Var::AssignIdent { + }; + let arg_assign = Var::AssignIdent { local: Some(Space::empty(span)), name: arg, s0: Space::empty(span), s1: Space::empty(span), - value: arg_call.boxed(), + value: arg_call.expr().boxed(), span, - }); + }; let body = BoundedSeparated::new(span) - .then(TableLitElem::Positional(arg_assign.boxed())) + .then(TableLitElem::Positional(arg_assign.expr().boxed())) .then(TableLitElem::Positional(body)) .table_lit(); - let new = Expr::FuncDef(Self::AnonNoArg { + let new = Self::AnonNoArg { s0: Space::empty(span), s1: Space::empty(span), s2: Space::empty(span), - body: Expr::Lit(Lit::Table(body)).boxed(), + body: Lit::Table(body).expr().boxed(), span, - }); - (new, true) + }; + (new.expr(), true) } Self::AnonDestr { diff --git a/src/desugar/program.rs b/src/desugar/program.rs index 73a3bda..d972273 100644 --- a/src/desugar/program.rs +++ b/src/desugar/program.rs @@ -14,7 +14,7 @@ impl Program { // -> `s0 table` let new = Self::Expr { s0, - expr: Expr::Lit(Lit::Table(elems.table_lit())), + expr: Lit::Table(elems.table_lit()).expr(), s1: Space::empty(span), span, }; diff --git a/src/desugar/table_constr.rs b/src/desugar/table_constr.rs index d4e3b68..f74c432 100644 --- a/src/desugar/table_constr.rs +++ b/src/desugar/table_constr.rs @@ -25,17 +25,15 @@ impl TableConstr { name: Ident::new("raw", span), s0: Space::empty(span), s1: Space::empty(span), - value: Expr::Lit(Lit::Table(elems.table_lit())).boxed(), + value: Lit::Table(elems.table_lit()).expr().boxed(), span, }; - let mut expr = Expr::Lit(Lit::Table( - BoundedSeparated::new(span).then(raw_elem).table_lit(), - )); + let mut expr = Lit::Table(BoundedSeparated::new(span).then(raw_elem).table_lit()).expr(); // `sl [ s0 index s1 ] s2 = s3 value sr` // -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value` for (s0, (s1, index, s2, s3, s4, value, span), s5) in setters { - expr = Expr::Field(Field::Assign { + expr = Field::Assign { expr: expr.boxed(), s0, s1, @@ -45,7 +43,8 @@ impl TableConstr { s4: s4.then_line(Line::Empty).then(s5), value, span, - }); + } + .expr(); } (expr, true) diff --git a/src/desugar/table_destr.rs b/src/desugar/table_destr.rs index 6428a3b..c07c400 100644 --- a/src/desugar/table_destr.rs +++ b/src/desugar/table_destr.rs @@ -9,7 +9,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr { .0 .map(|e| match e { TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional( - Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), + Lit::String(StringLit::from_ident(ident)).expr().boxed(), )), TablePatternElem::Named { @@ -22,7 +22,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr { name, s0, s1, - value: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), + value: Lit::String(StringLit::from_ident(ident)).expr().boxed(), span, }), }) @@ -42,7 +42,7 @@ impl TableDestr { let mut constr = BoundedSeparated::new(span) .then(TableConstrElem::Lit(TableLitElem::Positional( - Expr::TableConstr(pattern_to_constr(pattern)).boxed(), + pattern_to_constr(pattern).expr().boxed(), ))) .then(TableConstrElem::Lit(TableLitElem::Positional(value))); if local.is_some() { @@ -50,17 +50,17 @@ impl TableDestr { name: Ident::new("local", span), s0: Space::empty(span), s1: Space::empty(span), - value: Expr::Lit(Lit::Bool(true, span)).boxed(), + value: Lit::Bool(true, span).expr().boxed(), span, })); } - let new = Expr::Call(Call::Constr { - expr: Expr::Lit(Lit::Builtin(Builtin::Destructure, span)).boxed(), + let new = Call::Constr { + expr: Lit::Builtin(Builtin::Destructure, span).expr().boxed(), s0: Space::empty(span), constr: constr.table_constr(), span, - }); - (new, true) + }; + (new.expr(), true) } } diff --git a/src/desugar/var.rs b/src/desugar/var.rs index 821860d..b77aa34 100644 --- a/src/desugar/var.rs +++ b/src/desugar/var.rs @@ -15,21 +15,21 @@ impl Var { } => { // `[ s0 index s1 ]` // -> `'scope()[ s0 index s1 ]` - let scope = Expr::Call(Call::NoArg { - expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), + let scope = Call::NoArg { + expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(), s0: Space::empty(span), s1: Space::empty(span), span, - }); - let new = Expr::Field(Field::Access { - expr: scope.boxed(), + }; + let new = Field::Access { + expr: scope.expr().boxed(), s0: Space::empty(span), s1: s0, index, s2: s1, span, - }); - (new, true) + }; + (new.expr(), true) } Self::Assign { @@ -44,14 +44,14 @@ impl Var { } => { // `[ s0 index s1 ] s2 = s3 value` // -> `'scope()[ s0 index s1 ] s2 = s3 value` - let scope = Expr::Call(Call::NoArg { - expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), + let scope = Call::NoArg { + expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(), s0: Space::empty(span), s1: Space::empty(span), span, - }); - let new = Expr::Field(Field::Assign { - expr: scope.boxed(), + }; + let new = Field::Assign { + expr: scope.expr().boxed(), s0: Space::empty(span), s1: s0, index, @@ -60,8 +60,8 @@ impl Var { s4: s3, value, span, - }); - (new, true) + }; + (new.expr(), true) } Self::Assign { @@ -74,39 +74,39 @@ impl Var { value, span, } => { - let scope = Expr::Call(Call::NoArg { - expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), + let scope = Call::NoArg { + expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(), s0: Space::empty(span), s1: Space::empty(span), span, - }); + }; let constr = BoundedSeparated::new(span) .then(TableConstrElem::Lit(TableLitElem::Positional( - scope.boxed(), + scope.expr().boxed(), ))) .then(TableConstrElem::Lit(TableLitElem::Positional(index))) .then(TableConstrElem::Lit(TableLitElem::Positional(value))) .table_constr(); - let new = Expr::Call(Call::Constr { - expr: Expr::Lit(Lit::Builtin(Builtin::SetRaw, span)).boxed(), + let new = Call::Constr { + expr: Lit::Builtin(Builtin::SetRaw, span).expr().boxed(), s0: Space::empty(span), constr, span, - }); - (new, true) + }; + (new.expr(), true) } Self::AccessIdent(name) => { // `name` // -> `[ name_str ]` let span = name.span(); - let new = Expr::Var(Self::Access { + let new = Self::Access { s0: Space::empty(span), - index: Expr::Lit(Lit::String(StringLit::from_ident(name))).boxed(), + index: Lit::String(StringLit::from_ident(name)).expr().boxed(), s1: Space::empty(span), span, - }); - (new, true) + }; + (new.expr(), true) } Self::AssignIdent { @@ -119,17 +119,17 @@ impl Var { } => { // `local name s0 = s1 value` // -> `local [ name_str ] s0 = s1 value` - let new = Expr::Var(Self::Assign { + let new = Self::Assign { local, s0: Space::empty(span), - index: Expr::Lit(Lit::String(StringLit::from_ident(name))).boxed(), + index: Lit::String(StringLit::from_ident(name)).expr().boxed(), s1: Space::empty(span), s2: s0, s3: s1, value, span, - }); - (new, true) + }; + (new.expr(), true) } } } diff --git a/src/parser/suffix.rs b/src/parser/suffix.rs index d519c13..ca25830 100644 --- a/src/parser/suffix.rs +++ b/src/parser/suffix.rs @@ -59,29 +59,36 @@ impl Suffix { fn into_expr(self, span: Span, expr: Expr) -> Expr { let expr = expr.boxed(); match self { - Self::CallArg { s0, s1, arg, s2 } => Expr::Call(Call::Arg { + Self::CallArg { s0, s1, arg, s2 } => Call::Arg { expr, s0, s1, arg, s2, span, - }), - Self::CallNoArg { s0, s1 } => Expr::Call(Call::NoArg { expr, s0, s1, span }), - Self::CallConstr { s0, constr } => Expr::Call(Call::Constr { + } + .expr(), + + Self::CallNoArg { s0, s1 } => Call::NoArg { expr, s0, s1, span }.expr(), + + Self::CallConstr { s0, constr } => Call::Constr { expr, s0, constr, span, - }), - Self::FieldAccess { s0, s1, index, s2 } => Expr::Field(Field::Access { + } + .expr(), + + Self::FieldAccess { s0, s1, index, s2 } => Field::Access { expr, s0, s1, index, s2, span, - }), + } + .expr(), + Self::FieldAssign { s0, s1, @@ -90,7 +97,7 @@ impl Suffix { s3, s4, value, - } => Expr::Field(Field::Assign { + } => Field::Assign { expr, s0, s1, @@ -100,14 +107,18 @@ impl Suffix { s4, value, span, - }), - Self::FieldAccessIdent { s0, s1, ident } => Expr::Field(Field::AccessIdent { + } + .expr(), + + Self::FieldAccessIdent { s0, s1, ident } => Field::AccessIdent { expr, s0, s1, ident, span, - }), + } + .expr(), + Self::FieldAssignIdent { s0, s1, @@ -115,7 +126,7 @@ impl Suffix { s2, s3, value, - } => Expr::Field(Field::AssignIdent { + } => Field::AssignIdent { expr, s0, s1, @@ -124,7 +135,8 @@ impl Suffix { s3, value, span, - }), + } + .expr(), } } }