Commit 2aee2930

2aee2930b822Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified

Description

StringScanner support + nullable-scalar truthiness in conditions

Wire StringScanner (require "strscan") into the compiler as a new TY_STRINGSCANNER type mirroring TY_STRINGIO: StringScanner.new and the scan/check/scan_until/matched/pos/pre_match/rest/getch/peek/[]/... methods dispatch to the existing sp_StringScanner_* runtime. String-returning methods are nullable (NULL = nil); string inspect/p are now NULL-aware (render "nil"), single-evaluating the receiver.

emit_cond now accepts nullable-representable types as a condition with Ruby truthiness: a NULL string/pointer, SP_INT_NIL int, or NaN float reads falsy, every other concrete value truthy. This also unblocks several non-scanner tests that branched on such values.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
2aee2930b822
Parent
fcba30981477

src/analyze.cmodifiedView file

@@ -272,9 +272,21 @@
272272 if (cn && !strcmp(cn, "Array") && argc == 2) return ty_array_of(infer_type(c, argv[1]));
273273 if (cn && !strcmp(cn, "String")) return TY_STRING;
274274 if (cn && !strcmp(cn, "StringIO")) return TY_STRINGIO;
275+ if (cn && !strcmp(cn, "StringScanner")) return TY_STRINGSCANNER;
275276 }
276277 }
277278
279+ /* StringScanner instance methods */
280+ if (recv >= 0 && rt == TY_STRINGSCANNER) {
281+ if (!strcmp(name, "scan") || !strcmp(name, "check") || !strcmp(name, "scan_until") ||
282+ !strcmp(name, "matched") || !strcmp(name, "pre_match") || !strcmp(name, "post_match") ||
283+ !strcmp(name, "rest") || !strcmp(name, "string") || !strcmp(name, "getch") ||
284+ !strcmp(name, "peek") || !strcmp(name, "[]")) return TY_STRING; /* nullable via NULL */
285+ if (!strcmp(name, "matched?") || !strcmp(name, "eos?") || !strcmp(name, "rest?")) return TY_BOOL;
286+ if (!strcmp(name, "pos") || !strcmp(name, "charpos") || !strcmp(name, "rest_size")) return TY_INT;
287+ if (!strcmp(name, "reset") || !strcmp(name, "terminate") || !strcmp(name, "unscan")) return TY_STRINGSCANNER;
288+ }
289+
278290 /* StringIO.open(args) { |io| body } -> the block body's value */
279291 if (recv >= 0 && !strcmp(name, "open") && nt_type(nt, recv) &&
280292 !strcmp(nt_type(nt, recv), "ConstantReadNode") && nt_str(nt, recv, "name") &&