Commit 436dc8f8
436dc8f881dcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Random as a first-class type (TY_RANDOM)
Random.new(seed) yields a typed sp_Random* instead of poly, so rand(n), rand, and bytes(n) dispatch directly to the runtime PRNG helpers and .class on their results resolves statically. Adds the Random.rand / Random.bytes class-method forms over the default instance. Fixes i898.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 436dc8f881dc
- Parent
- 993f5140279d
src/analyze.cmodifiedView file
@@ -701,8 +701,9 @@
701701
if (cn && !strcmp(cn, "Fiber")) return TY_FIBER;702702
/* Thread.new { block }: modeled as a Fiber run to completion on #value. */703703
if (cn && !strcmp(cn, "Thread") && nt_ref(nt, id, "block") >= 0) return TY_FIBER;704+
if (cn && !strcmp(cn, "Random")) return TY_RANDOM;704705
if (cn && (!strcmp(cn, "Thread") || !strcmp(cn, "Mutex") || !strcmp(cn, "Monitor") ||705-
!strcmp(cn, "Random") || !strcmp(cn, "IO") || !strcmp(cn, "File") ||706+
!strcmp(cn, "IO") || !strcmp(cn, "File") ||706707
!strcmp(cn, "GzipReader") || !strcmp(cn, "GzipWriter"))) return TY_POLY;707708
}708709
}@@ -874,11 +875,16 @@
874875
if (cn2 && !strcmp(name, "new") && !strcmp(cn2, "Thread") &&875876
nt_ref(nt, id, "block") >= 0)876877
return TY_FIBER;878+
if (cn2 && !strcmp(name, "new") && !strcmp(cn2, "Random")) return TY_RANDOM;877879
if (cn2 && !strcmp(name, "new") &&878-
(!strcmp(cn2, "Thread") || !strcmp(cn2, "Mutex") || !strcmp(cn2, "Random")))880+
(!strcmp(cn2, "Thread") || !strcmp(cn2, "Mutex")))879881
return TY_POLY;880882
if (cn2 && !strcmp(cn2, "Fiber") && !strcmp(name, "current")) return TY_FIBER;881883
if (cn2 && !strcmp(cn2, "Fiber") && !strcmp(name, "yield")) return TY_POLY;884+
/* Random class methods: Random.rand(n)->int / Random.rand->float */885+
if (cn2 && !strcmp(cn2, "Random") && !strcmp(name, "rand"))886+
return argc >= 1 ? TY_INT : TY_FLOAT;887+
if (cn2 && !strcmp(cn2, "Random") && !strcmp(name, "bytes")) return TY_STRING;882888
}883889
}884890
@@ -889,6 +895,13 @@
889895
if (!strcmp(name, "value")) return TY_POLY;890896
}891897
898+
/* TY_RANDOM instance methods */899+
if (recv >= 0 && rt == TY_RANDOM) {900+
if (!strcmp(name, "rand")) return argc >= 1 ? TY_INT : TY_FLOAT;901+
if (!strcmp(name, "bytes")) return TY_STRING;902+
if (!strcmp(name, "seed")) return TY_INT;903+
}904+
892905
/* TY_IO (File/IO handle) instance methods */893906
if (recv >= 0 && rt == TY_IO) {894907
if (!strcmp(name, "read") || !strcmp(name, "gets") || !strcmp(name, "readline") ||src/codegen.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…