Skip to content

Commit a912818

Browse files
committed
feat: fix return statement and finally fix type annotation
1 parent 7e46997 commit a912818

14 files changed

+54
-43
lines changed

interpreter/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ fn eval_block_statements(statements: &Vec<Statement>, env: &Env) -> Result<Rc<Ob
3535
fn eval_statement(statement: &Statement, env: &Env) -> Result<Rc<Object>, EvalError> {
3636
match statement {
3737
Statement::Expr(expr) => eval_expression(expr, env),
38-
Statement::Return(expr) => {
39-
let val = eval_expression(expr, env)?;
38+
Statement::Return(ReturnStatement { argument, .. }) => {
39+
let val = eval_expression(argument, env)?;
4040
return Ok(Rc::new(Object::ReturnValue(val)));
41-
},
41+
}
4242
Statement::Let(Let { identifier: id, expr, .. }) => {
4343
let val = eval_expression(expr, &Rc::clone(env))?;
4444
let obj: Rc<Object> = Rc::clone(&val);

parser/ast.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use lexer::token::{Token, TokenKind, Span};
55
use serde::{Deserialize, Serialize};
66

77
// still wait for https://github.com/serde-rs/serde/issues/1402
8-
// or https://github.com/serde-rs/serde/issues/760
98
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
109
pub enum Node {
1110
Program(Program),
@@ -45,11 +44,12 @@ impl fmt::Display for Program {
4544
}
4645
}
4746

47+
4848
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
4949
#[serde(untagged)]
5050
pub enum Statement {
5151
Let(Let),
52-
Return(Expression), // todo refactor to struct
52+
Return(ReturnStatement),
5353
Expr(Expression),
5454
}
5555

@@ -61,6 +61,13 @@ pub struct Let {
6161
pub span: Span,
6262
}
6363

64+
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
65+
#[serde(tag = "type")]
66+
pub struct ReturnStatement {
67+
pub argument: Expression,
68+
pub span: Span,
69+
}
70+
6471
impl fmt::Display for Statement {
6572
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
6673
match self {
@@ -70,7 +77,7 @@ impl fmt::Display for Statement {
7077
}
7178
panic!("unreachable")
7279
},
73-
Statement::Return(expr) => write!(f, "return {};", expr),
80+
Statement::Return(ReturnStatement { argument, .. }) => write!(f, "return {};", argument),
7481
Statement::Expr(expr) => write!(f, "{}", expr),
7582
}
7683
}
@@ -92,7 +99,7 @@ impl fmt::Display for BlockStatement {
9299
}
93100

94101
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
95-
#[serde(tag = "expr_type")]
102+
#[serde(untagged)]
96103
pub enum Expression {
97104
IDENTIFIER(IDENTIFIER),
98105
LITERAL(Literal), // need to flatten
@@ -105,19 +112,22 @@ pub enum Expression {
105112
}
106113

107114
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
115+
#[serde(tag = "type")]
108116
pub struct IDENTIFIER {
109117
pub name: String,
110118
pub span: Span,
111119
}
112120

113121
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
122+
#[serde(tag = "type")]
114123
pub struct UnaryExpression {
115124
pub op: Token,
116125
pub operand: Box<Expression>,
117126
pub span: Span,
118127
}
119128

120129
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
130+
#[serde(tag = "type")]
121131
pub struct BinaryExpression {
122132
pub op: Token,
123133
pub left: Box<Expression>,
@@ -126,6 +136,7 @@ pub struct BinaryExpression {
126136
}
127137

128138
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
139+
#[serde(tag = "type")]
129140
pub struct IF {
130141
pub condition: Box<Expression>,
131142
pub consequent: BlockStatement,
@@ -134,6 +145,7 @@ pub struct IF {
134145
}
135146

136147
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
148+
#[serde(tag = "type")]
137149
pub struct FunctionDeclaration {
138150
pub params: Vec<String>,
139151
pub body: BlockStatement,
@@ -142,13 +154,15 @@ pub struct FunctionDeclaration {
142154

143155
// function can be Identifier or FunctionLiteral (think iife)
144156
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
157+
#[serde(tag = "type")]
145158
pub struct FunctionCall {
146159
pub callee: Box<Expression>,
147160
pub arguments: Vec<Expression>,
148161
pub span: Span,
149162
}
150163

151164
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
165+
#[serde(tag = "type")]
152166
pub struct Index {
153167
pub object: Box<Expression>,
154168
pub index: Box<Expression>,

parser/lib.rs

Lines changed: 8 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, FunctionCall, Index};
9+
use crate::ast::{Program, Statement, Expression, Node, Literal, BlockStatement, Let, Integer, Boolean, StringType, Array, Hash, UnaryExpression, BinaryExpression, IDENTIFIER, IF, FunctionDeclaration, FunctionCall, Index, ReturnStatement};
1010
use crate::precedences::{Precedence, get_token_precedence};
1111

1212
type ParseError = String;
@@ -124,15 +124,21 @@ impl<'a> Parser<'a> {
124124
}
125125

126126
fn parse_return_statement(&mut self) -> Result<Statement, ParseError> {
127+
let start = self.current_token.span.start;
127128
self.next_token();
128129

129130
let value = self.parse_expression(Precedence::LOWEST)?.0;
130131

131132
if self.peek_token_is(&TokenKind::SEMICOLON) {
132133
self.next_token();
133134
}
135+
let end = self.current_token.span.end;
134136

135-
return Ok(Statement::Return(value));
137+
return Ok(Statement::Return(
138+
ReturnStatement {
139+
argument: value,
140+
span: Span { start, end },
141+
}));
136142
}
137143

138144
fn parse_expression_statement(&mut self) -> Result<Statement, ParseError> {

parser/snapshots/parser__ast_tree_test__tests__test_array.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ expression: "[1, true]"
77
"type": "Program",
88
"body": [
99
{
10-
"expr_type": "LITERAL",
1110
"type": "Array",
1211
"elements": [
1312
{
14-
"expr_type": "LITERAL",
1513
"type": "Integer",
1614
"raw": 1,
1715
"span": {
@@ -20,7 +18,6 @@ expression: "[1, true]"
2018
}
2119
},
2220
{
23-
"expr_type": "LITERAL",
2421
"type": "Boolean",
2522
"raw": true,
2623
"span": {

parser/snapshots/parser__ast_tree_test__tests__test_binary.snap

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ expression: 1 + 2 * 3
77
"type": "Program",
88
"body": [
99
{
10-
"expr_type": "INFIX",
10+
"type": "BinaryExpression",
1111
"op": {
1212
"span": {
1313
"start": 2,
@@ -18,7 +18,6 @@ expression: 1 + 2 * 3
1818
}
1919
},
2020
"left": {
21-
"expr_type": "LITERAL",
2221
"type": "Integer",
2322
"raw": 1,
2423
"span": {
@@ -27,7 +26,7 @@ expression: 1 + 2 * 3
2726
}
2827
},
2928
"right": {
30-
"expr_type": "INFIX",
29+
"type": "BinaryExpression",
3130
"op": {
3231
"span": {
3332
"start": 6,
@@ -38,7 +37,6 @@ expression: 1 + 2 * 3
3837
}
3938
},
4039
"left": {
41-
"expr_type": "LITERAL",
4240
"type": "Integer",
4341
"raw": 2,
4442
"span": {
@@ -47,7 +45,6 @@ expression: 1 + 2 * 3
4745
}
4846
},
4947
"right": {
50-
"expr_type": "LITERAL",
5148
"type": "Integer",
5249
"raw": 3,
5350
"span": {

parser/snapshots/parser__ast_tree_test__tests__test_func_call.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ expression: "add(1, 2)"
77
"type": "Program",
88
"body": [
99
{
10-
"expr_type": "FunctionCall",
10+
"type": "FunctionCall",
1111
"callee": {
12-
"expr_type": "IDENTIFIER",
12+
"type": "IDENTIFIER",
1313
"name": "add",
1414
"span": {
1515
"start": 0,
@@ -18,7 +18,6 @@ expression: "add(1, 2)"
1818
},
1919
"arguments": [
2020
{
21-
"expr_type": "LITERAL",
2221
"type": "Integer",
2322
"raw": 1,
2423
"span": {
@@ -27,7 +26,6 @@ expression: "add(1, 2)"
2726
}
2827
},
2928
{
30-
"expr_type": "LITERAL",
3129
"type": "Integer",
3230
"raw": 2,
3331
"span": {

parser/snapshots/parser__ast_tree_test__tests__test_func_declaration.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ expression: "fn(x) { x };"
77
"type": "Program",
88
"body": [
99
{
10-
"expr_type": "FUNCTION",
10+
"type": "FunctionDeclaration",
1111
"params": [
1212
"x"
1313
],
1414
"body": [
1515
{
16-
"expr_type": "IDENTIFIER",
16+
"type": "IDENTIFIER",
1717
"name": "x",
1818
"span": {
1919
"start": 8,

parser/snapshots/parser__ast_tree_test__tests__test_hash.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ expression: "{\"a\": 1}"
77
"type": "Program",
88
"body": [
99
{
10-
"expr_type": "LITERAL",
1110
"type": "Hash",
1211
"elements": [
1312
[
1413
{
15-
"expr_type": "LITERAL",
1614
"type": "String",
1715
"raw": "a",
1816
"span": {
@@ -21,7 +19,6 @@ expression: "{\"a\": 1}"
2119
}
2220
},
2321
{
24-
"expr_type": "LITERAL",
2522
"type": "Integer",
2623
"raw": 1,
2724
"span": {

parser/snapshots/parser__ast_tree_test__tests__test_if.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ expression: "if (x < y) { x } else { y }"
77
"type": "Program",
88
"body": [
99
{
10-
"expr_type": "IF",
10+
"type": "IF",
1111
"condition": {
12-
"expr_type": "INFIX",
12+
"type": "BinaryExpression",
1313
"op": {
1414
"span": {
1515
"start": 6,
@@ -20,15 +20,15 @@ expression: "if (x < y) { x } else { y }"
2020
}
2121
},
2222
"left": {
23-
"expr_type": "IDENTIFIER",
23+
"type": "IDENTIFIER",
2424
"name": "x",
2525
"span": {
2626
"start": 4,
2727
"end": 5
2828
}
2929
},
3030
"right": {
31-
"expr_type": "IDENTIFIER",
31+
"type": "IDENTIFIER",
3232
"name": "y",
3333
"span": {
3434
"start": 8,
@@ -42,7 +42,7 @@ expression: "if (x < y) { x } else { y }"
4242
},
4343
"consequent": [
4444
{
45-
"expr_type": "IDENTIFIER",
45+
"type": "IDENTIFIER",
4646
"name": "x",
4747
"span": {
4848
"start": 13,
@@ -52,7 +52,7 @@ expression: "if (x < y) { x } else { y }"
5252
],
5353
"alternate": [
5454
{
55-
"expr_type": "IDENTIFIER",
55+
"type": "IDENTIFIER",
5656
"name": "y",
5757
"span": {
5858
"start": 24,

parser/snapshots/parser__ast_tree_test__tests__test_index.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ expression: "a[1]"
77
"type": "Program",
88
"body": [
99
{
10-
"expr_type": "Index",
10+
"type": "Index",
1111
"object": {
12-
"expr_type": "IDENTIFIER",
12+
"type": "IDENTIFIER",
1313
"name": "a",
1414
"span": {
1515
"start": 0,
1616
"end": 1
1717
}
1818
},
1919
"index": {
20-
"expr_type": "LITERAL",
2120
"type": "Integer",
2221
"raw": 1,
2322
"span": {

0 commit comments

Comments
 (0)