diff --git a/src/ast/table_constr.rs b/src/ast/table_constr.rs index 679dd74..52696a4 100644 --- a/src/ast/table_constr.rs +++ b/src/ast/table_constr.rs @@ -1,6 +1,6 @@ use crate::span::{HasSpan, Span}; -use super::{BoundedSeparated, Expr, Space, TableLitElem}; +use super::{BoundedSeparated, Expr, Ident, Space, TableLitElem}; #[derive(Debug, Clone)] pub enum TableConstrElem { @@ -30,6 +30,28 @@ impl HasSpan for TableConstrElem { } } +impl TableConstrElem { + pub fn positional(value: Box) -> Self { + Self::Lit(TableLitElem::Positional(value)) + } + + pub fn named(name: Ident, value: Box, span: Span) -> Self { + Self::Lit(TableLitElem::named(name, value, span)) + } + + pub fn indexed(index: Box, value: Box, span: Span) -> Self { + Self::Indexed { + s0: Space::empty(span), + index, + s1: Space::empty(span), + s2: Space::empty(span), + s3: Space::empty(span), + value, + span, + } + } +} + /// `{ a, b, foo: c, [d]: e }` #[derive(Debug, Clone)] pub struct TableConstr(pub BoundedSeparated); diff --git a/src/desugar/field.rs b/src/desugar/field.rs index f66a482..caf1cdc 100644 --- a/src/desugar/field.rs +++ b/src/desugar/field.rs @@ -1,6 +1,4 @@ -use crate::ast::{ - BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem, -}; +use crate::ast::{BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem}; use crate::builtin::Builtin; impl Field { @@ -15,8 +13,8 @@ impl Field { span, } => { let constr = BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional(expr))) - .then(TableConstrElem::Lit(TableLitElem::Positional(index))) + .then(TableConstrElem::positional(expr)) + .then(TableConstrElem::positional(index)) .table_constr(); let new = Call::Constr { expr: Lit::Builtin(Builtin::Get, span).expr().boxed(), @@ -39,9 +37,9 @@ impl Field { span, } => { let constr = BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional(expr))) - .then(TableConstrElem::Lit(TableLitElem::Positional(index))) - .then(TableConstrElem::Lit(TableLitElem::Positional(value))) + .then(TableConstrElem::positional(expr)) + .then(TableConstrElem::positional(index)) + .then(TableConstrElem::positional(value)) .table_constr(); let new = Call::Constr { expr: Lit::Builtin(Builtin::Set, span).expr().boxed(), diff --git a/src/desugar/func_def.rs b/src/desugar/func_def.rs index 86e3ad8..5ac4eb2 100644 --- a/src/desugar/func_def.rs +++ b/src/desugar/func_def.rs @@ -15,8 +15,10 @@ impl FuncDef { } => { let quote = BoundedSeparated::new(span) .then(TableLitElem::named(Ident::new("quote", span), body, span)) - .table_lit(); - let quote = quote.lit().expr().boxed(); + .table_lit() + .lit() + .expr() + .boxed(); let scope = Call::NoArg { expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(), s0: Space::empty(span), @@ -24,12 +26,12 @@ impl FuncDef { span, }; let new = BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional(quote))) - .then(TableConstrElem::Lit(TableLitElem::named( + .then(TableConstrElem::positional(quote)) + .then(TableConstrElem::named( Ident::new("scope", span), scope.expr().boxed(), span, - ))) + )) .table_constr() .expr(); (new, true) diff --git a/src/desugar/lit.rs b/src/desugar/lit.rs index 9a94278..a514ef9 100644 --- a/src/desugar/lit.rs +++ b/src/desugar/lit.rs @@ -41,7 +41,7 @@ impl Lit { match self { Self::Table(table) => { let (table, desugared) = table.desugar(); - (Self::Table(table), desugared) + (table.lit(), desugared) } lit => (lit, false), diff --git a/src/desugar/table_destr.rs b/src/desugar/table_destr.rs index fb1e070..5981dfa 100644 --- a/src/desugar/table_destr.rs +++ b/src/desugar/table_destr.rs @@ -8,9 +8,9 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr { pattern .0 .map(|e| match e { - TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional( - StringLit::from_ident(ident).lit().expr().boxed(), - )), + TablePatternElem::Positional(ident) => { + TableConstrElem::positional(StringLit::from_ident(ident).lit().expr().boxed()) + } TablePatternElem::Named { name, @@ -41,16 +41,16 @@ impl TableDestr { } = self; let mut constr = BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional( + .then(TableConstrElem::positional( pattern_to_constr(pattern).expr().boxed(), - ))) - .then(TableConstrElem::Lit(TableLitElem::Positional(value))); + )) + .then(TableConstrElem::positional(value)); if local.is_some() { - constr = constr.then(TableConstrElem::Lit(TableLitElem::named( + constr = constr.then(TableConstrElem::named( Ident::new("local", span), Lit::Bool(true, span).expr().boxed(), span, - ))); + )); } let new = Call::Constr { diff --git a/src/desugar/var.rs b/src/desugar/var.rs index ffad828..053e5d1 100644 --- a/src/desugar/var.rs +++ b/src/desugar/var.rs @@ -1,5 +1,5 @@ use crate::ast::{ - BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem, Var, + BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, Var, }; use crate::builtin::Builtin; use crate::span::HasSpan; @@ -81,11 +81,9 @@ impl Var { span, }; let constr = BoundedSeparated::new(span) - .then(TableConstrElem::Lit(TableLitElem::Positional( - scope.expr().boxed(), - ))) - .then(TableConstrElem::Lit(TableLitElem::Positional(index))) - .then(TableConstrElem::Lit(TableLitElem::Positional(value))) + .then(TableConstrElem::positional(scope.expr().boxed())) + .then(TableConstrElem::positional(index)) + .then(TableConstrElem::positional(value)) .table_constr(); let new = Call::Constr { expr: Lit::Builtin(Builtin::SetRaw, span).expr().boxed(), diff --git a/src/pretty/basic.rs b/src/pretty/basic.rs index c8c8d8f..abc55ee 100644 --- a/src/pretty/basic.rs +++ b/src/pretty/basic.rs @@ -1,6 +1,6 @@ use pretty::{DocAllocator, DocBuilder, Pretty}; -use crate::ast::{BoundedSeparated, Ident, Space}; +use crate::ast::{BoundedSeparated, Ident}; use super::NEST_DEPTH;