Commit 619e3e55

619e3e5516b8Merged

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

Description

Add Time object support (Time.at/now/local/utc, accessors, strftime)

New TY_TIME (sp_Time value type, no GC pointers) wired through the type plumbing and emit_boxed (sp_box_time). Constructors Time.now / at(int| float) / local|mktime / utc|gm (missing trailing parts default to mon=day=1, rest 0). Instance methods: utc/gmtime/getutc/localtime, year/mon/day/hour/min/sec/wday/yday, to_i/to_f/subsec/tv_sec/tv_usec/ usec/tv_nsec/nsec, utc?/gmt?/dst?, utc_offset, to_s/inspect/iso8601/ zone/strftime, class, and +/- numeric. All five Time tests pass.

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

Details

Commit
619e3e5516b8
Parent
464f985efb52

src/analyze.cmodifiedView file

@@ -63,16 +63,30 @@
6363 }
6464 }
6565
66- /* Class.cmethod(...) -> the class method's return type */
66+ /* Time.now / at / local / mktime / utc / gm -> a Time value */
6767 if (recv >= 0) {
6868 const char *rty = nt_type(nt, recv);
69- if (rty && !strcmp(rty, "ConstantReadNode")) {
70- int ci = comp_class_index(c, nt_str(nt, recv, "name"));
71- if (ci >= 0) {
72- int mi = comp_cmethod_in_chain(c, ci, name, NULL);
73- if (mi >= 0) return c->scopes[mi].ret;
74- }
75- }
69+ if (rty && !strcmp(rty, "ConstantReadNode") &&
70+ nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "Time") &&
71+ (!strcmp(name, "now") || !strcmp(name, "at") || !strcmp(name, "local") ||
72+ !strcmp(name, "mktime") || !strcmp(name, "utc") || !strcmp(name, "gm")))
73+ return TY_TIME;
74+ }
75+
76+ /* Time instance methods */
77+ if (recv >= 0 && rt == TY_TIME) {
78+ if (!strcmp(name, "utc") || !strcmp(name, "gmtime") || !strcmp(name, "getutc") ||
79+ !strcmp(name, "localtime") || !strcmp(name, "getlocal") || !strcmp(name, "+") ||
80+ !strcmp(name, "-")) return TY_TIME;
81+ if (!strcmp(name, "to_s") || !strcmp(name, "inspect") || !strcmp(name, "strftime") ||
82+ !strcmp(name, "iso8601") || !strcmp(name, "zone") || !strcmp(name, "asctime") ||
83+ !strcmp(name, "ctime")) return TY_STRING;
84+ if (!strcmp(name, "to_f") || !strcmp(name, "subsec")) return TY_FLOAT;
85+ if (!strcmp(name, "utc?") || !strcmp(name, "gmt?") || !strcmp(name, "dst?") ||
86+ !strcmp(name, "sunday?") || !strcmp(name, "monday?")) return TY_BOOL;
87+ if (!strcmp(name, "class")) return TY_STRING;
88+ /* year/mon/day/hour/min/sec/wday/yday/to_i/tv_sec/tv_usec/usec/tv_nsec/nsec/... */
89+ return TY_INT;
7690 }
7791
7892 /* obj.method(...) -> the method's return type (walks the superclass chain) */