Simplify creating TableConstrElem
This commit is contained in:
parent
c191486864
commit
58106c4c5a
7 changed files with 50 additions and 30 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::span::{HasSpan, Span};
|
use crate::span::{HasSpan, Span};
|
||||||
|
|
||||||
use super::{BoundedSeparated, Expr, Space, TableLitElem};
|
use super::{BoundedSeparated, Expr, Ident, Space, TableLitElem};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum TableConstrElem {
|
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 }`
|
/// `{ a, b, foo: c, [d]: e }`
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TableConstr(pub BoundedSeparated<TableConstrElem>);
|
pub struct TableConstr(pub BoundedSeparated<TableConstrElem>);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::ast::{
|
use crate::ast::{BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem};
|
||||||
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem,
|
|
||||||
};
|
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
|
|
||||||
impl Field {
|
impl Field {
|
||||||
|
|
@ -15,8 +13,8 @@ impl Field {
|
||||||
span,
|
span,
|
||||||
} => {
|
} => {
|
||||||
let constr = BoundedSeparated::new(span)
|
let constr = BoundedSeparated::new(span)
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
|
.then(TableConstrElem::positional(expr))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
.then(TableConstrElem::positional(index))
|
||||||
.table_constr();
|
.table_constr();
|
||||||
let new = Call::Constr {
|
let new = Call::Constr {
|
||||||
expr: Lit::Builtin(Builtin::Get, span).expr().boxed(),
|
expr: Lit::Builtin(Builtin::Get, span).expr().boxed(),
|
||||||
|
|
@ -39,9 +37,9 @@ impl Field {
|
||||||
span,
|
span,
|
||||||
} => {
|
} => {
|
||||||
let constr = BoundedSeparated::new(span)
|
let constr = BoundedSeparated::new(span)
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
|
.then(TableConstrElem::positional(expr))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
.then(TableConstrElem::positional(index))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
|
.then(TableConstrElem::positional(value))
|
||||||
.table_constr();
|
.table_constr();
|
||||||
let new = Call::Constr {
|
let new = Call::Constr {
|
||||||
expr: Lit::Builtin(Builtin::Set, span).expr().boxed(),
|
expr: Lit::Builtin(Builtin::Set, span).expr().boxed(),
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,10 @@ impl FuncDef {
|
||||||
} => {
|
} => {
|
||||||
let quote = BoundedSeparated::new(span)
|
let quote = BoundedSeparated::new(span)
|
||||||
.then(TableLitElem::named(Ident::new("quote", span), body, span))
|
.then(TableLitElem::named(Ident::new("quote", span), body, span))
|
||||||
.table_lit();
|
.table_lit()
|
||||||
let quote = quote.lit().expr().boxed();
|
.lit()
|
||||||
|
.expr()
|
||||||
|
.boxed();
|
||||||
let scope = Call::NoArg {
|
let scope = Call::NoArg {
|
||||||
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
|
expr: Lit::Builtin(Builtin::Scope, span).expr().boxed(),
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
|
|
@ -24,12 +26,12 @@ impl FuncDef {
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
let new = BoundedSeparated::new(span)
|
let new = BoundedSeparated::new(span)
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
|
.then(TableConstrElem::positional(quote))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::named(
|
.then(TableConstrElem::named(
|
||||||
Ident::new("scope", span),
|
Ident::new("scope", span),
|
||||||
scope.expr().boxed(),
|
scope.expr().boxed(),
|
||||||
span,
|
span,
|
||||||
)))
|
))
|
||||||
.table_constr()
|
.table_constr()
|
||||||
.expr();
|
.expr();
|
||||||
(new, true)
|
(new, true)
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ impl Lit {
|
||||||
match self {
|
match self {
|
||||||
Self::Table(table) => {
|
Self::Table(table) => {
|
||||||
let (table, desugared) = table.desugar();
|
let (table, desugared) = table.desugar();
|
||||||
(Self::Table(table), desugared)
|
(table.lit(), desugared)
|
||||||
}
|
}
|
||||||
|
|
||||||
lit => (lit, false),
|
lit => (lit, false),
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
|
||||||
pattern
|
pattern
|
||||||
.0
|
.0
|
||||||
.map(|e| match e {
|
.map(|e| match e {
|
||||||
TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional(
|
TablePatternElem::Positional(ident) => {
|
||||||
StringLit::from_ident(ident).lit().expr().boxed(),
|
TableConstrElem::positional(StringLit::from_ident(ident).lit().expr().boxed())
|
||||||
)),
|
}
|
||||||
|
|
||||||
TablePatternElem::Named {
|
TablePatternElem::Named {
|
||||||
name,
|
name,
|
||||||
|
|
@ -41,16 +41,16 @@ impl TableDestr {
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
let mut constr = BoundedSeparated::new(span)
|
let mut constr = BoundedSeparated::new(span)
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(
|
.then(TableConstrElem::positional(
|
||||||
pattern_to_constr(pattern).expr().boxed(),
|
pattern_to_constr(pattern).expr().boxed(),
|
||||||
)))
|
))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(value)));
|
.then(TableConstrElem::positional(value));
|
||||||
if local.is_some() {
|
if local.is_some() {
|
||||||
constr = constr.then(TableConstrElem::Lit(TableLitElem::named(
|
constr = constr.then(TableConstrElem::named(
|
||||||
Ident::new("local", span),
|
Ident::new("local", span),
|
||||||
Lit::Bool(true, span).expr().boxed(),
|
Lit::Bool(true, span).expr().boxed(),
|
||||||
span,
|
span,
|
||||||
)));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let new = Call::Constr {
|
let new = Call::Constr {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::ast::{
|
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::builtin::Builtin;
|
||||||
use crate::span::HasSpan;
|
use crate::span::HasSpan;
|
||||||
|
|
@ -81,11 +81,9 @@ impl Var {
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
let constr = BoundedSeparated::new(span)
|
let constr = BoundedSeparated::new(span)
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(
|
.then(TableConstrElem::positional(scope.expr().boxed()))
|
||||||
scope.expr().boxed(),
|
.then(TableConstrElem::positional(index))
|
||||||
)))
|
.then(TableConstrElem::positional(value))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(value)))
|
|
||||||
.table_constr();
|
.table_constr();
|
||||||
let new = Call::Constr {
|
let new = Call::Constr {
|
||||||
expr: Lit::Builtin(Builtin::SetRaw, span).expr().boxed(),
|
expr: Lit::Builtin(Builtin::SetRaw, span).expr().boxed(),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use pretty::{DocAllocator, DocBuilder, Pretty};
|
use pretty::{DocAllocator, DocBuilder, Pretty};
|
||||||
|
|
||||||
use crate::ast::{BoundedSeparated, Ident, Space};
|
use crate::ast::{BoundedSeparated, Ident};
|
||||||
|
|
||||||
use super::NEST_DEPTH;
|
use super::NEST_DEPTH;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue