boot: maps, for k, v in, for i, x in

map[K]V, [:] and ["a": 1] literals, m[k] reads and writes (m[k] += 1 starts from
zero), has/get/remove/clear/keys/values, .len, copy and printing, and a loop over
a map that stops with an error when it adds keys. for i, x in list gives the
index too.

boot.sh: 17 passed, 0 failed, 5 skipped (all five import C).
This commit is contained in:
Nexoniarz 2026-10-25 18:53:39 +01:00
commit 083d66c82b
4 changed files with 460 additions and 16 deletions

View file

@ -21,6 +21,14 @@ fn arrayOf(Type t) -> Type {
return out
}
// map[K]V: elem holds the key, then the value
fn mapOf(Type k, Type v) -> Type {
out := Type(kind: "map")
out.elem.push(k)
out.elem.push(v)
return out
}
fn ptrTo(Type t) -> Type {
out := Type(kind: "ptr")
out.elem.push(t)
@ -34,14 +42,16 @@ fn same(Type a, Type b) -> bool {
if a.kind == "struct" || a.kind == "named" {
return a.name == b.name && a.module == b.module
}
if a.kind == "array" || a.kind == "ptr" {
if a.kind == "array" || a.kind == "ptr" || a.kind == "map" {
if a.elem.len != b.elem.len {
return false
}
if a.elem.len == 0 {
return true
for i in 0..a.elem.len {
if !same(a.elem[i], b.elem[i]) {
return false
}
}
return same(a.elem[0], b.elem[0])
return true
}
return true
}
@ -50,6 +60,9 @@ fn show(Type t) -> str {
if t.kind == "array" {
return "[]" + show(t.elem[0])
}
if t.kind == "map" {
return "map[" + show(t.elem[0]) + "]" + show(t.elem[1])
}
if t.kind == "ptr" {
if t.elem.len == 0 {
return "ptr"