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 {
|
||||
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 {
|
||||
Expr::Var(self)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ impl Expr {
|
|||
match self {
|
||||
Self::Lit(lit) => {
|
||||
let (lit, desugared) = lit.desugar();
|
||||
(Self::Lit(lit), desugared)
|
||||
(lit.expr(), desugared)
|
||||
}
|
||||
|
||||
Self::Call(call) => call.desugar(),
|
||||
|
|
|
|||
|
|
@ -37,17 +37,8 @@ impl FuncDef {
|
|||
body,
|
||||
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_assign = Var::AssignIdent {
|
||||
local: Some(Space::empty(span)),
|
||||
name: arg,
|
||||
s0: Space::empty(span),
|
||||
s1: Space::empty(span),
|
||||
value: arg_call.expr().boxed(),
|
||||
span,
|
||||
};
|
||||
let arg_assign = Var::assign_ident(true, arg, arg_call.expr().boxed(), span);
|
||||
let body = BoundedSeparated::new(span)
|
||||
.then(TableLitElem::Positional(arg_assign.expr().boxed()))
|
||||
.then(TableLitElem::Positional(body))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
use crate::ast::{
|
||||
BoundedSeparated, Call, Expr, Field, Lit, Space, StringLit, TableConstrElem, Var,
|
||||
};
|
||||
use crate::ast::{BoundedSeparated, Call, Expr, Field, Lit, StringLit, TableConstrElem, Var};
|
||||
use crate::builtin::Builtin;
|
||||
use crate::span::HasSpan;
|
||||
|
||||
|
|
@ -58,38 +56,25 @@ impl Var {
|
|||
}
|
||||
|
||||
Self::AccessIdent(name) => {
|
||||
// `name`
|
||||
// -> `[ name_str ]`
|
||||
let span = name.span();
|
||||
let new = Self::Access {
|
||||
s0: Space::empty(span),
|
||||
index: StringLit::from_ident(name).lit().expr().boxed(),
|
||||
s1: Space::empty(span),
|
||||
span,
|
||||
};
|
||||
let new = Self::access(StringLit::from_ident(name).lit().expr().boxed(), span);
|
||||
(new.expr(), true)
|
||||
}
|
||||
|
||||
Self::AssignIdent {
|
||||
local,
|
||||
name,
|
||||
s0,
|
||||
s1,
|
||||
s0: _,
|
||||
s1: _,
|
||||
value,
|
||||
span,
|
||||
} => {
|
||||
// `local name s0 = s1 value`
|
||||
// -> `local [ name_str ] s0 = s1 value`
|
||||
let new = Self::Assign {
|
||||
local,
|
||||
s0: Space::empty(span),
|
||||
index: StringLit::from_ident(name).lit().expr().boxed(),
|
||||
s1: Space::empty(span),
|
||||
s2: s0,
|
||||
s3: s1,
|
||||
let new = Self::assign(
|
||||
local.is_some(),
|
||||
StringLit::from_ident(name).lit().expr().boxed(),
|
||||
value,
|
||||
span,
|
||||
};
|
||||
);
|
||||
(new.expr(), true)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue