Commit cbeed607

cbeed6072c24Merged

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

Description

Add StringIO support

New TY_STRINGIO (sp_StringIO *, a non-GC heap buffer -- not rooted or scanned). StringIO.new / new(str) / new(str, mode); methods string/pos/ tell/size/lineno, puts/print/write/<</putc, read/read(n), gets/getc (nil at eof -> poly), getbyte, rewind/seek/truncate, eof?/close/closed?/ flush/sync/isatty. Inference for all.

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

Details

Commit
cbeed6072c24
Parent
517672877dfa

src/analyze.cmodifiedView file

@@ -88,9 +88,21 @@
8888 if (ci >= 0) return ty_object(ci);
8989 if (cn && !strcmp(cn, "Array") && argc == 2) return ty_array_of(infer_type(c, argv[1]));
9090 if (cn && !strcmp(cn, "String")) return TY_STRING;
91+ if (cn && !strcmp(cn, "StringIO")) return TY_STRINGIO;
9192 }
9293 }
9394
95+ /* StringIO instance methods */
96+ if (recv >= 0 && rt == TY_STRINGIO) {
97+ if (!strcmp(name, "string") || !strcmp(name, "read")) return TY_STRING;
98+ if (!strcmp(name, "gets") || !strcmp(name, "getc")) return TY_POLY; /* nil at eof */
99+ if (!strcmp(name, "eof?") || !strcmp(name, "eof") || !strcmp(name, "closed?") ||
100+ !strcmp(name, "sync") || !strcmp(name, "isatty") || !strcmp(name, "tty?")) return TY_BOOL;
101+ if (!strcmp(name, "flush")) return TY_STRINGIO;
102+ /* pos/tell/size/length/lineno/write/puts/print/putc/getbyte/seek/... */
103+ return TY_INT;
104+ }
105+
94106 /* Time.now / at / local / mktime / utc / gm -> a Time value */
95107 if (recv >= 0) {
96108 const char *rty = nt_type(nt, recv);