Simplify creating Field

This commit is contained in:
Joscha 2022-11-22 17:06:56 +01:00
parent 009be99aaa
commit 6f7683ad1e
5 changed files with 79 additions and 75 deletions

View file

@ -69,6 +69,54 @@ impl HasSpan for Field {
} }
impl Field { impl Field {
pub fn access(base: Box<Expr>, index: Box<Expr>, span: Span) -> Self {
Self::Access {
expr: base,
s0: Space::empty(span),
s1: Space::empty(span),
index,
s2: Space::empty(span),
span,
}
}
pub fn assign(base: Box<Expr>, index: Box<Expr>, value: Box<Expr>, span: Span) -> Self {
Self::Assign {
expr: base,
s0: Space::empty(span),
s1: Space::empty(span),
index,
s2: Space::empty(span),
s3: Space::empty(span),
s4: Space::empty(span),
value,
span,
}
}
pub fn access_ident(base: Box<Expr>, ident: Ident, span: Span) -> Self {
Self::AccessIdent {
expr: base,
s0: Space::empty(span),
s1: Space::empty(span),
ident,
span,
}
}
pub fn assign_ident(base: Box<Expr>, ident: Ident, value: Box<Expr>, span: Span) -> Self {
Self::AssignIdent {
expr: base,
s0: Space::empty(span),
s1: Space::empty(span),
ident,
s2: Space::empty(span),
s3: Space::empty(span),
value,
span,
}
}
pub fn expr(self) -> Expr { pub fn expr(self) -> Expr {
Expr::Field(self) Expr::Field(self)
} }

View file

@ -16,10 +16,8 @@ impl Call {
let new = BoundedSeparated::new(span) let new = BoundedSeparated::new(span)
.then(TableLitElem::named(Ident::new("call", span), expr, span)) .then(TableLitElem::named(Ident::new("call", span), expr, span))
.then(TableLitElem::named(Ident::new("arg", span), arg, span)) .then(TableLitElem::named(Ident::new("arg", span), arg, span))
.table_lit() .table_lit();
.lit() (new.lit().expr(), true)
.expr();
(new, true)
} }
Self::NoArg { Self::NoArg {

View file

@ -1,4 +1,4 @@
use crate::ast::{BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem}; use crate::ast::{BoundedSeparated, Call, Expr, Field, Lit, StringLit, TableConstrElem};
use crate::builtin::Builtin; use crate::builtin::Builtin;
impl Field { impl Field {
@ -50,47 +50,35 @@ impl Field {
Self::AccessIdent { Self::AccessIdent {
expr, expr,
s0, s0: _,
s1, s1: _,
ident, ident,
span, span,
} => { } => {
// `expr s0 . s1 ident´ let new = Self::access(
// -> `expr s0 [ s1 ident_str ]`
let new = Self::Access {
expr, expr,
s0, StringLit::from_ident(ident).lit().expr().boxed(),
s1,
index: StringLit::from_ident(ident).lit().expr().boxed(),
s2: Space::empty(span),
span, span,
}; );
(new.expr(), true) (new.expr(), true)
} }
Self::AssignIdent { Self::AssignIdent {
expr, expr,
s0, s0: _,
s1, s1: _,
ident, ident,
s2, s2: _,
s3, s3: _,
value, value,
span, span,
} => { } => {
// `expr s0 . s1 ident s2 = s3 value` let new = Self::assign(
// -> `expr s0 [ s1 ident_str ] s2 = s3 value`
let new = Self::Assign {
expr, expr,
s0, StringLit::from_ident(ident).lit().expr().boxed(),
s1,
index: StringLit::from_ident(ident).lit().expr().boxed(),
s2: Space::empty(span),
s3: s2,
s4: s3,
value, value,
span, span,
}; );
(new.expr(), true) (new.expr(), true)
} }
} }

View file

@ -1,5 +1,5 @@
use crate::ast::{ use crate::ast::{
BoundedSeparated, Expr, Field, Ident, Line, TableConstr, TableConstrElem, TableLitElem, BoundedSeparated, Expr, Field, Ident, TableConstr, TableConstrElem, TableLitElem,
}; };
use crate::span::HasSpan; use crate::span::HasSpan;
@ -10,14 +10,14 @@ impl TableConstr {
let (elems, setters) = self.0.remove_map(|e| match e { let (elems, setters) = self.0.remove_map(|e| match e {
TableConstrElem::Lit(lit) => Ok(lit), TableConstrElem::Lit(lit) => Ok(lit),
TableConstrElem::Indexed { TableConstrElem::Indexed {
s0, s0: _,
index, index,
s1, s1: _,
s2, s2: _,
s3, s3: _,
value, value,
span, span,
} => Err((s0, index, s1, s2, s3, value, span)), } => Err((index, value, span)),
}); });
let mut expr = BoundedSeparated::new(span) let mut expr = BoundedSeparated::new(span)
@ -30,21 +30,8 @@ impl TableConstr {
.lit() .lit()
.expr(); .expr();
// `sl [ s0 index s1 ] s2 = s3 value sr` for (_, (index, value, span), _) in setters {
// -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value` expr = Field::assign(expr.boxed(), index, value, span).expr();
for (s0, (s1, index, s2, s3, s4, value, span), s5) in setters {
expr = Field::Assign {
expr: expr.boxed(),
s0,
s1,
index,
s2,
s3,
s4: s4.then_line(Line::Empty).then(s5),
value,
span,
}
.expr();
} }
(expr, true) (expr, true)

View file

@ -8,45 +8,28 @@ impl Var {
pub fn desugar(self) -> (Expr, bool) { pub fn desugar(self) -> (Expr, bool) {
match self { match self {
Self::Access { Self::Access {
s0, s0: _,
index, index,
s1, s1: _,
span, span,
} => { } => {
let scope = Call::no_arg(Lit::Builtin(Builtin::Scope, span).expr().boxed(), span); let scope = Call::no_arg(Lit::Builtin(Builtin::Scope, span).expr().boxed(), span);
let new = Field::Access { let new = Field::access(scope.expr().boxed(), index, span);
expr: scope.expr().boxed(),
s0: Space::empty(span),
s1: s0,
index,
s2: s1,
span,
};
(new.expr(), true) (new.expr(), true)
} }
Self::Assign { Self::Assign {
local: None, local: None,
s0, s0: _,
index, index,
s1, s1: _,
s2, s2: _,
s3, s3: _,
value, value,
span, span,
} => { } => {
let scope = Call::no_arg(Lit::Builtin(Builtin::Scope, span).expr().boxed(), span); let scope = Call::no_arg(Lit::Builtin(Builtin::Scope, span).expr().boxed(), span);
let new = Field::Assign { let new = Field::assign(scope.expr().boxed(), index, value, span);
expr: scope.expr().boxed(),
s0: Space::empty(span),
s1: s0,
index,
s2: s1,
s3: s2,
s4: s3,
value,
span,
};
(new.expr(), true) (new.expr(), true)
} }