Simplify creating TableLit and TableConstr

This commit is contained in:
Joscha 2022-11-22 15:21:29 +01:00
parent c769d9e16f
commit 5a977e6dde
9 changed files with 83 additions and 68 deletions

View file

@ -2,6 +2,8 @@ use std::fmt;
use crate::span::{HasSpan, Span};
use super::{TableConstr, TableConstrElem, TableLit, TableLitElem};
#[derive(Clone)]
pub enum Line {
Empty,
@ -105,3 +107,15 @@ impl<E> BoundedSeparated<E> {
self
}
}
impl BoundedSeparated<TableLitElem> {
pub fn table_lit(self) -> TableLit {
TableLit(self)
}
}
impl BoundedSeparated<TableConstrElem> {
pub fn table_constr(self) -> TableConstr {
TableConstr(self)
}
}

View file

@ -1,4 +1,4 @@
use crate::ast::{BoundedSeparated, Call, Expr, Ident, Lit, Space, TableLit, TableLitElem};
use crate::ast::{BoundedSeparated, Call, Expr, Ident, Lit, Space, TableLitElem};
// TODO Add span for just the parentheses to ast, or limit span to parentheses
@ -27,9 +27,9 @@ impl Call {
value: arg,
span,
};
let new = Expr::Lit(Lit::Table(TableLit(
BoundedSeparated::new(span).then(call).then(arg),
)));
let new = Expr::Lit(Lit::Table(
BoundedSeparated::new(span).then(call).then(arg).table_lit(),
));
(new, true)
}

View file

@ -1,6 +1,5 @@
use crate::ast::{
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstr, TableConstrElem,
TableLitElem,
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem,
};
use crate::builtin::Builtin;
@ -15,11 +14,10 @@ impl Field {
s2: _,
span,
} => {
let constr = TableConstr(
BoundedSeparated::new(span)
let constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
.then(TableConstrElem::Lit(TableLitElem::Positional(index))),
);
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.table_constr();
let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))),
s0: Space::empty(span),
@ -40,12 +38,11 @@ impl Field {
value,
span,
} => {
let constr = TableConstr(
BoundedSeparated::new(span)
let constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.then(TableConstrElem::Lit(TableLitElem::Positional(value))),
);
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
.table_constr();
let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))),
s0: Space::empty(span),

View file

@ -1,6 +1,5 @@
use crate::ast::{
BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstr, TableConstrElem,
TableLit, TableLitElem, Var,
BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstrElem, TableLitElem, Var,
};
use crate::builtin::Builtin;
@ -14,13 +13,15 @@ impl FuncDef {
body,
span,
} => {
let quote = TableLit(BoundedSeparated::new(span).then(TableLitElem::Named {
let quote = BoundedSeparated::new(span)
.then(TableLitElem::Named {
name: Ident::new("quote", span),
s0: Space::empty(span),
s1: Space::empty(span),
value: body,
span,
}));
})
.table_lit();
let quote = Box::new(Expr::Lit(Lit::Table(quote)));
let scope = Expr::Call(Call::NoArg {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))),
@ -28,7 +29,7 @@ impl FuncDef {
s1: Space::empty(span),
span,
});
let new = Expr::TableConstr(TableConstr(
let new = Expr::TableConstr(
BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
.then(TableConstrElem::Lit(TableLitElem::Named {
@ -37,8 +38,9 @@ impl FuncDef {
s1: Space::empty(span),
value: Box::new(scope),
span,
})),
));
}))
.table_constr(),
);
(new, true)
}
@ -69,12 +71,13 @@ impl FuncDef {
});
let body = BoundedSeparated::new(span)
.then(TableLitElem::Positional(Box::new(arg_assign)))
.then(TableLitElem::Positional(body));
.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(TableLit(body)))),
body: Box::new(Expr::Lit(Lit::Table(body))),
span,
});
(new, true)

View file

@ -32,7 +32,7 @@ impl TableLitElem {
impl TableLit {
pub fn desugar(self) -> (Self, bool) {
let (elems, desugared) = self.0.desugar(|e| e.desugar());
(Self(elems), desugared)
(elems.table_lit(), desugared)
}
}

View file

@ -1,4 +1,4 @@
use crate::ast::{Expr, Lit, Program, Space, TableLit};
use crate::ast::{Expr, Lit, Program, Space};
impl Program {
pub fn desugar(self) -> (Self, bool) {
@ -14,7 +14,7 @@ impl Program {
// -> `s0 table`
let new = Self::Expr {
s0,
expr: Expr::Lit(Lit::Table(TableLit(elems))),
expr: Expr::Lit(Lit::Table(elems.table_lit())),
s1: Space::empty(span),
span,
};

View file

@ -1,5 +1,5 @@
use crate::ast::{
BoundedSeparated, Expr, Field, Ident, Line, Lit, Space, TableConstr, TableConstrElem, TableLit,
BoundedSeparated, Expr, Field, Ident, Line, Lit, Space, TableConstr, TableConstrElem,
TableLitElem,
};
use crate::span::HasSpan;
@ -25,12 +25,12 @@ impl TableConstr {
name: Ident::new("raw", span),
s0: Space::empty(span),
s1: Space::empty(span),
value: Box::new(Expr::Lit(Lit::Table(TableLit(elems)))),
value: Box::new(Expr::Lit(Lit::Table(elems.table_lit()))),
span,
};
let mut expr = Expr::Lit(Lit::Table(TableLit(
BoundedSeparated::new(span).then(raw_elem),
)));
let mut expr = Expr::Lit(Lit::Table(
BoundedSeparated::new(span).then(raw_elem).table_lit(),
));
// `sl [ s0 index s1 ] s2 = s3 value sr`
// -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value`

View file

@ -5,7 +5,9 @@ use crate::ast::{
use crate::builtin::Builtin;
fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
TableConstr(pattern.0.map(|e| match e {
pattern
.0
.map(|e| match e {
TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional(
Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))),
)),
@ -23,7 +25,8 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
value: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))),
span,
}),
}))
})
.table_constr()
}
impl TableDestr {
@ -55,7 +58,7 @@ impl TableDestr {
let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Destructure, span))),
s0: Space::empty(span),
constr: TableConstr(constr),
constr: constr.table_constr(),
span,
});
(new, true)

View file

@ -1,6 +1,5 @@
use crate::ast::{
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstr, TableConstrElem,
TableLitElem, Var,
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem, Var,
};
use crate::builtin::Builtin;
use crate::span::HasSpan;
@ -81,14 +80,13 @@ impl Var {
s1: Space::empty(span),
span,
});
let constr = TableConstr(
BoundedSeparated::new(span)
let constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(Box::new(
scope,
))))
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.then(TableConstrElem::Lit(TableLitElem::Positional(value))),
);
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
.table_constr();
let new = Expr::Call(Call::Constr {
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))),
s0: Space::empty(span),