Simplify boxing Expr

This commit is contained in:
Joscha 2022-11-22 15:26:51 +01:00
parent 5a977e6dde
commit 42369628b6
16 changed files with 68 additions and 62 deletions

View file

@ -242,3 +242,9 @@ impl HasSpan for Expr {
} }
} }
} }
impl Expr {
pub fn boxed(self) -> Box<Self> {
Box::new(self)
}
}

View file

@ -38,7 +38,7 @@ impl Call {
expr, expr,
s0, s0,
s1, s1,
arg: Box::new(Expr::Lit(Lit::Nil(span))), arg: Expr::Lit(Lit::Nil(span)).boxed(),
s2: Space::empty(span), s2: Space::empty(span),
span, span,
}); });
@ -55,7 +55,7 @@ impl Call {
expr, expr,
s0, s0,
s1: Space::empty(span), s1: Space::empty(span),
arg: Box::new(Expr::TableConstr(constr)), arg: Expr::TableConstr(constr).boxed(),
s2: Space::empty(span), s2: Space::empty(span),
span, span,
}); });

View file

@ -19,7 +19,7 @@ impl Field {
.then(TableConstrElem::Lit(TableLitElem::Positional(index))) .then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.table_constr(); .table_constr();
let new = Expr::Call(Call::Constr { let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))), expr: Expr::Lit(Lit::Builtin(Builtin::Get, span)).boxed(),
s0: Space::empty(span), s0: Space::empty(span),
constr, constr,
span, span,
@ -44,7 +44,7 @@ impl Field {
.then(TableConstrElem::Lit(TableLitElem::Positional(value))) .then(TableConstrElem::Lit(TableLitElem::Positional(value)))
.table_constr(); .table_constr();
let new = Expr::Call(Call::Constr { let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))), expr: Expr::Lit(Lit::Builtin(Builtin::Set, span)).boxed(),
s0: Space::empty(span), s0: Space::empty(span),
constr, constr,
span, span,
@ -65,7 +65,7 @@ impl Field {
expr, expr,
s0, s0,
s1, s1,
index: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))), index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(),
s2: Space::empty(span), s2: Space::empty(span),
span, span,
}); });
@ -88,7 +88,7 @@ impl Field {
expr, expr,
s0, s0,
s1, s1,
index: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))), index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(),
s2: Space::empty(span), s2: Space::empty(span),
s3: s2, s3: s2,
s4: s3, s4: s3,

View file

@ -22,9 +22,9 @@ impl FuncDef {
span, span,
}) })
.table_lit(); .table_lit();
let quote = Box::new(Expr::Lit(Lit::Table(quote))); let quote = Expr::Lit(Lit::Table(quote)).boxed();
let scope = Expr::Call(Call::NoArg { let scope = Expr::Call(Call::NoArg {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))), expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
@ -36,7 +36,7 @@ impl FuncDef {
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: Box::new(scope), value: scope.boxed(),
span, span,
})) }))
.table_constr(), .table_constr(),
@ -56,7 +56,7 @@ 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 = Expr::Call(Call::NoArg {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Arg, span))), expr: Expr::Lit(Lit::Builtin(Builtin::Arg, span)).boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
@ -66,18 +66,18 @@ impl FuncDef {
name: arg, name: arg,
s0: Space::empty(span), s0: Space::empty(span),
s1: Space::empty(span), s1: Space::empty(span),
value: Box::new(arg_call), value: arg_call.boxed(),
span, span,
}); });
let body = BoundedSeparated::new(span) let body = BoundedSeparated::new(span)
.then(TableLitElem::Positional(Box::new(arg_assign))) .then(TableLitElem::Positional(arg_assign.boxed()))
.then(TableLitElem::Positional(body)) .then(TableLitElem::Positional(body))
.table_lit(); .table_lit();
let new = Expr::FuncDef(Self::AnonNoArg { let new = Expr::FuncDef(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: Box::new(Expr::Lit(Lit::Table(body))), body: Expr::Lit(Lit::Table(body)).boxed(),
span, span,
}); });
(new, true) (new, true)

View file

@ -5,7 +5,7 @@ impl TableLitElem {
match self { match self {
Self::Positional(expr) => { Self::Positional(expr) => {
let (expr, desugared) = expr.desugar(); let (expr, desugared) = expr.desugar();
(Self::Positional(Box::new(expr)), desugared) (Self::Positional(expr.boxed()), desugared)
} }
Self::Named { Self::Named {
@ -20,7 +20,7 @@ impl TableLitElem {
name, name,
s0, s0,
s1, s1,
value: Box::new(value), value: value.boxed(),
span, span,
}; };
(new, desugared) (new, desugared)

View file

@ -25,7 +25,7 @@ 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: Box::new(Expr::Lit(Lit::Table(elems.table_lit()))), value: Expr::Lit(Lit::Table(elems.table_lit())).boxed(),
span, span,
}; };
let mut expr = Expr::Lit(Lit::Table( let mut expr = Expr::Lit(Lit::Table(
@ -36,7 +36,7 @@ impl TableConstr {
// -> `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 = Expr::Field(Field::Assign {
expr: Box::new(expr), expr: expr.boxed(),
s0, s0,
s1, s1,
index, index,

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

View file

@ -16,13 +16,13 @@ impl Var {
// `[ s0 index s1 ]` // `[ s0 index s1 ]`
// -> `'scope()[ s0 index s1 ]` // -> `'scope()[ s0 index s1 ]`
let scope = Expr::Call(Call::NoArg { let scope = Expr::Call(Call::NoArg {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))), expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).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 = Expr::Field(Field::Access {
expr: Box::new(scope), expr: scope.boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: s0, s1: s0,
index, index,
@ -45,13 +45,13 @@ 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 = Expr::Call(Call::NoArg {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))), expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).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 = Expr::Field(Field::Assign {
expr: Box::new(scope), expr: scope.boxed(),
s0: Space::empty(span), s0: Space::empty(span),
s1: s0, s1: s0,
index, index,
@ -75,20 +75,20 @@ impl Var {
span, span,
} => { } => {
let scope = Expr::Call(Call::NoArg { let scope = Expr::Call(Call::NoArg {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))), expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).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(Box::new( .then(TableConstrElem::Lit(TableLitElem::Positional(
scope, scope.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 = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))), expr: Expr::Lit(Lit::Builtin(Builtin::SetRaw, span)).boxed(),
s0: Space::empty(span), s0: Space::empty(span),
constr, constr,
span, span,
@ -102,7 +102,7 @@ impl Var {
let span = name.span(); let span = name.span();
let new = Expr::Var(Self::Access { let new = Expr::Var(Self::Access {
s0: Space::empty(span), s0: Space::empty(span),
index: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(name)))), index: Expr::Lit(Lit::String(StringLit::from_ident(name))).boxed(),
s1: Space::empty(span), s1: Space::empty(span),
span, span,
}); });
@ -122,7 +122,7 @@ impl Var {
let new = Expr::Var(Self::Assign { let new = Expr::Var(Self::Assign {
local, local,
s0: Space::empty(span), s0: Space::empty(span),
index: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(name)))), index: Expr::Lit(Lit::String(StringLit::from_ident(name))).boxed(),
s1: Space::empty(span), s1: Space::empty(span),
s2: s0, s2: s0,
s3: s1, s3: s1,

View file

@ -18,7 +18,7 @@ fn atom_paren(
.then_ignore(just(')')) .then_ignore(just(')'))
.map_with_span(|((s0, inner), s1), span| Expr::Paren { .map_with_span(|((s0, inner), s1), span| Expr::Paren {
s0, s0,
inner: Box::new(inner), inner: inner.boxed(),
s1, s1,
span, span,
}) })
@ -63,11 +63,11 @@ fn left_assoc(
over.then(op_over.repeated()) over.then(op_over.repeated())
.foldl(|left, (s0, op, s1, right)| Expr::BinOp { .foldl(|left, (s0, op, s1, right)| Expr::BinOp {
span: left.span().join(right.span()), span: left.span().join(right.span()),
left: Box::new(left), left: left.boxed(),
s0, s0,
op, op,
s1, s1,
right: Box::new(right), right: right.boxed(),
}) })
.boxed() .boxed()
} }
@ -89,11 +89,11 @@ fn right_assoc(
.then(over) .then(over)
.foldr(|(left, s0, op, s1), right| Expr::BinOp { .foldr(|(left, s0, op, s1), right| Expr::BinOp {
span: left.span().join(right.span()), span: left.span().join(right.span()),
left: Box::new(left), left: left.boxed(),
s0, s0,
op, op,
s1, s1,
right: Box::new(right), right: right.boxed(),
}) })
.boxed() .boxed()
} }

View file

@ -19,7 +19,7 @@ fn func_def_anon_no_arg(
s0, s0,
s1, s1,
s2, s2,
body: Box::new(body), body: body.boxed(),
span, span,
}) })
} }
@ -45,7 +45,7 @@ fn func_def_anon_arg(
arg, arg,
s2, s2,
s3, s3,
body: Box::new(body), body: body.boxed(),
span, span,
}, },
) )
@ -65,7 +65,7 @@ fn func_def_anon_destr(
s0, s0,
pattern, pattern,
s1, s1,
body: Box::new(body), body: body.boxed(),
span, span,
}) })
} }
@ -94,7 +94,7 @@ fn func_def_named_no_arg(
s1, s1,
s2, s2,
s3, s3,
body: Box::new(body), body: body.boxed(),
span, span,
}, },
) )
@ -128,7 +128,7 @@ fn func_def_named_arg(
arg, arg,
s3, s3,
s4, s4,
body: Box::new(body), body: body.boxed(),
span, span,
}, },
) )
@ -157,7 +157,7 @@ fn func_def_named_destr(
s1, s1,
pattern, pattern,
s2, s2,
body: Box::new(body), body: body.boxed(),
span, span,
} }
}) })

View file

@ -132,7 +132,7 @@ pub fn table_lit_elem(
) -> EParser<TableLitElem> { ) -> EParser<TableLitElem> {
let positional = expr let positional = expr
.clone() .clone()
.map(|value| TableLitElem::Positional(Box::new(value))); .map(|value| TableLitElem::Positional(value.boxed()));
let named = ident let named = ident
.then(space.clone()) .then(space.clone())
@ -143,7 +143,7 @@ pub fn table_lit_elem(
name, name,
s0, s0,
s1, s1,
value: Box::new(value), value: value.boxed(),
span, span,
}); });

View file

@ -17,7 +17,7 @@ enum Prefix {
impl Prefix { impl Prefix {
fn into_expr(self, span: Span, expr: Expr) -> Expr { fn into_expr(self, span: Span, expr: Expr) -> Expr {
let expr = Box::new(expr); let expr = expr.boxed();
match self { match self {
Self::Neg { minus, s0 } => Expr::Neg { Self::Neg { minus, s0 } => Expr::Neg {
minus, minus,

View file

@ -57,7 +57,7 @@ enum Suffix {
impl Suffix { impl Suffix {
fn into_expr(self, span: Span, expr: Expr) -> Expr { fn into_expr(self, span: Span, expr: Expr) -> Expr {
let expr = Box::new(expr); let expr = expr.boxed();
match self { match self {
Self::CallArg { s0, s1, arg, s2 } => Expr::Call(Call::Arg { Self::CallArg { s0, s1, arg, s2 } => Expr::Call(Call::Arg {
expr, expr,
@ -143,7 +143,7 @@ fn suffix_call_arg(
.map(|(((s0, s1), arg), s2)| Suffix::CallArg { .map(|(((s0, s1), arg), s2)| Suffix::CallArg {
s0, s0,
s1, s1,
arg: Box::new(arg), arg: arg.boxed(),
s2, s2,
}) })
} }
@ -180,7 +180,7 @@ fn suffix_field_access(
.map(|(((s0, s1), index), s2)| Suffix::FieldAccess { .map(|(((s0, s1), index), s2)| Suffix::FieldAccess {
s0, s0,
s1, s1,
index: Box::new(index), index: index.boxed(),
s2, s2,
}) })
} }
@ -204,11 +204,11 @@ fn suffix_field_assign(
|((((((s0, s1), index), s2), s3), s4), value)| Suffix::FieldAssign { |((((((s0, s1), index), s2), s3), s4), value)| Suffix::FieldAssign {
s0, s0,
s1, s1,
index: Box::new(index), index: index.boxed(),
s2, s2,
s3, s3,
s4, s4,
value: Box::new(value), value: value.boxed(),
}, },
) )
} }
@ -246,7 +246,7 @@ fn suffix_field_assign_ident(
ident, ident,
s2, s2,
s3, s3,
value: Box::new(value), value: value.boxed(),
}, },
) )
} }

View file

@ -25,11 +25,11 @@ fn table_constr_elem(
.map_with_span( .map_with_span(
|(((((s0, index), s1), s2), s3), value), span| TableConstrElem::Indexed { |(((((s0, index), s1), s2), s3), value), span| TableConstrElem::Indexed {
s0, s0,
index: Box::new(index), index: index.boxed(),
s1, s1,
s2, s2,
s3, s3,
value: Box::new(value), value: value.boxed(),
span, span,
}, },
); );

View file

@ -59,7 +59,7 @@ pub fn table_destr(
pattern, pattern,
s0, s0,
s1, s1,
value: Box::new(value), value: value.boxed(),
span, span,
}) })
.boxed() .boxed()

View file

@ -14,7 +14,7 @@ fn var_access(space: EParser<Space>, expr: EParser<Expr>) -> impl Parser<char, V
.then_ignore(just(']')) .then_ignore(just(']'))
.map_with_span(|((s0, index), s1), span| Var::Access { .map_with_span(|((s0, index), s1), span| Var::Access {
s0, s0,
index: Box::new(index), index: index.boxed(),
s1, s1,
span, span,
}) })
@ -39,11 +39,11 @@ fn var_assign(
|((((((local, s0), index), s1), s2), s3), value), span| Var::Assign { |((((((local, s0), index), s1), s2), s3), value), span| Var::Assign {
local, local,
s0, s0,
index: Box::new(index), index: index.boxed(),
s1, s1,
s2, s2,
s3, s3,
value: Box::new(value), value: value.boxed(),
span, span,
}, },
) )
@ -67,7 +67,7 @@ fn var_assign_ident(
name, name,
s0, s0,
s1, s1,
value: Box::new(value), value: value.boxed(),
span, span,
}, },
) )