Skip to content

Commit ca64af0

Browse files
committed
feat: add func name in parser
1 parent bf4219f commit ca64af0

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

parser/ast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pub struct FunctionDeclaration {
152152
pub params: Vec<IDENTIFIER>,
153153
pub body: BlockStatement,
154154
pub span: Span,
155+
pub name: String,
155156
}
156157

157158
// function can be Identifier or FunctionLiteral (think iife)
@@ -189,13 +190,13 @@ impl fmt::Display for Expression {
189190
write!(f, "if {} {{ {} }}", condition, consequent,)
190191
}
191192
}
192-
Expression::FUNCTION(FunctionDeclaration { params, body, .. }) => {
193+
Expression::FUNCTION(FunctionDeclaration { name, params, body, .. }) => {
193194
let func_params = params
194195
.iter()
195196
.map(|stmt| stmt.to_string())
196197
.collect::<Vec<String>>()
197198
.join(", ");
198-
write!(f, "fn({}) {{ {} }}", func_params, body)
199+
write!(f, "fn {}({}) {{ {} }}", name, func_params, body)
199200
}
200201
Expression::FunctionCall(FunctionCall { callee, arguments, .. }) => {
201202
write!(f, "{}({})", callee, format_expressions(arguments))

parser/ast_tree_test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,10 @@ mod tests {
100100
let input = "a[1]";
101101
test_ast_tree("test_index", input)
102102
}
103+
104+
#[test]
105+
fn test_func_with_name() {
106+
let input = "let my_func = fn(x) { x };";
107+
test_ast_tree("test_func_with_name", input)
108+
}
103109
}

parser/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,24 @@ impl<'a> Parser<'a> {
9797
self.next_token();
9898

9999
let name = self.current_token.clone();
100+
let mut identifier_name = "".to_string();
100101
match &self.current_token.kind {
101-
TokenKind::IDENTIFIER { name: _ } => {}
102+
TokenKind::IDENTIFIER { name } => {
103+
identifier_name = name.to_string();
104+
}
102105
_ => return Err(format!("{} not an identifier", self.current_token)),
103106
};
104107

105108
self.expect_peek(&TokenKind::ASSIGN)?;
106109
self.next_token();
107110

108-
let value = self.parse_expression(Precedence::LOWEST)?.0;
111+
let mut value = self.parse_expression(Precedence::LOWEST)?.0;
112+
match value {
113+
Expression::FUNCTION(ref mut f) => {
114+
f.name = identifier_name;
115+
}
116+
_ => {}
117+
}
109118

110119
if self.peek_token_is(&TokenKind::SEMICOLON) {
111120
self.next_token();
@@ -330,6 +339,7 @@ impl<'a> Parser<'a> {
330339
params,
331340
body: function_body,
332341
span: Span { start, end },
342+
name: "".to_string(),
333343
}))
334344
}
335345

parser/snapshots/parser__ast_tree_test__tests__test_func_declaration.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ expression: "fn(x) { x };"
3838
"span": {
3939
"start": 0,
4040
"end": 11
41-
}
41+
},
42+
"name": ""
4243
}
4344
],
4445
"span": {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
source: parser/ast_tree_test.rs
3+
expression: "let my_func = fn(x) { x };"
4+
---
5+
{
6+
"Program": {
7+
"type": "Program",
8+
"body": [
9+
{
10+
"type": "Let",
11+
"identifier": {
12+
"kind": {
13+
"type": "IDENTIFIER",
14+
"value": {
15+
"name": "my_func"
16+
}
17+
},
18+
"span": {
19+
"start": 4,
20+
"end": 11
21+
}
22+
},
23+
"expr": {
24+
"type": "FunctionDeclaration",
25+
"params": [
26+
{
27+
"type": "IDENTIFIER",
28+
"name": "x",
29+
"span": {
30+
"start": 17,
31+
"end": 18
32+
}
33+
}
34+
],
35+
"body": {
36+
"type": "BlockStatement",
37+
"body": [
38+
{
39+
"type": "IDENTIFIER",
40+
"name": "x",
41+
"span": {
42+
"start": 22,
43+
"end": 23
44+
}
45+
}
46+
],
47+
"span": {
48+
"start": 20,
49+
"end": 25
50+
}
51+
},
52+
"span": {
53+
"start": 14,
54+
"end": 25
55+
},
56+
"name": "my_func"
57+
},
58+
"span": {
59+
"start": 0,
60+
"end": 26
61+
}
62+
}
63+
],
64+
"span": {
65+
"start": 0,
66+
"end": 27
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)