Commit 910ff23f

910ff23faf75Merged

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

Description

Add M5: scalar receiver methods (string/integer/float)

Add a focused but broad set of receiver-method dispatches against the runtime, evaluating the receiver once and splicing its text:

  • string: length/size, upcase/downcase/capitalize/reverse/strip/lstrip/ rstrip/chomp/chop, to_s/dup, empty?, include?/start_with?/end_with?, index, [] (char), split (-> str_array).
  • integer: to_s/to_f/to_i, abs, chr, even?/odd?/zero?/positive?/negative?.
  • float: floor/ceil/round/to_i (-> int), to_f/abs, to_s, zero?.

analyze infers the matching return types (string transforms -> string, floor/ceil/round -> int, split -> str_array). A mixed scalar-method program matches CRuby. make test: 6 pass.

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

Details

Commit
910ff23faf75
Parent
43648e5dee3d

src/analyze.cmodifiedView file

@@ -67,6 +67,27 @@
6767 if (!strcmp(name, "[]=")) return ty_array_elem(rt);
6868 }
6969
70+ /* string receiver methods */
71+ if (recv >= 0 && rt == TY_STRING) {
72+ if (!strcmp(name, "upcase") || !strcmp(name, "downcase") ||
73+ !strcmp(name, "capitalize") || !strcmp(name, "reverse") ||
74+ !strcmp(name, "strip") || !strcmp(name, "lstrip") ||
75+ !strcmp(name, "rstrip") || !strcmp(name, "chomp") ||
76+ !strcmp(name, "chop") || !strcmp(name, "chr")) return TY_STRING;
77+ if (!strcmp(name, "[]")) return TY_STRING;
78+ if (!strcmp(name, "index")) return TY_INT;
79+ if (!strcmp(name, "split")) return TY_STR_ARRAY;
80+ }
81+ /* integer receiver methods */
82+ if (recv >= 0 && rt == TY_INT) {
83+ if (!strcmp(name, "chr")) return TY_STRING;
84+ }
85+ /* float receiver methods */
86+ if (recv >= 0 && rt == TY_FLOAT) {
87+ if (!strcmp(name, "floor") || !strcmp(name, "ceil") ||
88+ !strcmp(name, "round")) return TY_INT;
89+ }
90+
7091 if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0)
7192 return ty_is_numeric(rt) ? rt : TY_UNKNOWN;
7293 if (!strcmp(name, "!")) return TY_BOOL;