From 3fcd82cc04c5eb2936358c8b65a816ba2d7fedb4 Mon Sep 17 00:00:00 2001 From: Toni Date: Wed, 11 Mar 2026 20:44:55 +0100 Subject: [PATCH] replace array with an actual struct now that we have them --- examples/chip8.zr | 10 +-- examples/curl.zr | 8 +- examples/puzzles/aoc2024_01.zr | 18 ++--- examples/puzzles/aoc2024_02.zr | 27 +++---- examples/puzzles/aoc2024_04.zr | 14 ++-- examples/puzzles/aoc2024_05.zr | 50 ++++++------ examples/puzzles/aoc2024_07.zr | 37 ++++----- examples/puzzles/aoc2025_01.zr | 10 +-- examples/puzzles/aoc2025_02.zr | 14 ++-- examples/puzzles/aoc2025_03.zr | 10 +-- examples/puzzles/euler_11.zr | 2 +- examples/puzzles/euler_16.zr | 6 +- examples/puzzles/euler_17.zr | 6 +- examples/puzzles/euler_18.zr | 4 +- examples/puzzles/euler_20.zr | 8 +- examples/quicksort.zr | 6 +- examples/rule110.zr | 15 ++-- examples/sqlite_todo.zr | 8 +- src/codegen_x86_64.rs | 13 +++- src/std/crypto.zr | 8 +- src/std/std.zr | 138 +++++++++++++++------------------ test.zr | 32 ++++---- 22 files changed, 220 insertions(+), 224 deletions(-) diff --git a/examples/chip8.zr b/examples/chip8.zr index 1646d7a..3ca94ba 100644 --- a/examples/chip8.zr +++ b/examples/chip8.zr @@ -11,15 +11,15 @@ extern IsKeyDown struct CHIP8 memory: ptr pc: i64 - stack: array + stack: Array sp: i64 reg: ptr I: i64 delay_timer: i64 sound_timer: i64 - keyboard: array + keyboard: Array display: ptr - keyboard_map: array + keyboard_map: Array func chip8_create[] : CHIP8 let c: CHIP8 = new CHIP8 @@ -33,7 +33,7 @@ func chip8_create[] : CHIP8 c->stack = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] c->keyboard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - let fonts: array = [0xf0, 0x90, 0x90, 0x90, 0xf0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xf0, 0x10, 0xf0, 0x80, 0xf0, 0xf0, 0x10, 0xf0, 0x10, 0xf0, 0x90, 0x90, 0xf0, 0x10, 0x10, 0xf0, 0x80, 0xf0, 0x10, 0xf0, 0xf0, 0x80, 0xf0, 0x90, 0xf0, 0xf0, 0x10, 0x20, 0x40, 0x40, 0xf0, 0x90, 0xf0, 0x90, 0xf0, 0xf0, 0x90, 0xf0, 0x10, 0xf0, 0xf0, 0x90, 0xf0, 0x90, 0x90, 0xe0, 0x90, 0xe0, 0x90, 0xe0, 0xf0, 0x80, 0x80, 0x80, 0xf0, 0xe0, 0x90, 0x90, 0x90, 0xe0, 0xf0, 0x80, 0xf0, 0x80, 0xf0, 0xf0, 0x80, 0xf0, 0x80, 0x80] + let fonts: Array = [0xf0, 0x90, 0x90, 0x90, 0xf0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xf0, 0x10, 0xf0, 0x80, 0xf0, 0xf0, 0x10, 0xf0, 0x10, 0xf0, 0x90, 0x90, 0xf0, 0x10, 0x10, 0xf0, 0x80, 0xf0, 0x10, 0xf0, 0xf0, 0x80, 0xf0, 0x90, 0xf0, 0xf0, 0x10, 0x20, 0x40, 0x40, 0xf0, 0x90, 0xf0, 0x90, 0xf0, 0xf0, 0x90, 0xf0, 0x10, 0xf0, 0xf0, 0x90, 0xf0, 0x90, 0x90, 0xe0, 0x90, 0xe0, 0x90, 0xe0, 0xf0, 0x80, 0x80, 0x80, 0xf0, 0xe0, 0x90, 0x90, 0x90, 0xe0, 0xf0, 0x80, 0xf0, 0x80, 0xf0, 0xf0, 0x80, 0xf0, 0x80, 0x80] for i in 0..80 c->memory[i] = array.nth(fonts, i) mem.free(fonts) @@ -314,7 +314,7 @@ func chip8_step[c: CHIP8] : void else if op == 0xb c->pc = nnn + c->reg[0] else if op == 0xc - c->reg[x] = (math.floor(os.urandom_i64()) % 256) & kk + c->reg[x] = (math.abs(os.urandom_i64()) % 256) & kk else if op == 0xd c->reg[0xf] = 0 for row in 0..n diff --git a/examples/curl.zr b/examples/curl.zr index f0e44b9..32828ca 100644 --- a/examples/curl.zr +++ b/examples/curl.zr @@ -1,14 +1,14 @@ func main[argc: i64, argv: ptr] : i64 if argc < 2 - dbg.panic("url missing") + panic("url missing") let url: str = mem.read64(argv + 8) if str.len(url) <= 7 - dbg.panic("missing url scheme") + panic("missing url scheme") if !str.equal(str.substr(url, 0, 7), "http://") - dbg.panic("invalid url scheme") + panic("invalid url scheme") let url_len: i64 = str.len(url) let host_start = 7 @@ -25,7 +25,7 @@ func main[argc: i64, argv: ptr] : i64 let s: i64 = net.connect(host, 80) if s < 0 - dbg.panic("failed to connect") + panic("failed to connect") // very leaky let req: str = "GET " diff --git a/examples/puzzles/aoc2024_01.zr b/examples/puzzles/aoc2024_01.zr index c827fba..476e43f 100644 --- a/examples/puzzles/aoc2024_01.zr +++ b/examples/puzzles/aoc2024_01.zr @@ -1,29 +1,29 @@ -func part1[l1: array, l2: array] : void +func part1[l1: Array, l2: Array] : void let out = 0 - for i in 0..array.size(l1) + for i in 0..l1->size out = out + math.abs(array.nth(l1, i) - array.nth(l2, i)) io.println_i64(out) -func part2[l1: array, l2: array] : void +func part2[l1: Array, l2: Array] : void let out = 0 - for i in 0..array.size(l1) + for i in 0..l1->size out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i)) io.println_i64(out) func main[] : i64 - let lines: array = io.read_file("input.txt") |> str.split("\n") + let lines: Array = io.read_file("input.txt") |> str.split("\n") - let l1: array = [] - let l2: array = [] + let l1: Array = [] + let l2: Array = [] - for i in 0..array.size(lines) + for i in 0..lines->size let line: str = array.nth(lines, i) - let parts: array = str.split(line, " ") + let parts: Array = str.split(line, " ") array.push(l1, str.parse_i64(array.nth(parts, 0))) array.push(l2, str.parse_i64(array.nth(parts, 1))) diff --git a/examples/puzzles/aoc2024_02.zr b/examples/puzzles/aoc2024_02.zr index 9c87539..4452167 100644 --- a/examples/puzzles/aoc2024_02.zr +++ b/examples/puzzles/aoc2024_02.zr @@ -1,7 +1,7 @@ -func check[report: array] : bool +func check[report: Array] : bool let increasing: bool = array.nth(report, 0) < array.nth(report, 1) - for i in 0..array.size(report)-1 + for i in 0..report->size-1 if array.nth(report, i) > array.nth(report, i + 1) && increasing return false if array.nth(report, i) < array.nth(report, i + 1) && !increasing @@ -13,24 +13,25 @@ func check[report: array] : bool return true -func part1[data: array] : void +func part1[data: Array] : void let out = 0 - for i in 0..array.size(data) + for i in 0..data->size if check(array.nth(data, i)) out = out + 1 io.println_i64(out) -func part2[data: array] : void +func part2[data: Array] : void let out = 0 - for i in 0..array.size(data) + for i in 0..data->size if check(array.nth(data, i)) out = out + 1 else - for j in 0..array.size(array.nth(data, i)) - let sliced: array = array.concat(array.slice(array.nth(data, i), 0, j), array.slice(array.nth(data, i), j + 1, array.size(array.nth(data, i)) - (j + 1))) + let arr: Array = array.nth(data, i) + for j in 0..arr->size + let sliced: Array = array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1))) if check(sliced) out = out + 1 @@ -39,14 +40,14 @@ func part2[data: array] : void io.println_i64(out) func main[] : i64 - let lines: array = io.read_file("input.txt") |> str.split("\n") + let lines: Array = io.read_file("input.txt") |> str.split("\n") - let data: array = [] - for i in 0..array.size(lines) + let data: Array = [] + for i in 0..lines->size let line: str = array.nth(lines, i) - let report: array = str.split(line, " ") - for i in 0..array.size(report) + let report: Array = str.split(line, " ") + for i in 0..report->size array.set(report, i, str.parse_i64(array.nth(report, i))) array.push(data, report) diff --git a/examples/puzzles/aoc2024_04.zr b/examples/puzzles/aoc2024_04.zr index f92b26a..d4b73c6 100644 --- a/examples/puzzles/aoc2024_04.zr +++ b/examples/puzzles/aoc2024_04.zr @@ -1,5 +1,5 @@ -func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool - if x + dx * 3 < 0 || x + dx * 3 >= array.size(lines) || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0)) +func check[lines: Array, x: i64, y: i64, dx: i64, dy: i64] : bool + if x + dx * 3 < 0 || x + dx * 3 >= lines->size || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0)) return false if array.nth(lines, x)[y] != 'X' @@ -13,10 +13,10 @@ func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool return true -func part1[lines: array] : void +func part1[lines: Array] : void let out = 0 - for x in 0..array.size(lines) + for x in 0..lines->size for y in 0..str.len(array.nth(lines, x)) if check(lines, x, y, 0, 1) out = out + 1 @@ -37,10 +37,10 @@ func part1[lines: array] : void io.println_i64(out) -func part2[lines: array] : void +func part2[lines: Array] : void let out = 0 - for x in 1..array.size(lines)-1 + for x in 1..lines->size-1 for y in 1..str.len(array.nth(lines, x))-1 if array.nth(lines, x)[y] == 'A' let s: str = mem.alloc(5) @@ -56,7 +56,7 @@ func part2[lines: array] : void io.println_i64(out) func main[] : i64 - let lines: array = io.read_file("input.txt") |> str.split("\n") + let lines: Array = io.read_file("input.txt") |> str.split("\n") part1(lines) part2(lines) diff --git a/examples/puzzles/aoc2024_05.zr b/examples/puzzles/aoc2024_05.zr index 4a8ba78..dd649cd 100644 --- a/examples/puzzles/aoc2024_05.zr +++ b/examples/puzzles/aoc2024_05.zr @@ -1,22 +1,22 @@ -func rule_exists[rules_left: array, rules_right: array, a: i64, b: i64] : bool - for k in 0..array.size(rules_left) +func rule_exists[rules_left: Array, rules_right: Array, a: i64, b: i64] : bool + for k in 0..rules_left->size if array.nth(rules_left, k) == a && array.nth(rules_right, k) == b return true return false -func check[update: array, rules_left: array, rules_right: array] : bool - for i in 0..array.size(update) +func check[update: Array, rules_left: Array, rules_right: Array] : bool + for i in 0..update->size for j in 0..i if rule_exists(rules_left, rules_right, array.nth(update, i), array.nth(update, j)) return false return true -func sort_by_rules[update: array, rules_left: array, rules_right: array] : void +func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void let swapped: bool = true while swapped swapped = false - for i in 0..array.size(update)-1 + for i in 0..update->size-1 let a: i64 = array.nth(update, i) let b: i64 = array.nth(update, i+1) if rule_exists(rules_left, rules_right, b, a) @@ -25,47 +25,47 @@ func sort_by_rules[update: array, rules_left: array, rules_right: array] : void array.set(update, i+1, tmp) swapped = true -func part1[updates: array, rules_left: array, rules_right: array] : void +func part1[updates: Array, rules_left: Array, rules_right: Array] : void let out = 0 - for i in 0..array.size(updates) - let update: array = array.nth(updates, i) + for i in 0..updates->size + let update: Array = array.nth(updates, i) if check(update, rules_left, rules_right) - out = out + array.nth(update, array.size(update) / 2) + out = out + array.nth(update, update->size / 2) io.println_i64(out) -func part2[updates: array, rules_left: array, rules_right: array] : void +func part2[updates: Array, rules_left: Array, rules_right: Array] : void let out = 0 - for i in 0..array.size(updates) - let update: array = array.nth(updates, i) + for i in 0..updates->size + let update: Array = array.nth(updates, i) if !check(update, rules_left, rules_right) sort_by_rules(update, rules_left, rules_right) - out = out + array.nth(update, array.size(update) / 2) + out = out + array.nth(update, update->size / 2) io.println_i64(out) func main[] : i64 - let data: array = io.read_file("input.txt") |> str.split("\n\n") + let data: Array = io.read_file("input.txt") |> str.split("\n\n") - let rules_left: array = [] - let rules_right: array = [] - let rules_lines: array = str.split(array.nth(data, 0), "\n") - for i in 0..array.size(rules_lines) + let rules_left: Array = [] + let rules_right: Array = [] + let rules_lines: Array = str.split(array.nth(data, 0), "\n") + for i in 0..rules_lines->size let line: str = array.nth(rules_lines, i) - let parts: array = str.split(line, "|") + let parts: Array = str.split(line, "|") array.push(rules_left, str.parse_i64(array.nth(parts, 0))) array.push(rules_right, str.parse_i64(array.nth(parts, 1))) - let updates: array = [] - let updates_lines: array = str.split(array.nth(data, 1), "\n") - for i in 0..array.size(updates_lines) + let updates: Array = [] + let updates_lines: Array = str.split(array.nth(data, 1), "\n") + for i in 0..updates_lines->size let line: str = array.nth(updates_lines, i) - let xs: array = str.split(line, ",") + let xs: Array = str.split(line, ",") - for i in 0..array.size(xs) + for i in 0..xs->size array.set(xs, i, str.parse_i64(array.nth(xs, i))) array.push(updates, xs) diff --git a/examples/puzzles/aoc2024_07.zr b/examples/puzzles/aoc2024_07.zr index aca4486..620b9ff 100644 --- a/examples/puzzles/aoc2024_07.zr +++ b/examples/puzzles/aoc2024_07.zr @@ -9,23 +9,24 @@ func concat[a: i64, b: i64] : i64 mem.free(ab_str) return out -func solve[ops: array, e: array] : i64 - let n: i64 = array.size(array.nth(e, 1)) - 1 - let indices: array = [] +func solve[ops: Array, e: Array] : i64 + let e_1: Array = array.nth(e, 1) + let n: i64 = e_1->size - 1 + let indices: Array = [] for i in 0..n array.push(indices, 0) while true - let res: i64 = array.nth(array.nth(e, 1), 0) + let res: i64 = array.nth(e_1, 0) for i in 0..n let op: str = array.nth(ops, array.nth(indices, i)) if str.equal(op, "add") - res = res + array.nth(array.nth(e, 1), i + 1) + res = res + array.nth(e_1, i + 1) else if str.equal(op, "mul") - res = res * array.nth(array.nth(e, 1), i + 1) + res = res * array.nth(e_1, i + 1) else if str.equal(op, "concat") - res = concat(res, array.nth(array.nth(e, 1), i + 1)) + res = concat(res, array.nth(e_1, i + 1)) if res == array.nth(e, 0) return res @@ -34,7 +35,7 @@ func solve[ops: array, e: array] : i64 while i >= 0 array.set(indices, i, array.nth(indices, i) + 1) - if array.nth(indices, i) < array.size(ops) + if array.nth(indices, i) < ops->size done = false break array.set(indices, i, 0) @@ -43,32 +44,32 @@ func solve[ops: array, e: array] : i64 if done return 0 -func part1[equations: array] : void +func part1[equations: Array] : void let out = 0 - for i in 0..array.size(equations) + for i in 0..equations->size out = out + solve(["add", "mul"], array.nth(equations, i)) io.println_i64(out) -func part2[equations: array] : void +func part2[equations: Array] : void let out = 0 - for i in 0..array.size(equations) + for i in 0..equations->size out = out + solve(["add", "mul", "concat"], array.nth(equations, i)) io.println_i64(out) func main[] : i64 - let lines: array = io.read_file("input.txt") |> str.split("\n") - let equations: array = [] + let lines: Array = io.read_file("input.txt") |> str.split("\n") + let equations: Array = [] - for i in 0..array.size(lines) + for i in 0..lines->size let line: str = array.nth(lines, i) - let parts: array = str.split(line, ": ") + let parts: Array = str.split(line, ": ") - let xs: array = str.split(array.nth(parts, 1), " ") - for j in 0..array.size(xs) + let xs: Array = str.split(array.nth(parts, 1), " ") + for j in 0..xs->size array.set(xs, j, str.parse_i64(array.nth(xs, j))) array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs]) diff --git a/examples/puzzles/aoc2025_01.zr b/examples/puzzles/aoc2025_01.zr index 4e130c5..5c76760 100644 --- a/examples/puzzles/aoc2025_01.zr +++ b/examples/puzzles/aoc2025_01.zr @@ -1,8 +1,8 @@ -func part1[lines: array] : void +func part1[lines: Array] : void let password = 0 let dial = 50 - for i in 0..array.size(lines) + for i in 0..lines->size let line: str = array.nth(lines, i) let dir: u8 = line[0] let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64() @@ -21,11 +21,11 @@ func part1[lines: array] : void io.println_i64(password) -func part2[lines: array] : void +func part2[lines: Array] : void let password = 0 let dial = 50 - for i in 0..array.size(lines) + for i in 0..lines->size let line: str = array.nth(lines, i) let dir: u8 = line[0] let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64() @@ -47,7 +47,7 @@ func part2[lines: array] : void io.println_i64(password) func main[] : i64 - let lines: array = io.read_file("input.txt") |> str.split("\n") + let lines: Array = io.read_file("input.txt") |> str.split("\n") part1(lines) part2(lines) diff --git a/examples/puzzles/aoc2025_02.zr b/examples/puzzles/aoc2025_02.zr index 025bede..07a4643 100644 --- a/examples/puzzles/aoc2025_02.zr +++ b/examples/puzzles/aoc2025_02.zr @@ -8,11 +8,11 @@ func part1_is_invalid_id[s: str] : bool return str.equal(first, second) -func part1[ranges: array] : void +func part1[ranges: Array] : void let sum = 0 - for i in 0..array.size(ranges) - let parts: array = array.nth(ranges, i) |> str.split("-") + for i in 0..ranges->size + let parts: Array = array.nth(ranges, i) |> str.split("-") let start: i64 = array.nth(parts, 0) |> str.parse_i64() let end: i64 = array.nth(parts, 1) |> str.parse_i64() @@ -39,11 +39,11 @@ func part2_is_invalid_id[s: str] : bool return true return false -func part2[ranges: array] : void +func part2[ranges: Array] : void let sum = 0 - for i in 0..array.size(ranges) - let parts: array = array.nth(ranges, i) |> str.split("-") + for i in 0..ranges->size + let parts: Array = array.nth(ranges, i) |> str.split("-") let start: i64 = array.nth(parts, 0) |> str.parse_i64() let end: i64 = array.nth(parts, 1) |> str.parse_i64() @@ -54,7 +54,7 @@ func part2[ranges: array] : void io.println_i64(sum) func main[] : i64 - let ranges: array = io.read_file("input.txt") |> str.split(",") + let ranges: Array = io.read_file("input.txt") |> str.split(",") part1(ranges) part2(ranges) diff --git a/examples/puzzles/aoc2025_03.zr b/examples/puzzles/aoc2025_03.zr index 8121da5..fe16acf 100644 --- a/examples/puzzles/aoc2025_03.zr +++ b/examples/puzzles/aoc2025_03.zr @@ -1,7 +1,7 @@ -func part1[lines: array] : void +func part1[lines: Array] : void let sum = 0 - for i in 0..array.size(lines) + for i in 0..lines->size let line: str = array.nth(lines, i) let largest = 0 @@ -35,17 +35,17 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64 else return largest -func part2[lines: array] : void +func part2[lines: Array] : void let sum = 0 - for i in 0..array.size(lines) + for i in 0..lines->size let line: str = array.nth(lines, i) sum = sum + part2_rec(line, 0, 12) io.println_i64(sum) func main[] : i64 - let lines: array = io.read_file("input.txt") |> str.split("\n") + let lines: Array = io.read_file("input.txt") |> str.split("\n") part1(lines) part2(lines) diff --git a/examples/puzzles/euler_11.zr b/examples/puzzles/euler_11.zr index 8e1d8fa..7c7fc7e 100644 --- a/examples/puzzles/euler_11.zr +++ b/examples/puzzles/euler_11.zr @@ -1,7 +1,7 @@ func main[] : i64 let N = 20 - let grid: array = [] + let grid: Array = [] array.push(grid, [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8]) array.push(grid, [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0]) array.push(grid, [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65]) diff --git a/examples/puzzles/euler_16.zr b/examples/puzzles/euler_16.zr index c8e6563..2a6df10 100644 --- a/examples/puzzles/euler_16.zr +++ b/examples/puzzles/euler_16.zr @@ -1,10 +1,10 @@ func main[] : i64 - let n: array = [] + let n: Array = [] array.push(n, 1) for j in 0..1000 let carry = 0 - for i in 0..array.size(n) + for i in 0..n->size let tmp: i64 = array.nth(n, i) * 2 + carry array.set(n, i, tmp % 10) carry = tmp / 10 @@ -13,7 +13,7 @@ func main[] : i64 carry = carry / 10 let sum = 0 - for i in 0..array.size(n) + for i in 0..n->size sum = sum + array.nth(n, i) io.println_i64(sum) diff --git a/examples/puzzles/euler_17.zr b/examples/puzzles/euler_17.zr index 55f8d10..1727574 100644 --- a/examples/puzzles/euler_17.zr +++ b/examples/puzzles/euler_17.zr @@ -1,7 +1,7 @@ func main[] : i64 - let s1: array = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4] - let s2: array = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8] - let s3: array = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6] + let s1: Array = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4] + let s2: Array = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8] + let s3: Array = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6] let sum = 0 diff --git a/examples/puzzles/euler_18.zr b/examples/puzzles/euler_18.zr index 9ae77d1..8ca44a5 100644 --- a/examples/puzzles/euler_18.zr +++ b/examples/puzzles/euler_18.zr @@ -1,4 +1,4 @@ -func findmax[triangle: array, row: i64, col: i64] : i64 +func findmax[triangle: Array, row: i64, col: i64] : i64 if row == 14 return array.nth(array.nth(triangle, row), col) @@ -8,7 +8,7 @@ func findmax[triangle: array, row: i64, col: i64] : i64 return array.nth(array.nth(triangle, row), col) + math.max(left, right) func main[] : i64 - let triangle: array = [] + let triangle: Array = [] array.push(triangle, [75]) array.push(triangle, [95, 64]) array.push(triangle, [17, 47, 82]) diff --git a/examples/puzzles/euler_20.zr b/examples/puzzles/euler_20.zr index 2e22edf..4d631f6 100644 --- a/examples/puzzles/euler_20.zr +++ b/examples/puzzles/euler_20.zr @@ -1,6 +1,6 @@ -func multiply[n: array, x: i64] : void +func multiply[n: Array, x: i64] : void let carry = 0 - for i in 0..array.size(n) + for i in 0..n->size let prod: i64 = array.nth(n, i) * x + carry array.set(n, i, prod % 10) carry = prod / 10 @@ -9,13 +9,13 @@ func multiply[n: array, x: i64] : void carry = carry / 10 func main[] : i64 - let n: array = [1] + let n: Array = [1] for i in 2..101 multiply(n, i) let sum = 0 - for i in 0..array.size(n) + for i in 0..n->size sum = sum + array.nth(n, i) io.println_i64(sum) diff --git a/examples/quicksort.zr b/examples/quicksort.zr index 253f3be..e0315d8 100644 --- a/examples/quicksort.zr +++ b/examples/quicksort.zr @@ -1,15 +1,15 @@ func main[] : i64 - let arr: array = [] + let arr: Array = [] for i in 0..10 array.push(arr, math.abs(os.urandom_i64()) % 1000) - for i in 0..array.size(arr) + for i in 0..arr->size io.println_i64(array.nth(arr, i)) io.println("------------") alg.quicksort(arr) - for i in 0..array.size(arr) + for i in 0..arr->size io.println_i64(array.nth(arr, i)) array.free(arr) diff --git a/examples/rule110.zr b/examples/rule110.zr index a92eb13..0b42b63 100644 --- a/examples/rule110.zr +++ b/examples/rule110.zr @@ -1,22 +1,21 @@ -func rule110_step[state: array] : array - let new_state: array = [] - let state_len: i64 = array.size(state) +func rule110_step[state: Array] : Array + let new_state: Array = [] - for i in 0..state_len + for i in 0..state->size let left: bool = false if i - 1 >= 0 left = array.nth(state, i - 1) let center: bool = array.nth(state, i) let right: bool = false - if i + 1 < state_len + if i + 1 < state->size right = array.nth(state, i + 1) array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right))) return new_state -func print_state[state: array]: void - for i in 0..array.size(state) +func print_state[state: Array]: void + for i in 0..state->size if array.nth(state, i) io.print_char('#') else @@ -26,7 +25,7 @@ func print_state[state: array]: void func main[] : i64 let SIZE = 60 - let state: array = [] + let state: Array = [] for i in 0..SIZE array.push(state, false) array.push(state, true) diff --git a/examples/sqlite_todo.zr b/examples/sqlite_todo.zr index fe27004..4ce2f44 100644 --- a/examples/sqlite_todo.zr +++ b/examples/sqlite_todo.zr @@ -17,11 +17,11 @@ func main[] : i64 rc = sqlite3_open("todo.db", ^db) if rc - dbg.panic("failed to open db") + 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) if rc - dbg.panic(sqlite3_errmsg(db)) + panic(sqlite3_errmsg(db)) while true io.println("1. List tasks") @@ -54,7 +54,7 @@ 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) != 101 - dbg.panic(sqlite3_errmsg(db)) + panic(sqlite3_errmsg(db)) io.println("\nTask added\n") else if choice == 3 @@ -64,7 +64,7 @@ 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) != 101 - dbg.panic(sqlite3_errmsg(db)) + panic(sqlite3_errmsg(db)) io.println("\nTask deleted\n") diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index 8805d69..d138ab7 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -70,7 +70,7 @@ macro_rules! emit { static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"]; // TODO: currently they are all just 64 bit values -static BUILTIN_TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "array"]; +static BUILTIN_TYPES: [&str; 6] = ["void", "u8", "i64", "str", "bool", "ptr"]; pub struct CodegenX86_64<'a> { output: String, @@ -226,7 +226,7 @@ _builtin_environ: Stmt::Function { name, params, - return_type: _, + return_type, body, exported, } => { @@ -239,10 +239,17 @@ _builtin_environ: emit!(&mut self.output, " mov rbp, rsp"); emit!(&mut self.output, " sub rsp, 256"); // TODO + if !self.is_valid_type_name(&return_type.lexeme) { + return error!( + &return_type.loc, + "unrecognized type: ".to_owned() + &return_type.lexeme + ); + } + for (i, param) in params.iter().enumerate() { if !self.is_valid_type_name(¶m.var_type.lexeme) { return error!( - &name.loc, + ¶m.var_name.loc, "unrecognized type: ".to_owned() + ¶m.var_type.lexeme ); } diff --git a/src/std/crypto.zr b/src/std/crypto.zr index 8e5b6a2..c15bd39 100644 --- a/src/std/crypto.zr +++ b/src/std/crypto.zr @@ -22,7 +22,7 @@ func crypto.blake2b._G[v: ptr, a: i64, b: i64, c: i64, d: i64, x: i64, y: i64] : mem.write64(v + c * 8, mem.read64(v + c * 8) + mem.read64(v + d * 8)) mem.write64(v + b * 8, crypto.rotr64(mem.read64(v + b * 8) ^ mem.read64(v + c * 8), 63)) -func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: array, sigma: array] : void +func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: Array, sigma: Array] : void let v: ptr = mem.alloc(16 * 8) let m: ptr = mem.alloc(16 * 8) @@ -60,17 +60,17 @@ func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, i func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputlen: i64] : ptr if outlen == 0 || outlen > 64 || keylen > 64 - dbg.panic("invalid length passed to crypto.blake2b.hash") + panic("invalid length passed to crypto.blake2b.hash") let out: ptr = mem.alloc(outlen) // IV[i] = floor(2**w * frac(sqrt(prime(i+1)))), where prime(i) // is the i:th prime number ( 2, 3, 5, 7, 11, 13, 17, 19 ) // and sqrt(x) is the square root of x. // https://www.ietf.org/rfc/rfc7693#section-2.6 - let iv: array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179] + let iv: Array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179] // https://crypto.stackexchange.com/questions/108987/rationale-for-blake2-message-schedule - let sigma: array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3] + let sigma: Array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3] let h: ptr = mem.alloc(8 * 8) let t0 = 0 diff --git a/src/std/std.zr b/src/std/std.zr index d98ddef..29bd2f2 100644 --- a/src/std/std.zr +++ b/src/std/std.zr @@ -3,7 +3,7 @@ extern realloc extern free extern gethostbyname -func dbg.panic[msg: str] : void +func panic[msg: str] : void io.print("PANIC: ") io.println(msg) (0 / 0) // crashes program which is kinda better since you get a gdb backtrace @@ -90,7 +90,7 @@ func io.read_line[]: str func io.read_file[path: str] : str let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) if fd <= 0 - dbg.panic("failed to open file") + panic("failed to open file") let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2) _builtin_syscall(SYS_lseek, fd, 0, 0) @@ -104,7 +104,7 @@ func io.read_file[path: str] : str func io.read_binary_file[path: str, len_ptr: ptr] : ptr let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) if fd < 0 - dbg.panic("failed to open file") + panic("failed to open file") let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2) _builtin_syscall(SYS_lseek, fd, 0, 0) @@ -118,7 +118,7 @@ func io.read_binary_file[path: str, len_ptr: ptr] : ptr func io.write_file[path: str, content: str] : void let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644) if fd < 0 - dbg.panic("failed to open file") + panic("failed to open file") _builtin_syscall(SYS_write, fd, content, str.len(content)) _builtin_syscall(SYS_close, fd) @@ -195,7 +195,7 @@ func str.find[haystack: str, needle: str] : i64 func str.substr[s: str, start: i64, length: i64] : str if start < 0 || length < 0 || start + length > str.len(s) - dbg.panic("str.substr out of bounds") + panic("str.substr out of bounds") let out: str = mem.alloc(length + 1) for i in 0..length @@ -219,10 +219,10 @@ func str.trim[s: str] : str return str.substr(s, start, end - start + 1) -func str.split[haystack: str, needle: str]: array +func str.split[haystack: str, needle: str]: Array let haystack_len: i64 = str.len(haystack) let needle_len: i64 = str.len(needle) - let result: array = [] + let result: Array = [] if !needle_len if !haystack_len @@ -410,7 +410,7 @@ func math.lcm[a: i64, b: i64] : i64 func math.isqrt[n: i64] : i64 if n < 0 - dbg.panic("negative number passed to math.isqrt") + panic("negative number passed to math.isqrt") if n == 0 || n == 1 return n @@ -438,85 +438,74 @@ func math.is_prime[n: i64]: bool i = i + 6 return true -func array.new[] : array - // [ 8 bytes - ptr to data ] [ 8 bytes - size ] [ 8 bytes - capacity ] - let arr: ptr = mem.alloc(24) - mem.zero(arr, 24) - return arr +struct Array + data: ptr + size: i64 + capacity: i64 -func array.nth[xs: array, n: i64] : i64 - if n < 0 || n >= array.size(xs) - dbg.panic("array.nth out of bounds") - let data: ptr = mem.read64(xs) - return mem.read64(data + n * 8) +func array.new[] : Array + return new Array -func array.set[xs: array, n: i64, x: i64] : void - if n < 0 || n >= array.size(xs) - dbg.panic("array.set out of bounds") - let data: ptr = mem.read64(xs) - mem.write64(data + n * 8, x) +func array.nth[xs: Array, n: i64] : i64 + if n < 0 || n >= xs->size + panic("array.nth out of bounds") + return mem.read64(xs->data + n * 8) -func array.push[xs: array, x: i64] : void - let data: ptr = mem.read64(xs) - let capacity: i64 = mem.read64(xs + 8) - let size: i64 = mem.read64(xs + 16) +func array.set[xs: Array, n: i64, x: i64] : void + if n < 0 || n >= xs->size + panic("array.set out of bounds") + mem.write64(xs->data + n * 8, x) - if size == capacity +func array.push[xs: Array, x: i64] : void + if xs->size == xs->capacity let new_capacity = 4 - if capacity != 0 - new_capacity = capacity * 2 - let new_data: ptr = realloc(data, new_capacity * 8) - mem.write64(xs, new_data) - mem.write64(xs + 8, new_capacity) - data = new_data + if xs->capacity != 0 + new_capacity = xs->capacity * 2 + xs->data = realloc(xs->data, new_capacity * 8) + xs->capacity = new_capacity - mem.write64(data + size * 8, x) - mem.write64(xs + 16, size + 1) + mem.write64(xs->data + xs->size * 8, x) + xs->size = xs->size + 1 -func array.size[xs: array] : i64 - return mem.read64(xs + 16) - -func array.free[xs: array] : void - let data: ptr = mem.read64(xs) - if data != 0 - mem.free(data) +func array.free[xs: Array] : void + if xs->data != 0 + mem.free(xs->data) mem.free(xs) -func array.pop[xs: array] : i64 - let size: i64 = array.size(xs) - if size == 0 - dbg.panic("array.pop on empty array") - let x: i64 = array.nth(xs, size - 1) - mem.write64(xs + 16, size - 1) +func array.pop[xs: Array] : i64 + if xs->size == 0 + panic("array.pop on empty array") + let x: i64 = array.nth(xs, xs->size - 1) + xs->size = xs->size - 1 return x -func array.slice[xs: array, start: i64, length: i64] : array - if start < 0 || length < 0 || start + length > array.size(xs) - dbg.panic("array.slice out of bounds") +func array.slice[xs: Array, start: i64, length: i64] : Array + if start < 0 || length < 0 || start + length > xs->size + panic("array.slice out of bounds") - let new_array: array = [] + let new_array: Array = [] for i in 0..length array.push(new_array, array.nth(xs, start + i)) return new_array -func array.concat[a: array, b: array] : array - let new_array: array = [] - for i in 0..array.size(a) +func array.concat[a: Array, b: Array] : Array + let new_array: Array = [] + for i in 0..a->size array.push(new_array, array.nth(a, i)) - for i in 0..array.size(b) + for i in 0..b->size array.push(new_array, array.nth(b, i)) return new_array -func alg.quicksort[arr: array] : void - alg._do_quicksort(arr, 0, array.size(arr) - 1) +func alg.quicksort[arr: Array] : void + alg._do_quicksort(arr, 0, arr->size - 1) -func alg._do_quicksort[arr: array, low: i64, high: i64] : void +func alg._do_quicksort[arr: Array, low: i64, high: i64] : void if low < high let i: i64 = alg._partition(arr, low, high) alg._do_quicksort(arr, low, i - 1) alg._do_quicksort(arr, i + 1, high) -func alg._partition[arr: array, low: i64, high: i64] : i64 +func alg._partition[arr: Array, low: i64, high: i64] : i64 let pivot: i64 = array.nth(arr, high) let i: i64 = low - 1 for j in (low)..high @@ -530,29 +519,28 @@ func alg._partition[arr: array, low: i64, high: i64] : i64 array.set(arr, high, temp) return i + 1 -func alg.count[arr: array, item: i64] : i64 +func alg.count[arr: Array, item: i64] : i64 let count = 0 - let size: i64 = array.size(arr) - for i in 0..size + for i in 0..arr->size if array.nth(arr, i) == item count = count + 1 return count -func alg.map[arr: array, fn: ptr] : array - let out: array = [] - for i in 0..array.size(arr) +func alg.map[arr: Array, fn: ptr] : Array + let out: Array = [] + for i in 0..arr->size array.push(out, fn(array.nth(arr, i))) return out -func alg.filter[arr: array, fn: ptr] : array - let out: array = [] - for i in 0..array.size(arr) +func alg.filter[arr: Array, fn: ptr] : Array + let out: Array = [] + for i in 0..arr->size if fn(array.nth(arr, i)) array.push(out, array.nth(arr, i)) return out -func alg.reduce[arr: array, fn: ptr, acc: i64] : i64 - for i in 0..array.size(arr) +func alg.reduce[arr: Array, fn: ptr, acc: i64] : i64 + for i in 0..arr->size acc = fn(acc, array.nth(arr, i)) return acc @@ -585,7 +573,7 @@ func os.shell[command: str] : i64 let pid: i64 = _builtin_syscall(SYS_fork) if pid == 0 // leaky but not sure where can i free it - let argv: array = ["sh", "-c", command, 0] + let argv: Array = ["sh", "-c", command, 0] _builtin_syscall(SYS_execve, "/bin/sh", mem.read64(argv), _builtin_environ()) _builtin_syscall(SYS_exit, 1) else @@ -600,12 +588,12 @@ func os.shell[command: str] : i64 else return -(st & 0x7f) -func os.listdir[path: str] : array +func os.listdir[path: str] : Array let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) if fd < 0 return [] - let files: array = [] + let files: Array = [] let buf: ptr = mem.alloc(1024) while true let n: i64 = _builtin_syscall(SYS_getdents64, fd, buf, 1024) diff --git a/test.zr b/test.zr index b5770d9..08fff04 100644 --- a/test.zr +++ b/test.zr @@ -1,17 +1,17 @@ func run_test[x: str] : void - let build_blacklist: array = ["examples/puzzles", "examples/raylib.zr", "examples/x11.zr", "examples/sqlite_todo.zr"] - let run_blacklist: array = ["/aoc", "guess_number.zr", "tcp_server.zr"] + let build_blacklist: Array = ["examples/puzzles", "examples/raylib.zr", "examples/chip8.zr", "examples/sqlite_todo.zr"] + let run_blacklist: Array = ["/aoc", "guess_number.zr", "tcp_server.zr"] - for i in 0..array.size(build_blacklist) + for i in 0..build_blacklist->size if str.equal(x, array.nth(build_blacklist, i)) - io.print("\033[93mSkipping ") + io.print("Skipping ") io.print(x) - io.println("...\033[0m") + io.println("...") return 0 - io.print("\033[94mBuilding ") + io.print("Building ") io.print(x) - io.print("...\033[0m ") + io.print("... ") let build_start_time: i64 = os.time() if os.shell(str.concat("./target/release/zern ", x)) != 0 @@ -21,16 +21,16 @@ func run_test[x: str] : void io.print_i64(build_end_time - build_start_time) io.println("ms") - for i in 0..array.size(run_blacklist) + for i in 0..run_blacklist->size if str.find(x, array.nth(run_blacklist, i)) != -1 - io.print("\033[93mSkipping ") + io.print("Skipping ") io.print(x) - io.println("...\033[0m") + io.println("...") return 0 - io.print("\033[95mRunning ") + io.print("Running ") io.print(x) - io.println("...\033[0m") + io.println("...") let run_cmd: str = "./out" if str.equal(x, "examples/curl.zr") @@ -43,15 +43,15 @@ func run_test[x: str] : void os.exit(1) let run_end_time: i64 = os.time() - io.print("\033[92mRunning ") + io.print("Running ") io.print(x) - io.print(" took\033[0m ") + io.print(" took ") io.print_i64(run_end_time - run_start_time) io.println("ms") func run_directory[dir: str] : void - let files: array = os.listdir(dir) - for i in 0..array.size(files) + let files: Array = os.listdir(dir) + for i in 0..files->size run_test(str.concat(dir, array.nth(files, i))) array.free(files)