Skip to content

Commit 1f49291

Browse files
committed
Support NODE_LVAR
And fix NODE_BLOCK can't generate correct Array
1 parent e320ab9 commit 1f49291

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

ext/mjollnir/mjollnir.c

+21-7
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ static VALUE
5656
node_block_to_hash(const NODE *node)
5757
{
5858
VALUE result = rb_ary_new();
59-
NODE *nd_head = RNODE_BLOCK(node)->nd_head;
60-
NODE *nd_end = RNODE_BLOCK(node)->nd_end;
61-
62-
while (RNODE_BLOCK(node)->nd_next && nd_head != nd_end) {
63-
rb_ary_push(result, ast_to_values(Qnil, nd_head));
64-
nd_head = RNODE_BLOCK(node)->nd_next;
59+
const NODE *current_node = node;
60+
61+
while (current_node) {
62+
rb_ary_push(result, ast_to_values(Qnil, RNODE_BLOCK(current_node)->nd_head));
63+
current_node = RNODE_BLOCK(current_node)->nd_next;
6564
}
6665

6766
return result;
@@ -76,6 +75,14 @@ left_assign_to_hash(const NODE *node)
7675
return result;
7776
}
7877

78+
static VALUE
79+
node_lvar_to_hash(const NODE *node)
80+
{
81+
VALUE result = rb_hash_new();
82+
rb_hash_aset(result, rb_str_new2("vid"), ID2SYM(RNODE_LVAR(node)->nd_vid));
83+
return result;
84+
}
85+
7986
static VALUE
8087
literal_node_to_hash(const NODE *node)
8188
{
@@ -143,13 +150,20 @@ ast_to_values(VALUE hash, const NODE *node)
143150
return result;
144151
}
145152
case NODE_BLOCK: {
146-
return node_block_to_hash(node);
153+
VALUE result = rb_hash_new();
154+
rb_hash_aset(result, rb_str_new2("NODE_BLOCK"), node_block_to_hash(node));
155+
return result;
147156
}
148157
case NODE_LASGN: {
149158
VALUE result = rb_hash_new();
150159
rb_hash_aset(result, rb_str_new2("NODE_LASGN"), left_assign_to_hash(node));
151160
return result;
152161
}
162+
case NODE_LVAR: {
163+
VALUE result = rb_hash_new();
164+
rb_hash_aset(result, rb_str_new2("NODE_LVAR"), node_lvar_to_hash(node));
165+
return result;
166+
}
153167
case NODE_LIST: {
154168
VALUE result = rb_hash_new();
155169
rb_hash_aset(result, rb_str_new2("NODE_LIST"), list_node_to_hash(node));

test/mjollnir/parse_lvar_test.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class ParseLvarTest < Minitest::Test
6+
def test_parse_lvar
7+
result = Mjollnir.parse(<<~CODE)
8+
v = 117
9+
p v
10+
CODE
11+
12+
expected = {
13+
'NODE_SCOPE' => {
14+
'NODE_BLOCK' => [
15+
{
16+
'NODE_LASGN' => {
17+
'id' => :v,
18+
'value' => {
19+
'NODE_INTEGER' => 117
20+
}
21+
}
22+
},
23+
{
24+
'NODE_FCALL' => {
25+
'mid' => :p,
26+
'args' => {
27+
'NODE_LIST' => [
28+
{
29+
'NODE_LVAR' => {
30+
'vid' => :v
31+
}
32+
}
33+
]
34+
}
35+
}
36+
}
37+
]
38+
}
39+
}
40+
41+
assert_equal expected, result
42+
end
43+
end

0 commit comments

Comments
 (0)