Simplify creating Lit

This commit is contained in:
Joscha 2022-11-22 16:29:31 +01:00
parent fafc567447
commit 45caafdc38
8 changed files with 38 additions and 14 deletions

View file

@ -53,6 +53,12 @@ impl HasSpan for NumLit {
} }
} }
impl NumLit {
pub fn lit(self) -> Lit {
Lit::Num(self)
}
}
#[derive(Clone)] #[derive(Clone)]
pub enum StringLitElem { pub enum StringLitElem {
/// Normal unescaped characters /// Normal unescaped characters
@ -100,6 +106,10 @@ impl StringLit {
span: ident.span, span: ident.span,
} }
} }
pub fn lit(self) -> Lit {
Lit::String(self)
}
} }
impl HasSpan for StringLit { impl HasSpan for StringLit {
@ -144,6 +154,12 @@ impl HasSpan for TableLit {
} }
} }
impl TableLit {
pub fn lit(self) -> Lit {
Lit::Table(self)
}
}
#[derive(Clone)] #[derive(Clone)]
pub enum Lit { pub enum Lit {
/// `nil` /// `nil`

View file

@ -27,8 +27,12 @@ impl Call {
value: arg, value: arg,
span, span,
}; };
let new = let new = BoundedSeparated::new(span)
Lit::Table(BoundedSeparated::new(span).then(call).then(arg).table_lit()).expr(); .then(call)
.then(arg)
.table_lit()
.lit()
.expr();
(new, true) (new, true)
} }

View file

@ -65,7 +65,7 @@ impl Field {
expr, expr,
s0, s0,
s1, s1,
index: Lit::String(StringLit::from_ident(ident)).expr().boxed(), index: StringLit::from_ident(ident).lit().expr().boxed(),
s2: Space::empty(span), s2: Space::empty(span),
span, span,
}; };
@ -88,7 +88,7 @@ impl Field {
expr, expr,
s0, s0,
s1, s1,
index: Lit::String(StringLit::from_ident(ident)).expr().boxed(), index: StringLit::from_ident(ident).lit().expr().boxed(),
s2: Space::empty(span), s2: Space::empty(span),
s3: s2, s3: s2,
s4: s3, s4: s3,

View file

@ -22,7 +22,7 @@ impl FuncDef {
span, span,
}) })
.table_lit(); .table_lit();
let quote = Lit::Table(quote).expr().boxed(); let quote = quote.lit().expr().boxed();
let scope = Call::NoArg { let scope = Call::NoArg {
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(), expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
@ -76,7 +76,7 @@ impl FuncDef {
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
s2: Space::empty(span), s2: Space::empty(span),
body: Lit::Table(body).expr().boxed(), body: body.lit().expr().boxed(),
span, span,
}; };
(new.expr(), true) (new.expr(), true)

View file

@ -1,4 +1,4 @@
use crate::ast::{Expr, Lit, Program, Space}; use crate::ast::{Program, Space};
impl Program { impl Program {
pub fn desugar(self) -> (Self, bool) { pub fn desugar(self) -> (Self, bool) {
@ -14,7 +14,7 @@ impl Program {
// -> `s0 table` // -> `s0 table`
let new = Self::Expr { let new = Self::Expr {
s0, s0,
expr: Lit::Table(elems.table_lit()).expr(), expr: elems.table_lit().lit().expr(),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}; };

View file

@ -25,10 +25,14 @@ impl TableConstr {
name: Ident::new("raw", span), name: Ident::new("raw", span),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
value: Lit::Table(elems.table_lit()).expr().boxed(), value: elems.table_lit().lit().expr().boxed(),
span, span,
}; };
let mut expr = Lit::Table(BoundedSeparated::new(span).then(raw_elem).table_lit()).expr(); let mut expr = BoundedSeparated::new(span)
.then(raw_elem)
.table_lit()
.lit()
.expr();
// `sl [ s0 index s1 ] s2 = s3 value sr` // `sl [ s0 index s1 ] s2 = s3 value sr`
// -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value` // -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value`

View file

@ -9,7 +9,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
.0 .0
.map(|e| match e { .map(|e| match e {
TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional( TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional(
Lit::String(StringLit::from_ident(ident)).expr().boxed(), StringLit::from_ident(ident).lit().expr().boxed(),
)), )),
TablePatternElem::Named { TablePatternElem::Named {
@ -22,7 +22,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
name, name,
s0, s0,
s1, s1,
value: Lit::String(StringLit::from_ident(ident)).expr().boxed(), value: StringLit::from_ident(ident).lit().expr().boxed(),
span, span,
}), }),
}) })

View file

@ -102,7 +102,7 @@ impl Var {
let span = name.span(); let span = name.span();
let new = Self::Access { let new = Self::Access {
s0: Space::empty(span), s0: Space::empty(span),
index: Lit::String(StringLit::from_ident(name)).expr().boxed(), index: StringLit::from_ident(name).lit().expr().boxed(),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}; };
@ -122,7 +122,7 @@ impl Var {
let new = Self::Assign { let new = Self::Assign {
local, local,
s0: Space::empty(span), s0: Space::empty(span),
index: Lit::String(StringLit::from_ident(name)).expr().boxed(), index: StringLit::from_ident(name).lit().expr().boxed(),
s1: Space::empty(span), s1: Space::empty(span),
s2: s0, s2: s0,
s3: s1, s3: s1,