Skip to content

Commit 4704859

Browse files
committed
Change ExprKind::Function and ExprKind::Lambda to Ident. Part of #8
1 parent 6567ce6 commit 4704859

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct Expr {
99
}
1010

1111
#[derive(Clone, Debug, PartialEq)]
12-
pub struct Ident(String);
12+
pub struct Ident(pub String);
1313

1414
impl From<Ident> for String {
1515
fn from(ident: Ident) -> Self {
@@ -37,9 +37,9 @@ pub enum ExprKind {
3737
Unop(String, Box<Expr>),
3838
Access(Box<Expr>, Ident),
3939
Ident(Ident),
40-
Function(Option<String>, Vec<String>, Box<Expr>),
40+
Function(Option<Ident>, Vec<Ident>, Box<Expr>),
4141
Class(String, Box<Expr>, Option<Box<Expr>>),
42-
Lambda(Vec<String>, Box<Expr>),
42+
Lambda(Vec<Ident>, Box<Expr>),
4343
Match(Box<Expr>, Vec<(Box<Expr>, Box<Expr>)>, Option<Box<Expr>>),
4444
If(Box<Expr>, Box<Expr>, Option<Box<Expr>>),
4545
ConstInt(i64),

src/codegen.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,9 @@ impl Context {
654654

655655
pub fn compile_function(
656656
&mut self,
657-
params: &[String],
657+
params: &[Ident],
658658
e: &Box<Expr>,
659-
vname: Option<String>,
659+
vname: Option<Ident>,
660660
) -> u32 {
661661
let mut ctx = Context {
662662
g: self.g.clone(),
@@ -681,15 +681,15 @@ impl Context {
681681
ctx.stack += 1;
682682
let r = ctx.new_reg();
683683
ctx.write(Instruction::Pop(r));
684-
ctx.locals.insert(p.to_owned(), r as _);
684+
ctx.locals.insert(String::from(p.to_owned()), r as _);
685685
}
686686

687687
let gid = ctx.g.borrow().table.len();
688688
if vname.is_some() {
689-
ctx.g
690-
.borrow_mut()
691-
.globals
692-
.insert(Global::Var(vname.as_ref().unwrap().to_owned()), gid as i32);
689+
ctx.g.borrow_mut().globals.insert(
690+
Global::Var(String::from(vname.as_ref().unwrap().to_owned())),
691+
gid as i32,
692+
);
693693
}
694694
ctx.g.borrow_mut().table.push(Global::Func(gid as i32, -1));
695695
let r = ctx.compile(e, true);
@@ -704,7 +704,9 @@ impl Context {
704704
ctx.pos.clone(),
705705
gid as i32,
706706
params.len() as i32,
707-
vname.unwrap_or(String::from("<anonymous>")),
707+
vname
708+
.map(String::from)
709+
.unwrap_or(String::from("<anonymous>")),
708710
));
709711

710712
if ctx.nenv > 0 {

src/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ fn parse_function_param<'b>(i: Span) -> Result<String, MsgWithPos> {
176176

177177
fn parse_function<'b>(i: Span) -> EResult {
178178
let fn_arg_sep = tag(",");
179-
let fn_arg = expect_identifier;
179+
let fn_arg = map(expect_identifier, Ident);
180180
let tup = tuple((
181-
opt(expect_identifier),
181+
opt(map(expect_identifier, Ident)),
182182
tag("("),
183183
separated_list(fn_arg_sep, fn_arg),
184184
tag(")"),
@@ -447,7 +447,7 @@ fn advance_token(/*&mut self*/) -> Result<Token, MsgWithPos> {
447447

448448
fn parse_lambda<'b>(i: Span) -> EResult {
449449
let fn_arg_sep = tag(",");
450-
let fn_arg = expect_identifier;
450+
let fn_arg = map(expect_identifier, Ident);
451451
let tup = tuple((
452452
tag("|"),
453453
separated_list(fn_arg_sep, fn_arg),

0 commit comments

Comments
 (0)