Simplify creating Expr

This commit is contained in:
Joscha 2022-11-22 15:48:47 +01:00
parent 42369628b6
commit fafc567447
15 changed files with 163 additions and 112 deletions

View file

@ -46,3 +46,9 @@ impl HasSpan for Call {
} }
} }
} }
impl Call {
pub fn expr(self) -> Expr {
Expr::Call(self)
}
}

View file

@ -67,3 +67,9 @@ impl HasSpan for Field {
} }
} }
} }
impl Field {
pub fn expr(self) -> Expr {
Expr::Field(self)
}
}

View file

@ -99,3 +99,9 @@ impl HasSpan for FuncDef {
} }
} }
} }
impl FuncDef {
pub fn expr(self) -> Expr {
Expr::FuncDef(self)
}
}

View file

@ -198,3 +198,9 @@ impl HasSpan for Lit {
} }
} }
} }
impl Lit {
pub fn expr(self) -> Expr {
Expr::Lit(self)
}
}

View file

@ -39,3 +39,9 @@ impl HasSpan for TableConstr {
self.0.span() self.0.span()
} }
} }
impl TableConstr {
pub fn expr(self) -> Expr {
Expr::TableConstr(self)
}
}

View file

@ -61,3 +61,9 @@ impl HasSpan for TableDestr {
self.span self.span
} }
} }
impl TableDestr {
pub fn expr(self) -> Expr {
Expr::TableDestr(self)
}
}

View file

@ -56,3 +56,9 @@ impl HasSpan for Var {
} }
} }
} }
impl Var {
pub fn expr(self) -> Expr {
Expr::Var(self)
}
}

View file

@ -27,22 +27,21 @@ impl Call {
value: arg, value: arg,
span, span,
}; };
let new = Expr::Lit(Lit::Table( let new =
BoundedSeparated::new(span).then(call).then(arg).table_lit(), Lit::Table(BoundedSeparated::new(span).then(call).then(arg).table_lit()).expr();
));
(new, true) (new, true)
} }
Self::NoArg { expr, s0, s1, span } => { Self::NoArg { expr, s0, s1, span } => {
let new = Expr::Call(Self::Arg { let new = Self::Arg {
expr, expr,
s0, s0,
s1, s1,
arg: Expr::Lit(Lit::Nil(span)).boxed(), arg: Lit::Nil(span).expr().boxed(),
s2: Space::empty(span), s2: Space::empty(span),
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::Constr { Self::Constr {
@ -51,15 +50,15 @@ impl Call {
constr, constr,
span, span,
} => { } => {
let new = Expr::Call(Self::Arg { let new = Self::Arg {
expr, expr,
s0, s0,
s1: Space::empty(span), s1: Space::empty(span),
arg: Expr::TableConstr(constr).boxed(), arg: constr.expr().boxed(),
s2: Space::empty(span), s2: Space::empty(span),
span, span,
}); };
(new, true) (new.expr(), true)
} }
} }
} }

View file

@ -18,13 +18,13 @@ impl Field {
.then(TableConstrElem::Lit(TableLitElem::Positional(expr))) .then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
.then(TableConstrElem::Lit(TableLitElem::Positional(index))) .then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.table_constr(); .table_constr();
let new = Expr::Call(Call::Constr { let new = Call::Constr {
expr: Expr::Lit(Lit::Builtin(Builtin::Get, span)).boxed(), expr: Lit::Builtin(Builtin::Get, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
constr, constr,
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::Assign { Self::Assign {
@ -43,13 +43,13 @@ impl Field {
.then(TableConstrElem::Lit(TableLitElem::Positional(index))) .then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.then(TableConstrElem::Lit(TableLitElem::Positional(value))) .then(TableConstrElem::Lit(TableLitElem::Positional(value)))
.table_constr(); .table_constr();
let new = Expr::Call(Call::Constr { let new = Call::Constr {
expr: Expr::Lit(Lit::Builtin(Builtin::Set, span)).boxed(), expr: Lit::Builtin(Builtin::Set, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
constr, constr,
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::AccessIdent { Self::AccessIdent {
@ -61,15 +61,15 @@ impl Field {
} => { } => {
// `expr s0 . s1 ident´ // `expr s0 . s1 ident´
// -> `expr s0 [ s1 ident_str ]` // -> `expr s0 [ s1 ident_str ]`
let new = Expr::Field(Self::Access { let new = Self::Access {
expr, expr,
s0, s0,
s1, s1,
index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), index: Lit::String(StringLit::from_ident(ident)).expr().boxed(),
s2: Space::empty(span), s2: Space::empty(span),
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::AssignIdent { Self::AssignIdent {
@ -84,18 +84,18 @@ impl Field {
} => { } => {
// `expr s0 . s1 ident s2 = s3 value` // `expr s0 . s1 ident s2 = s3 value`
// -> `expr s0 [ s1 ident_str ] s2 = s3 value` // -> `expr s0 [ s1 ident_str ] s2 = s3 value`
let new = Expr::Field(Self::Assign { let new = Self::Assign {
expr, expr,
s0, s0,
s1, s1,
index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), index: Lit::String(StringLit::from_ident(ident)).expr().boxed(),
s2: Space::empty(span), s2: Space::empty(span),
s3: s2, s3: s2,
s4: s3, s4: s3,
value, value,
span, span,
}); };
(new, true) (new.expr(), true)
} }
} }
} }

View file

@ -22,25 +22,24 @@ impl FuncDef {
span, span,
}) })
.table_lit(); .table_lit();
let quote = Expr::Lit(Lit::Table(quote)).boxed(); let quote = Lit::Table(quote).expr().boxed();
let scope = Expr::Call(Call::NoArg { let scope = Call::NoArg {
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}); };
let new = Expr::TableConstr( let new = BoundedSeparated::new(span)
BoundedSeparated::new(span) .then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
.then(TableConstrElem::Lit(TableLitElem::Positional(quote))) .then(TableConstrElem::Lit(TableLitElem::Named {
.then(TableConstrElem::Lit(TableLitElem::Named { name: Ident::new("scope", span),
name: Ident::new("scope", span), s0: Space::empty(span),
s0: Space::empty(span), s1: Space::empty(span),
s1: Space::empty(span), value: scope.expr().boxed(),
value: scope.boxed(), span,
span, }))
})) .table_constr()
.table_constr(), .expr();
);
(new, true) (new, true)
} }
@ -55,32 +54,32 @@ impl FuncDef {
} => { } => {
// `function s0 ( s1 arg s2 ) s3 body` // `function s0 ( s1 arg s2 ) s3 body`
// -> `function ( ) '{ local arg = 'arg(), body }` // -> `function ( ) '{ local arg = 'arg(), body }`
let arg_call = Expr::Call(Call::NoArg { let arg_call = Call::NoArg {
expr: Expr::Lit(Lit::Builtin(Builtin::Arg, span)).boxed(), expr: Lit::Builtin(Builtin::Arg, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}); };
let arg_assign = Expr::Var(Var::AssignIdent { let arg_assign = Var::AssignIdent {
local: Some(Space::empty(span)), local: Some(Space::empty(span)),
name: arg, name: arg,
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
value: arg_call.boxed(), value: arg_call.expr().boxed(),
span, span,
}); };
let body = BoundedSeparated::new(span) let body = BoundedSeparated::new(span)
.then(TableLitElem::Positional(arg_assign.boxed())) .then(TableLitElem::Positional(arg_assign.expr().boxed()))
.then(TableLitElem::Positional(body)) .then(TableLitElem::Positional(body))
.table_lit(); .table_lit();
let new = Expr::FuncDef(Self::AnonNoArg { let new = Self::AnonNoArg {
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: Expr::Lit(Lit::Table(body)).boxed(), body: Lit::Table(body).expr().boxed(),
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::AnonDestr { Self::AnonDestr {

View file

@ -14,7 +14,7 @@ impl Program {
// -> `s0 table` // -> `s0 table`
let new = Self::Expr { let new = Self::Expr {
s0, s0,
expr: Expr::Lit(Lit::Table(elems.table_lit())), expr: Lit::Table(elems.table_lit()).expr(),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}; };

View file

@ -25,17 +25,15 @@ 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: Expr::Lit(Lit::Table(elems.table_lit())).boxed(), value: Lit::Table(elems.table_lit()).expr().boxed(),
span, span,
}; };
let mut expr = Expr::Lit(Lit::Table( let mut expr = Lit::Table(BoundedSeparated::new(span).then(raw_elem).table_lit()).expr();
BoundedSeparated::new(span).then(raw_elem).table_lit(),
));
// `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`
for (s0, (s1, index, s2, s3, s4, value, span), s5) in setters { for (s0, (s1, index, s2, s3, s4, value, span), s5) in setters {
expr = Expr::Field(Field::Assign { expr = Field::Assign {
expr: expr.boxed(), expr: expr.boxed(),
s0, s0,
s1, s1,
@ -45,7 +43,8 @@ impl TableConstr {
s4: s4.then_line(Line::Empty).then(s5), s4: s4.then_line(Line::Empty).then(s5),
value, value,
span, span,
}); }
.expr();
} }
(expr, true) (expr, true)

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(
Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), Lit::String(StringLit::from_ident(ident)).expr().boxed(),
)), )),
TablePatternElem::Named { TablePatternElem::Named {
@ -22,7 +22,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
name, name,
s0, s0,
s1, s1,
value: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(), value: Lit::String(StringLit::from_ident(ident)).expr().boxed(),
span, span,
}), }),
}) })
@ -42,7 +42,7 @@ impl TableDestr {
let mut constr = BoundedSeparated::new(span) let mut constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional( .then(TableConstrElem::Lit(TableLitElem::Positional(
Expr::TableConstr(pattern_to_constr(pattern)).boxed(), pattern_to_constr(pattern).expr().boxed(),
))) )))
.then(TableConstrElem::Lit(TableLitElem::Positional(value))); .then(TableConstrElem::Lit(TableLitElem::Positional(value)));
if local.is_some() { if local.is_some() {
@ -50,17 +50,17 @@ impl TableDestr {
name: Ident::new("local", span), name: Ident::new("local", span),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
value: Expr::Lit(Lit::Bool(true, span)).boxed(), value: Lit::Bool(true, span).expr().boxed(),
span, span,
})); }));
} }
let new = Expr::Call(Call::Constr { let new = Call::Constr {
expr: Expr::Lit(Lit::Builtin(Builtin::Destructure, span)).boxed(), expr: Lit::Builtin(Builtin::Destructure, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
constr: constr.table_constr(), constr: constr.table_constr(),
span, span,
}); };
(new, true) (new.expr(), true)
} }
} }

View file

@ -15,21 +15,21 @@ impl Var {
} => { } => {
// `[ s0 index s1 ]` // `[ s0 index s1 ]`
// -> `'scope()[ s0 index s1 ]` // -> `'scope()[ s0 index s1 ]`
let scope = Expr::Call(Call::NoArg { let scope = Call::NoArg {
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}); };
let new = Expr::Field(Field::Access { let new = Field::Access {
expr: scope.boxed(), expr: scope.expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: s0, s1: s0,
index, index,
s2: s1, s2: s1,
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::Assign { Self::Assign {
@ -44,14 +44,14 @@ impl Var {
} => { } => {
// `[ s0 index s1 ] s2 = s3 value` // `[ s0 index s1 ] s2 = s3 value`
// -> `'scope()[ s0 index s1 ] s2 = s3 value` // -> `'scope()[ s0 index s1 ] s2 = s3 value`
let scope = Expr::Call(Call::NoArg { let scope = Call::NoArg {
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}); };
let new = Expr::Field(Field::Assign { let new = Field::Assign {
expr: scope.boxed(), expr: scope.expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: s0, s1: s0,
index, index,
@ -60,8 +60,8 @@ impl Var {
s4: s3, s4: s3,
value, value,
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::Assign { Self::Assign {
@ -74,39 +74,39 @@ impl Var {
value, value,
span, span,
} => { } => {
let scope = Expr::Call(Call::NoArg { let scope = Call::NoArg {
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(), expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}); };
let constr = BoundedSeparated::new(span) let constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional( .then(TableConstrElem::Lit(TableLitElem::Positional(
scope.boxed(), scope.expr().boxed(),
))) )))
.then(TableConstrElem::Lit(TableLitElem::Positional(index))) .then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.then(TableConstrElem::Lit(TableLitElem::Positional(value))) .then(TableConstrElem::Lit(TableLitElem::Positional(value)))
.table_constr(); .table_constr();
let new = Expr::Call(Call::Constr { let new = Call::Constr {
expr: Expr::Lit(Lit::Builtin(Builtin::SetRaw, span)).boxed(), expr: Lit::Builtin(Builtin::SetRaw, span).expr().boxed(),
s0: Space::empty(span), s0: Space::empty(span),
constr, constr,
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::AccessIdent(name) => { Self::AccessIdent(name) => {
// `name` // `name`
// -> `[ name_str ]` // -> `[ name_str ]`
let span = name.span(); let span = name.span();
let new = Expr::Var(Self::Access { let new = Self::Access {
s0: Space::empty(span), 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), s1: Space::empty(span),
span, span,
}); };
(new, true) (new.expr(), true)
} }
Self::AssignIdent { Self::AssignIdent {
@ -119,17 +119,17 @@ impl Var {
} => { } => {
// `local name s0 = s1 value` // `local name s0 = s1 value`
// -> `local [ name_str ] s0 = s1 value` // -> `local [ name_str ] s0 = s1 value`
let new = Expr::Var(Self::Assign { let new = Self::Assign {
local, local,
s0: Space::empty(span), 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), s1: Space::empty(span),
s2: s0, s2: s0,
s3: s1, s3: s1,
value, value,
span, span,
}); };
(new, true) (new.expr(), true)
} }
} }
} }

View file

@ -59,29 +59,36 @@ impl Suffix {
fn into_expr(self, span: Span, expr: Expr) -> Expr { fn into_expr(self, span: Span, expr: Expr) -> Expr {
let expr = expr.boxed(); let expr = expr.boxed();
match self { match self {
Self::CallArg { s0, s1, arg, s2 } => Expr::Call(Call::Arg { Self::CallArg { s0, s1, arg, s2 } => Call::Arg {
expr, expr,
s0, s0,
s1, s1,
arg, arg,
s2, s2,
span, span,
}), }
Self::CallNoArg { s0, s1 } => Expr::Call(Call::NoArg { expr, s0, s1, span }), .expr(),
Self::CallConstr { s0, constr } => Expr::Call(Call::Constr {
Self::CallNoArg { s0, s1 } => Call::NoArg { expr, s0, s1, span }.expr(),
Self::CallConstr { s0, constr } => Call::Constr {
expr, expr,
s0, s0,
constr, constr,
span, span,
}), }
Self::FieldAccess { s0, s1, index, s2 } => Expr::Field(Field::Access { .expr(),
Self::FieldAccess { s0, s1, index, s2 } => Field::Access {
expr, expr,
s0, s0,
s1, s1,
index, index,
s2, s2,
span, span,
}), }
.expr(),
Self::FieldAssign { Self::FieldAssign {
s0, s0,
s1, s1,
@ -90,7 +97,7 @@ impl Suffix {
s3, s3,
s4, s4,
value, value,
} => Expr::Field(Field::Assign { } => Field::Assign {
expr, expr,
s0, s0,
s1, s1,
@ -100,14 +107,18 @@ impl Suffix {
s4, s4,
value, value,
span, span,
}), }
Self::FieldAccessIdent { s0, s1, ident } => Expr::Field(Field::AccessIdent { .expr(),
Self::FieldAccessIdent { s0, s1, ident } => Field::AccessIdent {
expr, expr,
s0, s0,
s1, s1,
ident, ident,
span, span,
}), }
.expr(),
Self::FieldAssignIdent { Self::FieldAssignIdent {
s0, s0,
s1, s1,
@ -115,7 +126,7 @@ impl Suffix {
s2, s2,
s3, s3,
value, value,
} => Expr::Field(Field::AssignIdent { } => Field::AssignIdent {
expr, expr,
s0, s0,
s1, s1,
@ -124,7 +135,8 @@ impl Suffix {
s3, s3,
value, value,
span, span,
}), }
.expr(),
} }
} }
} }