Desugar table constructors
This commit is contained in:
parent
74d1f640b5
commit
af6c171eb4
4 changed files with 80 additions and 4 deletions
|
|
@ -4,4 +4,5 @@ mod expr;
|
||||||
mod field;
|
mod field;
|
||||||
mod lit;
|
mod lit;
|
||||||
mod program;
|
mod program;
|
||||||
|
mod table_constr;
|
||||||
mod var;
|
mod var;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::ast::BoundedSeparated;
|
use crate::ast::{BoundedSeparated, Space};
|
||||||
|
|
||||||
impl<E> BoundedSeparated<E> {
|
impl<E> BoundedSeparated<E> {
|
||||||
pub fn desugar(self, desugar_elem: impl Fn(E) -> (E, bool)) -> (Self, bool) {
|
pub fn desugar(self, desugar_elem: impl Fn(E) -> (E, bool)) -> (Self, bool) {
|
||||||
|
|
@ -21,4 +21,24 @@ impl<E> BoundedSeparated<E> {
|
||||||
};
|
};
|
||||||
(new, desugared)
|
(new, desugared)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove_map<E1, E2>(
|
||||||
|
self,
|
||||||
|
f: impl Fn(E) -> Result<E1, E2>,
|
||||||
|
) -> (BoundedSeparated<E1>, Vec<(Space, E2, Space)>) {
|
||||||
|
let mut kept = vec![];
|
||||||
|
let mut removed = vec![];
|
||||||
|
for (s0, elem, s1) in self.elems {
|
||||||
|
match f(elem) {
|
||||||
|
Ok(elem) => kept.push((s0, elem, s1)),
|
||||||
|
Err(elem) => removed.push((s0, elem, s1)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let new = BoundedSeparated {
|
||||||
|
elems: kept,
|
||||||
|
trailing: self.trailing,
|
||||||
|
span: self.span,
|
||||||
|
};
|
||||||
|
(new, removed)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,10 @@ impl Expr {
|
||||||
Self::Call(call) => call.desugar(),
|
Self::Call(call) => call.desugar(),
|
||||||
Self::Field(field) => field.desugar(),
|
Self::Field(field) => field.desugar(),
|
||||||
Self::Var(var) => var.desugar(),
|
Self::Var(var) => var.desugar(),
|
||||||
|
Self::TableConstr(constr) => constr.desugar(),
|
||||||
|
|
||||||
Self::TableConstr(constr) => (Self::TableConstr(constr), false), // TODO Implement
|
Self::TableDestr(destr) => (Self::TableDestr(destr), false), // TODO Implement
|
||||||
Self::TableDestr(destr) => (Self::TableDestr(destr), false), // TODO Implement
|
Self::FuncDef(def) => (Self::FuncDef(def), false), // TODO Implement
|
||||||
Self::FuncDef(def) => (Self::FuncDef(def), false), // TODO Implement
|
|
||||||
|
|
||||||
Self::Paren {
|
Self::Paren {
|
||||||
s0,
|
s0,
|
||||||
|
|
|
||||||
55
src/desugar/table_constr.rs
Normal file
55
src/desugar/table_constr.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
use crate::ast::{
|
||||||
|
BoundedSeparated, Expr, Field, Ident, Line, Lit, Space, TableConstr, TableConstrElem, TableLit,
|
||||||
|
TableLitElem,
|
||||||
|
};
|
||||||
|
use crate::span::HasSpan;
|
||||||
|
|
||||||
|
impl TableConstr {
|
||||||
|
pub fn desugar(self) -> (Expr, bool) {
|
||||||
|
let span = self.span();
|
||||||
|
|
||||||
|
let (elems, setters) = self.0.remove_map(|e| match e {
|
||||||
|
TableConstrElem::Lit(lit) => Ok(lit),
|
||||||
|
TableConstrElem::Indexed {
|
||||||
|
s0,
|
||||||
|
index,
|
||||||
|
s1,
|
||||||
|
s2,
|
||||||
|
s3,
|
||||||
|
value,
|
||||||
|
span,
|
||||||
|
} => Err((s0, index, s1, s2, s3, value, span)),
|
||||||
|
});
|
||||||
|
|
||||||
|
let raw_elem = TableLitElem::Named {
|
||||||
|
name: Ident::new("raw", span),
|
||||||
|
s0: Space::empty(span),
|
||||||
|
s1: Space::empty(span),
|
||||||
|
value: Box::new(Expr::Lit(Lit::Table(TableLit(elems)))),
|
||||||
|
span,
|
||||||
|
};
|
||||||
|
let mut expr = Expr::Lit(Lit::Table(TableLit(BoundedSeparated {
|
||||||
|
elems: vec![(Space::empty(span), raw_elem, Space::empty(span))],
|
||||||
|
trailing: None,
|
||||||
|
span,
|
||||||
|
})));
|
||||||
|
|
||||||
|
// `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: Box::new(expr),
|
||||||
|
s0,
|
||||||
|
s1,
|
||||||
|
index,
|
||||||
|
s2,
|
||||||
|
s3,
|
||||||
|
s4: s4.then_line(Line::Empty).then(s5),
|
||||||
|
value,
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
(expr, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue