Simplify creating Var
This commit is contained in:
parent
6f7683ad1e
commit
40c28d9496
4 changed files with 55 additions and 34 deletions
|
|
@ -58,6 +58,51 @@ impl HasSpan for Var {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Var {
|
impl Var {
|
||||||
|
pub fn access(index: Box<Expr>, span: Span) -> Self {
|
||||||
|
Self::Access {
|
||||||
|
s0: Space::empty(span),
|
||||||
|
index,
|
||||||
|
s1: Space::empty(span),
|
||||||
|
span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn assign(local: bool, index: Box<Expr>, value: Box<Expr>, span: Span) -> Self {
|
||||||
|
let local = if local {
|
||||||
|
Some(Space::empty(span))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
Self::Assign {
|
||||||
|
local,
|
||||||
|
s0: Space::empty(span),
|
||||||
|
index,
|
||||||
|
s1: Space::empty(span),
|
||||||
|
s2: Space::empty(span),
|
||||||
|
s3: Space::empty(span),
|
||||||
|
value,
|
||||||
|
span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn assign_ident(local: bool, name: Ident, value: Box<Expr>, span: Span) -> Self {
|
||||||
|
let local = if local {
|
||||||
|
Some(Space::empty(span))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
Self::AssignIdent {
|
||||||
|
local,
|
||||||
|
name,
|
||||||
|
s0: Space::empty(span),
|
||||||
|
s1: Space::empty(span),
|
||||||
|
value,
|
||||||
|
span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn expr(self) -> Expr {
|
pub fn expr(self) -> Expr {
|
||||||
Expr::Var(self)
|
Expr::Var(self)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ impl Expr {
|
||||||
match self {
|
match self {
|
||||||
Self::Lit(lit) => {
|
Self::Lit(lit) => {
|
||||||
let (lit, desugared) = lit.desugar();
|
let (lit, desugared) = lit.desugar();
|
||||||
(Self::Lit(lit), desugared)
|
(lit.expr(), desugared)
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::Call(call) => call.desugar(),
|
Self::Call(call) => call.desugar(),
|
||||||
|
|
|
||||||
|
|
@ -37,17 +37,8 @@ impl FuncDef {
|
||||||
body,
|
body,
|
||||||
span,
|
span,
|
||||||
} => {
|
} => {
|
||||||
// `function s0 ( s1 arg s2 ) s3 body`
|
|
||||||
// -> `function ( ) '{ local arg = 'arg(), body }`
|
|
||||||
let arg_call = Call::no_arg(Lit::Builtin(Builtin::Arg, span).expr().boxed(), span);
|
let arg_call = Call::no_arg(Lit::Builtin(Builtin::Arg, span).expr().boxed(), span);
|
||||||
let arg_assign = Var::AssignIdent {
|
let arg_assign = Var::assign_ident(true, arg, arg_call.expr().boxed(), span);
|
||||||
local: Some(Space::empty(span)),
|
|
||||||
name: arg,
|
|
||||||
s0: Space::empty(span),
|
|
||||||
s1: Space::empty(span),
|
|
||||||
value: arg_call.expr().boxed(),
|
|
||||||
span,
|
|
||||||
};
|
|
||||||
let body = BoundedSeparated::new(span)
|
let body = BoundedSeparated::new(span)
|
||||||
.then(TableLitElem::Positional(arg_assign.expr().boxed()))
|
.then(TableLitElem::Positional(arg_assign.expr().boxed()))
|
||||||
.then(TableLitElem::Positional(body))
|
.then(TableLitElem::Positional(body))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::ast::{
|
use crate::ast::{BoundedSeparated, Call, Expr, Field, Lit, StringLit, TableConstrElem, 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;
|
||||||
|
|
||||||
|
|
@ -58,38 +56,25 @@ impl Var {
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::AccessIdent(name) => {
|
Self::AccessIdent(name) => {
|
||||||
// `name`
|
|
||||||
// -> `[ name_str ]`
|
|
||||||
let span = name.span();
|
let span = name.span();
|
||||||
let new = Self::Access {
|
let new = Self::access(StringLit::from_ident(name).lit().expr().boxed(), span);
|
||||||
s0: Space::empty(span),
|
|
||||||
index: StringLit::from_ident(name).lit().expr().boxed(),
|
|
||||||
s1: Space::empty(span),
|
|
||||||
span,
|
|
||||||
};
|
|
||||||
(new.expr(), true)
|
(new.expr(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::AssignIdent {
|
Self::AssignIdent {
|
||||||
local,
|
local,
|
||||||
name,
|
name,
|
||||||
s0,
|
s0: _,
|
||||||
s1,
|
s1: _,
|
||||||
value,
|
value,
|
||||||
span,
|
span,
|
||||||
} => {
|
} => {
|
||||||
// `local name s0 = s1 value`
|
let new = Self::assign(
|
||||||
// -> `local [ name_str ] s0 = s1 value`
|
local.is_some(),
|
||||||
let new = Self::Assign {
|
StringLit::from_ident(name).lit().expr().boxed(),
|
||||||
local,
|
|
||||||
s0: Space::empty(span),
|
|
||||||
index: StringLit::from_ident(name).lit().expr().boxed(),
|
|
||||||
s1: Space::empty(span),
|
|
||||||
s2: s0,
|
|
||||||
s3: s1,
|
|
||||||
value,
|
value,
|
||||||
span,
|
span,
|
||||||
};
|
);
|
||||||
(new.expr(), true)
|
(new.expr(), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue