Commit 5d688451

5d688451179bMerged

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

Description

Add M9: case/when, string comparison, bitwise + builtin methods

  • case/when (statement form): predicate evaluated once into a temp, an if / else-if chain (else on its own line per project style); int/symbol via ==, string via sp_str_eq, subjectless via boolean when-conditions.
  • string comparison <,>,<=,>= via inline strcmp.
  • integer bitwise &,|,^,<<,>> as C operators.
  • string methods: * (repeat), gsub/sub/tr, center/ljust/rjust, lines, bytes, to_i/to_f.
  • integer methods: gcd/lcm/clamp, digits, to_s(base).
  • float: truncate.

All verified against CRuby. make test whole-test count steady at 13 (these features appear in tests that still need other features), but coverage is substantially broader.

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

Details

Commit
5d688451179b
Parent
d195b324e4e1

src/analyze.cmodifiedView file

@@ -108,17 +108,26 @@
108108 !strcmp(name, "rstrip") || !strcmp(name, "chomp") ||
109109 !strcmp(name, "chop") || !strcmp(name, "chr")) return TY_STRING;
110110 if (!strcmp(name, "[]")) return TY_STRING;
111- if (!strcmp(name, "index")) return TY_INT;
112- if (!strcmp(name, "split")) return TY_STR_ARRAY;
111+ if (!strcmp(name, "index") || !strcmp(name, "to_i")) return TY_INT;
112+ if (!strcmp(name, "to_f")) return TY_FLOAT;
113+ if (!strcmp(name, "split") || !strcmp(name, "lines")) return TY_STR_ARRAY;
114+ if (!strcmp(name, "bytes")) return TY_INT_ARRAY;
115+ if (!strcmp(name, "gsub") || !strcmp(name, "sub") || !strcmp(name, "tr") ||
116+ !strcmp(name, "center") || !strcmp(name, "ljust") || !strcmp(name, "rjust"))
117+ return TY_STRING;
118+ if (!strcmp(name, "*")) return TY_STRING;
113119 }
114120 /* integer receiver methods */
115121 if (recv >= 0 && rt == TY_INT) {
116122 if (!strcmp(name, "chr")) return TY_STRING;
123+ if (!strcmp(name, "gcd") || !strcmp(name, "lcm") || !strcmp(name, "clamp")) return TY_INT;
124+ if (!strcmp(name, "digits")) return TY_INT_ARRAY;
125+ if (!strcmp(name, "to_s") && argc == 1) return TY_STRING;
117126 }
118127 /* float receiver methods */
119128 if (recv >= 0 && rt == TY_FLOAT) {
120129 if (!strcmp(name, "floor") || !strcmp(name, "ceil") ||
121- !strcmp(name, "round")) return TY_INT;
130+ !strcmp(name, "round") || !strcmp(name, "truncate")) return TY_INT;
122131 }
123132
124133 if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0)
@@ -140,6 +149,12 @@
140149 }
141150 if (argc == 1 && is_eq_op(name)) return TY_BOOL;
142151
152+ /* integer bitwise operators */
153+ if (recv >= 0 && argc == 1 && rt == TY_INT &&
154+ (!strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^") ||
155+ !strcmp(name, "<<") || !strcmp(name, ">>")))
156+ return TY_INT;
157+
143158 size_t nl = strlen(name);
144159 if (nl > 0 && name[nl - 1] == '?') return TY_BOOL;
145160