Skip to content

Commit e27c1d9

Browse files
committed
feat: adding defining and calling functions
1 parent 53b8621 commit e27c1d9

File tree

27 files changed

+345
-33
lines changed

27 files changed

+345
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "monkey_interpreter"
3-
version = "0.14.0"
3+
version = "0.15.0"
44
description = "Implementation of an interpreter for the Monkey language written in Rust, currently under active development."
55
authors = ["C <[email protected]>"]
66
edition = "2021"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Monkey Interpreter
2-
[![pipeline status](https://img.shields.io/badge/Version-0.14.0-blue)](https://gitlab.com/DeveloperC/monkey_interpreter/commits/master) [![pipeline status](https://gitlab.com/DeveloperC/monkey_interpreter/badges/master/pipeline.svg)](https://gitlab.com/DeveloperC/monkey_interpreter/commits/master) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![License: AGPL v3](https://img.shields.io/badge/License-AGPLv3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
2+
[![pipeline status](https://img.shields.io/badge/Version-0.15.0-blue)](https://gitlab.com/DeveloperC/monkey_interpreter/commits/master) [![pipeline status](https://gitlab.com/DeveloperC/monkey_interpreter/badges/master/pipeline.svg)](https://gitlab.com/DeveloperC/monkey_interpreter/commits/master) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![License: AGPL v3](https://img.shields.io/badge/License-AGPLv3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
33

44

55
![The Monkey Programming Language Logo](https://cloud.githubusercontent.com/assets/1013641/22617482/9c60c27c-eb09-11e6-9dfa-b04c7fe498ea.png)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::evaluator::model::object::{ErrorType, Object};
2+
use crate::evaluator::Evaluator;
3+
use crate::syntax_analysis::model::abstract_syntax_tree::syntax_tree_node::Expression;
4+
5+
impl Evaluator {
6+
pub(super) fn evaluate_call_expression(
7+
&mut self,
8+
function: Expression,
9+
arguments: Vec<Expression>,
10+
) -> Object {
11+
match self.evaluate_expression(function) {
12+
Object::Function { parameters, block } => {
13+
self.environment.push();
14+
15+
for (argument, parameter_identifier) in arguments.into_iter().zip(parameters) {
16+
let argument_evaluation = self.evaluate_expression(argument);
17+
18+
if let Object::Error { error_type } = argument_evaluation.clone() {
19+
self.environment.pop();
20+
return Object::Error { error_type };
21+
}
22+
23+
self.environment
24+
.set(parameter_identifier, argument_evaluation);
25+
}
26+
27+
let block_call_evaluation = self.evaluate_block(block);
28+
self.environment.pop();
29+
block_call_evaluation
30+
}
31+
Object::Error { error_type } => Object::Error { error_type },
32+
_ => Object::Error {
33+
error_type: ErrorType::UncallableObject,
34+
},
35+
}
36+
}
37+
}
38+
39+
#[cfg(test)]
40+
mod tests;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use rstest::rstest;
2+
3+
#[rstest(
4+
code,
5+
snapshot_name,
6+
case(
7+
"let identity = fn(x) { x; }; identity(5);",
8+
"test_evaluator_call_nodes_case1"
9+
),
10+
case(
11+
"let identity = fn(x) { return x; }; identity(5);",
12+
"test_evaluator_call_nodes_case2"
13+
),
14+
case(
15+
"let double = fn(x) { x * 2 }; double(5);",
16+
"test_evaluator_call_nodes_case3"
17+
),
18+
case(
19+
"let add = fn(x, y) { x + y }; add(2, 3);",
20+
"test_evaluator_call_nodes_case4"
21+
),
22+
case(
23+
"let add = fn(x, y) { x + y }; add(1, add(2, 3));",
24+
"test_evaluator_call_nodes_case5"
25+
),
26+
case("fn(y) { y }(13)", "test_evaluator_call_nodes_case6")
27+
)]
28+
fn test_evaluator_call_nodes(code: &str, snapshot_name: &str) {
29+
assert_expected_returned_object!(code, snapshot_name);
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: src/evaluator/expression/call/tests/mod.rs
3+
expression: "evaluator.evaluate(crate::syntax_analysis::SyntaxAnalysis::from(crate::lexical_analysis::LexicalAnalysis::from(code)))"
4+
5+
---
6+
Integer {
7+
value: 5,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: src/evaluator/expression/call/tests/mod.rs
3+
expression: "evaluator.evaluate(crate::syntax_analysis::SyntaxAnalysis::from(crate::lexical_analysis::LexicalAnalysis::from(code)))"
4+
5+
---
6+
Integer {
7+
value: 5,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: src/evaluator/expression/call/tests/mod.rs
3+
expression: "evaluator.evaluate(crate::syntax_analysis::SyntaxAnalysis::from(crate::lexical_analysis::LexicalAnalysis::from(code)))"
4+
5+
---
6+
Integer {
7+
value: 10,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: src/evaluator/expression/call/tests/mod.rs
3+
expression: "evaluator.evaluate(crate::syntax_analysis::SyntaxAnalysis::from(crate::lexical_analysis::LexicalAnalysis::from(code)))"
4+
5+
---
6+
Integer {
7+
value: 5,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: src/evaluator/expression/call/tests/mod.rs
3+
expression: "evaluator.evaluate(crate::syntax_analysis::SyntaxAnalysis::from(crate::lexical_analysis::LexicalAnalysis::from(code)))"
4+
5+
---
6+
Integer {
7+
value: 6,
8+
}

0 commit comments

Comments
 (0)