diff --git a/README.md b/README.md index 77d36a7..e2e5879 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,19 @@ A very cool language ## Features * Clean indentation-based syntax * Compiles to x86_64 Assembly -* Growing [standard library](https://git.ton1.dev/toni/zern/src/branch/main/src/std) -* Produces tiny static executables (11KB for `hello.zr`) * No libc required! -* Has type inference, variadics, dynamic arrays, hashmaps, DNS resolver, etc. +* Produces tiny static executables (11KB for `hello.zr`) +* Has type inference, [UFCS](https://en.wikipedia.org/wiki/Uniform_function_call_syntax), variadics, dynamic arrays, hashmaps, DNS resolver, etc. +* Growing [standard library](https://git.ton1.dev/toni/zern/src/branch/main/src/std) ## Syntax ```rust func main[] : i64 - answer := math.abs(os.urandom_i64()) % 100 + answer := os.urandom_i64()->abs() % 100 while true io.println("Guess a number: ") - guess := str.parse_i64(io.read_line()) + guess := io.read_line()->parse_i64() if guess == answer io.println("You win!") diff --git a/examples/brainfuck.zr b/examples/brainfuck.zr index 26f0027..303044b 100644 --- a/examples/brainfuck.zr +++ b/examples/brainfuck.zr @@ -1,7 +1,7 @@ func main[] : i64 // https://brainfuck.org/sierpinski.b src := "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]" - src_len := str.len(src) + src_len := src->len() i := 0 memory := mem.alloc(30000) diff --git a/examples/chip8.zr b/examples/chip8.zr index 6f0f433..0980782 100644 --- a/examples/chip8.zr +++ b/examples/chip8.zr @@ -35,8 +35,8 @@ func chip8_create[] : CHIP8 fonts := [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) - array.free(fonts) + c->memory[i] = fonts->nth(i) + fonts->free() c->pc = 0x200 @@ -47,11 +47,11 @@ func chip8_create[] : CHIP8 func chip8_free[c: CHIP8] : void mem.free(c->memory) - array.free(c->stack) + c->stack->free() mem.free(c->reg) - array.free(c->keyboard) + c->keyboard->free() mem.free(c->display) - array.free(c->keyboard_map) + c->keyboard_map->free() mem.free(c) func chip8_disassemble[c: CHIP8, ins_count: i64] : void @@ -170,13 +170,13 @@ func chip8_step[c: CHIP8] : void if nnn == 0x0e0 mem.zero(c->display, 64 * 32) else if nnn == 0x0ee - c->pc = array.nth(c->stack, c->sp) + c->pc = c->stack->nth(c->sp) c->sp = c->sp - 1 else if op == 0x1 c->pc = nnn else if op == 0x2 c->sp += 1 - array.set(c->stack, c->sp, c->pc) + c->stack->set(c->sp, c->pc) c->pc = nnn else if op == 0x3 if c->reg[x] == kk @@ -224,7 +224,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.abs(os.urandom_i64()) % 256) & kk + c->reg[x] = (os.urandom_i64()->abs() % 256) & kk else if op == 0xd c->reg[0xf] = 0 for row in 0..n @@ -239,10 +239,10 @@ func chip8_step[c: CHIP8] : void c->display[offset] = c->display[offset] ^ 1 else if op == 0xe if kk == 0x9e - if IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64)) + if IsKeyDown(c->keyboard_map->nth(c->reg[x] as i64)) c->pc += 2 else if kk == 0xa1 - if !IsKeyDown(array.nth(c->keyboard_map, c->reg[x] as i64)) + if !IsKeyDown(c->keyboard_map->nth(c->reg[x] as i64)) c->pc += 2 else if op == 0xf if kk == 0x07 @@ -251,7 +251,7 @@ func chip8_step[c: CHIP8] : void key_pressed := false while !key_pressed && !WindowShouldClose() for i in 0..16 - if IsKeyDown(array.nth(c->keyboard_map, i)) + if IsKeyDown(c->keyboard_map->nth(i)) key_pressed = true break else if kk == 0x15 @@ -279,7 +279,7 @@ func main[argc: i64, argv: ptr] : i64 for i in 1..argc arg := mem.read64(argv + i * 8) as str - if str.equal(arg, "-d") + if arg->equal("-d") disassemble = true else path = arg diff --git a/examples/curl.zr b/examples/curl.zr index 221d74b..67d5974 100644 --- a/examples/curl.zr +++ b/examples/curl.zr @@ -5,15 +5,15 @@ func main[argc: i64, argv: ptr] : i64 url := mem.read64(argv + 8) as str - if str.len(url) <= 7 + if url->len() <= 7 io.println("ERROR: invalid url (Did you forget \"http://\"?)") return 1 - if !str.equal(str.substr(url, 0, 7), "http://") + if !url->substr(0, 7)->equal("http://") io.println("ERROR: only http scheme is supported") return 1 - url_len := str.len(url) + url_len := url->len() host_start := 7 i := host_start while i < url_len @@ -21,10 +21,10 @@ func main[argc: i64, argv: ptr] : i64 break i += 1 - host := str.substr(url, host_start, i - host_start) + host := url->substr(host_start, i - host_start) path := "/" if i < url_len - path = str.substr(url, i, url_len - i) + path = url->substr(i, url_len - i) ~addr, ok := net.resolve(host) if !ok @@ -35,11 +35,11 @@ func main[argc: i64, argv: ptr] : i64 panic("failed to connect") req := new str.Builder - str.Builder.append(req, "GET ") - str.Builder.append(req, path) - str.Builder.append(req, " HTTP/1.0\r\nHost: ") - str.Builder.append(req, host) - str.Builder.append(req, "\r\nConnection: close\r\n\r\n") + req->append("GET ") + req->append(path) + req->append(" HTTP/1.0\r\nHost: ") + req->append(host) + req->append("\r\nConnection: close\r\n\r\n") net.send(s, req->data, req->size) mem.free(req->data) diff --git a/examples/guess_number.zr b/examples/guess_number.zr index 2637acd..d1d4a2d 100644 --- a/examples/guess_number.zr +++ b/examples/guess_number.zr @@ -1,9 +1,9 @@ func main[] : i64 - answer := math.abs(os.urandom_i64()) % 100 + answer := os.urandom_i64()->abs() % 100 while true io.println("Guess a number: ") - guess := str.parse_i64(io.read_line()) + guess := io.read_line()->parse_i64() if guess == answer io.println("You win!") diff --git a/examples/puzzles/aoc2024_01.zr b/examples/puzzles/aoc2024_01.zr index 514750b..8fed0b2 100644 --- a/examples/puzzles/aoc2024_01.zr +++ b/examples/puzzles/aoc2024_01.zr @@ -2,7 +2,7 @@ func part1[l1: Array, l2: Array] : void out := 0 for i in 0..l1->size - out += math.abs(array.nth(l1, i) - array.nth(l2, i)) + out += (l1->nth(i) as i64 - l2->nth(i) as i64)->abs() io.println_i64(out) @@ -10,7 +10,7 @@ func part2[l1: Array, l2: Array] : void out := 0 for i in 0..l1->size - out += array.nth(l1, i) * alg.count(l2, array.nth(l1, i)) + out += l1->nth(i) * l2->count(l1->nth(i)) io.println_i64(out) @@ -19,21 +19,21 @@ func main[] : i64 if !ok panic("failed to open input.txt") - lines := str.split(input, "\n") + lines := input->split("\n") l1 := [] l2 := [] for i in 0..lines->size - line : str = array.nth(lines, i) + line : str = lines->nth(i) - parts := str.split(line, " ") + parts := line->split(" ") - array.push(l1, str.parse_i64(array.nth(parts, 0))) - array.push(l2, str.parse_i64(array.nth(parts, 1))) + l1->push((parts->nth(0) as str)->parse_i64()) + l2->push((parts->nth(1) as str)->parse_i64()) - alg.quicksort(l1) - alg.quicksort(l2) + l1->quicksort() + l2->quicksort() part1(l1, l2) part2(l1, l2) diff --git a/examples/puzzles/aoc2024_02.zr b/examples/puzzles/aoc2024_02.zr index a8b2347..f920cff 100644 --- a/examples/puzzles/aoc2024_02.zr +++ b/examples/puzzles/aoc2024_02.zr @@ -1,13 +1,13 @@ func check[report: Array] : bool - increasing := array.nth(report, 0) < array.nth(report, 1) + increasing := report->nth(0) < report->nth(1) for i in 0..report->size-1 - if array.nth(report, i) > array.nth(report, i + 1) && increasing + if report->nth(i) > report->nth(i + 1) && increasing return false - if array.nth(report, i) < array.nth(report, i + 1) && !increasing + if report->nth(i) < report->nth(i + 1) && !increasing return false - diff := math.abs(array.nth(report, i) - array.nth(report, i + 1)) + diff := (report->nth(i) as i64 - report->nth(i + 1) as i64)->abs() if diff < 1 || diff > 3 return false @@ -17,7 +17,7 @@ func part1[data: Array] : void out := 0 for i in 0..data->size - if check(array.nth(data, i)) + if check(data->nth(i)) out += 1 io.println_i64(out) @@ -26,12 +26,12 @@ func part2[data: Array] : void out := 0 for i in 0..data->size - if check(array.nth(data, i)) + if check(data->nth(i)) out += 1 else - arr : Array = array.nth(data, i) + arr : Array = data->nth(i) for j in 0..arr->size - sliced := array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1))) + sliced := arr->slice(0, j)->concat(arr->slice(j + 1, arr->size - (j + 1))) if check(sliced) out += 1 @@ -44,16 +44,16 @@ func main[] : i64 if !ok panic("failed to open input.txt") - lines := str.split(input, "\n") + lines := input->split("\n") data := [] for i in 0..lines->size - line : str = array.nth(lines, i) + line : str = lines->nth(i) - report := str.split(line, " ") + report := line->split(" ") for i in 0..report->size - array.set(report, i, str.parse_i64(array.nth(report, i))) - array.push(data, report) + report->set(i, (report->nth(i) as str)->parse_i64()) + data->push(report) part1(data) part2(data) diff --git a/examples/puzzles/aoc2024_04.zr b/examples/puzzles/aoc2024_04.zr index eb0b2f0..772dbe2 100644 --- a/examples/puzzles/aoc2024_04.zr +++ b/examples/puzzles/aoc2024_04.zr @@ -1,14 +1,14 @@ 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)) + if x + dx * 3 < 0 || x + dx * 3 >= lines->size || y + dy * 3 < 0 || y + dy * 3 >= (lines->nth(0) as str)->len() return false - if array.nth(lines, x)[y] != 'X' + if lines->nth(x)[y] != 'X' return false - if array.nth(lines, x + dx)[y + dy] != 'M' + if lines->nth(x + dx)[y + dy] != 'M' return false - if array.nth(lines, x + dx * 2)[y + dy * 2] != 'A' + if lines->nth(x + dx * 2)[y + dy * 2] != 'A' return false - if array.nth(lines, x + dx * 3)[y + dy * 3] != 'S' + if lines->nth(x + dx * 3)[y + dy * 3] != 'S' return false return true @@ -17,7 +17,7 @@ func part1[lines: Array] : void out := 0 for x in 0..lines->size - for y in 0..str.len(array.nth(lines, x)) + for y in 0..(lines->nth(x) as str)->len() if check(lines, x, y, 0, 1) out += 1 if check(lines, x, y, 0, -1) @@ -42,15 +42,16 @@ func part2[lines: Array] : void s := _stackalloc(5) as str 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' - s[0] = array.nth(lines, x - 1)[y - 1] - s[1] = array.nth(lines, x + 1)[y - 1] - s[2] = array.nth(lines, x + 1)[y + 1] - s[3] = array.nth(lines, x - 1)[y + 1] + line_len := (lines->nth(x) as str)->len() + for y in 1..line_len-1 + if lines->nth(x)[y] == 'A' + s[0] = lines->nth(x - 1)[y - 1] + s[1] = lines->nth(x + 1)[y - 1] + s[2] = lines->nth(x + 1)[y + 1] + s[3] = lines->nth(x - 1)[y + 1] s[4] = 0 - if str.equal(s, "MSSM") || str.equal(s, "SMMS") || str.equal(s, "MMSS") || str.equal(s, "SSMM") + if s->equal("MSSM") || s->equal("SMMS") || s->equal("MMSS") || s->equal("SSMM") out += 1 io.println_i64(out) @@ -60,7 +61,7 @@ func main[] : i64 if !ok panic("failed to open input.txt") - lines := str.split(input, "\n") + lines := input->split("\n") part1(lines) part2(lines) diff --git a/examples/puzzles/aoc2024_05.zr b/examples/puzzles/aoc2024_05.zr index bc8b246..49bb8e2 100644 --- a/examples/puzzles/aoc2024_05.zr +++ b/examples/puzzles/aoc2024_05.zr @@ -1,13 +1,13 @@ 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 + if rules_left->nth(k) == a && rules_right->nth(k) == b return true return false 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)) + if rule_exists(rules_left, rules_right, update->nth(i), update->nth(j)) return false return true @@ -17,21 +17,21 @@ func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void while swapped swapped = false for i in 0..update->size-1 - a : i64 = array.nth(update, i) - b : i64 = array.nth(update, i+1) + a : i64 = update->nth(i) + b : i64 = update->nth(i+1) if rule_exists(rules_left, rules_right, b, a) tmp := a - array.set(update, i, b) - array.set(update, i+1, tmp) + update->set(i, b) + update->set(i+1, tmp) swapped = true func part1[updates: Array, rules_left: Array, rules_right: Array] : void out := 0 for i in 0..updates->size - update : Array = array.nth(updates, i) + update : Array = updates->nth(i) if check(update, rules_left, rules_right) - out += array.nth(update, update->size / 2) + out += update->nth(update->size / 2) io.println_i64(out) @@ -39,10 +39,10 @@ func part2[updates: Array, rules_left: Array, rules_right: Array] : void out := 0 for i in 0..updates->size - update : Array = array.nth(updates, i) + update : Array = updates->nth(i) if !check(update, rules_left, rules_right) sort_by_rules(update, rules_left, rules_right) - out += array.nth(update, update->size / 2) + out += update->nth(update->size / 2) io.println_i64(out) @@ -51,27 +51,27 @@ func main[] : i64 if !ok panic("failed to open input.txt") - data := str.split(input, "\n\n") + data := input->split("\n\n") rules_left := [] rules_right := [] - rules_lines := str.split(array.nth(data, 0), "\n") + rules_lines := (data->nth(0) as str)->split("\n") for i in 0..rules_lines->size - line : str = array.nth(rules_lines, i) - parts := str.split(line, "|") + line : str = rules_lines->nth(i) + parts := line->split("|") - array.push(rules_left, str.parse_i64(array.nth(parts, 0))) - array.push(rules_right, str.parse_i64(array.nth(parts, 1))) + rules_left->push((parts->nth(0) as str)->parse_i64()) + rules_right->push((parts->nth(1) as str)->parse_i64()) updates := [] - updates_lines := str.split(array.nth(data, 1), "\n") + updates_lines := (data->nth(1) as str)->split("\n") for i in 0..updates_lines->size - line : str = array.nth(updates_lines, i) - xs := str.split(line, ",") + line : str = updates_lines->nth(i) + xs := line->split(",") for i in 0..xs->size - array.set(xs, i, str.parse_i64(array.nth(xs, i))) - array.push(updates, xs) + xs->set(i, (xs->nth(i) as str)->parse_i64()) + updates->push(xs) part1(updates, rules_left, rules_right) part2(updates, rules_left, rules_right) diff --git a/examples/puzzles/aoc2024_07.zr b/examples/puzzles/aoc2024_07.zr index 52a65d1..bb67a33 100644 --- a/examples/puzzles/aoc2024_07.zr +++ b/examples/puzzles/aoc2024_07.zr @@ -1,38 +1,38 @@ func concat[a: i64, b: i64] : i64 ab_str := _stackalloc(50) as str - io.snprintf(ab_str, 50, "%d%d", a, b) - return str.parse_i64(ab_str) + ab_str->format_into(50, "%d%d", a, b) + return ab_str->parse_i64() func solve[ops: Array, e: Array] : i64 - e_1 : Array = array.nth(e, 1) + e_1 : Array = e->nth(1) n := e_1->size - 1 indices := [] for i in 0..n - array.push(indices, 0) + indices->push(0) while true - res : i64 = array.nth(e_1, 0) + res : i64 = e_1->nth(0) for i in 0..n - op : str = array.nth(ops, array.nth(indices, i)) + op : str = ops->nth(indices->nth(i)) - if str.equal(op, "add") - res += array.nth(e_1, i + 1) - else if str.equal(op, "mul") - res = res * array.nth(e_1, i + 1) - else if str.equal(op, "concat") - res = concat(res, array.nth(e_1, i + 1)) - if res == array.nth(e, 0) + if op->equal("add") + res += e_1->nth(i + 1) + else if op->equal("mul") + res = res * e_1->nth(i + 1) + else if op->equal("concat") + res = concat(res, e_1->nth(i + 1)) + if res == e->nth(0) return res done := true i := n - 1 while i >= 0 - array.set(indices, i, array.nth(indices, i) + 1) - if array.nth(indices, i) < ops->size + indices->set(i, indices->nth(i) + 1) + if indices->nth(i) < ops->size done = false break - array.set(indices, i, 0) + indices->set(i, 0) i -= 1 if done @@ -42,7 +42,7 @@ func part1[equations: Array] : void out := 0 for i in 0..equations->size - out += solve(["add", "mul"], array.nth(equations, i)) + out += solve(["add", "mul"], equations->nth(i)) io.println_i64(out) @@ -50,7 +50,7 @@ func part2[equations: Array] : void out := 0 for i in 0..equations->size - out += solve(["add", "mul", "concat"], array.nth(equations, i)) + out += solve(["add", "mul", "concat"], equations->nth(i)) io.println_i64(out) @@ -59,18 +59,18 @@ func main[] : i64 if !ok panic("failed to open input.txt") - lines := str.split(input, "\n") + lines := input->split("\n") equations := [] for i in 0..lines->size - line : str = array.nth(lines, i) - parts := str.split(line, ": ") + line : str = lines->nth(i) + parts := line->split(": ") - xs := str.split(array.nth(parts, 1), " ") + xs := (parts->nth(1) as str)->split(" ") for j in 0..xs->size - array.set(xs, j, str.parse_i64(array.nth(xs, j))) + xs->set(j, (xs->nth(j) as str)->parse_i64()) - array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs]) + equations->push([(parts->nth(0) as str)->parse_i64(), xs]) part1(equations) part2(equations) diff --git a/examples/puzzles/aoc2025_01.zr b/examples/puzzles/aoc2025_01.zr index 1b9b616..3e8c5b9 100644 --- a/examples/puzzles/aoc2025_01.zr +++ b/examples/puzzles/aoc2025_01.zr @@ -3,9 +3,9 @@ func part1[lines: Array] : void dial := 50 for i in 0..lines->size - line : str = array.nth(lines, i) + line : str = lines->nth(i) dir := line[0] - n := str.parse_i64(str.substr(line, 1, str.len(line) - 1)) + n := line->substr(1, line->len() - 1)->parse_i64() if dir == 'L' dial -= n @@ -26,9 +26,9 @@ func part2[lines: Array] : void dial := 50 for i in 0..lines->size - line : str = array.nth(lines, i) + line : str = lines->nth(i) dir := line[0] - n := str.parse_i64(str.substr(line, 1, str.len(line) - 1)) + n := line->substr(1, line->len() - 1)->parse_i64() if dir == 'L' for i in 0..n @@ -51,7 +51,7 @@ func main[] : i64 if !ok panic("failed to open input.txt") - lines := str.split(input, "\n") + lines := input->split("\n") part1(lines) part2(lines) diff --git a/examples/puzzles/aoc2025_02.zr b/examples/puzzles/aoc2025_02.zr index d3cd10f..7b6b00a 100644 --- a/examples/puzzles/aoc2025_02.zr +++ b/examples/puzzles/aoc2025_02.zr @@ -1,39 +1,39 @@ func part1_is_invalid_id[s: str] : bool - len := str.len(s) + len := s->len() if len % 2 != 0 return false - first := str.substr(s, 0, len / 2) - second := str.substr(s, len / 2, len / 2) + first := s->substr(0, len / 2) + second := s->substr(len / 2, len / 2) - return str.equal(first, second) + return first->equal(second) func part1[ranges: Array] : void sum := 0 for i in 0..ranges->size - parts := str.split(array.nth(ranges, i), "-") - start := str.parse_i64(array.nth(parts, 0)) - end := str.parse_i64(array.nth(parts, 1)) + parts := (ranges->nth(i) as str)->split("-") + start := (parts->nth(0) as str)->parse_i64() + end := (parts->nth(1) as str)->parse_i64() for n in (start)..end+1 - if part1_is_invalid_id(str.from_i64(n)) + if part1_is_invalid_id(n->to_str()) sum += n io.println_i64(sum) // had to cheat this one func part2_is_invalid_id[s: str] : bool - len := str.len(s) + len := s->len() if len < 2 return false for div in 1..(len / 2 + 1) if len % div == 0 - u := str.substr(s, 0, div) + u := s->substr(0, div) is_repeat := true for k in 1..(len / div) - segment := str.substr(s, k * div, div) - if !str.equal(segment, u) + segment := s->substr(k * div, div) + if !segment->equal(u) is_repeat = false if is_repeat return true @@ -43,12 +43,12 @@ func part2[ranges: Array] : void sum := 0 for i in 0..ranges->size - parts := str.split(array.nth(ranges, i), "-") - start := str.parse_i64(array.nth(parts, 0)) - end := str.parse_i64(array.nth(parts, 1)) + parts := (ranges->nth(i) as str)->split("-") + start := (parts->nth(0) as str)->parse_i64() + end := (parts->nth(1) as str)->parse_i64() for n in (start)..end+1 - if part2_is_invalid_id(str.from_i64(n)) + if part2_is_invalid_id(n->to_str()) sum += n io.println_i64(sum) @@ -58,7 +58,7 @@ func main[] : i64 if !ok panic("failed to open input.txt") - ranges := str.split(input, ",") + ranges := input->split(",") part1(ranges) part2(ranges) diff --git a/examples/puzzles/aoc2025_03.zr b/examples/puzzles/aoc2025_03.zr index da49b6f..c97a931 100644 --- a/examples/puzzles/aoc2025_03.zr +++ b/examples/puzzles/aoc2025_03.zr @@ -3,15 +3,15 @@ func part1[lines: Array] : void s := _stackalloc(3) as str for i in 0..lines->size - line : str = array.nth(lines, i) + line : str = lines->nth(i) largest := 0 - for j in 0..str.len(line) - for k in (j+1)..str.len(line) + for j in 0..line->len() + for k in (j+1)..line->len() s[0] = line[j] s[1] = line[k] s[2] = 0 - n := str.parse_i64(s) + n := s->parse_i64() if n > largest largest = n @@ -22,7 +22,7 @@ func part1[lines: Array] : void func part2_rec[bank: str, start: i64, remaining: i64] : i64 largest := 0 largest_idx := start - len := str.len(bank) + len := bank->len() for i in (start)..(len-remaining+1) v := (bank[i] - '0') as i64 @@ -39,7 +39,7 @@ func part2[lines: Array] : void sum := 0 for i in 0..lines->size - line : str = array.nth(lines, i) + line : str = lines->nth(i) sum += part2_rec(line, 0, 12) io.println_i64(sum) @@ -49,7 +49,7 @@ func main[] : i64 if !ok panic("failed to open input.txt") - lines := str.split(input, "\n") + lines := input->split("\n") part1(lines) part2(lines) diff --git a/examples/puzzles/euler_04.zr b/examples/puzzles/euler_04.zr index 1fc1866..01b4db4 100644 --- a/examples/puzzles/euler_04.zr +++ b/examples/puzzles/euler_04.zr @@ -4,9 +4,9 @@ func main[] : i64 for a in 500..1000 for b in 500..1000 if a * b > out - s := str.from_i64(a * b) - s_rev := str.reverse(s) - if str.equal(s, s_rev) + s := (a * b)->to_str() + s_rev := s->reverse() + if s->equal(s_rev) out = a * b mem.free(s) mem.free(s_rev) diff --git a/examples/puzzles/euler_07.zr b/examples/puzzles/euler_07.zr index e42fba7..a485c67 100644 --- a/examples/puzzles/euler_07.zr +++ b/examples/puzzles/euler_07.zr @@ -3,7 +3,7 @@ func main[] : i64 i := 1 while true - if math.is_prime(i) + if i->is_prime() found += 1 if found == 10001 io.println_i64(i) diff --git a/examples/puzzles/euler_08.zr b/examples/puzzles/euler_08.zr index 08466f8..d268413 100644 --- a/examples/puzzles/euler_08.zr +++ b/examples/puzzles/euler_08.zr @@ -2,7 +2,7 @@ func main[] : i64 n := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" out := 0 - max := str.len(n) - 13 + max := n->len() - 13 for i in 0..max s := 1 j := 0 diff --git a/examples/puzzles/euler_10.zr b/examples/puzzles/euler_10.zr index 7057e28..4ef7488 100644 --- a/examples/puzzles/euler_10.zr +++ b/examples/puzzles/euler_10.zr @@ -2,6 +2,6 @@ func main[] : i64 sum := 0 for i in 0..2000000 - if math.is_prime(i) + if i->is_prime() sum += i io.println_i64(sum) diff --git a/examples/puzzles/euler_11.zr b/examples/puzzles/euler_11.zr index 51047e4..6dc68a0 100644 --- a/examples/puzzles/euler_11.zr +++ b/examples/puzzles/euler_11.zr @@ -2,35 +2,35 @@ func main[] : i64 N := 20 grid := [] - 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]) - array.push(grid, [52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91]) - array.push(grid, [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80]) - array.push(grid, [24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50]) - array.push(grid, [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70]) - array.push(grid, [67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21]) - array.push(grid, [24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72]) - array.push(grid, [21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95]) - array.push(grid, [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92]) - array.push(grid, [16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57]) - array.push(grid, [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58]) - array.push(grid, [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40]) - array.push(grid, [4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66]) - array.push(grid, [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69]) - array.push(grid, [4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36]) - array.push(grid, [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16]) - array.push(grid, [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54]) - array.push(grid, [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]) + 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]) + grid->push([52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91]) + grid->push([22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80]) + grid->push([24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50]) + grid->push([32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70]) + grid->push([67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21]) + grid->push([24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72]) + grid->push([21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95]) + grid->push([78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92]) + grid->push([16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57]) + grid->push([86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58]) + grid->push([19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40]) + grid->push([4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66]) + grid->push([88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69]) + grid->push([4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36]) + grid->push([20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16]) + grid->push([20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54]) + grid->push([1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]) out := 0 for i in 0..N-3 for j in 0..N-3 - h : i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i), j+1) * array.nth(array.nth(grid, i), j+2) * array.nth(array.nth(grid, i), j+3) - v : i64 = array.nth(array.nth(grid, j), i) * array.nth(array.nth(grid, j+1), i) * array.nth(array.nth(grid, j+2), i) * array.nth(array.nth(grid, j+3), i) - d1 : i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i+1), j+1) * array.nth(array.nth(grid, i+2), j+2) * array.nth(array.nth(grid, i+3), j+3) - d2 : i64 = array.nth(array.nth(grid, i), N-j-1) * array.nth(array.nth(grid, i+1), N-j-2) * array.nth(array.nth(grid, i+2), N-j-3) * array.nth(array.nth(grid, i+3), N-j-4) + 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) out = math.max(out, math.max(h, math.max(v, math.max(d1, d2)))) io.println_i64(out) diff --git a/examples/puzzles/euler_12.zr b/examples/puzzles/euler_12.zr index 9e550d4..33910d8 100644 --- a/examples/puzzles/euler_12.zr +++ b/examples/puzzles/euler_12.zr @@ -1,5 +1,5 @@ func num_divisors[n: i64] : i64 - end := math.isqrt(n) + end := n->isqrt() out := 0 for i in 1..end+1 diff --git a/examples/puzzles/euler_13.zr b/examples/puzzles/euler_13.zr index 1916aa3..0efb7fb 100644 --- a/examples/puzzles/euler_13.zr +++ b/examples/puzzles/euler_13.zr @@ -1,4 +1,3 @@ func main[] : i64 n := 37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226 - n_str := str.from_i64(n) - io.println(str.substr(n_str, 0, 10)) + io.println(n->to_str()->substr(0, 10)) diff --git a/examples/puzzles/euler_16.zr b/examples/puzzles/euler_16.zr index 1ba8408..ef07e6e 100644 --- a/examples/puzzles/euler_16.zr +++ b/examples/puzzles/euler_16.zr @@ -1,19 +1,18 @@ func main[] : i64 - n := [] - array.push(n, 1) + n := [1] for j in 0..1000 carry := 0 for i in 0..n->size - tmp : i64 = array.nth(n, i) * 2 + carry - array.set(n, i, tmp % 10) + tmp : i64 = n->nth(i) * 2 + carry + n->set(i, tmp % 10) carry = tmp / 10 while carry > 0 - array.push(n, carry % 10) + n->push(carry % 10) carry = carry / 10 sum := 0 for i in 0..n->size - sum += array.nth(n, i) + sum += n->nth(i) io.println_i64(sum) diff --git a/examples/puzzles/euler_17.zr b/examples/puzzles/euler_17.zr index 2546d5f..7dcfeba 100644 --- a/examples/puzzles/euler_17.zr +++ b/examples/puzzles/euler_17.zr @@ -6,21 +6,21 @@ func main[] : i64 sum := 0 for i in 1..10 - sum += array.nth(s1, i) + sum += s1->nth(i) for i in 0..10 - sum += array.nth(s2, i) + sum += s2->nth(i) for i in 20..100 - sum += array.nth(s3, i / 10) + array.nth(s1, i % 10) + sum += s3->nth(i / 10) + s1->nth(i % 10) for i in 1..10 - sum += array.nth(s1, i) + 7 + sum += s1->nth(i) + 7 for j in 1..10 - sum += array.nth(s1, i) + 7 + 3 + array.nth(s1, j) + sum += s1->nth(i) + 7 + 3 + s1->nth(j) for j in 0..10 - sum += array.nth(s1, i) + 7 + 3 + array.nth(s2, j) + sum += s1->nth(i) + 7 + 3 + s2->nth(j) for j in 20..100 - sum += array.nth(s1, i) + 7 + 3 + array.nth(s3, j / 10) + array.nth(s1, j % 10) + sum += s1->nth(i) + 7 + 3 + s3->nth(j / 10) + s1->nth(j % 10) - sum += array.nth(s1, 1) + 8 + sum += s1->nth(1) + 8 io.println_i64(sum) diff --git a/examples/puzzles/euler_18.zr b/examples/puzzles/euler_18.zr index 03abe38..69979b7 100644 --- a/examples/puzzles/euler_18.zr +++ b/examples/puzzles/euler_18.zr @@ -1,28 +1,28 @@ func findmax[triangle: Array, row: i64, col: i64] : i64 if row == 14 - return array.nth(array.nth(triangle, row), col) + return (triangle->nth(row) as Array)->nth(col) left := findmax(triangle, row + 1, col) right := findmax(triangle, row + 1, col + 1) - return array.nth(array.nth(triangle, row), col) + math.max(left, right) + return (triangle->nth(row) as Array)->nth(col) + math.max(left, right) func main[] : i64 triangle := [] - array.push(triangle, [75]) - array.push(triangle, [95, 64]) - array.push(triangle, [17, 47, 82]) - array.push(triangle, [18, 35, 87, 10]) - array.push(triangle, [20, 4, 82, 47, 65]) - array.push(triangle, [19, 1, 23, 75, 3, 34]) - array.push(triangle, [88, 2, 77, 73, 7, 63, 67]) - array.push(triangle, [99, 65, 4, 28, 6, 16, 70, 92]) - array.push(triangle, [41, 41, 26, 56, 83, 40, 80, 70, 33]) - array.push(triangle, [41, 48, 72, 33, 47, 32, 37, 16, 94, 29]) - array.push(triangle, [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14]) - array.push(triangle, [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57]) - array.push(triangle, [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48]) - array.push(triangle, [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31]) - array.push(triangle, [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]) + triangle->push([75]) + triangle->push([95, 64]) + triangle->push([17, 47, 82]) + triangle->push([18, 35, 87, 10]) + triangle->push([20, 4, 82, 47, 65]) + triangle->push([19, 1, 23, 75, 3, 34]) + triangle->push([88, 2, 77, 73, 7, 63, 67]) + triangle->push([99, 65, 4, 28, 6, 16, 70, 92]) + triangle->push([41, 41, 26, 56, 83, 40, 80, 70, 33]) + triangle->push([41, 48, 72, 33, 47, 32, 37, 16, 94, 29]) + triangle->push([53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14]) + triangle->push([70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57]) + triangle->push([91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48]) + triangle->push([63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31]) + triangle->push([4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]) io.println_i64(findmax(triangle, 0, 0)) diff --git a/examples/puzzles/euler_20.zr b/examples/puzzles/euler_20.zr index 19ff04a..fc1e5cb 100644 --- a/examples/puzzles/euler_20.zr +++ b/examples/puzzles/euler_20.zr @@ -1,11 +1,11 @@ func multiply[n: Array, x: i64] : void carry := 0 for i in 0..n->size - prod : i64 = array.nth(n, i) * x + carry - array.set(n, i, prod % 10) + prod : i64 = n->nth(i) * x + carry + n->set(i, prod % 10) carry = prod / 10 while carry > 0 - array.push(n, carry % 10) + n->push(carry % 10) carry = carry / 10 func main[] : i64 @@ -16,6 +16,6 @@ func main[] : i64 sum := 0 for i in 0..n->size - sum += array.nth(n, i) + sum += n->nth(i) io.println_i64(sum) diff --git a/examples/quicksort.zr b/examples/quicksort.zr index 4545264..fc6b203 100644 --- a/examples/quicksort.zr +++ b/examples/quicksort.zr @@ -1,15 +1,15 @@ func main[] : i64 arr := [] for i in 0..10 - array.push(arr, math.abs(os.urandom_i64()) % 1000) + arr->push(os.urandom_i64()->abs() % 1000) for i in 0..arr->size - io.println_i64(array.nth(arr, i)) + io.println_i64(arr->nth(i)) io.println("------------") - alg.quicksort(arr) + arr->quicksort() for i in 0..arr->size - io.println_i64(array.nth(arr, i)) + io.println_i64(arr->nth(i)) - array.free(arr) + arr->free() diff --git a/examples/rule110.zr b/examples/rule110.zr index 5db8949..1a2179a 100644 --- a/examples/rule110.zr +++ b/examples/rule110.zr @@ -4,20 +4,20 @@ func rule110_step[state: Array] : void for i in 0..state->size left := false if i - 1 >= 0 - left = array.nth(state, i - 1) - center : bool = array.nth(state, i) + left = state->nth(i - 1) + center : bool = state->nth(i) right := false if i + 1 < state->size - right = array.nth(state, i + 1) + right = state->nth(i + 1) - array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right))) + new_state->push(!((!left && !center && !right) || (left && !center && !right) || (left && center && right))) mem.copy(new_state->data, state->data, state->size * 8) - array.free(new_state) + new_state->free() func print_state[state: Array]: void for i in 0..state->size - if array.nth(state, i) + if state->nth(i) io.print_char('#') else io.print_char(' ') @@ -28,12 +28,12 @@ func main[] : i64 state := [] for i in 0..SIZE - array.push(state, false) - array.push(state, true) + state->push(false) + state->push(true) print_state(state) for i in 0..SIZE rule110_step(state) print_state(state) - array.free(state) + state->free() diff --git a/examples/sqlite_todo.zr b/examples/sqlite_todo.zr index bb998f7..f05bd6b 100644 --- a/examples/sqlite_todo.zr +++ b/examples/sqlite_todo.zr @@ -30,7 +30,7 @@ func main[] : i64 io.println("0. Quit") io.print("\n> ") - choice := str.parse_i64(io.read_line()) + choice := io.read_line()->parse_i64() if choice == 0 break @@ -39,14 +39,14 @@ func main[] : i64 sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, ^stmt, 0) while sqlite3_step(stmt) == 100 - id := sqlite3_column_int(stmt, 0) - task := sqlite3_column_text(stmt, 1) + id := sqlite3_column_int(stmt, 0) as i64 + task := sqlite3_column_text(stmt, 1) as str io.printf("%d - %s\n", id, task) io.println("============") else if choice == 2 io.print("\nEnter new task: ") - task := str.trim(io.read_line()) + task := io.read_line()->trim() sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0) sqlite3_bind_text(stmt, 1, task, -1, 0) @@ -56,7 +56,7 @@ func main[] : i64 io.println("\nTask added\n") else if choice == 3 io.print("\nEnter task id: ") - id := str.parse_i64(io.read_line()) + id := io.read_line()->parse_i64() sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0) sqlite3_bind_int(stmt, 1, id, -1, 0) diff --git a/examples/udp_server.zr b/examples/udp_server.zr index b016019..3b2db08 100644 --- a/examples/udp_server.zr +++ b/examples/udp_server.zr @@ -7,7 +7,7 @@ func main[] : i64 while true pkt := net.udp_receive(s, 60000) if pkt->size <= 0 - net.UDPPacket.free(pkt) + pkt->free() continue io.printf("%d.%d.%d.%d:%d\n", pkt->source_addr[4], pkt->source_addr[5], pkt->source_addr[6], pkt->source_addr[7], mem.read16be(pkt->source_addr + 2)) @@ -16,4 +16,4 @@ func main[] : i64 io.println("") net.udp_send_to(s, pkt->source_addr, pkt->data, pkt->size) - net.UDPPacket.free(pkt) + pkt->free() diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index ef2e616..fd1b8d9 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -667,41 +667,10 @@ _builtin_environ: } let arg_count = args.len(); - if arg_count <= 6 { - for i in (0..arg_count).rev() { - emit!(&mut self.output, " pop {}", REGISTERS[i]); - } - } else { - for (i, reg) in REGISTERS.iter().enumerate() { - let offset = 8 * (arg_count - 1 - i); - emit!( - &mut self.output, - " mov {}, QWORD [rsp + {}]", - reg, - offset - ); - } - // TODO: since all zern values are 64bit large we currently cannot call - // external functions that expect a non-64bit value past the 6th argument - let num_stack = arg_count - 6; - for i in 0..num_stack { - let arg_idx = arg_count - 1 - i; - let offset = 8 * (arg_count - 1 - arg_idx); - emit!( - &mut self.output, - " mov rax, QWORD [rsp + {}]", - offset + 8 * i - ); - emit!(&mut self.output, " push rax"); - } - } + self.emit_call_setup(arg_count); if let ExprKind::Variable(callee_name) = &callee.kind { - if self - .symbol_table - .functions - .contains_key(&callee_name.lexeme) - { + if self.symbol_table.functions.contains_key(&callee_name.lexeme) { // its a function (defined/builtin/extern) emit!(&mut self.output, " call {}", callee_name.lexeme); } else { @@ -715,11 +684,7 @@ _builtin_environ: emit!(&mut self.output, " call rax"); } - if arg_count > 6 { - let num_stack = arg_count - 6; - emit!(&mut self.output, " add rsp, {}", 8 * num_stack); - emit!(&mut self.output, " add rsp, {}", 8 * arg_count); - } + self.emit_call_cleanup(arg_count); } ExprKind::ArrayLiteral(exprs) => { emit!(&mut self.output, " mov rdi, 24"); @@ -736,7 +701,7 @@ _builtin_environ: emit!(&mut self.output, " mov rsi, rax"); emit!(&mut self.output, " pop rdi"); emit!(&mut self.output, " push rdi"); - emit!(&mut self.output, " call array.push"); + emit!(&mut self.output, " call Array.push"); } emit!(&mut self.output, " pop rax"); } @@ -809,10 +774,64 @@ _builtin_environ: ExprKind::Cast { expr, type_name: _ } => { self.compile_expr(env, expr)?; } + ExprKind::MethodCall { expr, method, args } => { + let receiver_type = &self.expr_types[&expr.id]; + let func_name = format!("{}.{}", receiver_type, method.lexeme); + + self.compile_expr(env, expr)?; + emit!(&mut self.output, " push rax"); + for arg in args { + self.compile_expr(env, arg)?; + emit!(&mut self.output, " push rax"); + } + + let arg_count = 1 + args.len(); + self.emit_call_setup(arg_count); + emit!(&mut self.output, " call {}", func_name); + self.emit_call_cleanup(arg_count); + } } Ok(()) } + fn emit_call_setup(&mut self, arg_count: usize) { + if arg_count <= 6 { + for i in (0..arg_count).rev() { + emit!(&mut self.output, " pop {}", REGISTERS[i]); + } + } else { + for (i, reg) in REGISTERS.iter().enumerate() { + let offset = 8 * (arg_count - 1 - i); + emit!( + &mut self.output, + " mov {}, QWORD [rsp + {}]", + reg, + offset + ); + } + // TODO: since all zern values are 64bit large we currently cannot call + // external functions that expect a non-64bit value past the 6th argument + let num_stack = arg_count - 6; + for i in 0..num_stack { + let arg_idx = arg_count - 1 - i; + let offset = 8 * (arg_count - 1 - arg_idx); + emit!( + &mut self.output, + " mov rax, QWORD [rsp + {}]", + offset + 8 * i + ); + emit!(&mut self.output, " push rax"); + } + } + } + + fn emit_call_cleanup(&mut self, arg_count: usize) { + if arg_count > 6 { + emit!(&mut self.output, " add rsp, {}", 8 * (arg_count - 6)); + emit!(&mut self.output, " add rsp, {}", 8 * arg_count); + } + } + fn get_field_offset(&self, left: &Expr, field: &Token) -> Result { let struct_name = &self.expr_types[&left.id]; diff --git a/src/parser.rs b/src/parser.rs index fdf1f6a..e9d7c61 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -143,6 +143,11 @@ pub enum ExprKind { expr: Box, type_name: Token, }, + MethodCall { + expr: Box, + method: Token, + args: Vec, + }, } pub struct Parser { @@ -605,12 +610,32 @@ impl Parser { index: Box::new(index), }) } else if self.match_token(&[TokenType::Arrow]) { - let field = - self.consume(TokenType::Identifier, "expected field name after '->'")?; - expr = Expr::new(ExprKind::MemberAccess { - left: Box::new(expr), - field, - }) + if self.check(&TokenType::Identifier) && self.check_ahead(&TokenType::LeftParen) { + let method = self.consume(TokenType::Identifier, "expected method name")?; + self.consume(TokenType::LeftParen, "expected '('")?; + let mut args = vec![]; + if !self.check(&TokenType::RightParen) { + loop { + args.push(self.expression()?); + if !self.match_token(&[TokenType::Comma]) { + break; + } + } + } + self.consume(TokenType::RightParen, "expected ')'")?; + expr = Expr::new(ExprKind::MethodCall { + expr: Box::new(expr), + method, + args, + }); + } else { + let field = + self.consume(TokenType::Identifier, "expected field name after '->'")?; + expr = Expr::new(ExprKind::MemberAccess { + left: Box::new(expr), + field, + }); + } } else { break; } diff --git a/src/std/net.zr b/src/std/net.zr index 6647784..041cb50 100644 --- a/src/std/net.zr +++ b/src/std/net.zr @@ -95,7 +95,7 @@ func net.create_udp_server[packed_host: i64, port: i64] : net.UDPSocket, bool return 0 as net.UDPSocket, false if _builtin_syscall(SYS_bind, s->fd, s->addr, 16) < 0 - net.UDPSocket.close_and_free(s) + s->close_and_free() return 0 as net.UDPSocket, false return s, true @@ -129,7 +129,7 @@ func net.UDPSocket.close_and_free[s: net.UDPSocket] : void mem.free(s) func net.encode_dns_name[domain: str] : Blob - domain_len := str.len(domain) + domain_len := domain->len() buf := Blob.alloc(domain_len + 2) out_pos := 0 part_start := 0 @@ -152,7 +152,7 @@ func net.build_dns_query[domain_name: str, record_type: i64] : Blob out := Blob.alloc(12 + name->size + 4) // header - id := math.abs(os.urandom_i64() % 65536) + id := (os.urandom_i64() % 65536)->abs() mem.write16be(out->data + 0, id) mem.write16be(out->data + 2, DNS_RECURSION_DESIRED) mem.write16be(out->data + 4, 1) // num_questions @@ -165,7 +165,7 @@ func net.build_dns_query[domain_name: str, record_type: i64] : Blob mem.write16be(out->data + 12 + name->size, record_type) mem.write16be(out->data + 12 + name->size + 2, DNS_CLASS_IN) - Blob.free(name) + name->free() return out // TODO: dont resolve if its already an IP @@ -175,13 +175,13 @@ func net.resolve[domain: str] : i64, bool // TODO: this probably shouldnt be hardcoded ~s, ok := net.create_udp_client(net.pack_addr(1, 1, 1, 1), 53) if !ok - Blob.free(query) + query->free() return -1, false net.udp_send_to(s, s->addr, query->data, query->size) - Blob.free(query) + query->free() pkt := net.udp_receive(s, 1024) - net.UDPSocket.close_and_free(s) + s->close_and_free() // TODO: do actual parsing @@ -195,5 +195,5 @@ func net.resolve[domain: str] : i64, bool pos += 12 // skip name(2), type(2), class(2), ttl(4), rdlength(2) out := net.pack_addr(pkt->data[pos] as i64, pkt->data[pos+1] as i64, pkt->data[pos+2] as i64, pkt->data[pos+3] as i64) - net.UDPPacket.free(pkt) + pkt->free() return out, true diff --git a/src/std/std.zr b/src/std/std.zr index b16c22a..90b2eab 100644 --- a/src/std/std.zr +++ b/src/std/std.zr @@ -16,7 +16,7 @@ struct mem.Block func mem.align[x: i64] : i64 return (x + 7) & -8 -func mem._split_block[blk: mem.Block, needed: i64] : void +func mem.Block._split[blk: mem.Block, needed: i64] : void if blk->size >= needed + MEM_BLOCK_SIZE + 8 new_blk := (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block new_blk->size = blk->size - needed - MEM_BLOCK_SIZE @@ -62,7 +62,7 @@ func mem.alloc[size: i64] : ptr cur := mem.read64(_builtin_heap_head()) as mem.Block while cur as ptr if cur->free && cur->size >= size - mem._split_block(cur, size) + cur->_split(size) cur->free = false return cur as ptr + MEM_BLOCK_SIZE cur = cur->next @@ -72,7 +72,7 @@ func mem.alloc[size: i64] : ptr if !mem.read64(_builtin_heap_head()) mem.write64(_builtin_heap_head(), blk) - mem._split_block(blk, size) + blk->_split(size) return blk as ptr + MEM_BLOCK_SIZE @@ -136,7 +136,7 @@ func mem.realloc[x: ptr, new_size: i64] : ptr blk := (x - MEM_BLOCK_SIZE) as mem.Block if blk->size >= new_size - mem._split_block(blk, new_size) + blk->_split(new_size) return x next := blk->next @@ -151,7 +151,7 @@ func mem.realloc[x: ptr, new_size: i64] : ptr next->next->prev = blk if mem.read64(_builtin_heap_tail()) == next as ptr mem.write64(_builtin_heap_tail(), blk) - mem._split_block(blk, new_size) + blk->_split(new_size) return x new_ptr := mem.alloc(new_size) @@ -270,7 +270,7 @@ func io.printf[..] : void io.print_char(s[0]) s += 1 -func io.snprintf[..] : i64 +func str.format_into[..] : i64 buf := _var_arg(0) as ptr size := _var_arg(1) as i64 if size <= 0 @@ -279,29 +279,29 @@ func io.snprintf[..] : i64 i := 3 n := 0 - tmp := _stackalloc(21) + tmp := _stackalloc(21) as str while s[0] if s[0] == '%' s += 1 if s[0] == 'd' - str.from_i64_buf(tmp, _var_arg(i) as i64) - tmp_len := str.len(tmp as str) + (_var_arg(i) as i64)->to_str_buf(tmp as ptr) + tmp_len := tmp->len() remaining := size - n - 1 - mem.copy(tmp, buf + n, math.min(tmp_len, remaining)) + mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) n += tmp_len i += 1 else if s[0] == 'x' - str.hex_from_i64_buf(tmp, _var_arg(i) as i64) - tmp_len := str.len(tmp as str) + (_var_arg(i) as i64)->to_hex_str_buf(tmp as ptr) + tmp_len := tmp->len() remaining := size - n - 1 - mem.copy(tmp, buf + n, math.min(tmp_len, remaining)) + mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) n += tmp_len i += 1 else if s[0] == 's' tmp_str := _var_arg(i) as str - tmp_len := str.len(tmp_str) + tmp_len := tmp_str->len() remaining := size - n - 1 mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining)) n += tmp_len @@ -318,7 +318,7 @@ func io.snprintf[..] : i64 else if s[0] == 0 break else - panic("io.snprintf: unrecognized format") + panic("str.format_into: unrecognized format") else if n < size - 1 buf[n] = s[0] @@ -335,7 +335,7 @@ func io.print_sized[x: ptr, size: i64] : void _builtin_syscall(SYS_write, STDOUT, x, size) func io.print[x: str] : void - io.print_sized(x as ptr, str.len(x)) + io.print_sized(x as ptr, x->len()) func io.println[x: str] : void io.print(x) @@ -352,17 +352,17 @@ func io.print_bool[x: bool] : void func io.print_i64[x: i64] : void s := _stackalloc(21) - str.from_i64_buf(s, x) + x->to_str_buf(s) io.print(s as str) func io.print_i64_hex[x: i64] : void s := _stackalloc(17) - str.hex_from_i64_buf(s, x) + x->to_hex_str_buf(s) io.print(s as str) func io.println_i64[x: i64] : void s := _stackalloc(21) - str.from_i64_buf(s, x) + x->to_str_buf(s) io.println(s as str) func io.read_char[] : u8 @@ -376,8 +376,8 @@ func io.read_line[] : str c := io.read_char() if c == '\n' break - str.Builder.append_char(b, c) - s := str.Builder.build(b) + b->append_char(c) + s := b->build() mem.free(b->data) return s @@ -427,13 +427,13 @@ func io.read_binary_file[path: str] : Blob, bool _builtin_syscall(SYS_close, fd) if n != buf->size - Blob.free(buf) + buf->free() return 0 as Blob, false return buf, true func io.write_file[path: str, content: str] : bool - return io.write_binary_file(path, content as ptr, str.len(content)) + return io.write_binary_file(path, content as ptr, content->len()) func io.write_binary_file[path: str, content: ptr, size: i64] : bool fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_WRONLY|O_CREAT|O_TRUNC, 0o644) @@ -453,7 +453,7 @@ func str.len[s: str] : i64 return i func str.make_copy[s: str] : str - size := str.len(s) + 1 + size := s->len() + 1 dup := mem.alloc(size) as str mem.copy(s as ptr, dup as ptr, size) return dup @@ -488,8 +488,8 @@ func str.is_alphanumeric[x: u8] : bool return str.is_letter(x) || str.is_digit(x) func str.concat[a: str, b: str] : str - a_len := str.len(a) - b_len := str.len(b) + a_len := a->len() + b_len := b->len() out := mem.alloc(a_len + b_len + 1) as str mem.copy(a as ptr, out as ptr, a_len) mem.copy(b as ptr, out as ptr + a_len, b_len) @@ -497,11 +497,11 @@ func str.concat[a: str, b: str] : str return out func str.contains[haystack: str, needle: str] : bool - return str.find(haystack, needle) != -1 + return haystack->find(needle) != -1 func str.find[haystack: str, needle: str] : i64 - haystack_len := str.len(haystack) - needle_len := str.len(needle) + haystack_len := haystack->len() + needle_len := needle->len() if needle_len == 0 return 0 @@ -533,7 +533,7 @@ func str.substr[s: str, start: i64, length: i64] : str return out func str.trim[s: str] : str - len := str.len(s) + len := s->len() if len == 0 out := mem.alloc(1) as str out[0] = 0 @@ -548,11 +548,11 @@ func str.trim[s: str] : str while end >= start && str.is_whitespace(s[end]) end -= 1 - return str.substr(s, start, end - start + 1) + return s->substr(start, end - start + 1) func str.split[haystack: str, needle: str]: Array - haystack_len := str.len(haystack) - needle_len := str.len(needle) + haystack_len := haystack->len() + needle_len := needle->len() result := [] if !needle_len @@ -560,7 +560,7 @@ func str.split[haystack: str, needle: str]: Array return result else for i in 0..haystack_len - array.push(result, str.substr(haystack, i, 1)) + result->push(haystack->substr(i, 1)) return result start := 0 @@ -573,17 +573,17 @@ func str.split[haystack: str, needle: str]: Array match = false break if match - array.push(result, str.substr(haystack, start, i - start)) + result->push(haystack->substr(start, i - start)) start = i + needle_len i += needle_len continue i += 1 - array.push(result, str.substr(haystack, start, haystack_len - start)) + result->push(haystack->substr(start, haystack_len - start)) return result func str.reverse[s: str] : str - len := str.len(s) + len := s->len() out := mem.alloc(len + 1) as str for i in 0..len @@ -591,12 +591,12 @@ func str.reverse[s: str] : str out[len] = 0 return out -func str.from_i64[n: i64] : str +func i64.to_str[n: i64] : str out := mem.alloc(21) - str.from_i64_buf(out, n) + n->to_str_buf(out) return out as str -func str.from_i64_buf[buf: ptr, n: i64] : void +func i64.to_str_buf[n: i64, buf: ptr] : void if n == 0 buf[0] = '0' buf[1] = 0 @@ -624,12 +624,12 @@ func str.from_i64_buf[buf: ptr, n: i64] : void buf[i] = tmp[end + 1 + i] buf[len] = 0 -func str.hex_from_i64[n: i64] : str +func i64.to_hex_str[n: i64] : str out := mem.alloc(17) - str.hex_from_i64_buf(out, n) + n->to_hex_str_buf(out) return out as str -func str.hex_from_i64_buf[buf: ptr, n: i64] : void +func i64.to_hex_str_buf[n: i64, buf: ptr] : void hex_chars := "0123456789abcdef" if n == 0 @@ -653,14 +653,14 @@ func str.hex_from_i64_buf[buf: ptr, n: i64] : void buf[len] = 0 -func str.from_char[c: u8] : str +func u8.to_str[c: u8] : str s := mem.alloc(2) as str s[0] = c s[1] = 0 return s func str.parse_i64[s: str] : i64 - len := str.len(s) + len := s->len() i := 0 sign := 1 @@ -698,7 +698,7 @@ func str._hex_digit_to_int[d: u8] : u8 panic("invalid hex digit passed to str._hex_digit_to_int") func str.hex_decode[s: str] : str - s_len := str.len(s) + s_len := s->len() if s_len % 2 != 0 panic("invalid hex string passed to str.hex_decode") @@ -729,13 +729,13 @@ func str.Builder._grow[b: str.Builder, needed: i64] : void b->capacity = new_capacity func str.Builder.append_char[b: str.Builder, c: u8] : void - str.Builder._grow(b, 1) + b->_grow(1) b->data[b->size] = c b->size += 1 func str.Builder.append[b: str.Builder, s: str] : void - len := str.len(s) - str.Builder._grow(b, len) + len := s->len() + b->_grow(len) mem.copy(s as ptr, b->data + b->size, len) b->size += len @@ -746,8 +746,8 @@ func str.Builder.build[b: str.Builder] : str return s func math.gcd[a: i64, b: i64] : i64 - a = math.abs(a) - b = math.abs(b) + a = a->abs() + b = b->abs() while b != 0 tmp := b b = a % b @@ -764,14 +764,14 @@ func math.max[a: i64, b: i64] : i64 return a return b -func math.abs[n: i64] : i64 +func i64.abs[n: i64] : i64 if n == -9223372036854775808 panic("MIN_I64 passed to math.abs") if n < 0 return -n return n -func math.sign[n: i64] : i64 +func i64.sign[n: i64] : i64 if n < 0 return -1 else if n > 0 @@ -793,7 +793,7 @@ func math.pow[b: i64, e: i64] : i64 func math.lcm[a: i64, b: i64] : i64 return (a / math.gcd(a, b)) * b -func math.isqrt[n: i64] : i64 +func i64.isqrt[n: i64] : i64 if n < 0 panic("negative number passed to math.isqrt") if n == 0 || n == 1 @@ -808,7 +808,7 @@ func math.isqrt[n: i64] : i64 return guess -func math.is_prime[n: i64]: bool +func i64.is_prime[n: i64]: bool if n <= 1 return false if n == 2 || n == 3 @@ -828,7 +828,7 @@ struct Array size: i64 capacity: i64 -func array.new_preallocated_and_zeroed[size: i64] : Array +func Array.new_preallocated_and_zeroed[size: i64] : Array xs := new* Array if size > 0 xs->data = mem.alloc(size * 8) @@ -837,17 +837,17 @@ func array.new_preallocated_and_zeroed[size: i64] : Array xs->capacity = size return xs -func array.nth[xs: Array, n: i64] : any +func Array.nth[xs: Array, n: i64] : any if n < 0 || n >= xs->size panic("array.nth out of bounds") return mem.read64(xs->data + n * 8) -func array.set[xs: Array, n: i64, x: any] : void +func Array.set[xs: Array, n: i64, x: any] : void if n < 0 || n >= xs->size panic("array.set out of bounds") mem.write64(xs->data + n * 8, x) -func array.push[xs: Array, x: any] : void +func Array.push[xs: Array, x: any] : void if xs->size == xs->capacity new_capacity := 4 if xs->capacity != 0 @@ -858,35 +858,35 @@ func array.push[xs: Array, x: any] : void mem.write64(xs->data + xs->size * 8, x) xs->size += 1 -func array.free[xs: Array] : void +func Array.free[xs: Array] : void if xs->data != 0 mem.free(xs->data) mem.free(xs) -func array.free_with_items[xs: Array] : void +func Array.free_with_items[xs: Array] : void if xs->data != 0 for i in 0..xs->size - mem.free(array.nth(xs, i)) + mem.free(xs->nth(i)) mem.free(xs->data) mem.free(xs) -func array.pop[xs: Array] : any +func Array.pop[xs: Array] : any if xs->size == 0 panic("array.pop on empty array") - x : any = array.nth(xs, xs->size - 1) + x : any = xs->nth(xs->size - 1) xs->size = xs->size - 1 return x -func array.slice[xs: Array, start: i64, length: i64] : Array +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") - new_array := array.new_preallocated_and_zeroed(length) + new_array := Array.new_preallocated_and_zeroed(length) mem.copy(xs->data + start * 8, new_array->data, length * 8) return new_array -func array.concat[a: Array, b: Array] : Array - new_array := array.new_preallocated_and_zeroed(a->size + b->size) +func Array.concat[a: Array, b: Array] : Array + new_array := Array.new_preallocated_and_zeroed(a->size + b->size) mem.copy(a->data, new_array->data, a->size * 8) mem.copy(b->data, new_array->data + a->size * 8, b->size * 8) return new_array @@ -903,31 +903,31 @@ struct HashMap._Node func HashMap.new[] : HashMap map := new* HashMap - map->table = array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE) + map->table = Array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE) return map func HashMap.insert[map: HashMap, key: str, value: any] : void index := HashMap._djb2(key) - current : HashMap._Node = array.nth(map->table, index) + current : HashMap._Node = map->table->nth(index) while current as ptr - if str.equal(current->key, key) + if current->key->equal(key) current->value = value return 0 current = current->next new_node := new* HashMap._Node - new_node->key = str.make_copy(key) + new_node->key = key->make_copy() new_node->value = value - new_node->next = array.nth(map->table, index) - array.set(map->table, index, new_node) + new_node->next = map->table->nth(index) + map->table->set(index, new_node) func HashMap.get[map: HashMap, key: str] : any, bool index := HashMap._djb2(key) - current : HashMap._Node = array.nth(map->table, index) + current : HashMap._Node = map->table->nth(index) while current as ptr - if str.equal(current->key, key) + if current->key->equal(key) return current->value, true current = current->next @@ -935,15 +935,15 @@ func HashMap.get[map: HashMap, key: str] : any, bool func HashMap.delete[map: HashMap, key: str] : void index := HashMap._djb2(key) - current : HashMap._Node = array.nth(map->table, index) + current : HashMap._Node = map->table->nth(index) prev := 0 as HashMap._Node while current as ptr - if str.equal(current->key, key) + if current->key->equal(key) if prev as ptr prev->next = current->next else - array.set(map->table, index, current->next) + map->table->set(index, current->next) mem.free(current->key) mem.free(current) @@ -954,70 +954,69 @@ func HashMap.delete[map: HashMap, key: str] : void func HashMap._djb2[key: str] : i64 hash := 5381 - key_len := str.len(key) - for i in 0..key_len + for i in 0..key->len() hash = ((hash << 5) + hash) + key[i] hash = (hash & 0x7fffffffffffffff) // prevent negative return hash % HashMap._TABLE_SIZE func HashMap.free[map: HashMap] : void for i in 0..HashMap._TABLE_SIZE - current : HashMap._Node = array.nth(map->table, i) + current : HashMap._Node = map->table->nth(i) while current as ptr tmp := current current = current->next mem.free(tmp->key) mem.free(tmp) - array.free(map->table) + map->table->free() mem.free(map) -func alg.quicksort[arr: Array] : void - alg._do_quicksort(arr, 0, arr->size - 1) +func Array.quicksort[arr: Array] : void + arr->_do_quicksort(0, arr->size - 1) -func alg._do_quicksort[arr: Array, low: i64, high: i64] : void +func Array._do_quicksort[arr: Array, low: i64, high: i64] : void if low < high - i := alg._partition(arr, low, high) - alg._do_quicksort(arr, low, i - 1) - alg._do_quicksort(arr, i + 1, high) + i := arr->_partition(low, high) + arr->_do_quicksort(low, i - 1) + arr->_do_quicksort(i + 1, high) -func alg._partition[arr: Array, low: i64, high: i64] : i64 - pivot : i64 = array.nth(arr, high) +func Array._partition[arr: Array, low: i64, high: i64] : i64 + pivot : i64 = arr->nth(high) i := low - 1 for j in (low)..high - if array.nth(arr, j) as i64 <= pivot + if arr->nth(j) as i64 <= pivot i += 1 - temp : i64 = array.nth(arr ,i) - array.set(arr, i, array.nth(arr, j)) - array.set(arr, j, temp) - temp : i64 = array.nth(arr, i + 1) - array.set(arr, i + 1, array.nth(arr, high)) - array.set(arr, high, temp) + temp : i64 = arr->nth(i) + arr->set(i, arr->nth(j)) + arr->set(j, temp) + temp : i64 = arr->nth(i + 1) + arr->set(i + 1, arr->nth(high)) + arr->set(high, temp) return i + 1 -func alg.count[arr: Array, item: any] : i64 +func Array.count[arr: Array, item: any] : i64 count := 0 for i in 0..arr->size - if array.nth(arr, i) == item + if arr->nth(i) == item count += 1 return count -func alg.map[arr: Array, fn: fnptr] : Array - out := array.new_preallocated_and_zeroed(arr->size) +func Array.map[arr: Array, fn: fnptr] : Array + out := Array.new_preallocated_and_zeroed(arr->size) for i in 0..arr->size - array.set(out, i, fn(array.nth(arr, i))) + out->set(i, fn(arr->nth(i))) return out -func alg.filter[arr: Array, fn: fnptr] : Array +func Array.filter[arr: Array, fn: fnptr] : Array out := [] for i in 0..arr->size - if fn(array.nth(arr, i)) - array.push(out, array.nth(arr, i)) + if fn(arr->nth(i)) + out->push(arr->nth(i)) return out -func alg.reduce[arr: Array, fn: fnptr, acc: any] : any +func Array.reduce[arr: Array, fn: fnptr, acc: any] : any for i in 0..arr->size - acc = fn(acc, array.nth(arr, i)) + acc = fn(acc, arr->nth(i)) return acc func os.exit[code: i64] : void @@ -1098,7 +1097,7 @@ func os.list_directory[path: str] : Array, bool while true n := _builtin_syscall(SYS_getdents64, fd, buf, 1024) if n < 0 - array.free_with_items(files) + files->free_with_items() _builtin_syscall(SYS_close, fd) return 0 as Array, false else if n == 0 @@ -1118,7 +1117,7 @@ func os.list_directory[path: str] : Array, bool if name[2] == 0 skip = true if !skip - array.push(files, str.make_copy(name as str)) + files->push((name as str)->make_copy()) pos += len _builtin_syscall(SYS_close, fd) diff --git a/src/typechecker.rs b/src/typechecker.rs index 30500ac..c91968d 100644 --- a/src/typechecker.rs +++ b/src/typechecker.rs @@ -564,6 +564,54 @@ impl<'a> TypeChecker<'a> { } Ok(type_name.lexeme.clone()) } + ExprKind::MethodCall { expr, method, args } => { + let receiver_type = self.typecheck_expr(env, expr)?; + let func_name = format!("{}.{}", receiver_type, method.lexeme); + + let func_type = match self.symbol_table.functions.get(&func_name) { + Some(f) => f, + None => { + return error!( + method.loc, + format!( + "method {} not found on on type {}", + method.lexeme, receiver_type + ) + ); + } + }; + + if let Some(params) = &func_type.params { + if params.len() != args.len() + 1 { + return error!( + method.loc, + format!( + "expected {} arguments, got {}", + params.len() - 1, + args.len() + ) + ); + } + if params[0] != receiver_type { + return error!( + method.loc, + format!( + "first parameter of the method must be of type {}", + receiver_type + ) + ); + } + for (i, arg) in args.iter().enumerate() { + expect_type!(self.typecheck_expr(env, arg)?, params[i + 1], method.loc); + } + Ok(func_type.return_type.clone()) + } else { + for arg in args { + self.typecheck_expr(env, arg)?; + } + Ok(func_type.return_type.clone()) + } + } }?; self.expr_types.insert(expr.id, expr_type.clone()); diff --git a/test.zr b/test.zr index ec0ae49..e2959fc 100644 --- a/test.zr +++ b/test.zr @@ -3,14 +3,14 @@ func run_test[x: str] : void run_blacklist := ["/aoc", "guess_number.zr", "tcp_server.zr", "udp_server.zr"] for i in 0..build_blacklist->size - if str.equal(x, array.nth(build_blacklist, i)) + if x->equal(build_blacklist->nth(i)) io.printf("Skipping %s...\n", x) return 0 io.printf("Building %s...\n", x) build_start_time := os.time() - ~status, ok := os.run_shell_command(str.concat("./target/release/zern ", x)) + ~status, ok := os.run_shell_command("./target/release/zern "->concat(x)) if !ok || status != 0 os.exit(1) build_end_time := os.time() @@ -18,15 +18,15 @@ func run_test[x: str] : void io.printf("%dms\n", build_end_time - build_start_time) for i in 0..run_blacklist->size - if str.find(x, array.nth(run_blacklist, i)) != -1 + if x->find(run_blacklist->nth(i)) != -1 io.printf("Skipping %s...\n", x) return 0 io.printf("Running %s...\n", x) run_cmd := "./out" - if str.equal(x, "examples/curl.zr") - run_cmd = str.concat(run_cmd, " http://example.com") + if x->equal("examples/curl.zr") + run_cmd = run_cmd->concat(" http://example.com") run_start_time := os.time() ~status, ok := os.run_shell_command(run_cmd) @@ -41,7 +41,7 @@ func run_directory[dir: str] : void if !ok panic("failed to open test directory") for i in 0..files->size - run_test(str.concat(dir, array.nth(files, i))) + run_test(dir->concat(files->nth(i))) func main[] : i64 ~status, ok := os.run_shell_command("cargo build --release")