Skip to content

Commit 53b8621

Browse files
committed
feat: implemented let statement and identifiers in expressions
1 parent c9bdc09 commit 53b8621

File tree

42 files changed

+190
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+190
-94
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.13.0"
3+
version = "0.14.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.13.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.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)
33

44

55
![The Monkey Programming Language Logo](https://cloud.githubusercontent.com/assets/1013641/22617482/9c60c27c-eb09-11e6-9dfa-b04c7fe498ea.png)

src/evaluator/expression/if_expression/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::syntax_analysis::model::abstract_syntax_tree::syntax_tree_node::{Bloc
44

55
impl Evaluator {
66
pub(super) fn evaluate_if_expression(
7-
&self,
7+
&mut self,
88
condition: Expression,
99
consequence: Block,
1010
alternative: Option<Block>,

src/evaluator/expression/infix/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::syntax_analysis::model::abstract_syntax_tree::syntax_tree_node::Expre
55

66
impl Evaluator {
77
pub(super) fn evaluate_infix_expression(
8-
&self,
8+
&mut self,
99
left_hand: Expression,
1010
operator_token: Token,
1111
right_hand: Expression,

src/evaluator/expression/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod integer;
99
mod prefix;
1010

1111
impl Evaluator {
12-
pub(crate) fn evaluate_expression(&self, expression: Expression) -> Object {
12+
pub(crate) fn evaluate_expression(&mut self, expression: Expression) -> Object {
1313
match expression {
1414
Expression::Integer { integer_token } => self.evaluate_integer(integer_token),
1515
Expression::Boolean { boolean_token } => self.evaluate_boolean(boolean_token),
@@ -27,6 +27,11 @@ impl Evaluator {
2727
consequence,
2828
alternative,
2929
} => self.evaluate_if_expression(*condition, *consequence, *alternative),
30+
Expression::Identifier { identifier } => self
31+
.variables
32+
.get(&identifier)
33+
.unwrap_or(&Object::Null)
34+
.clone(),
3035
_ => Object::Null,
3136
}
3237
}

src/evaluator/expression/prefix/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::syntax_analysis::model::abstract_syntax_tree::syntax_tree_node::Expre
55

66
impl Evaluator {
77
pub(super) fn evaluate_prefix_expression(
8-
&self,
8+
&mut self,
99
prefix_token: Token,
1010
right_hand_expression: Expression,
1111
) -> Object {

src/evaluator/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::evaluator::model::object::Object;
2-
32
use crate::syntax_analysis::model::abstract_syntax_tree::syntax_tree_node::*;
43
use crate::syntax_analysis::model::abstract_syntax_tree::AbstractSyntaxTree;
54

5+
use std::collections::HashMap;
6+
67
pub(crate) mod model;
78

89
#[cfg(test)]
@@ -12,14 +13,19 @@ mod tests;
1213
mod expression;
1314
mod statement;
1415

15-
pub(crate) struct Evaluator {}
16+
#[derive(Debug)]
17+
pub(crate) struct Evaluator {
18+
variables: HashMap<String, Object>,
19+
}
1620

1721
impl Evaluator {
1822
pub(crate) fn new() -> Evaluator {
19-
Evaluator {}
23+
Evaluator {
24+
variables: HashMap::new(),
25+
}
2026
}
2127

22-
pub(crate) fn evaluate(&self, abstract_syntax_tree: AbstractSyntaxTree) -> Object {
28+
pub(crate) fn evaluate(&mut self, abstract_syntax_tree: AbstractSyntaxTree) -> Object {
2329
if !abstract_syntax_tree.syntax_parsing_errors.is_empty() {
2430
panic!("Syntax errors unable to evaluate.");
2531
}
@@ -42,7 +48,7 @@ impl Evaluator {
4248
object
4349
}
4450

45-
fn evaluate_block(&self, block: Block) -> Object {
51+
fn evaluate_block(&mut self, block: Block) -> Object {
4652
let mut object = Object::Null;
4753

4854
for syntax_tree_node in block.nodes {
@@ -57,7 +63,7 @@ impl Evaluator {
5763
object
5864
}
5965

60-
fn evaluate_node(&self, syntax_tree_node: SyntaxTreeNode) -> Object {
66+
fn evaluate_node(&mut self, syntax_tree_node: SyntaxTreeNode) -> Object {
6167
match syntax_tree_node {
6268
SyntaxTreeNode::Expression { expression } => self.evaluate_expression(expression),
6369
SyntaxTreeNode::Statement { statement } => self.evaluate_statement(statement),

src/evaluator/model/object/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ pub(crate) enum Object {
1212
pub(crate) enum ErrorType {
1313
TypeMismatch,
1414
UnknownOperator,
15+
UnassignableObject,
1516
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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::*;
4+
5+
impl Evaluator {
6+
pub(super) fn evaluate_let_statement(
7+
&mut self,
8+
identifier: String,
9+
expression: Expression,
10+
) -> Object {
11+
let expression = self.evaluate_expression(expression);
12+
13+
match expression.clone() {
14+
Object::Return { value: _ } => {
15+
return Object::Error {
16+
error_type: ErrorType::UnassignableObject,
17+
};
18+
}
19+
Object::Error { error_type } => {
20+
return Object::Error { error_type };
21+
}
22+
_ => {}
23+
}
24+
25+
self.variables.insert(identifier, expression);
26+
27+
Object::Null
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests;

0 commit comments

Comments
 (0)