Skip to content

Commit 89faebe

Browse files
committed
feat: refactor function params to IDENTIFIER type.
close #53.
1 parent 0236a30 commit 89faebe

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

interpreter/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn apply_function(function: &Rc<Object>, args: &Vec<Rc<Object>>) -> Result<Rc<Ob
126126
let mut env = Environment::new_enclosed_environment(&env);
127127

128128
params.iter().enumerate().for_each(|(i, param)| {
129-
env.set(param.clone(), args[i].clone());
129+
env.set(param.name.clone(), args[i].clone());
130130
});
131131

132132
let evaluated = eval_block_statements(&body.body, &Rc::new(RefCell::new(env)))?;

interpreter/object.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt;
22
use std::fmt::Formatter;
33
use std::rc::Rc;
44
use crate::environment::Env;
5-
use parser::ast::BlockStatement;
5+
use parser::ast::{BlockStatement, IDENTIFIER};
66
use std::collections::HashMap;
77
use std::hash::{Hash, Hasher};
88

@@ -18,7 +18,7 @@ pub enum Object {
1818
Hash(HashMap<Rc<Object>, Rc<Object>>),
1919
Null,
2020
ReturnValue(Rc<Object>),
21-
Function(Vec<String>, BlockStatement, Env),
21+
Function(Vec<IDENTIFIER>, BlockStatement, Env),
2222
Builtin(BuiltinFunc),
2323
Error(String),
2424
}
@@ -32,7 +32,13 @@ impl fmt::Display for Object {
3232
Object::Null => write!(f, "null"),
3333
Object::ReturnValue(expr) => write!(f, "{}", expr),
3434
Object::Function(params, body, _env) => {
35-
write!(f, "fn({}) {{ {} }}", params.join(", "), body)
35+
let func_params =
36+
params
37+
.iter()
38+
.map(|stmt| stmt.to_string())
39+
.collect::<Vec<String>>()
40+
.join(", ");
41+
write!(f, "fn({}) {{ {} }}", func_params, body)
3642
},
3743
Object::Builtin(_) => write!(f, "[builtin function]"),
3844
Object::Error(e) => write!(f, "{}", e),

parser/ast.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ pub struct IDENTIFIER {
116116
pub span: Span,
117117
}
118118

119+
impl fmt::Display for IDENTIFIER {
120+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
121+
write!(f, "{}", &self.name)
122+
}
123+
}
124+
119125
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
120126
#[serde(tag = "type")]
121127
pub struct UnaryExpression {
@@ -145,7 +151,7 @@ pub struct IF {
145151
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
146152
#[serde(tag = "type")]
147153
pub struct FunctionDeclaration {
148-
pub params: Vec<String>,
154+
pub params: Vec<IDENTIFIER>,
149155
pub body: BlockStatement,
150156
pub span: Span,
151157
}
@@ -195,7 +201,14 @@ impl fmt::Display for Expression {
195201
}
196202
}
197203
Expression::FUNCTION(FunctionDeclaration { params, body, .. }) => {
198-
write!(f, "fn({}) {{ {} }}", params.join(", "), body)
204+
let func_params =
205+
params
206+
.iter()
207+
.map(|stmt| stmt.to_string())
208+
.collect::<Vec<String>>()
209+
.join(", ")
210+
;
211+
write!(f, "fn({}) {{ {} }}", func_params, body)
199212
}
200213
Expression::FunctionCall(FunctionCall { callee, arguments, .. }) => {
201214
write!(f, "{}({})", callee, format_expressions(arguments))

parser/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl<'a> Parser<'a> {
345345
let end = self.current_token.span.end;
346346

347347
Ok(Expression::FUNCTION(FunctionDeclaration {
348-
params: params,
348+
params,
349349
body: function_body,
350350
span: Span {
351351
start,
@@ -354,7 +354,7 @@ impl<'a> Parser<'a> {
354354
}))
355355
}
356356

357-
fn parse_fn_parameters(&mut self) -> Result<Vec<String>, ParseError> {
357+
fn parse_fn_parameters(&mut self) -> Result<Vec<IDENTIFIER>, ParseError> {
358358
let mut params = Vec::new();
359359
if self.peek_token_is(&TokenKind::RPAREN) {
360360
self.next_token();
@@ -364,15 +364,21 @@ impl<'a> Parser<'a> {
364364
self.next_token();
365365

366366
match &self.current_token.kind {
367-
TokenKind::IDENTIFIER { name } => params.push(name.clone()),
367+
TokenKind::IDENTIFIER { name } => params.push(IDENTIFIER {
368+
name: name.clone(),
369+
span: self.current_token.span.clone(),
370+
}),
368371
token => return Err(format!("expected function params to be an identifier, got {}", token))
369372
}
370373

371374
while self.peek_token_is(&TokenKind::COMMA) {
372375
self.next_token();
373376
self.next_token();
374377
match &self.current_token.kind {
375-
TokenKind::IDENTIFIER { name } => params.push(name.clone()),
378+
TokenKind::IDENTIFIER { name } => params.push(IDENTIFIER {
379+
name: name.clone(),
380+
span: self.current_token.span.clone(),
381+
}),
376382
token => return Err(format!("expected function params to be an identifier, got {}", token))
377383
}
378384
}

parser/snapshots/parser__ast_tree_test__tests__test_func_declaration.snap

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ expression: "fn(x) { x };"
99
{
1010
"type": "FunctionDeclaration",
1111
"params": [
12-
"x"
12+
{
13+
"type": "IDENTIFIER",
14+
"name": "x",
15+
"span": {
16+
"start": 3,
17+
"end": 4
18+
}
19+
}
1320
],
1421
"body": {
1522
"type": "BlockStatement",

0 commit comments

Comments
 (0)