Skip to content

Commit 55c3808

Browse files
committed
feat: add function call ast
1 parent 0b50d79 commit 55c3808

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed

interpreter/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ fn eval_expression(expression: &Expression, env: &Env) -> Result<Rc<Object>, Eva
8585
Expression::FUNCTION(FunctionDeclaration { params, body, .. }) => {
8686
return Ok(Rc::new(Object::Function(params.clone(), body.clone(), Rc::clone(env))));
8787
}
88-
Expression::FunctionCall(func, args) => {
89-
let func = eval_expression(func, &Rc::clone(env))?;
90-
let args = eval_expressions(args, env)?;
88+
Expression::FunctionCall(FunctionCall { callee, arguments, .. }) => {
89+
let func = eval_expression(callee, &Rc::clone(env))?;
90+
let args = eval_expressions(arguments, env)?;
9191
apply_function(&func, &args)
9292
}
9393
Expression::Index(left, index) => {

parser/ast.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ impl fmt::Display for BlockStatement {
8383
}
8484
}
8585

86+
// function can be Identifier or FunctionLiteral (think iife)
87+
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
88+
pub struct FunctionCall {
89+
pub callee: Box<Expression>,
90+
pub arguments: Vec<Expression>,
91+
pub span: Span,
92+
}
93+
8694
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
8795
pub enum Expression {
8896
IDENTIFIER(IDENTIFIER),
@@ -91,7 +99,7 @@ pub enum Expression {
9199
INFIX(BinaryExpression),
92100
IF(IF),
93101
FUNCTION(FunctionDeclaration),
94-
FunctionCall(Box<Expression>, Vec<Expression>), // function can be Identifier or FunctionLiteral (think iife)
102+
FunctionCall(FunctionCall),
95103
Index(Box<Expression>, Box<Expression>),
96104
}
97105

@@ -161,8 +169,8 @@ impl fmt::Display for Expression {
161169
Expression::FUNCTION(FunctionDeclaration { params, body, .. }) => {
162170
write!(f, "fn({}) {{ {} }}", params.join(", "), body)
163171
}
164-
Expression::FunctionCall(function, args) => {
165-
write!(f, "{}({})", function, format_expressions(args))
172+
Expression::FunctionCall(FunctionCall { callee, arguments, .. }) => {
173+
write!(f, "{}({})", callee, format_expressions(arguments))
166174
}
167175
Expression::Index(left, index) => {
168176
write!(f, "({}[{}])", left, index)

parser/ast_tree_test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,10 @@ mod tests {
6666
let input = "fn(x) { x };";
6767
test_ast_tree("test_func_declaration", input)
6868
}
69+
70+
#[test]
71+
fn test_func_call() {
72+
let input = "add(1, 2)";
73+
test_ast_tree("test_func_call", input)
74+
}
6975
}

parser/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub extern crate lexer;
66

77
use lexer::token::{TokenKind, Token, Span};
88
use lexer::Lexer;
9-
use crate::ast::{Program, Statement, Expression, Node, Literal, BlockStatement, Let, Integer, Boolean, StringType, Array, Hash, UnaryExpression, BinaryExpression, IDENTIFIER, IF, FunctionDeclaration};
9+
use crate::ast::{Program, Statement, Expression, Node, Literal, BlockStatement, Let, Integer, Boolean, StringType, Array, Hash, UnaryExpression, BinaryExpression, IDENTIFIER, IF, FunctionDeclaration, FunctionCall};
1010
use crate::precedences::{Precedence, get_token_precedence};
1111

1212
type ParseError = String;
@@ -365,8 +365,17 @@ impl<'a> Parser<'a> {
365365
}
366366

367367
fn parse_fn_call_expression(&mut self, expr: Expression) -> Result<Expression, ParseError> {
368+
let start = self.current_token.span.start;
368369
let (arguments, ..) = self.parse_expression_list(&TokenKind::RPAREN)?;
369-
Ok(Expression::FunctionCall(Box::new(expr), arguments))
370+
let end = self.current_token.span.end;
371+
Ok(Expression::FunctionCall(FunctionCall {
372+
callee: Box::new(expr),
373+
arguments,
374+
span: Span {
375+
start,
376+
end,
377+
}
378+
}))
370379
}
371380

372381
fn parse_expression_list(&mut self, end: &TokenKind) -> Result<(Vec<Expression>, Span), ParseError> {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
source: parser/ast_tree_test.rs
3+
expression: let_ast
4+
---
5+
{
6+
"Program": {
7+
"body": [
8+
{
9+
"type": "Expr",
10+
"FunctionCall": {
11+
"callee": {
12+
"IDENTIFIER": {
13+
"name": "add",
14+
"span": {
15+
"start": 0,
16+
"end": 3
17+
}
18+
}
19+
},
20+
"arguments": [
21+
{
22+
"LITERAL": {
23+
"type": "Integer",
24+
"raw": 1,
25+
"span": {
26+
"start": 4,
27+
"end": 5
28+
}
29+
}
30+
},
31+
{
32+
"LITERAL": {
33+
"type": "Integer",
34+
"raw": 2,
35+
"span": {
36+
"start": 7,
37+
"end": 8
38+
}
39+
}
40+
}
41+
],
42+
"span": {
43+
"start": 3,
44+
"end": 9
45+
}
46+
}
47+
}
48+
]
49+
}
50+
}

0 commit comments

Comments
 (0)