Skip to content

Commit d711950

Browse files
committed
Update ordering of integer base flags
1 parent 0d813c3 commit d711950

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,10 @@ flags:
357357
values:
358358
- name: BINARY
359359
comment: "0b prefix"
360-
- name: OCTAL
361-
comment: "0o or 0 prefix"
362360
- name: DECIMAL
363361
comment: "0d or no prefix"
362+
- name: OCTAL
363+
comment: "0o or 0 prefix"
364364
- name: HEXADECIMAL
365365
comment: "0x prefix"
366366
comment: Flags for integer nodes that correspond to the base of the integer.

test/prism/ruby_api_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,29 @@ def test_heredoc?
124124
assert parse_expression("<<~`HERE`\nfoo \#{1}\nHERE\n").heredoc?
125125
end
126126

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+
127150
private
128151

129152
def parse_expression(source)

0 commit comments

Comments
 (0)