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 + 1
498500 nid
499501 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 = n
558563 end
559564
@@ -603,6 +608,15 @@
603608 # codegen can surface a precise compile error.
604609 @nd_content[nid] = val
605610 end
611+ # Issue #841: RationalNode carries numerator and denominator
612+ # as decimal-text strings (loaded into separate slots so
613+ # codegen can synthesize sp_rational_new(num, den) literals).
614+ if field == "rat_num"
615+ @nd_rat_num[nid] = val
616+ end
617+ if field == "rat_den"
618+ @nd_rat_den[nid] = val
619+ end
606620 end
607621
608622 def set_int_field(nid, field, val)
@@ -680,6 +694,12 @@
680694 if field == "expression"
681695 @nd_expression[nid] = ref_id
682696 end
697+ # Issue #840: ImaginaryNode "numeric" field carries the imaginary
698+ # coefficient (IntegerNode or FloatNode); reuse the @nd_expression
699+ # slot so codegen reads it the same way.
700+ if field == "numeric"
701+ @nd_expression[nid] = ref_id
702+ end
683703 if field == "target"
684704 @nd_target[nid] = ref_id
685705 end