Simplify creating Expr
This commit is contained in:
parent
42369628b6
commit
fafc567447
15 changed files with 163 additions and 112 deletions
|
|
@ -46,3 +46,9 @@ impl HasSpan for Call {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Call {
|
||||
pub fn expr(self) -> Expr {
|
||||
Expr::Call(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,3 +67,9 @@ impl HasSpan for Field {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Field {
|
||||
pub fn expr(self) -> Expr {
|
||||
Expr::Field(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,3 +99,9 @@ impl HasSpan for FuncDef {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FuncDef {
|
||||
pub fn expr(self) -> Expr {
|
||||
Expr::FuncDef(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -198,3 +198,9 @@ impl HasSpan for Lit {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Lit {
|
||||
pub fn expr(self) -> Expr {
|
||||
Expr::Lit(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,3 +39,9 @@ impl HasSpan for TableConstr {
|
|||
self.0.span()
|
||||
}
|
||||
}
|
||||
|
||||
impl TableConstr {
|
||||
pub fn expr(self) -> Expr {
|
||||
Expr::TableConstr(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,3 +61,9 @@ impl HasSpan for TableDestr {
|
|||
self.span
|
||||
}
|
||||
}
|
||||
|
||||
impl TableDestr {
|
||||
pub fn expr(self) -> Expr {
|
||||
Expr::TableDestr(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,3 +56,9 @@ impl HasSpan for Var {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Var {
|
||||
pub fn expr(self) -> Expr {
|
||||
Expr::Var(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,22 +27,21 @@ impl Call {
|
|||
value: arg,
|
||||
span,
|
||||
};
|
||||
let new = Expr::Lit(Lit::Table(
|
||||
BoundedSeparated::new(span).then(call).then(arg).table_lit(),
|
||||
));
|
||||
let new =
|
||||
Lit::Table(BoundedSeparated::new(span).then(call).then(arg).table_lit()).expr();
|
||||
(new, true)
|
||||
}
|
||||
|
||||
Self::NoArg { expr, s0, s1, span } => {
|
||||
let new = Expr::Call(Self::Arg {
|
||||
let new = Self::Arg {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
arg: Expr::Lit(Lit::Nil(span)).boxed(),
|
||||
arg: Lit::Nil(span).expr().boxed(),
|
||||
s2: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::Constr {
|
||||
|
|
@ -51,15 +50,15 @@ impl Call {
|
|||
constr,
|
||||
span,
|
||||
} => {
|
||||
let new = Expr::Call(Self::Arg {
|
||||
let new = Self::Arg {
|
||||
expr,
|
||||
s0,
|
||||
s1: Space::empty(span),
|
||||
arg: Expr::TableConstr(constr).boxed(),
|
||||
arg: constr.expr().boxed(),
|
||||
s2: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,13 +18,13 @@ impl Field {
|
|||
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
||||
.table_constr();
|
||||
let new = Expr::Call(Call::Constr {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Get, span)).boxed(),
|
||||
let new = Call::Constr {
|
||||
expr: Lit::Builtin(Builtin::Get, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
constr,
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::Assign {
|
||||
|
|
@ -43,13 +43,13 @@ impl Field {
|
|||
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
|
||||
.table_constr();
|
||||
let new = Expr::Call(Call::Constr {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Set, span)).boxed(),
|
||||
let new = Call::Constr {
|
||||
expr: Lit::Builtin(Builtin::Set, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
constr,
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::AccessIdent {
|
||||
|
|
@ -61,15 +61,15 @@ impl Field {
|
|||
} => {
|
||||
// `expr s0 . s1 ident´
|
||||
// -> `expr s0 [ s1 ident_str ]`
|
||||
let new = Expr::Field(Self::Access {
|
||||
let new = Self::Access {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(),
|
||||
index: Lit::String(StringLit::from_ident(ident)).expr().boxed(),
|
||||
s2: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::AssignIdent {
|
||||
|
|
@ -84,18 +84,18 @@ impl Field {
|
|||
} => {
|
||||
// `expr s0 . s1 ident s2 = s3 value`
|
||||
// -> `expr s0 [ s1 ident_str ] s2 = s3 value`
|
||||
let new = Expr::Field(Self::Assign {
|
||||
let new = Self::Assign {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
index: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(),
|
||||
index: Lit::String(StringLit::from_ident(ident)).expr().boxed(),
|
||||
s2: Space::empty(span),
|
||||
s3: s2,
|
||||
s4: s3,
|
||||
value,
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,25 +22,24 @@ impl FuncDef {
|
|||
span,
|
||||
})
|
||||
.table_lit();
|
||||
let quote = Expr::Lit(Lit::Table(quote)).boxed();
|
||||
let scope = Expr::Call(Call::NoArg {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(),
|
||||
let quote = Lit::Table(quote).expr().boxed();
|
||||
let scope = Call::NoArg {
|
||||
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
let new = Expr::TableConstr(
|
||||
BoundedSeparated::new(span)
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
|
||||
.then(TableConstrElem::Lit(TableLitElem::Named {
|
||||
name: Ident::new("scope", span),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
value: scope.boxed(),
|
||||
span,
|
||||
}))
|
||||
.table_constr(),
|
||||
);
|
||||
};
|
||||
let new = BoundedSeparated::new(span)
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
|
||||
.then(TableConstrElem::Lit(TableLitElem::Named {
|
||||
name: Ident::new("scope", span),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
value: scope.expr().boxed(),
|
||||
span,
|
||||
}))
|
||||
.table_constr()
|
||||
.expr();
|
||||
(new, true)
|
||||
}
|
||||
|
||||
|
|
@ -55,32 +54,32 @@ impl FuncDef {
|
|||
} => {
|
||||
// `function s0 ( s1 arg s2 ) s3 body`
|
||||
// -> `function ( ) '{ local arg = 'arg(), body }`
|
||||
let arg_call = Expr::Call(Call::NoArg {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Arg, span)).boxed(),
|
||||
let arg_call = Call::NoArg {
|
||||
expr: Lit::Builtin(Builtin::Arg, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
let arg_assign = Expr::Var(Var::AssignIdent {
|
||||
};
|
||||
let arg_assign = Var::AssignIdent {
|
||||
local: Some(Space::empty(span)),
|
||||
name: arg,
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
value: arg_call.boxed(),
|
||||
value: arg_call.expr().boxed(),
|
||||
span,
|
||||
});
|
||||
};
|
||||
let body = BoundedSeparated::new(span)
|
||||
.then(TableLitElem::Positional(arg_assign.boxed()))
|
||||
.then(TableLitElem::Positional(arg_assign.expr().boxed()))
|
||||
.then(TableLitElem::Positional(body))
|
||||
.table_lit();
|
||||
let new = Expr::FuncDef(Self::AnonNoArg {
|
||||
let new = Self::AnonNoArg {
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
s2: Space::empty(span),
|
||||
body: Expr::Lit(Lit::Table(body)).boxed(),
|
||||
body: Lit::Table(body).expr().boxed(),
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::AnonDestr {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ impl Program {
|
|||
// -> `s0 table`
|
||||
let new = Self::Expr {
|
||||
s0,
|
||||
expr: Expr::Lit(Lit::Table(elems.table_lit())),
|
||||
expr: Lit::Table(elems.table_lit()).expr(),
|
||||
s1: Space::empty(span),
|
||||
span,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -25,17 +25,15 @@ impl TableConstr {
|
|||
name: Ident::new("raw", span),
|
||||
s0: 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,
|
||||
};
|
||||
let mut expr = Expr::Lit(Lit::Table(
|
||||
BoundedSeparated::new(span).then(raw_elem).table_lit(),
|
||||
));
|
||||
let mut expr = Lit::Table(BoundedSeparated::new(span).then(raw_elem).table_lit()).expr();
|
||||
|
||||
// `sl [ s0 index s1 ] s2 = s3 value sr`
|
||||
// -> `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 = Field::Assign {
|
||||
expr: expr.boxed(),
|
||||
s0,
|
||||
s1,
|
||||
|
|
@ -45,7 +43,8 @@ impl TableConstr {
|
|||
s4: s4.then_line(Line::Empty).then(s5),
|
||||
value,
|
||||
span,
|
||||
});
|
||||
}
|
||||
.expr();
|
||||
}
|
||||
|
||||
(expr, true)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
|
|||
.0
|
||||
.map(|e| match e {
|
||||
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 {
|
||||
|
|
@ -22,7 +22,7 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
|
|||
name,
|
||||
s0,
|
||||
s1,
|
||||
value: Expr::Lit(Lit::String(StringLit::from_ident(ident))).boxed(),
|
||||
value: Lit::String(StringLit::from_ident(ident)).expr().boxed(),
|
||||
span,
|
||||
}),
|
||||
})
|
||||
|
|
@ -42,7 +42,7 @@ impl TableDestr {
|
|||
|
||||
let mut constr = BoundedSeparated::new(span)
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(
|
||||
Expr::TableConstr(pattern_to_constr(pattern)).boxed(),
|
||||
pattern_to_constr(pattern).expr().boxed(),
|
||||
)))
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(value)));
|
||||
if local.is_some() {
|
||||
|
|
@ -50,17 +50,17 @@ impl TableDestr {
|
|||
name: Ident::new("local", span),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
value: Expr::Lit(Lit::Bool(true, span)).boxed(),
|
||||
value: Lit::Bool(true, span).expr().boxed(),
|
||||
span,
|
||||
}));
|
||||
}
|
||||
|
||||
let new = Expr::Call(Call::Constr {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Destructure, span)).boxed(),
|
||||
let new = Call::Constr {
|
||||
expr: Lit::Builtin(Builtin::Destructure, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
constr: constr.table_constr(),
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,21 +15,21 @@ impl Var {
|
|||
} => {
|
||||
// `[ s0 index s1 ]`
|
||||
// -> `'scope()[ s0 index s1 ]`
|
||||
let scope = Expr::Call(Call::NoArg {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(),
|
||||
let scope = Call::NoArg {
|
||||
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
let new = Expr::Field(Field::Access {
|
||||
expr: scope.boxed(),
|
||||
};
|
||||
let new = Field::Access {
|
||||
expr: scope.expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
s1: s0,
|
||||
index,
|
||||
s2: s1,
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::Assign {
|
||||
|
|
@ -44,14 +44,14 @@ impl Var {
|
|||
} => {
|
||||
// `[ s0 index s1 ] s2 = s3 value`
|
||||
// -> `'scope()[ s0 index s1 ] s2 = s3 value`
|
||||
let scope = Expr::Call(Call::NoArg {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(),
|
||||
let scope = Call::NoArg {
|
||||
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
let new = Expr::Field(Field::Assign {
|
||||
expr: scope.boxed(),
|
||||
};
|
||||
let new = Field::Assign {
|
||||
expr: scope.expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
s1: s0,
|
||||
index,
|
||||
|
|
@ -60,8 +60,8 @@ impl Var {
|
|||
s4: s3,
|
||||
value,
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::Assign {
|
||||
|
|
@ -74,39 +74,39 @@ impl Var {
|
|||
value,
|
||||
span,
|
||||
} => {
|
||||
let scope = Expr::Call(Call::NoArg {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::Scope, span)).boxed(),
|
||||
let scope = Call::NoArg {
|
||||
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
span,
|
||||
});
|
||||
};
|
||||
let constr = BoundedSeparated::new(span)
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(
|
||||
scope.boxed(),
|
||||
scope.expr().boxed(),
|
||||
)))
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
||||
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
|
||||
.table_constr();
|
||||
let new = Expr::Call(Call::Constr {
|
||||
expr: Expr::Lit(Lit::Builtin(Builtin::SetRaw, span)).boxed(),
|
||||
let new = Call::Constr {
|
||||
expr: Lit::Builtin(Builtin::SetRaw, span).expr().boxed(),
|
||||
s0: Space::empty(span),
|
||||
constr,
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::AccessIdent(name) => {
|
||||
// `name`
|
||||
// -> `[ name_str ]`
|
||||
let span = name.span();
|
||||
let new = Expr::Var(Self::Access {
|
||||
let new = Self::Access {
|
||||
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),
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::AssignIdent {
|
||||
|
|
@ -119,17 +119,17 @@ impl Var {
|
|||
} => {
|
||||
// `local name s0 = s1 value`
|
||||
// -> `local [ name_str ] s0 = s1 value`
|
||||
let new = Expr::Var(Self::Assign {
|
||||
let new = Self::Assign {
|
||||
local,
|
||||
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),
|
||||
s2: s0,
|
||||
s3: s1,
|
||||
value,
|
||||
span,
|
||||
});
|
||||
(new, true)
|
||||
};
|
||||
(new.expr(), true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,29 +59,36 @@ impl Suffix {
|
|||
fn into_expr(self, span: Span, expr: Expr) -> Expr {
|
||||
let expr = expr.boxed();
|
||||
match self {
|
||||
Self::CallArg { s0, s1, arg, s2 } => Expr::Call(Call::Arg {
|
||||
Self::CallArg { s0, s1, arg, s2 } => Call::Arg {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
arg,
|
||||
s2,
|
||||
span,
|
||||
}),
|
||||
Self::CallNoArg { s0, s1 } => Expr::Call(Call::NoArg { expr, s0, s1, span }),
|
||||
Self::CallConstr { s0, constr } => Expr::Call(Call::Constr {
|
||||
}
|
||||
.expr(),
|
||||
|
||||
Self::CallNoArg { s0, s1 } => Call::NoArg { expr, s0, s1, span }.expr(),
|
||||
|
||||
Self::CallConstr { s0, constr } => Call::Constr {
|
||||
expr,
|
||||
s0,
|
||||
constr,
|
||||
span,
|
||||
}),
|
||||
Self::FieldAccess { s0, s1, index, s2 } => Expr::Field(Field::Access {
|
||||
}
|
||||
.expr(),
|
||||
|
||||
Self::FieldAccess { s0, s1, index, s2 } => Field::Access {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
index,
|
||||
s2,
|
||||
span,
|
||||
}),
|
||||
}
|
||||
.expr(),
|
||||
|
||||
Self::FieldAssign {
|
||||
s0,
|
||||
s1,
|
||||
|
|
@ -90,7 +97,7 @@ impl Suffix {
|
|||
s3,
|
||||
s4,
|
||||
value,
|
||||
} => Expr::Field(Field::Assign {
|
||||
} => Field::Assign {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
|
|
@ -100,14 +107,18 @@ impl Suffix {
|
|||
s4,
|
||||
value,
|
||||
span,
|
||||
}),
|
||||
Self::FieldAccessIdent { s0, s1, ident } => Expr::Field(Field::AccessIdent {
|
||||
}
|
||||
.expr(),
|
||||
|
||||
Self::FieldAccessIdent { s0, s1, ident } => Field::AccessIdent {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
ident,
|
||||
span,
|
||||
}),
|
||||
}
|
||||
.expr(),
|
||||
|
||||
Self::FieldAssignIdent {
|
||||
s0,
|
||||
s1,
|
||||
|
|
@ -115,7 +126,7 @@ impl Suffix {
|
|||
s2,
|
||||
s3,
|
||||
value,
|
||||
} => Expr::Field(Field::AssignIdent {
|
||||
} => Field::AssignIdent {
|
||||
expr,
|
||||
s0,
|
||||
s1,
|
||||
|
|
@ -124,7 +135,8 @@ impl Suffix {
|
|||
s3,
|
||||
value,
|
||||
span,
|
||||
}),
|
||||
}
|
||||
.expr(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue