Commit c5f8b525
c5f8b5252746Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 7 files · +2 added · ~5 modified
Description
parse,runtime,analyze,codegen: Imaginary/Rational literals + Integer#quo
Issues #840 #841 #872. spinel previously emitted Imaginary
(2i) and Rational (2r) as UnsupportedNode with a warning +
literal 1 fallback. Integer#quo fell through to the unresolved-
call path.
- parse: PM_IMAGINARY_NODE -> ImaginaryNode{numeric}; PM_RATIONAL_NODE -> RationalNode{rat_num, rat_den} (carrying the reduced numerator/denominator extracted from prism's pm_integer_t).
- runtime: sp_Rational value type {num, den}, sp_rational_new reducing factor + sign, sp_rational_inspect ("(n/d)"), sp_complex_inspect ("(re+imi)" / "(re-imi)") for the existing sp_Complex value type.
- analyze: ImaginaryNode -> "complex", RationalNode -> "rational", Integer#quo on int recv -> "rational"; c_type maps both new base types.
- codegen: literal arms in compile_expr emit value-type compound
literals; recv_type arms dispatch real/imag/numerator/denominator
- inspect/to_s; Integer#quo lowers to sp_rational_new(self, arg).
- puts / Kernel#p / compile_inspect_for: emit using the new helpers.
Deferred: arithmetic between mixed Integer/Rational/Complex
(1 + 2i, Rational(1) + Rational(2,3)), Rational/Complex method
dispatch beyond inspect/accessors, Rational#numerator types
(currently int -- a num overflowing int would need bigint).
Refs #840 #841 #872
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- c5f8b5252746
- Parent
- 210881000a6c
compiler_helpers.rbmodifiedView file
@@ -494,6 +494,8 @@
494494
@nd_inferred_type.push("")495495
@nd_scope_names.push("")496496
@nd_scope_types.push("")497+
@nd_rat_num.push("")498+
@nd_rat_den.push("")497499
@nd_count = @nd_count + 1498500
nid499501
end@@ -554,6 +556,9 @@
554556
@nd_inferred_type = Array.new(n, "")555557
@nd_scope_names = Array.new(n, "")556558
@nd_scope_types = Array.new(n, "")559+
# Issue #841: RationalNode literal slots.560+
@nd_rat_num = Array.new(n, "")561+
@nd_rat_den = Array.new(n, "")557562
@nd_count = n558563
end559564
@@ -603,6 +608,15 @@
603608
# codegen can surface a precise compile error.604609
@nd_content[nid] = val605610
end611+
# Issue #841: RationalNode carries numerator and denominator612+
# as decimal-text strings (loaded into separate slots so613+
# codegen can synthesize sp_rational_new(num, den) literals).614+
if field == "rat_num"615+
@nd_rat_num[nid] = val616+
end617+
if field == "rat_den"618+
@nd_rat_den[nid] = val619+
end606620
end607621
608622
def set_int_field(nid, field, val)@@ -680,6 +694,12 @@
680694
if field == "expression"681695
@nd_expression[nid] = ref_id682696
end697+
# Issue #840: ImaginaryNode "numeric" field carries the imaginary698+
# coefficient (IntegerNode or FloatNode); reuse the @nd_expression699+
# slot so codegen reads it the same way.700+
if field == "numeric"701+
@nd_expression[nid] = ref_id702+
end683703
if field == "target"684704
@nd_target[nid] = ref_id685705
endlib/sp_runtime.hmodifiedView file
Loading diff…
spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…
test/rational_complex_literals.rbaddedView file
Loading diff…
test/rational_complex_literals.rb.expectedaddedView file
Loading diff…