Simplify creating TableLit and TableConstr
This commit is contained in:
parent
c769d9e16f
commit
5a977e6dde
9 changed files with 83 additions and 68 deletions
|
|
@ -2,6 +2,8 @@ use std::fmt;
|
||||||
|
|
||||||
use crate::span::{HasSpan, Span};
|
use crate::span::{HasSpan, Span};
|
||||||
|
|
||||||
|
use super::{TableConstr, TableConstrElem, TableLit, TableLitElem};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum Line {
|
pub enum Line {
|
||||||
Empty,
|
Empty,
|
||||||
|
|
@ -105,3 +107,15 @@ impl<E> BoundedSeparated<E> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl BoundedSeparated<TableLitElem> {
|
||||||
|
pub fn table_lit(self) -> TableLit {
|
||||||
|
TableLit(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BoundedSeparated<TableConstrElem> {
|
||||||
|
pub fn table_constr(self) -> TableConstr {
|
||||||
|
TableConstr(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
// TODO Add span for just the parentheses to ast, or limit span to parentheses
|
||||||
|
|
||||||
|
|
@ -27,9 +27,9 @@ impl Call {
|
||||||
value: arg,
|
value: arg,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
let new = Expr::Lit(Lit::Table(TableLit(
|
let new = Expr::Lit(Lit::Table(
|
||||||
BoundedSeparated::new(span).then(call).then(arg),
|
BoundedSeparated::new(span).then(call).then(arg).table_lit(),
|
||||||
)));
|
));
|
||||||
(new, true)
|
(new, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstr, TableConstrElem,
|
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem,
|
||||||
TableLitElem,
|
|
||||||
};
|
};
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
|
|
||||||
|
|
@ -15,11 +14,10 @@ impl Field {
|
||||||
s2: _,
|
s2: _,
|
||||||
span,
|
span,
|
||||||
} => {
|
} => {
|
||||||
let constr = TableConstr(
|
let constr = BoundedSeparated::new(span)
|
||||||
BoundedSeparated::new(span)
|
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
|
||||||
.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 {
|
let new = Expr::Call(Call::Constr {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))),
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
|
|
@ -40,12 +38,11 @@ impl Field {
|
||||||
value,
|
value,
|
||||||
span,
|
span,
|
||||||
} => {
|
} => {
|
||||||
let constr = TableConstr(
|
let constr = BoundedSeparated::new(span)
|
||||||
BoundedSeparated::new(span)
|
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(expr)))
|
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
||||||
.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 {
|
let new = Expr::Call(Call::Constr {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))),
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstr, TableConstrElem,
|
BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstrElem, TableLitElem, Var,
|
||||||
TableLit, TableLitElem, Var,
|
|
||||||
};
|
};
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
|
|
||||||
|
|
@ -14,13 +13,15 @@ impl FuncDef {
|
||||||
body,
|
body,
|
||||||
span,
|
span,
|
||||||
} => {
|
} => {
|
||||||
let quote = TableLit(BoundedSeparated::new(span).then(TableLitElem::Named {
|
let quote = BoundedSeparated::new(span)
|
||||||
name: Ident::new("quote", span),
|
.then(TableLitElem::Named {
|
||||||
s0: Space::empty(span),
|
name: Ident::new("quote", span),
|
||||||
s1: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
value: body,
|
s1: Space::empty(span),
|
||||||
span,
|
value: body,
|
||||||
}));
|
span,
|
||||||
|
})
|
||||||
|
.table_lit();
|
||||||
let quote = Box::new(Expr::Lit(Lit::Table(quote)));
|
let quote = Box::new(Expr::Lit(Lit::Table(quote)));
|
||||||
let scope = Expr::Call(Call::NoArg {
|
let scope = Expr::Call(Call::NoArg {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Scope, span))),
|
||||||
|
|
@ -28,7 +29,7 @@ impl FuncDef {
|
||||||
s1: Space::empty(span),
|
s1: Space::empty(span),
|
||||||
span,
|
span,
|
||||||
});
|
});
|
||||||
let new = Expr::TableConstr(TableConstr(
|
let new = Expr::TableConstr(
|
||||||
BoundedSeparated::new(span)
|
BoundedSeparated::new(span)
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
|
.then(TableConstrElem::Lit(TableLitElem::Positional(quote)))
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Named {
|
.then(TableConstrElem::Lit(TableLitElem::Named {
|
||||||
|
|
@ -37,8 +38,9 @@ impl FuncDef {
|
||||||
s1: Space::empty(span),
|
s1: Space::empty(span),
|
||||||
value: Box::new(scope),
|
value: Box::new(scope),
|
||||||
span,
|
span,
|
||||||
})),
|
}))
|
||||||
));
|
.table_constr(),
|
||||||
|
);
|
||||||
(new, true)
|
(new, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,12 +71,13 @@ impl FuncDef {
|
||||||
});
|
});
|
||||||
let body = BoundedSeparated::new(span)
|
let body = BoundedSeparated::new(span)
|
||||||
.then(TableLitElem::Positional(Box::new(arg_assign)))
|
.then(TableLitElem::Positional(Box::new(arg_assign)))
|
||||||
.then(TableLitElem::Positional(body));
|
.then(TableLitElem::Positional(body))
|
||||||
|
.table_lit();
|
||||||
let new = Expr::FuncDef(Self::AnonNoArg {
|
let new = Expr::FuncDef(Self::AnonNoArg {
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
s1: Space::empty(span),
|
s1: Space::empty(span),
|
||||||
s2: 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,
|
span,
|
||||||
});
|
});
|
||||||
(new, true)
|
(new, true)
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ impl TableLitElem {
|
||||||
impl TableLit {
|
impl TableLit {
|
||||||
pub fn desugar(self) -> (Self, bool) {
|
pub fn desugar(self) -> (Self, bool) {
|
||||||
let (elems, desugared) = self.0.desugar(|e| e.desugar());
|
let (elems, desugared) = self.0.desugar(|e| e.desugar());
|
||||||
(Self(elems), desugared)
|
(elems.table_lit(), desugared)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::ast::{Expr, Lit, Program, Space, TableLit};
|
use crate::ast::{Expr, Lit, Program, Space};
|
||||||
|
|
||||||
impl Program {
|
impl Program {
|
||||||
pub fn desugar(self) -> (Self, bool) {
|
pub fn desugar(self) -> (Self, bool) {
|
||||||
|
|
@ -14,7 +14,7 @@ impl Program {
|
||||||
// -> `s0 table`
|
// -> `s0 table`
|
||||||
let new = Self::Expr {
|
let new = Self::Expr {
|
||||||
s0,
|
s0,
|
||||||
expr: Expr::Lit(Lit::Table(TableLit(elems))),
|
expr: Expr::Lit(Lit::Table(elems.table_lit())),
|
||||||
s1: Space::empty(span),
|
s1: Space::empty(span),
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
BoundedSeparated, Expr, Field, Ident, Line, Lit, Space, TableConstr, TableConstrElem, TableLit,
|
BoundedSeparated, Expr, Field, Ident, Line, Lit, Space, TableConstr, TableConstrElem,
|
||||||
TableLitElem,
|
TableLitElem,
|
||||||
};
|
};
|
||||||
use crate::span::HasSpan;
|
use crate::span::HasSpan;
|
||||||
|
|
@ -25,12 +25,12 @@ impl TableConstr {
|
||||||
name: Ident::new("raw", span),
|
name: Ident::new("raw", span),
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
s1: 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,
|
span,
|
||||||
};
|
};
|
||||||
let mut expr = Expr::Lit(Lit::Table(TableLit(
|
let mut expr = Expr::Lit(Lit::Table(
|
||||||
BoundedSeparated::new(span).then(raw_elem),
|
BoundedSeparated::new(span).then(raw_elem).table_lit(),
|
||||||
)));
|
));
|
||||||
|
|
||||||
// `sl [ s0 index s1 ] s2 = s3 value sr`
|
// `sl [ s0 index s1 ] s2 = s3 value sr`
|
||||||
// -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value`
|
// -> `expr s0 [ s1 index s2 ] s3 = s4 s5 value`
|
||||||
|
|
|
||||||
|
|
@ -5,25 +5,28 @@ use crate::ast::{
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
|
|
||||||
fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
|
fn pattern_to_constr(pattern: TablePattern) -> TableConstr {
|
||||||
TableConstr(pattern.0.map(|e| match e {
|
pattern
|
||||||
TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional(
|
.0
|
||||||
Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))),
|
.map(|e| match e {
|
||||||
)),
|
TablePatternElem::Positional(ident) => TableConstrElem::Lit(TableLitElem::Positional(
|
||||||
|
Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))),
|
||||||
|
)),
|
||||||
|
|
||||||
TablePatternElem::Named {
|
TablePatternElem::Named {
|
||||||
name,
|
name,
|
||||||
s0,
|
s0,
|
||||||
s1,
|
s1,
|
||||||
ident,
|
ident,
|
||||||
span,
|
span,
|
||||||
} => TableConstrElem::Lit(TableLitElem::Named {
|
} => TableConstrElem::Lit(TableLitElem::Named {
|
||||||
name,
|
name,
|
||||||
s0,
|
s0,
|
||||||
s1,
|
s1,
|
||||||
value: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))),
|
value: Box::new(Expr::Lit(Lit::String(StringLit::from_ident(ident)))),
|
||||||
span,
|
span,
|
||||||
}),
|
}),
|
||||||
}))
|
})
|
||||||
|
.table_constr()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TableDestr {
|
impl TableDestr {
|
||||||
|
|
@ -55,7 +58,7 @@ impl TableDestr {
|
||||||
let new = Expr::Call(Call::Constr {
|
let new = Expr::Call(Call::Constr {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Destructure, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Destructure, span))),
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
constr: TableConstr(constr),
|
constr: constr.table_constr(),
|
||||||
span,
|
span,
|
||||||
});
|
});
|
||||||
(new, true)
|
(new, true)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstr, TableConstrElem,
|
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, TableLitElem, Var,
|
||||||
TableLitElem, Var,
|
|
||||||
};
|
};
|
||||||
use crate::builtin::Builtin;
|
use crate::builtin::Builtin;
|
||||||
use crate::span::HasSpan;
|
use crate::span::HasSpan;
|
||||||
|
|
@ -81,14 +80,13 @@ impl Var {
|
||||||
s1: Space::empty(span),
|
s1: Space::empty(span),
|
||||||
span,
|
span,
|
||||||
});
|
});
|
||||||
let constr = TableConstr(
|
let constr = BoundedSeparated::new(span)
|
||||||
BoundedSeparated::new(span)
|
.then(TableConstrElem::Lit(TableLitElem::Positional(Box::new(
|
||||||
.then(TableConstrElem::Lit(TableLitElem::Positional(Box::new(
|
scope,
|
||||||
scope,
|
))))
|
||||||
))))
|
.then(TableConstrElem::Lit(TableLitElem::Positional(index)))
|
||||||
.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 {
|
let new = Expr::Call(Call::Constr {
|
||||||
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))),
|
expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))),
|
||||||
s0: Space::empty(span),
|
s0: Space::empty(span),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue