Commit ec468237
ec4682375e85Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Complex and Rational value types
Wire the existing sp_Complex / sp_Rational runtime (16-byte value types, passed by value) into the analyzer and code generator:
- types: TY_COMPLEX / TY_RATIONAL, with c-type, default, is_scalar_ret, and local-declaration ({0}) support.
- analyze: ImaginaryNode -> Complex, RationalNode -> Rational; Kernel#Complex, Complex.polar, Integer#quo; method return types for .real/.imaginary/ .conjugate/.numerator/.denominator/.inspect/.to_s and + - * between Complex.
- codegen: literal emission (
1i-> (sp_Complex){0,1},3r-> (sp_Rational){3,1}); Complex()/Complex.polar/quo construction; .re/.im/.num/ .den field access; sp_complex_add/mul/conjugate/inspect and sp_rational_new/inspect; p of a Complex/Rational renders via inspect.
Fixes kernel_complex, complex_polar, rational_complex_literals.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- ec4682375e85
- Parent
- 18a2f6a25bbc
src/analyze.cmodifiedView file
@@ -458,6 +458,29 @@
458458
TyKind rt = recv >= 0 ? infer_type(c, recv) : TY_UNKNOWN;459459
TyKind a0 = argc >= 1 ? infer_type(c, argv[0]) : TY_UNKNOWN;460460
461+
/* Complex / Rational value types. */462+
if (recv < 0 && !strcmp(name, "Complex")) return TY_COMPLEX;463+
if (recv >= 0) {464+
const char *rrty = nt_type(nt, recv);465+
if (rrty && !strcmp(rrty, "ConstantReadNode") && nt_str(nt, recv, "name") &&466+
!strcmp(nt_str(nt, recv, "name"), "Complex") && !strcmp(name, "polar"))467+
return TY_COMPLEX;468+
}469+
if (rt == TY_COMPLEX) {470+
if (!strcmp(name, "real") || !strcmp(name, "imaginary") || !strcmp(name, "imag") ||471+
!strcmp(name, "abs") || !strcmp(name, "magnitude")) return TY_FLOAT;472+
if (!strcmp(name, "conjugate") || !strcmp(name, "conj") ||473+
!strcmp(name, "+") || !strcmp(name, "-") || !strcmp(name, "*")) return TY_COMPLEX;474+
if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) return TY_STRING;475+
}476+
if (rt == TY_INT && !strcmp(name, "quo")) return TY_RATIONAL;477+
if (rt == TY_RATIONAL) {478+
if (!strcmp(name, "numerator") || !strcmp(name, "denominator")) return TY_INT;479+
if (!strcmp(name, "to_f")) return TY_FLOAT;480+
if (!strcmp(name, "to_i")) return TY_INT;481+
if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) return TY_STRING;482+
}483+
461484
/* Safe navigation &. : nil receiver always short-circuits to nil */462485
{463486
const char *call_op = nt_str(nt, id, "call_operator");@@ -2286,6 +2309,8 @@
22862309
22872310
if (!strcmp(ty, "IntegerNode")) return TY_INT;22882311
if (!strcmp(ty, "FloatNode")) return TY_FLOAT;2312+
if (!strcmp(ty, "ImaginaryNode")) return TY_COMPLEX;2313+
if (!strcmp(ty, "RationalNode")) return TY_RATIONAL;22892314
if (!strcmp(ty, "StringNode")) return TY_STRING;22902315
if (!strcmp(ty, "SourceFileNode")) return TY_STRING;22912316
if (!strcmp(ty, "SourceLineNode")) return TY_INT;src/codegen.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…