Skip to content

Commit aa31ae0

Browse files
committed
feat: add index ast
1 parent 55c3808 commit aa31ae0

File tree

5 files changed

+77
-14
lines changed

5 files changed

+77
-14
lines changed

interpreter/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn eval_expression(expression: &Expression, env: &Env) -> Result<Rc<Object>, Eva
9090
let args = eval_expressions(arguments, env)?;
9191
apply_function(&func, &args)
9292
}
93-
Expression::Index(left, index) => {
93+
Expression::Index(Index { object: left, index, .. }) => {
9494
let literal = eval_expression(left, &Rc::clone(env))?;
9595
let index = eval_expression(index, env)?;
9696
eval_index_expression(&literal, &index)

parser/ast.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ 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-
9486
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
9587
pub enum Expression {
9688
IDENTIFIER(IDENTIFIER),
@@ -100,7 +92,7 @@ pub enum Expression {
10092
IF(IF),
10193
FUNCTION(FunctionDeclaration),
10294
FunctionCall(FunctionCall),
103-
Index(Box<Expression>, Box<Expression>),
95+
Index(Index),
10496
}
10597

10698
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
@@ -139,6 +131,21 @@ pub struct FunctionDeclaration {
139131
pub span: Span,
140132
}
141133

134+
// function can be Identifier or FunctionLiteral (think iife)
135+
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
136+
pub struct FunctionCall {
137+
pub callee: Box<Expression>,
138+
pub arguments: Vec<Expression>,
139+
pub span: Span,
140+
}
141+
142+
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
143+
pub struct Index {
144+
pub object: Box<Expression>,
145+
pub index: Box<Expression>,
146+
pub span: Span,
147+
}
148+
142149
impl fmt::Display for Expression {
143150
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
144151
match self {
@@ -172,8 +179,8 @@ impl fmt::Display for Expression {
172179
Expression::FunctionCall(FunctionCall { callee, arguments, .. }) => {
173180
write!(f, "{}({})", callee, format_expressions(arguments))
174181
}
175-
Expression::Index(left, index) => {
176-
write!(f, "({}[{}])", left, index)
182+
Expression::Index(Index { object, index, .. }) => {
183+
write!(f, "({}[{}])", object, index)
177184
}
178185
}
179186
}

parser/ast_tree_test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ mod tests {
6767
test_ast_tree("test_func_declaration", input)
6868
}
6969

70+
// JS: https://astexplorer.net/#/gist/b263eabba126aba94f1ad1c00ccce45e/2eb24376e622e07cf04915ee7eea1bc1c49d6122
7071
#[test]
7172
fn test_func_call() {
7273
let input = "add(1, 2)";
7374
test_ast_tree("test_func_call", input)
7475
}
76+
77+
// JS: https://astexplorer.net/#/gist/785eba71d71896939d5e91e0d445d357/a7bc3d915da84c16313da691a42b4a410fc3eef0
78+
#[test]
79+
fn test_index() {
80+
let input = "a[1]";
81+
test_ast_tree("test_index", input)
82+
}
7583
}

parser/lib.rs

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

1212
type ParseError = String;
@@ -410,12 +410,22 @@ impl<'a> Parser<'a> {
410410
}
411411

412412
fn parse_index_expression(&mut self, left: Expression) -> Result<Expression, ParseError> {
413+
let start = self.current_token.span.start;
413414
self.next_token();
414415
let index = self.parse_expression(Precedence::LOWEST)?.0;
415416

416417
self.expect_peek(&TokenKind::RBRACKET)?;
417418

418-
return Ok(Expression::Index(Box::new(left), Box::new(index)));
419+
let end = self.current_token.span.end;
420+
421+
return Ok(Expression::Index(Index {
422+
object: Box::new(left),
423+
index: Box::new(index),
424+
span: Span {
425+
start,
426+
end,
427+
}
428+
}));
419429
}
420430

421431
fn parse_hash_expression(&mut self) -> Result<Expression, ParseError> {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
source: parser/ast_tree_test.rs
3+
expression: let_ast
4+
---
5+
{
6+
"Program": {
7+
"body": [
8+
{
9+
"type": "Expr",
10+
"Index": {
11+
"object": {
12+
"IDENTIFIER": {
13+
"name": "a",
14+
"span": {
15+
"start": 0,
16+
"end": 1
17+
}
18+
}
19+
},
20+
"index": {
21+
"LITERAL": {
22+
"type": "Integer",
23+
"raw": 1,
24+
"span": {
25+
"start": 2,
26+
"end": 3
27+
}
28+
}
29+
},
30+
"span": {
31+
"start": 1,
32+
"end": 4
33+
}
34+
}
35+
}
36+
]
37+
}
38+
}

0 commit comments

Comments
 (0)