generic types, any -> opaque

This commit is contained in:
2026-07-17 11:44:05 +02:00
parent af08d1888a
commit 1471c229c2
20 changed files with 284 additions and 167 deletions

View File

@@ -14,15 +14,15 @@ extern IsKeyDown
struct CHIP8
memory: ptr
pc: i64
stack: Array
stack: Array<i64>
sp: i64
reg: ptr
I: i64
delay_timer: u8
sound_timer: u8
keyboard: Array
keyboard: Array<i64>
display: ptr
keyboard_map: Array
keyboard_map: Array<i64>
func CHIP8.init[c: CHIP8] : void
c->memory = mem.alloc(4096)
@@ -242,14 +242,14 @@ func CHIP8.step[c: CHIP8] : void
if IsKeyDown(c->keyboard_map->nth(c->reg[x] as i64))
c->pc += 2
else if kk == 0xa1
if !IsKeyDown(c->keyboard_map->nth(c->reg[x] as i64))
if !(IsKeyDown(c->keyboard_map->nth(c->reg[x] as i64)) as bool)
c->pc += 2
else if op == 0xf
if kk == 0x07
c->reg[x] = c->delay_timer
else if kk == 0x0A
key_pressed := false
while !key_pressed && !WindowShouldClose()
while !key_pressed && !(WindowShouldClose() as bool)
for i in 0..16
if IsKeyDown(c->keyboard_map->nth(i))
key_pressed = true
@@ -305,7 +305,7 @@ func main[argc: i64, argv: ptr] : i64
InitWindow(640, 320, "CHIP-8")
SetTargetFPS(60)
while !WindowShouldClose()
while !(WindowShouldClose() as bool)
if c->delay_timer > 0
c->delay_timer = c->delay_timer - 1
if c->sound_timer > 0

View File

@@ -4,7 +4,7 @@ include "$/containers.zr"
func main[] : i64
N := 20
grid := []
grid := [] as Array<Array<i64> >
grid->push([8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8])
grid->push([49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0])
grid->push([81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65])
@@ -30,10 +30,10 @@ func main[] : i64
for i in 0..N-3
for j in 0..N-3
h : i64 = (grid->nth(i) as Array)->nth(j) * (grid->nth(i) as Array)->nth(j+1) * (grid->nth(i) as Array)->nth(j+2) * (grid->nth(i) as Array)->nth(j+3)
v : i64 = (grid->nth(j) as Array)->nth(i) * (grid->nth(j+1) as Array)->nth(i) * (grid->nth(j+2) as Array)->nth(i) * (grid->nth(j+3) as Array)->nth(i)
d1 : i64 = (grid->nth(i) as Array)->nth(j) * (grid->nth(i+1) as Array)->nth(j+1) * (grid->nth(i+2) as Array)->nth(j+2) * (grid->nth(i+3) as Array)->nth(j+3)
d2 : i64 = (grid->nth(i) as Array)->nth(N-j-1) * (grid->nth(i+1) as Array)->nth(N-j-2) * (grid->nth(i+2) as Array)->nth(N-j-3) * (grid->nth(i+3) as Array)->nth(N-j-4)
h : i64 = grid->nth(i)->nth(j) * grid->nth(i)->nth(j+1) * grid->nth(i)->nth(j+2) * grid->nth(i)->nth(j+3)
v : i64 = grid->nth(j)->nth(i) * grid->nth(j+1)->nth(i) * grid->nth(j+2)->nth(i) * grid->nth(j+3)->nth(i)
d1 : i64 = grid->nth(i)->nth(j) * grid->nth(i+1)->nth(j+1) * grid->nth(i+2)->nth(j+2) * grid->nth(i+3)->nth(j+3)
d2 : i64 = grid->nth(i)->nth(N-j-1) * grid->nth(i+1)->nth(N-j-2) * grid->nth(i+2)->nth(N-j-3) * grid->nth(i+3)->nth(N-j-4)
out = math.max(out, math.max(h, math.max(v, math.max(d1, d2))))
io.println_i64(out)

View File

@@ -1,17 +1,17 @@
include "$/io.zr"
include "$/containers.zr"
func findmax[triangle: Array, row: i64, col: i64] : i64
func findmax[triangle: Array<Array<i64> >, row: i64, col: i64] : i64
if row == 14
return (triangle->nth(row) as Array)->nth(col)
return triangle->nth(row)->nth(col)
left := findmax(triangle, row + 1, col)
right := findmax(triangle, row + 1, col + 1)
return (triangle->nth(row) as Array)->nth(col) + math.max(left, right)
return triangle->nth(row)->nth(col) + math.max(left, right)
func main[] : i64
triangle := []
triangle := [] as Array<Array<i64> >
triangle->push([75])
triangle->push([95, 64])
triangle->push([17, 47, 82])

View File

@@ -1,7 +1,7 @@
include "$/io.zr"
include "$/containers.zr"
func multiply[n: Array, x: i64] : void
func multiply[n: Array<i64>, x: i64] : void
carry := 0
for i in 0..n->size
prod : i64 = n->nth(i) * x + carry

View File

@@ -26,14 +26,14 @@ func main[] : i64
InitWindow(800, 600, "Hello, World!")
SetTargetFPS(60)
while !WindowShouldClose()
if IsKeyDown(KEY_W) & 255
while !(WindowShouldClose() as bool)
if IsKeyDown(KEY_W) as u8 & 255
y -= 10
if IsKeyDown(KEY_S) & 255
if IsKeyDown(KEY_S) as u8 & 255
y += 10
if IsKeyDown(KEY_A) & 255
if IsKeyDown(KEY_A) as u8 & 255
x -= 10
if IsKeyDown(KEY_D) & 255
if IsKeyDown(KEY_D) as u8 & 255
x += 10
BeginDrawing()

View File

@@ -1,8 +1,8 @@
include "$/io.zr"
include "$/containers.zr"
func rule110_step[state: Array] : void
new_state := []
func rule110_step[state: Array<bool>] : void
new_state := [] as Array<bool>
for i in 0..state->size
left := false
@@ -18,7 +18,7 @@ func rule110_step[state: Array] : void
mem.copy(new_state->data, state->data, state->size * 8)
new_state->free()
func print_state[state: Array]: void
func print_state[state: Array<bool>]: void
for i in 0..state->size
if state->nth(i)
io.print_char('#')
@@ -29,7 +29,7 @@ func print_state[state: Array]: void
func main[] : i64
SIZE := 60
state := []
state := [] as Array<bool>
for i in 0..SIZE
state->push(false)
state->push(true)

View File

@@ -20,13 +20,13 @@ func main[] : i64
db := 0
stmt := 0
rc = sqlite3_open("todo.db", ^db)
rc = sqlite3_open("todo.db", ^db) as i64
if rc
panic("failed to open db")
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS todo(id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL);", 0, 0, 0)
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS todo(id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL);", 0, 0, 0) as i64
if rc
panic(sqlite3_errmsg(db))
panic(sqlite3_errmsg(db) as str)
while true
io.println("1. List tasks")
@@ -43,7 +43,7 @@ func main[] : i64
io.println("============")
sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, ^stmt, 0)
while sqlite3_step(stmt) == SQLITE_ROW
while sqlite3_step(stmt) as i64 == SQLITE_ROW
id := sqlite3_column_int(stmt, 0) as i64
task := sqlite3_column_text(stmt, 1) as str
io.printf("%d - %s\n", id, task)
@@ -55,8 +55,8 @@ func main[] : i64
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
sqlite3_bind_text(stmt, 1, task, -1, 0)
if sqlite3_step(stmt) != SQLITE_DONE
panic(sqlite3_errmsg(db))
if sqlite3_step(stmt) as i64 != SQLITE_DONE
panic(sqlite3_errmsg(db) as str)
io.println("\nTask added\n")
else if choice == 3
@@ -65,8 +65,8 @@ func main[] : i64
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
sqlite3_bind_int(stmt, 1, id, -1, 0)
if sqlite3_step(stmt) != SQLITE_DONE
panic(sqlite3_errmsg(db))
if sqlite3_step(stmt) as i64 != SQLITE_DONE
panic(sqlite3_errmsg(db) as str)
io.println("\nTask deleted\n")