diff --git a/src/ast/basic.rs b/src/ast/basic.rs index 3528b7d..ee6428a 100644 --- a/src/ast/basic.rs +++ b/src/ast/basic.rs @@ -2,6 +2,8 @@ use std::fmt; use crate::span::{HasSpan, Span}; +use super::{TableConstr, TableConstrElem, TableLit, TableLitElem}; + #[derive(Clone)] pub enum Line { Empty, @@ -105,3 +107,15 @@ impl BoundedSeparated { self } } + +impl BoundedSeparated { + pub fn table_lit(self) -> TableLit { + TableLit(self) + } +} + +impl BoundedSeparated { + pub fn table_constr(self) -> TableConstr { + TableConstr(self) + } +} diff --git a/src/desugar/call.rs b/src/desugar/call.rs index b09b7a3..bef48b1 100644 --- a/src/desugar/call.rs +++ b/src/desugar/call.rs @@ -1,4 +1,4 @@ -use crate::ast::{BoundedSeparated, Call, Expr, Ident, Lit, Space, TableLit, TableLitElem}; +use crate::ast::{BoundedSeparated, Call, Expr, Ident, Lit, Space, TableLitElem}; // TODO Add span for just the parentheses to ast, or limit span to parentheses @@ -27,9 +27,9 @@ impl Call { value: arg, span, }; - let new = Expr::Lit(Lit::Table(TableLit( - BoundedSeparated::new(span).then(call).then(arg), - ))); + let new = Expr::Lit(Lit::Table( + BoundedSeparated::new(span).then(call).then(arg).table_lit(), + )); (new, true) } diff --git a/src/desugar/field.rs b/src/desugar/field.rs index 84005b7..c429f87 100644 --- a/src/desugar/field.rs +++ b/src/desugar/field.rs @@ -1,6 +1,5 @@ use crate::ast::{ - BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstr, TableConstrElem, - TableLitElem, + BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem, }; use crate::builtin::Builtin; @@ -15,11 +14,10 @@ impl Field { s2: _, span, } => { - let constr = TableConstr( - BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional(expr))) - .then(TableConstrElem::Lit(TableLitElem::Positional(index))), - ); + let constr = BoundedSeparated::new(span) + .then(TableConstrElem::Lit(TableLitElem::Positional(expr))) + .then(TableConstrElem::Lit(TableLitElem::Positional(index))) + .table_constr(); let new = Expr::Call(Call::Constr { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))), s0: Space::empty(span), @@ -40,12 +38,11 @@ impl Field { value, span, } => { - let constr = TableConstr( - BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional(expr))) - .then(TableConstrElem::Lit(TableLitElem::Positional(index))) - .then(TableConstrElem::Lit(TableLitElem::Positional(value))), - ); + let constr = BoundedSeparated::new(span) + .then(TableConstrElem::Lit(TableLitElem::Positional(expr))) + .then(TableConstrElem::Lit(TableLitElem::Positional(index))) + .then(TableConstrElem::Lit(TableLitElem::Positional(value))) + .table_constr(); let new = Expr::Call(Call::Constr { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))), s0: Space::empty(span), diff --git a/src/desugar/func_def.rs b/src/desugar/func_def.rs index 4f3c396..1b370eb 100644 --- a/src/desugar/func_def.rs +++ b/src/desugar/func_def.rs @@ -1,6 +1,5 @@ use crate::ast::{ - BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstr, TableConstrElem, - TableLit, TableLitElem, Var, + BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstrElem, TableLitElem, Var, }; use crate::builtin::Builtin; @@ -14,13 +13,15 @@ impl FuncDef { body, span, } => { - let quote = TableLit(BoundedSeparated::new(span).then(TableLitElem::Named { - name: Ident::new("quote", span), - s0: Space::empty(span), - s1: Space::empty(span), - value: body, - span, - })); + let quote = BoundedSeparated::new(span) + .then(TableLitElem::Named { + name: Ident::new("quote", span), + s0: Space::empty(span), + s1: Space::empty(span), + value: body, + span, + }) + .table_lit(); let quote = Box::new(Expr::Lit(Lit::Table(quote))); let scope = Expr::Call(Call::NoArg { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))), @@ -28,7 +29,7 @@ impl FuncDef { s1: Space::empty(span), span, }); - let new = Expr::TableConstr(TableConstr( + let new = Expr::TableConstr( BoundedSeparated::new(span) .then(TableConstrElem::Lit(TableLitElem::Positional(quote))) .then(TableConstrElem::Lit(TableLitElem::Named { @@ -37,8 +38,9 @@ impl FuncDef { s1: Space::empty(span), value: Box::new(scope), span, - })), - )); + })) + .table_constr(), + ); (new, true) } @@ -69,12 +71,13 @@ impl FuncDef { }); let body = BoundedSeparated::new(span) .then(TableLitElem::Positional(Box::new(arg_assign))) - .then(TableLitElem::Positional(body)); + .then(TableLitElem::Positional(body)) + .table_lit(); let new = Expr::FuncDef(Self::AnonNoArg { s0: Space::empty(span), s1: Space::empty(span), s2: Space::empty(span), - body: Box::new(Expr::Lit(Lit::Table(TableLit(body)))), + body: Box::new(Expr::Lit(Lit::Table(body))), span, }); (new, true) diff --git a/src/desugar/lit.rs b/src/desugar/lit.rs index 4f32a38..f88efa6 100644 --- a/src/desugar/lit.rs +++ b/src/desugar/lit.rs @@ -32,7 +32,7 @@ impl TableLitElem { impl TableLit { pub fn desugar(self) -> (Self, bool) { let (elems, desugared) = self.0.desugar(|e| e.desugar()); - (Self(elems), desugared) + (elems.table_lit(), desugared) } } diff --git a/src/desugar/program.rs b/src/desugar/program.rs index 50b11eb..73a3bda 100644 --- a/src/desugar/program.rs +++ b/src/desugar/program.rs @@ -1,4 +1,4 @@ -use crate::ast::{Expr, Lit, Program, Space, TableLit}; +use crate::ast::{Expr, Lit, Program, Space}; impl Program { pub fn desugar(self) -> (Self, bool) { @@ -14,7 +14,7 @@ impl Program { // -> `s0 table` let new = Self::Expr { s0, - expr: Expr::Lit(Lit::Table(TableLit(elems))), + expr: Expr::Lit(Lit::Table(elems.table_lit())), s1: Space::empty(span), span, }; diff --git a/src/desugar/table_constr.rs b/src/desugar/table_constr.rs index 2bb4eff..42123b1 100644 --- a/src/desugar/table_constr.rs +++ b/src/desugar/table_constr.rs @@ -1,5 +1,5 @@ use crate::ast::{ - BoundedSeparated, Expr, Field, Ident, Line, Lit, Space, TableConstr, TableConstrElem, TableLit, + BoundedSeparated, Expr, Field, Ident, Line, Lit, Space, TableConstr, TableConstrElem, TableLitElem, }; use crate::span::HasSpan; @@ -25,12 +25,12 @@ impl TableConstr { name: Ident::new("raw", span), s0: Space::empty(span), s1: Space::empty(span), - value: Box::new(Expr::Lit(Lit::Table(TableLit(elems)))), + value: Box::new(Expr::Lit(Lit::Table(elems.table_lit()))), span, }; - let mut expr = Expr::Lit(Lit::Table(TableLit( - BoundedSeparated::new(span).then(raw_elem), - ))); + let mut expr = Expr::Lit(Lit::Table( + BoundedSeparated::new(span).then(raw_elem).table_lit(), + )); // `sl [ s0 index s1 ] s2 = s3 value sr` // -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value` diff --git a/src/desugar/table_destr.rs b/src/desugar/table_destr.rs index 8d68a00..831464b 100644 --- a/src/desugar/table_destr.rs +++ b/src/desugar/table_destr.rs @@ -5,25 +5,28 @@ use crate::ast::{ use crate::builtin::Builtin; fn pattern_to_constr(pattern: TablePattern) -> TableConstr { - TableConstr(pattern.0.map(|e| match e { - TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional( - Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))), - )), + pattern + .0 + .map(|e| match e { + TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional( + Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))), + )), - TablePatternElem::Named { - name, - s0, - s1, - ident, - span, - } => TableConstrElem::Lit(TableLitElem::Named { - name, - s0, - s1, - value: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))), - span, - }), - })) + TablePatternElem::Named { + name, + s0, + s1, + ident, + span, + } => TableConstrElem::Lit(TableLitElem::Named { + name, + s0, + s1, + value: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))), + span, + }), + }) + .table_constr() } impl TableDestr { @@ -55,7 +58,7 @@ impl TableDestr { let new = Expr::Call(Call::Constr { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Destructure, span))), s0: Space::empty(span), - constr: TableConstr(constr), + constr: constr.table_constr(), span, }); (new, true) diff --git a/src/desugar/var.rs b/src/desugar/var.rs index 7019702..39af1e5 100644 --- a/src/desugar/var.rs +++ b/src/desugar/var.rs @@ -1,6 +1,5 @@ use crate::ast::{ - BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstr, TableConstrElem, - TableLitElem, Var, + BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem, Var, }; use crate::builtin::Builtin; use crate::span::HasSpan; @@ -81,14 +80,13 @@ impl Var { s1: Space::empty(span), span, }); - let constr = TableConstr( - BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional(Box::new( - scope, - )))) - .then(TableConstrElem::Lit(TableLitElem::Positional(index))) - .then(TableConstrElem::Lit(TableLitElem::Positional(value))), - ); + let constr = BoundedSeparated::new(span) + .then(TableConstrElem::Lit(TableLitElem::Positional(Box::new( + scope, + )))) + .then(TableConstrElem::Lit(TableLitElem::Positional(index))) + .then(TableConstrElem::Lit(TableLitElem::Positional(value))) + .table_constr(); let new = Expr::Call(Call::Constr { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))), s0: Space::empty(span),