Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the syntax of Primitive.attr! to Symbol #7501

Merged
merged 2 commits into from Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions kernel.rb
Expand Up @@ -16,7 +16,7 @@ module Kernel
#++
#
def class
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_obj_class(self)'
end

Expand Down Expand Up @@ -65,7 +65,7 @@ def clone(freeze: nil)
#++
#
def frozen?
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_obj_frozen_p(self)'
end

Expand Down
28 changes: 14 additions & 14 deletions numeric.rb
Expand Up @@ -86,7 +86,7 @@ class Integer
#
# Returns +int+, negated.
def -@
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_uminus(self)'
end

Expand All @@ -102,7 +102,7 @@ def -@
#
# sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
def ~
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_comp(self)'
end

Expand All @@ -117,7 +117,7 @@ def ~
# 12345.abs #=> 12345
#
def abs
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_abs(self)'
end

Expand Down Expand Up @@ -163,7 +163,7 @@ def abs
# raise "overflow"
# end
def bit_length
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_bit_length(self)'
end

Expand All @@ -172,7 +172,7 @@ def bit_length
#
# Returns +true+ if +int+ is an even number.
def even?
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_even_p(self)'
end

Expand All @@ -191,7 +191,7 @@ def integer?
#
# Returns +true+ if +int+ is an odd number.
def odd?
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_odd_p(self)'
end

Expand Down Expand Up @@ -226,7 +226,7 @@ def ord
# (256**40 - 1).size #=> 40
#
def size
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_size(self)'
end

Expand All @@ -251,7 +251,7 @@ def to_int
#
# Returns +true+ if +int+ has a zero value.
def zero?
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_int_zero_p(self)'
end

Expand Down Expand Up @@ -316,12 +316,12 @@ def to_f
# 34.56.abs #=> 34.56
#
def abs
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_float_abs(self)'
end

def magnitude
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_float_abs(self)'
end

Expand All @@ -332,7 +332,7 @@ def magnitude
# Returns +float+, negated.
#
def -@
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'rb_float_uminus(self)'
end

Expand All @@ -343,7 +343,7 @@ def -@
# Returns +true+ if +float+ is 0.0.
#
def zero?
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'RBOOL(FLOAT_ZERO_P(self))'
end

Expand All @@ -354,7 +354,7 @@ def zero?
# Returns +true+ if +float+ is greater than 0.
#
def positive?
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) > 0.0)'
end

Expand All @@ -365,7 +365,7 @@ def positive?
# Returns +true+ if +float+ is less than 0.
#
def negative?
Primitive.attr! 'inline'
Primitive.attr! :inline
Primitive.cexpr! 'RBOOL(RFLOAT_VALUE(self) < 0.0)'
end

Expand Down
26 changes: 22 additions & 4 deletions tool/mk_builtin_loader.rb
Expand Up @@ -6,6 +6,7 @@

SUBLIBS = {}
REQUIRED = {}
BUILTIN_ATTRS = %w[inline]

def string_literal(lit, str = [])
while lit
Expand All @@ -25,13 +26,33 @@ def string_literal(lit, str = [])
end
end

# e.g. [:symbol_literal, [:symbol, [:@ident, "inline", [19, 21]]]]
def symbol_literal(lit)
symbol_literal, symbol_lit = lit
raise "#{lit.inspect} was not :symbol_literal" if symbol_literal != :symbol_literal
symbol, ident_lit = symbol_lit
raise "#{symbol_lit.inspect} was not :symbol" if symbol != :symbol
ident, symbol_name, = ident_lit
raise "#{ident.inspect} was not :@ident" if ident != :@ident
symbol_name
end

def inline_text argc, arg1
raise "argc (#{argc}) of inline! should be 1" unless argc == 1
arg1 = string_literal(arg1)
raise "1st argument should be string literal" unless arg1
arg1.join("").rstrip
end

def inline_attr(argc, arg1)
raise "argc (#{argc}) of attr! should be 1" unless argc == 1
attr = symbol_literal(arg1)
unless BUILTIN_ATTRS.include?(attr)
raise "attr (#{attr}) was not in: #{BUILTIN_ATTRS.join(', ')}"
end
attr
end

def make_cfunc_name inlines, name, lineno
case name
when /\[\]/
Expand Down Expand Up @@ -138,10 +159,7 @@ def collect_builtin base, tree, name, bs, inlines, locals = nil
if /(.+)[\!\?]\z/ =~ func_name
case $1
when 'attr'
text = inline_text(argc, args.first)
if text != 'inline'
raise "Only 'inline' is allowed to be annotated (but got: '#{text}')"
end
text = inline_attr(argc, args.first)
break
when 'cstmt'
text = inline_text argc, args.first
Expand Down