Simplify creating TableConstrElem

This commit is contained in:
Joscha 2022-11-22 16:43:24 +01:00
parent c191486864
commit 58106c4c5a
7 changed files with 50 additions and 30 deletions

View file

@ -1,6 +1,6 @@
use crate::span::{HasSpan, Span};
use super::{BoundedSeparated, Expr, Space, TableLitElem};
use super::{BoundedSeparated, Expr, Ident, Space, TableLitElem};
#[derive(Debug, Clone)]
pub enum TableConstrElem {
@ -30,6 +30,28 @@ impl HasSpan for TableConstrElem {
}
}
impl TableConstrElem {
pub fn positional(value: Box<Expr>) -> Self {
Self::Lit(TableLitElem::Positional(value))
}
pub fn named(name: Ident, value: Box<Expr>, span: Span) -> Self {
Self::Lit(TableLitElem::named(name, value, span))
}
pub fn indexed(index: Box<Expr>, value: Box<Expr>, span: Span) -> Self {
Self::Indexed {
s0: Space::empty(span),
index,
s1: Space::empty(span),
s2: Space::empty(span),
s3: Space::empty(span),
value,
span,
}
}
}
/// `{ a, b, foo: c, [d]: e }`
#[derive(Debug, Clone)]
pub struct TableConstr(pub BoundedSeparated<TableConstrElem>);

View file

@ -1,6 +1,4 @@
use crate::ast::{
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem,
};
use crate::ast::{BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem};
use crate::builtin::Builtin;
impl Field {
@ -15,8 +13,8 @@ impl Field {
span,
} => {
let constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.then(TableConstrElem::positional(expr))
.then(TableConstrElem::positional(index))
.table_constr();
let new = Call::Constr {
expr: Lit::Builtin(Builtin::Get, span).expr().boxed(),
@ -39,9 +37,9 @@ impl Field {
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::positional(expr))
.then(TableConstrElem::positional(index))
.then(TableConstrElem::positional(value))
.table_constr();
let new = Call::Constr {
expr: Lit::Builtin(Builtin::Set, span).expr().boxed(),

View file

@ -15,8 +15,10 @@ impl FuncDef {
} => {
let quote = BoundedSeparated::new(span)
.then(TableLitElem::named(Ident::new("quote", span), body, span))
.table_lit();
let quote = quote.lit().expr().boxed();
.table_lit()
.lit()
.expr()
.boxed();
let scope = Call::NoArg {
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
s0: Space::empty(span),
@ -24,12 +26,12 @@ impl FuncDef {
span,
};
let new = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
.then(TableConstrElem::Lit(TableLitElem::named(
.then(TableConstrElem::positional(quote))
.then(TableConstrElem::named(
Ident::new("scope", span),
scope.expr().boxed(),
span,
)))
))
.table_constr()
.expr();
(new, true)

View file

@ -41,7 +41,7 @@ impl Lit {
match self {
Self::Table(table) => {
let (table, desugared) = table.desugar();
(Self::Table(table), desugared)
(table.lit(), desugared)
}
lit => (lit, false),

View file

@ -8,9 +8,9 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
pattern
.0
.map(|e| match e {
TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional(
StringLit::from_ident(ident).lit().expr().boxed(),
)),
TablePatternElem::Positional(ident) => {
TableConstrElem::positional(StringLit::from_ident(ident).lit().expr().boxed())
}
TablePatternElem::Named {
name,
@ -41,16 +41,16 @@ impl TableDestr {
} = self;
let mut constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(
.then(TableConstrElem::positional(
pattern_to_constr(pattern).expr().boxed(),
)))
.then(TableConstrElem::Lit(TableLitElem::Positional(value)));
))
.then(TableConstrElem::positional(value));
if local.is_some() {
constr = constr.then(TableConstrElem::Lit(TableLitElem::named(
constr = constr.then(TableConstrElem::named(
Ident::new("local", span),
Lit::Bool(true, span).expr().boxed(),
span,
)));
));
}
let new = Call::Constr {

View file

@ -1,5 +1,5 @@
use crate::ast::{
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem, Var,
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, Var,
};
use crate::builtin::Builtin;
use crate::span::HasSpan;
@ -81,11 +81,9 @@ impl Var {
span,
};
let constr = BoundedSeparated::new(span)
.then(TableConstrElem::Lit(TableLitElem::Positional(
scope.expr().boxed(),
)))
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
.then(TableConstrElem::positional(scope.expr().boxed()))
.then(TableConstrElem::positional(index))
.then(TableConstrElem::positional(value))
.table_constr();
let new = Call::Constr {
expr: Lit::Builtin(Builtin::SetRaw, span).expr().boxed(),

View file

@ -1,6 +1,6 @@
use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::{BoundedSeparated, Ident, Space};
use crate::ast::{BoundedSeparated, Ident};
use super::NEST_DEPTH;