Skip to content

Commit

Permalink
Support NODE_UNLES
Browse files Browse the repository at this point in the history
S-H-GAMELINKS committed Sep 1, 2024
1 parent d456514 commit 4ba5e98
Showing 2 changed files with 75 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ext/refine_tree/refine_tree.c
Original file line number Diff line number Diff line change
@@ -117,6 +117,18 @@ node_if_to_hash(const NODE *node)
return result;
}

static VALUE
node_unless_to_hash(const NODE *node)
{
VALUE result = rb_hash_new();

rb_hash_aset(result, symbol("cond"), ast_to_hash(RNODE_UNLESS(node)->nd_cond));
rb_hash_aset(result, symbol("body"), ast_to_hash(RNODE_UNLESS(node)->nd_body));
rb_hash_aset(result, symbol("else"), ast_to_hash(RNODE_UNLESS(node)->nd_else));

return result;
}

static VALUE
node_const_to_hash(const NODE *node)
{
@@ -332,6 +344,11 @@ ast_to_hash(const NODE *node)
rb_hash_aset(result, symbol("NODE_IF"), node_if_to_hash(node));
return result;
}
case NODE_UNLESS: {
VALUE result = rb_hash_new();
rb_hash_aset(result, symbol("NODE_UNLESS"), node_unless_to_hash(node));
return result;
}
case NODE_LIST: {
VALUE result = rb_hash_new();
rb_hash_aset(result, symbol("NODE_LIST"), node_list_to_hash(node));
58 changes: 58 additions & 0 deletions test/refine_tree/parse_unless_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'test_helper'

class ParseUnlessTest < Minitest::Test
def test_parse_unless
result = RefineTree.parse(<<~CODE)
v = 117
unless v
p v
end
CODE

expected = {
NODE_SCOPE: {
args: nil,
body: {
NODE_BLOCK: [
{
NODE_LASGN: {
id: :v,
value: {
NODE_INTEGER: 117
}
}
},
{
NODE_UNLESS: {
cond: {
NODE_LVAR: {
vid: :v
}
},
body: {
NODE_FCALL: {
mid: :p,
args: {
NODE_LIST: [
{
NODE_LVAR: {
vid: :v
}
}
]
}
}
},
else: nil
}
}
]
}
}
}

assert_equal expected, result
end
end

0 comments on commit 4ba5e98

Please sign in to comment.