Move the engine core into core/, a project of its own (pplus-core)
What programs mean (the languages, translators, Mojo checker and
interpreter, prover, fuzzer, machine model, catalog problems and the Mojo
corpus with godbolt's stored answers) was mixed into the site's tree, so a
change to the prover and a change to a button went through the same gate
and the same docs. This moves all of it under core/, laid out as its own
repository, ready to be extracted with its history and pinned here as a
git submodule.
- core/: src/{engines,services,sync,mojo,exercises,curriculum,common},
mojo/, syntax/mojo.xml, its tests (with Workspace and workForProblem in
place of the site's Session), scripts (corpus pipeline, play, the
exercise and probe starters, engines export/diff), and its docs
(engines, trust, mojo-corpus, exercises, coverage). Its own pixi.toml,
package.json, README, AGENTS.md and CI workflow.
- core/tests/boundary.test.mjs: library modules use no DOM, import no
page widget, and import nothing outside src/.
- core/scripts/cli.mjs: pixi run cli check|run|prove|fuzz|translate.
- core/scripts/dafny.py: Dafny 4.11.0 pinned by version and SHA-256 per
platform, fetched into .tools/ (pixi run dafny-fetch, pixi run dafny).
- The site imports core only as #core/<path>.js: an import map in
index.html, "imports" in package.json for node, core's declarations for
tsc. The site's TypeScript builds core first (npm run build).
- assemble.sh, the Quarto engine copy, the render cache key, check-pages,
quarto-export and quarto-css read core/dist and core/mojo; quarto-css
copies core's syntax/mojo.xml into the extension.
- Site tests that covered engines moved to core; the rest keep Session.
- README, AGENTS.md, docs/app.md, docs/deploy.md and the CI workflow
describe the split (checkout with submodules; core's tests and corpus
status run against the pinned commit).
Gate: site 110 pass; core 307 pass; corpus status --strict passes.
Manual check: the assembled site opens Bridge (count-base-proved),
Developer and Hardware (count-base) with the editor, the stepper and the
board, and no module errors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
1567 files changed
+3670
-981
No files matched your search
@@ -0,0 +1,11 @@
|
||||
# probe: the compiler's words for passing one value as mut and as a second argument
|
||||
# expect: compile-error
|
||||
def append_twice(mut s: String, other: String):
|
||||
s += other
|
||||
s += other
|
||||
|
||||
|
||||
def main():
|
||||
var my_string = String("o")
|
||||
append_twice(my_string, my_string)
|
||||
print(my_string)
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: the compiler's words for copying a type that is not ImplicitlyCopyable
|
||||
# expect: compile-error
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
var b = a
|
||||
print(a.name, b.name)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: 1.1.0 notes: assigning an undeclared name is an error
|
||||
# expect: compile-error
|
||||
def main():
|
||||
x = 5
|
||||
print(x)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: calling a raising function from a non-raising one
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var a = input()
|
||||
print(a)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: the compiler's words for reading a variable before it is set
|
||||
# expect: compile-error
|
||||
def main():
|
||||
var x: Int
|
||||
print(x)
|
||||
@@ -0,0 +1,30 @@
|
||||
# probe: moved out on one path, read after the branch: the compiler's words
|
||||
# expect: compile-error
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def take(var t: L):
|
||||
print("took", t.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
if flag:
|
||||
take(a^)
|
||||
print(a.n)
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: the compiler's words for using a value after ^ moved it
|
||||
# expect: compile-error
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
var b = a^
|
||||
print(a.name, b.name)
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: comptime for and comptime if in a function body
|
||||
comptime N = 3
|
||||
|
||||
|
||||
def main():
|
||||
comptime for i in range(N):
|
||||
print(i)
|
||||
comptime if N > 2:
|
||||
print("big")
|
||||
@@ -0,0 +1,14 @@
|
||||
# probe: 1.0.0 notes said __del__ still works, deprecated; does it in 1.1.0?
|
||||
struct Old:
|
||||
var v: Int
|
||||
|
||||
def __init__(out self, v: Int):
|
||||
self.v = v
|
||||
|
||||
def __del__(deinit self):
|
||||
print("old-style destructor ran")
|
||||
|
||||
|
||||
def main():
|
||||
var o = Old(1)
|
||||
print(o.v)
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: 1.1.0 notes: fn is removed
|
||||
# expect: compile-error
|
||||
fn main():
|
||||
print("fn still works")
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: 1.1.0 notes: the read convention is a hard error
|
||||
# expect: compile-error
|
||||
def show(read x: Int):
|
||||
print(x)
|
||||
|
||||
|
||||
def main():
|
||||
show(1)
|
||||
@@ -0,0 +1,11 @@
|
||||
# probe: deprecated in 0.26.2: is @register_passable("trivial") an error or a warning in 1.1.0?
|
||||
@register_passable("trivial")
|
||||
struct P:
|
||||
var v: Int
|
||||
|
||||
def __init__(out self, v: Int):
|
||||
self.v = v
|
||||
|
||||
|
||||
def main():
|
||||
print(P(1).v)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: is the @value decorator still accepted?
|
||||
@value
|
||||
struct V:
|
||||
var x: Int
|
||||
|
||||
|
||||
def main():
|
||||
print(V(1).x)
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: what does reading a missing Dict key raise, and how is it printed?
|
||||
def main() raises:
|
||||
var d = {"a": 1}
|
||||
try:
|
||||
print(d["b"])
|
||||
except e:
|
||||
print("caught:", e)
|
||||
@@ -0,0 +1,12 @@
|
||||
# probe: does a Dict iterate in insertion order, and after a pop and re-insert?
|
||||
def main() raises:
|
||||
var d = Dict[Int, String]()
|
||||
d[3] = "c"
|
||||
d[1] = "a"
|
||||
d[2] = "b"
|
||||
for k in d.keys():
|
||||
print(k)
|
||||
_ = d.pop(1)
|
||||
d[1] = "a again"
|
||||
for e in d.items():
|
||||
print(e.key, e.value)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: is there an assert statement, and what does a failing one do?
|
||||
def main():
|
||||
var x = 1
|
||||
assert x == 2, "x is not 2"
|
||||
print("after")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: is debug_assert checked by default on godbolt's build?
|
||||
def main():
|
||||
print("before")
|
||||
debug_assert(False, "debug_assert fired")
|
||||
print("after")
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: does std.sys.exit set the exit code after output?
|
||||
from std.sys import exit
|
||||
|
||||
|
||||
def main():
|
||||
print("bye")
|
||||
exit(3)
|
||||
@@ -0,0 +1,10 @@
|
||||
# probe: the order of try, except and finally
|
||||
def main():
|
||||
try:
|
||||
print("try")
|
||||
raise Error("x")
|
||||
except e:
|
||||
print("except", e)
|
||||
finally:
|
||||
print("finally")
|
||||
print("after")
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: what reaches stdout, stderr, and the exit code when main raises?
|
||||
def main() raises:
|
||||
print("before")
|
||||
raise Error("boom")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: can an Int and a Float64 be combined without a conversion?
|
||||
def main():
|
||||
var n = 10
|
||||
var x = 2.5
|
||||
print(n * x)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does a Float64 product past the largest double give inf at run time, and inf - inf nan?
|
||||
def main():
|
||||
var big = 1.0e308
|
||||
var x = big * 10.0
|
||||
print(x, x - x)
|
||||
@@ -0,0 +1,12 @@
|
||||
# probe: how does print format Float64 values?
|
||||
def main():
|
||||
var a = 0.1
|
||||
var b = 0.2
|
||||
print(a + b)
|
||||
print(1.0)
|
||||
print(Float64(3))
|
||||
print(1e20)
|
||||
print(1e-7)
|
||||
print(1.0 / 3.0)
|
||||
print(-0.0)
|
||||
print(123456789.125)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what does round() do on halves?
|
||||
def main():
|
||||
var a = 2.5
|
||||
var b = 3.5
|
||||
var c = -2.5
|
||||
print(round(a), round(b), round(c))
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: how are inf, -inf and nan printed after a run-time division by zero?
|
||||
def main():
|
||||
var z = 0.0
|
||||
print(1.0 / z, -1.0 / z, z / z)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does Int(x) truncate toward zero?
|
||||
def main():
|
||||
var a = 3.9
|
||||
var b = -3.9
|
||||
print(Int(a), Int(b))
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: how does print format Float32 values?
|
||||
def main():
|
||||
var a = Float32(0.1)
|
||||
print(a)
|
||||
print(Float32(1) / Float32(3))
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: does one request return both the listing and the program's output?
|
||||
# mode: both
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: the listing for a small function: labels, and source line numbers on instructions
|
||||
# mode: asm
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,10 @@
|
||||
# probe: the Compile button's request: -O0, line tables, listing and output in one answer
|
||||
# mode: both
|
||||
# args: -O0 --debug-level=line-tables
|
||||
# expect-stdout: 5
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: with -O0 and line tables, are source lines attached, and is the function list unchanged?
|
||||
# mode: asm
|
||||
# args: -O0 --debug-level=line-tables
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: with --debug-level=line-tables, does godbolt attach source lines to instructions (optimised build)?
|
||||
# mode: asm
|
||||
# args: --debug-level=line-tables
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: is -O0 accepted, and how does the listing change?
|
||||
# mode: asm
|
||||
# args: -O0
|
||||
def add(a: Int, b: Int) -> Int:
|
||||
return a + b
|
||||
|
||||
|
||||
def main():
|
||||
print(add(2, 3))
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: does the Compile button's request (compile and run in one) deliver stdin? (run mode did not: input-lines)
|
||||
# mode: both
|
||||
# args: -O0 --debug-level=line-tables
|
||||
# stdin: 41\n
|
||||
# expect-stdout: 42
|
||||
def main() raises:
|
||||
var s = input()
|
||||
print(atol(s) + 1)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: what does input() do at the end of input?
|
||||
# stdin:
|
||||
def main() raises:
|
||||
var a = input()
|
||||
print("got [" + a + "]")
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: input() reads one line per call; atol parses an Int
|
||||
# stdin: 42\nhello\n
|
||||
def main() raises:
|
||||
var a = input()
|
||||
var b = input()
|
||||
print(atol(a) + 1)
|
||||
print(b)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what does atol raise on text that is not a number?
|
||||
def main():
|
||||
try:
|
||||
print(atol("x1"))
|
||||
except e:
|
||||
print("error:", e)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what happens on integer division by zero at run time (output, exit code)?
|
||||
def main():
|
||||
var z = 0
|
||||
print("before")
|
||||
print(7 // z)
|
||||
print("after")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does // floor toward negative infinity on Int?
|
||||
def main():
|
||||
var a = 7
|
||||
var b = 2
|
||||
print(a // b, (-a) // b, a // (-b), (-a) // (-b))
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: is a literal too big for Int a compile error?
|
||||
def main():
|
||||
var x: Int = 2 ** 64
|
||||
print(x)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: what sign does % give with negative operands?
|
||||
def main():
|
||||
var a = 7
|
||||
var m = 3
|
||||
print(a % m, (-a) % m, a % (-m), (-a) % (-m))
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: what is Int.MAX, and what is Int.MAX + 1 at run time?
|
||||
def main():
|
||||
var m = Int.MAX
|
||||
print(m)
|
||||
m += 1
|
||||
print(m)
|
||||
print(Int.MIN)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: does Int8 wrap on overflow at run time?
|
||||
def main():
|
||||
var x: Int8 = 127
|
||||
x += 1
|
||||
print(x)
|
||||
var y: Int8 = -128
|
||||
y -= 1
|
||||
print(y)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: what do ** and shifts give on Int at run time?
|
||||
def main():
|
||||
var b = 2
|
||||
var e = 10
|
||||
print(b ** e)
|
||||
var one = 1
|
||||
var neg = -16
|
||||
print(one << 3, neg >> 2)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what does / do on two Int values, positive and negative?
|
||||
def main():
|
||||
var a = 7
|
||||
var b = 2
|
||||
print(a / b)
|
||||
print(-a / b)
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: does UInt8 wrap below zero?
|
||||
def main():
|
||||
var u: UInt8 = 0
|
||||
u -= 1
|
||||
print(u)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: is parallelize importable from std.algorithm in 1.1.0?
|
||||
from std.algorithm import parallelize
|
||||
|
||||
|
||||
def main():
|
||||
print("parallelize imported")
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: is vectorize importable from std.algorithm in 1.1.0?
|
||||
from std.algorithm import vectorize
|
||||
|
||||
|
||||
def main():
|
||||
print("vectorize imported")
|
||||
@@ -0,0 +1,24 @@
|
||||
# probe: the manual's ASAP example: which values are destroyed, in what order, inside one expression?
|
||||
@fieldwise_init
|
||||
struct Number(Writable):
|
||||
var value: Int
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print(t"Destroying Number(value={self.value})")
|
||||
|
||||
def __add__(self, other: Number) -> Number:
|
||||
return Number(self.value + other.value)
|
||||
|
||||
def __sub__(self, other: Number) -> Number:
|
||||
return Number(self.value - other.value)
|
||||
|
||||
def __mul__(self, other: Number) -> Number:
|
||||
return Number(self.value * other.value)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Number(1)
|
||||
var b = Number(2)
|
||||
var c = Number(3)
|
||||
var d = Number(4)
|
||||
print(a + (b - c) * d)
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: Stage 4 prediction: an unread value dies after its declaration; a value borrowed by print lives until print returns
|
||||
# expect-stdout: init a
|
||||
# expect-stdout: init b
|
||||
# expect-stdout: bye b
|
||||
# expect-stdout: using a
|
||||
# expect-stdout: bye a
|
||||
# expect-stdout: end
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.n = copy.n + "2"
|
||||
print("copy", copy.n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
var b = L("b")
|
||||
print("using", a.n)
|
||||
print("end")
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: used in both branches: does it die right after its last use in each?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
if flag:
|
||||
print("then uses", a.n)
|
||||
print("then after use")
|
||||
else:
|
||||
print("else uses", a.n)
|
||||
print("else after use")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
print("---")
|
||||
run(False)
|
||||
@@ -0,0 +1,26 @@
|
||||
# probe: declared inside a branch and used there: does it die within the branch?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
if flag:
|
||||
var a = L("a")
|
||||
print("then uses", a.n)
|
||||
print("then after use")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
@@ -0,0 +1,31 @@
|
||||
# probe: used in the then branch only: in the else branch, where does it die (before the else body, after it, or after the if)?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
print("before if")
|
||||
if flag:
|
||||
print("then uses", a.n)
|
||||
print("then after use")
|
||||
else:
|
||||
print("else body")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
print("---")
|
||||
run(False)
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: which lifecycle methods run for .copy(), ^, and the end of main?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("x")
|
||||
var b = a.copy()
|
||||
var c = a^
|
||||
print("holding", b.name, c.name)
|
||||
@@ -0,0 +1,29 @@
|
||||
# probe: an early return: is the value destroyed before the return on that path?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def run(flag: Bool) -> Int:
|
||||
var a = L("a")
|
||||
if flag:
|
||||
print("returning early")
|
||||
return 1
|
||||
print("late uses", a.n)
|
||||
return 2
|
||||
|
||||
|
||||
def main():
|
||||
print(run(True))
|
||||
print("---")
|
||||
print(run(False))
|
||||
@@ -0,0 +1,32 @@
|
||||
# probe: in what order are a struct's fields destroyed?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
struct Pair(Movable):
|
||||
var first: Loud
|
||||
var second: Loud
|
||||
|
||||
def __init__(out self, var first: Loud, var second: Loud):
|
||||
self.first = first^
|
||||
self.second = second^
|
||||
|
||||
|
||||
def main():
|
||||
var p = Pair(Loud("x"), Loud("y"))
|
||||
print("made pair", p.first.name, p.second.name)
|
||||
@@ -0,0 +1,26 @@
|
||||
# probe: is a variable destroyed at its last use, before later statements run?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
print("a made")
|
||||
var b = Loud("b")
|
||||
print("b made, a used last at:", a.name)
|
||||
print("end of main")
|
||||
@@ -0,0 +1,25 @@
|
||||
# probe: when a List of values is destroyed, in what order do its elements go?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var l = List[Loud]()
|
||||
l.append(Loud("one"))
|
||||
l.append(Loud("two"))
|
||||
print("list built", len(l))
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: declared inside a loop body: does each pass destroy its own value at its last use?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
for i in range(2):
|
||||
var a = L(String(i))
|
||||
print("pass uses", a.n)
|
||||
print("pass after use")
|
||||
print("after loop")
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: declared before a loop, read in every pass: does it die after the loop, or in the last pass?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
for i in range(2):
|
||||
print("pass", i, a.n)
|
||||
print("pass after use", i)
|
||||
print("after loop")
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: read before a loop and never after: does it die before the loop starts?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
print("uses", a.n)
|
||||
for i in range(2):
|
||||
print("pass", i)
|
||||
print("after loop")
|
||||
@@ -0,0 +1,37 @@
|
||||
# probe: Stage 4 prediction: x^ into a var argument runs no constructor; into a new variable the move constructor runs
|
||||
# expect-stdout: init a
|
||||
# expect-stdout: copy a
|
||||
# expect-stdout: took a2
|
||||
# expect-stdout: bye a2
|
||||
# expect-stdout: move a
|
||||
# expect-stdout: kept a
|
||||
# expect-stdout: bye a
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.n = copy.n + "2"
|
||||
print("copy", copy.n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def take(var t: L):
|
||||
print("took", t.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("a")
|
||||
var c = a.copy()
|
||||
take(c^)
|
||||
var d = a^
|
||||
print("kept", d.n)
|
||||
@@ -0,0 +1,34 @@
|
||||
# probe: moved out on one path only: on the other path, when does it die?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def take(var t: L):
|
||||
print("took", t.n)
|
||||
|
||||
|
||||
def run(flag: Bool):
|
||||
var a = L("a")
|
||||
if flag:
|
||||
take(a^)
|
||||
print("then after take")
|
||||
else:
|
||||
print("else body")
|
||||
print("after if")
|
||||
|
||||
|
||||
def main():
|
||||
run(True)
|
||||
print("---")
|
||||
run(False)
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: a mut argument changes the caller's variable
|
||||
def bump(mut x: Int):
|
||||
x += 1
|
||||
|
||||
|
||||
def main():
|
||||
var n = 1
|
||||
bump(n)
|
||||
print(n)
|
||||
@@ -0,0 +1,22 @@
|
||||
# probe: reassigned in every pass and read after the loop: when does each old value die?
|
||||
struct L(Copyable, Movable):
|
||||
var n: String
|
||||
|
||||
def __init__(out self, n: String):
|
||||
self.n = n
|
||||
print("init", n)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.n = move.n^
|
||||
print("move", self.n)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("bye", self.n)
|
||||
|
||||
|
||||
def main():
|
||||
var a = L("0")
|
||||
for i in range(2):
|
||||
a = L(String(i + 1))
|
||||
print("pass", i)
|
||||
print("after loop", a.n)
|
||||
@@ -0,0 +1,24 @@
|
||||
# probe: when a variable is reassigned, when is the old value destroyed?
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("first")
|
||||
a = Loud("second")
|
||||
print("after reassign:", a.name)
|
||||
@@ -0,0 +1,29 @@
|
||||
# probe: passing to a var argument: with ^, and with .copy()
|
||||
struct Loud(Copyable, Movable):
|
||||
var name: String
|
||||
|
||||
def __init__(out self, name: String):
|
||||
self.name = name
|
||||
print("init", name)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.name = copy.name + "'"
|
||||
print("copy", copy.name)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
print("move", self.name)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("destroy", self.name)
|
||||
|
||||
|
||||
def take(var t: Loud):
|
||||
print("took", t.name)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Loud("a")
|
||||
take(a.copy())
|
||||
take(a^)
|
||||
print("end")
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: what happens when a List is indexed past its end at run time?
|
||||
def main():
|
||||
var l = [1, 2, 3]
|
||||
var i = 3
|
||||
print("before")
|
||||
print(l[i])
|
||||
print("after")
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: is a run-time negative index allowed on a List?
|
||||
def main():
|
||||
var l = [1, 2, 3]
|
||||
var i = -1
|
||||
print(l[i])
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: append, sort, pop and len on a List
|
||||
def main():
|
||||
var l = [3, 1, 2]
|
||||
l.append(5)
|
||||
print(len(l))
|
||||
sort(l)
|
||||
print(l)
|
||||
print(l.pop())
|
||||
print(l)
|
||||
@@ -0,0 +1,7 @@
|
||||
# probe: how are Bool and Optional printed?
|
||||
def main():
|
||||
print(True, False)
|
||||
var none: Optional[Int] = None
|
||||
var some: Optional[Int] = 3
|
||||
print(none)
|
||||
print(some)
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: can a Dict be printed directly?
|
||||
def main():
|
||||
var d = {"a": 1, "b": 2}
|
||||
print(d)
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: can a List be printed, and how do its elements look?
|
||||
def main():
|
||||
var nums = [1, 2, 3]
|
||||
print(nums)
|
||||
var words = ["a", "b"]
|
||||
print(words)
|
||||
var empty = List[Int]()
|
||||
print(empty)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: do print's sep and end work as in Python?
|
||||
def main():
|
||||
print(1, 2, 3, sep="-", end="!\n")
|
||||
print("a", "b")
|
||||
print()
|
||||
print("last")
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: can a Tuple be printed directly?
|
||||
def main():
|
||||
var t = (1, "a", 2.5)
|
||||
print(t)
|
||||
@@ -0,0 +1,9 @@
|
||||
# probe: range with a negative step, an empty range, and a reversed bound
|
||||
def main():
|
||||
for i in range(5, 0, -2):
|
||||
print(i)
|
||||
for i in range(0):
|
||||
print("never")
|
||||
for i in range(3, 1):
|
||||
print("never either")
|
||||
print("done")
|
||||
@@ -0,0 +1,8 @@
|
||||
# probe: in what order does a Set of Int iterate?
|
||||
from std.collections import Set
|
||||
|
||||
|
||||
def main():
|
||||
var s: Set[Int] = {5, 1, 3, 10, 2}
|
||||
for x in s:
|
||||
print(x)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: printing a SIMD value, elementwise *, and reduce_add
|
||||
def main():
|
||||
var v = SIMD[DType.float32, 4](1, 2, 3, 4)
|
||||
print(v)
|
||||
print(v * 2)
|
||||
print(v.reduce_add())
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: String(n), +, and t-string interpolation
|
||||
def main():
|
||||
var n = 3
|
||||
print("n=" + String(n))
|
||||
print(t"n={n} twice={n * 2}")
|
||||
print(String(2.5))
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: how do String comparisons and chained comparisons behave?
|
||||
def main():
|
||||
print("apple" < "banana", "a" == "a", "B" < "a")
|
||||
print(1 < 2 < 3, 3 > 2 > 2)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: what do s[byte=i] and s[codepoint=i] return on a non-ASCII string?
|
||||
def main():
|
||||
var s = String("héllo")
|
||||
print(s[byte=0])
|
||||
print(s[codepoint=1])
|
||||
print(String(s[codepoint=1]))
|
||||
@@ -0,0 +1,5 @@
|
||||
# probe: what does iterating codepoint_slices() yield?
|
||||
def main():
|
||||
var s = String("hé!")
|
||||
for cp in s.codepoint_slices():
|
||||
print(cp)
|
||||
@@ -0,0 +1,6 @@
|
||||
# probe: len(String) counts bytes; what do byte_length and count_codepoints give?
|
||||
def main():
|
||||
var s = String("héllo")
|
||||
print(len(s))
|
||||
print(s.byte_length())
|
||||
print(s.count_codepoints())
|
||||
@@ -0,0 +1,4 @@
|
||||
# probe: does s[codepoint=a:b] slice by codepoint?
|
||||
def main():
|
||||
var s = String("héllo")
|
||||
print(s[codepoint=1:3])
|
||||
@@ -0,0 +1,11 @@
|
||||
# probe: a struct that implements write_to prints through print()
|
||||
@fieldwise_init
|
||||
struct P(Writable):
|
||||
var x: Int
|
||||
|
||||
def write_to(self, mut writer: Some[Writer]):
|
||||
writer.write("P(", self.x, ")")
|
||||
|
||||
|
||||
def main():
|
||||
print(P(3))
|
||||
Reference in new issue
Block a user