Skip to content

Commit

Permalink
Support NODE_LASGN
Browse files Browse the repository at this point in the history
S-H-GAMELINKS committed Aug 13, 2024
1 parent d6c4e96 commit 2724cfe
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ext/mjollnir/mjollnir.c
Original file line number Diff line number Diff line change
@@ -43,6 +43,15 @@ list_node_to_hash(const NODE *node)
return result;
}

VALUE
left_assign_to_hash(const NODE *node)
{
VALUE result = rb_hash_new();
rb_hash_aset(result, rb_str_new2("id"), ID2SYM(RNODE_LASGN(node)->nd_vid));
rb_hash_aset(result, rb_str_new2("value"), ast_to_values(Qnil, RNODE_LASGN(node)->nd_value));
return result;
}

VALUE
ast_to_values(VALUE hash, const NODE *node)
{
@@ -73,6 +82,11 @@ ast_to_values(VALUE hash, const NODE *node)
rb_hash_aset(result, rb_str_new2("NODE_BLOCK"), ast_to_values(hash, RNODE_BLOCK(node)->nd_head));
return result;
}
case NODE_LASGN: {
VALUE result = rb_hash_new();
rb_hash_aset(result, rb_str_new2("NODE_LASGN"), left_assign_to_hash(node));
return result;
}
case NODE_LIST: {
VALUE result = rb_hash_new();
rb_hash_aset(result, rb_str_new2("NODE_LIST"), list_node_to_hash(node));
22 changes: 22 additions & 0 deletions test/mjollnir/parse_lasgn_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'test_helper'

class ParseLasgnTest < Minitest::Test
def test_parse_lasgn
result = Mjollnir.parse('var = 117')

expected = {
'NODE_SCOPE' => {
'NODE_LASGN' => {
'id' => :var,
'value' => {
'NODE_INTEGER' => 117
}
}
}
}

assert_equal expected, result
end
end

0 comments on commit 2724cfe

Please sign in to comment.