Skip to content

Commit 0236a30

Browse files
committed
fix: if span issue
close #47.
1 parent bb5bfb8 commit 0236a30

File tree

5 files changed

+63
-35
lines changed

5 files changed

+63
-35
lines changed

interpreter/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ fn eval_expression(expression: &Expression, env: &Env) -> Result<Rc<Object>, Eva
7373
Expression::IF(IF { condition, consequent, alternate, .. }) => {
7474
let condition = eval_expression(condition, &Rc::clone(env))?;
7575
if is_truthy(&condition) {
76-
eval_block_statements(&(consequent.0), env)
76+
eval_block_statements(&(consequent.body), env)
7777
} else {
7878
match alternate {
79-
Some(alt) => eval_block_statements(&(alt.0), env),
79+
Some(alt) => eval_block_statements(&(alt.body), env),
8080
None => Ok(Rc::new(Object::Null))
8181
}
8282
}
@@ -129,7 +129,7 @@ fn apply_function(function: &Rc<Object>, args: &Vec<Rc<Object>>) -> Result<Rc<Ob
129129
env.set(param.clone(), args[i].clone());
130130
});
131131

132-
let evaluated = eval_block_statements(&body.0, &Rc::new(RefCell::new(env)))?;
132+
let evaluated = eval_block_statements(&body.body, &Rc::new(RefCell::new(env)))?;
133133
return unwrap_return(evaluated);
134134

135135
},

parser/ast.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,15 @@ impl fmt::Display for Statement {
8484
}
8585

8686
#[derive(Clone, Debug, Eq, Hash, Serialize, Deserialize, PartialEq)]
87-
pub struct BlockStatement(pub Vec<Statement>);
88-
89-
impl BlockStatement {
90-
pub fn new(statements: Vec<Statement>) -> BlockStatement {
91-
BlockStatement(statements)
92-
}
87+
#[serde(tag = "type")]
88+
pub struct BlockStatement {
89+
pub body: Vec<Statement>,
90+
pub span: Span,
9391
}
9492

9593
impl fmt::Display for BlockStatement {
9694
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
97-
write!(f, "{}", format_statements(&self.0))
95+
write!(f, "{}", format_statements(&self.body))
9896
}
9997
}
10098

parser/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl<'a> Parser<'a> {
309309
}
310310

311311
fn parse_block_statement(&mut self) -> Result<BlockStatement, ParseError> {
312+
let start = self.current_token.span.start;
312313
self.next_token();
313314
let mut block_statement = Vec::new();
314315

@@ -320,7 +321,15 @@ impl<'a> Parser<'a> {
320321
self.next_token();
321322
}
322323

323-
Ok(BlockStatement::new(block_statement))
324+
let end = self.current_token.span.end;
325+
326+
Ok(BlockStatement {
327+
body: block_statement,
328+
span: Span {
329+
start,
330+
end,
331+
}
332+
})
324333
}
325334

326335
fn parse_fn_expression(&mut self) -> Result<Expression, ParseError> {

parser/snapshots/parser__ast_tree_test__tests__test_func_declaration.snap

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@ expression: "fn(x) { x };"
1111
"params": [
1212
"x"
1313
],
14-
"body": [
15-
{
16-
"type": "IDENTIFIER",
17-
"name": "x",
18-
"span": {
19-
"start": 8,
20-
"end": 9
14+
"body": {
15+
"type": "BlockStatement",
16+
"body": [
17+
{
18+
"type": "IDENTIFIER",
19+
"name": "x",
20+
"span": {
21+
"start": 8,
22+
"end": 9
23+
}
2124
}
25+
],
26+
"span": {
27+
"start": 6,
28+
"end": 11
2229
}
23-
],
30+
},
2431
"span": {
2532
"start": 0,
2633
"end": 11

parser/snapshots/parser__ast_tree_test__tests__test_if.snap

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,40 @@ expression: "if (x < y) { x } else { y }"
4040
"end": 9
4141
}
4242
},
43-
"consequent": [
44-
{
45-
"type": "IDENTIFIER",
46-
"name": "x",
47-
"span": {
48-
"start": 13,
49-
"end": 14
43+
"consequent": {
44+
"type": "BlockStatement",
45+
"body": [
46+
{
47+
"type": "IDENTIFIER",
48+
"name": "x",
49+
"span": {
50+
"start": 13,
51+
"end": 14
52+
}
5053
}
54+
],
55+
"span": {
56+
"start": 11,
57+
"end": 16
5158
}
52-
],
53-
"alternate": [
54-
{
55-
"type": "IDENTIFIER",
56-
"name": "y",
57-
"span": {
58-
"start": 24,
59-
"end": 25
59+
},
60+
"alternate": {
61+
"type": "BlockStatement",
62+
"body": [
63+
{
64+
"type": "IDENTIFIER",
65+
"name": "y",
66+
"span": {
67+
"start": 24,
68+
"end": 25
69+
}
6070
}
71+
],
72+
"span": {
73+
"start": 22,
74+
"end": 27
6175
}
62-
],
76+
},
6377
"span": {
6478
"start": 0,
6579
"end": 27

0 commit comments

Comments
 (0)