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

View file

@ -19,7 +19,7 @@ impl Field {
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.table_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),
constr,
span,
@ -44,7 +44,7 @@ impl Field {
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
.table_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),
constr,
span,
@ -65,7 +65,7 @@ impl Field {
expr,
s0,
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),
span,
});
@ -88,7 +88,7 @@ impl Field {
expr,
s0,
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),
s3: s2,
s4: s3,

View file

@ -22,9 +22,9 @@ impl FuncDef {
span,
})
.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 {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))),
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(),
s0: Space::empty(span),
s1: Space::empty(span),
span,
@ -36,7 +36,7 @@ impl FuncDef {
name: Ident::new("scope", span),
s0: Space::empty(span),
s1: Space::empty(span),
value: Box::new(scope),
value: scope.boxed(),
span,
}))
.table_constr(),
@ -56,7 +56,7 @@ impl FuncDef {
// `function s0 ( s1 arg s2 ) s3 body`
// -> `function ( ) '{ local arg = 'arg(), body }`
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),
s1: Space::empty(span),
span,
@ -66,18 +66,18 @@ impl FuncDef {
name: arg,
s0: Space::empty(span),
s1: Space::empty(span),
value: Box::new(arg_call),
value: arg_call.boxed(),
span,
});
let body = BoundedSeparated::new(span)
.then(TableLitElem::Positional(Box::new(arg_assign)))
.then(TableLitElem::Positional(arg_assign.boxed()))
.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(body))),
body: Expr::Lit(Lit::Table(body)).boxed(),
span,
});
(new, true)

View file

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

View file

@ -25,7 +25,7 @@ impl TableConstr {
name: Ident::new("raw", span),
s0: 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,
};
let mut expr = Expr::Lit(Lit::Table(
@ -36,7 +36,7 @@ impl TableConstr {
// -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value`
for (s0, (s1, index, s2, s3, s4, value, span), s5) in setters {
expr = Expr::Field(Field::Assign {
expr: Box::new(expr),
expr: expr.boxed(),
s0,
s1,
index,

View file

@ -9,7 +9,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
.0
.map(|e| match e {
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 {
@ -22,7 +22,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
name,
s0,
s1,
value: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))),
value: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(),
span,
}),
})
@ -41,22 +41,22 @@ impl TableDestr {
} = self;
let mut constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(Box::new(
Expr::TableConstr(pattern_to_constr(pattern)),
))))
.then(TableConstrElem::Lit(TableLitElem::Positional(
Expr::TableConstr(pattern_to_constr(pattern)).boxed(),
)))
.then(TableConstrElem::Lit(TableLitElem::Positional(value)));
if local.is_some() {
constr = constr.then(TableConstrElem::Lit(TableLitElem::Named {
name: Ident::new("local", span),
s0: 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,
}));
}
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),
constr: constr.table_constr(),
span,

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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