Skip to content

Commit 9002055

Browse files
committed
refactor: moved Block from enum to strut
1 parent 02044e8 commit 9002055

8 files changed

+58
-77
lines changed

src/evaluator/if_statement/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ use crate::syntax_analysis::abstract_syntax_tree::syntax_tree_node::*;
44
pub fn evaluate(condition: Expression, consequence: Block, alternative: Option<Block>) -> Object {
55
match crate::evaluator::evaluate_expression(condition) {
66
Object::NULL | Object::FALSE => match alternative {
7-
Some(block) => match block {
8-
Block::BLOCK { blocks } => crate::evaluator::evaluate_nodes(blocks),
9-
},
7+
Some(block) => crate::evaluator::evaluate_nodes(block.blocks),
8+
109
None => Object::NULL,
1110
},
12-
_ => match consequence {
13-
Block::BLOCK { blocks } => crate::evaluator::evaluate_nodes(blocks),
14-
},
11+
_ => crate::evaluator::evaluate_nodes(consequence.blocks),
1512
}
1613
}
1714

src/syntax_analysis/abstract_syntax_tree/syntax_tree_node/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use serde::{Deserialize, Serialize};
33
use crate::lexical_analysis::token::Token;
44

55
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
6-
pub enum Block {
7-
BLOCK { blocks: Vec<SyntaxTreeNode> },
6+
pub struct Block {
7+
pub blocks: Vec<SyntaxTreeNode>,
88
}
99

1010
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]

src/syntax_analysis/expressions/function_expression/snapshots/rust_monkey_interpreter__syntax_analysis__expressions__function_expression__tests__test_syntax_analysis_for_function_expression_case1.snap

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,35 @@ expression: "crate::syntax_analysis::get_abstract_syntax_tree(crate::lexical_ana
2929
}
3030
],
3131
"block": {
32-
"BLOCK": {
33-
"blocks": [
34-
{
35-
"EXPRESSION": {
36-
"expression": {
37-
"INFIX": {
38-
"left_hand": {
39-
"IDENTIFIER": {
40-
"identifier_token": {
41-
"IDENTIFIER": {
42-
"literal": "x"
43-
}
32+
"blocks": [
33+
{
34+
"EXPRESSION": {
35+
"expression": {
36+
"INFIX": {
37+
"left_hand": {
38+
"IDENTIFIER": {
39+
"identifier_token": {
40+
"IDENTIFIER": {
41+
"literal": "x"
4442
}
4543
}
46-
},
47-
"operator_token": "PLUS",
48-
"right_hand": {
49-
"IDENTIFIER": {
50-
"identifier_token": {
51-
"IDENTIFIER": {
52-
"literal": "y"
53-
}
44+
}
45+
},
46+
"operator_token": "PLUS",
47+
"right_hand": {
48+
"IDENTIFIER": {
49+
"identifier_token": {
50+
"IDENTIFIER": {
51+
"literal": "y"
5452
}
5553
}
5654
}
5755
}
5856
}
5957
}
6058
}
61-
]
62-
}
59+
}
60+
]
6361
}
6462
}
6563
}

src/syntax_analysis/expressions/function_expression/snapshots/rust_monkey_interpreter__syntax_analysis__expressions__function_expression__tests__test_syntax_analysis_for_function_expression_case2.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ expression: "crate::syntax_analysis::get_abstract_syntax_tree(crate::lexical_ana
1010
"FUNCTION": {
1111
"parameters": [],
1212
"block": {
13-
"BLOCK": {
14-
"blocks": []
15-
}
13+
"blocks": []
1614
}
1715
}
1816
}

src/syntax_analysis/expressions/function_expression/snapshots/rust_monkey_interpreter__syntax_analysis__expressions__function_expression__tests__test_syntax_analysis_for_function_expression_case3.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ expression: "crate::syntax_analysis::get_abstract_syntax_tree(crate::lexical_ana
2020
}
2121
],
2222
"block": {
23-
"BLOCK": {
24-
"blocks": []
25-
}
23+
"blocks": []
2624
}
2725
}
2826
}

src/syntax_analysis/expressions/if_expression/snapshots/rust_monkey_interpreter__syntax_analysis__expressions__if_expression__tests__test_syntax_analysis_for_if_expression_case1.snap

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,21 @@ expression: "crate::syntax_analysis::get_abstract_syntax_tree(crate::lexical_ana
3232
}
3333
},
3434
"consequence": {
35-
"BLOCK": {
36-
"blocks": [
37-
{
38-
"EXPRESSION": {
39-
"expression": {
40-
"IDENTIFIER": {
41-
"identifier_token": {
42-
"IDENTIFIER": {
43-
"literal": "x"
44-
}
35+
"blocks": [
36+
{
37+
"EXPRESSION": {
38+
"expression": {
39+
"IDENTIFIER": {
40+
"identifier_token": {
41+
"IDENTIFIER": {
42+
"literal": "x"
4543
}
4644
}
4745
}
4846
}
4947
}
50-
]
51-
}
48+
}
49+
]
5250
},
5351
"alternative": null
5452
}

src/syntax_analysis/expressions/if_expression/snapshots/rust_monkey_interpreter__syntax_analysis__expressions__if_expression__tests__test_syntax_analysis_for_if_expression_case2.snap

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,38 @@ expression: "crate::syntax_analysis::get_abstract_syntax_tree(crate::lexical_ana
3232
}
3333
},
3434
"consequence": {
35-
"BLOCK": {
36-
"blocks": [
37-
{
38-
"EXPRESSION": {
39-
"expression": {
40-
"IDENTIFIER": {
41-
"identifier_token": {
42-
"IDENTIFIER": {
43-
"literal": "x"
44-
}
35+
"blocks": [
36+
{
37+
"EXPRESSION": {
38+
"expression": {
39+
"IDENTIFIER": {
40+
"identifier_token": {
41+
"IDENTIFIER": {
42+
"literal": "x"
4543
}
4644
}
4745
}
4846
}
4947
}
50-
]
51-
}
48+
}
49+
]
5250
},
5351
"alternative": {
54-
"BLOCK": {
55-
"blocks": [
56-
{
57-
"EXPRESSION": {
58-
"expression": {
59-
"IDENTIFIER": {
60-
"identifier_token": {
61-
"IDENTIFIER": {
62-
"literal": "y"
63-
}
52+
"blocks": [
53+
{
54+
"EXPRESSION": {
55+
"expression": {
56+
"IDENTIFIER": {
57+
"identifier_token": {
58+
"IDENTIFIER": {
59+
"literal": "y"
6460
}
6561
}
6662
}
6763
}
6864
}
69-
]
70-
}
65+
}
66+
]
7167
}
7268
}
7369
}

src/syntax_analysis/expressions/utilities/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,5 @@ pub fn parse_block(
4242
None
4343
);
4444

45-
(
46-
iterator,
47-
syntax_parsing_errors,
48-
Some(Block::BLOCK { blocks }),
49-
)
45+
(iterator, syntax_parsing_errors, Some(Block { blocks }))
5046
}

0 commit comments

Comments
 (0)