File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -357,10 +357,10 @@ flags:
357
357
values :
358
358
- name : BINARY
359
359
comment : " 0b prefix"
360
- - name : OCTAL
361
- comment : " 0o or 0 prefix"
362
360
- name : DECIMAL
363
361
comment : " 0d or no prefix"
362
+ - name : OCTAL
363
+ comment : " 0o or 0 prefix"
364
364
- name : HEXADECIMAL
365
365
comment : " 0x prefix"
366
366
comment : Flags for integer nodes that correspond to the base of the integer.
Original file line number Diff line number Diff line change @@ -124,6 +124,29 @@ def test_heredoc?
124
124
assert parse_expression ( "<<~`HERE`\n foo \# {1}\n HERE\n " ) . heredoc?
125
125
end
126
126
127
+ # Through some bit hackery, we want to allow consumers to use the integer
128
+ # base flags as the base itself. It has a nice property that the current
129
+ # alignment provides them in the correct order. So here we test that our
130
+ # assumption holds so that it doesn't change out from under us.
131
+ #
132
+ # In C, this would look something like:
133
+ #
134
+ # ((flags & ~DECIMAL) << 1) || 10
135
+ #
136
+ # We have to do some other work in Ruby because 0 is truthy and ~ on an
137
+ # integer doesn't have a fixed width.
138
+ def test_integer_base_flags
139
+ base = -> ( node ) do
140
+ value = ( node . send ( :flags ) & ( 0b1111 - IntegerBaseFlags ::DECIMAL ) ) << 1
141
+ value == 0 ? 10 : value
142
+ end
143
+
144
+ assert_equal 2 , base [ parse_expression ( "0b1" ) ]
145
+ assert_equal 8 , base [ parse_expression ( "0o1" ) ]
146
+ assert_equal 10 , base [ parse_expression ( "0d1" ) ]
147
+ assert_equal 16 , base [ parse_expression ( "0x1" ) ]
148
+ end
149
+
127
150
private
128
151
129
152
def parse_expression ( source )
You can’t perform that action at this time.
0 commit comments