From c527aceecdbaf0ab6720e2db0313c742a3a4e7d0 Mon Sep 17 00:00:00 2001 From: Toni Date: Tue, 23 Dec 2025 18:10:56 +0100 Subject: [PATCH] infer var type when initializer is a number constant --- examples/brainfuck.zr | 8 +++---- examples/curl.zr | 4 ++-- examples/fib.zr | 4 ++-- examples/puzzles/aoc2024_01.zr | 4 ++-- examples/puzzles/aoc2024_02.zr | 4 ++-- examples/puzzles/aoc2024_04.zr | 4 ++-- examples/puzzles/aoc2024_05.zr | 4 ++-- examples/puzzles/aoc2024_07.zr | 4 ++-- examples/puzzles/aoc2025_01.zr | 8 +++---- examples/puzzles/aoc2025_02.zr | 4 ++-- examples/puzzles/aoc2025_03.zr | 8 +++---- examples/puzzles/euler_00.zr | 2 +- examples/puzzles/euler_01.zr | 2 +- examples/puzzles/euler_02.zr | 6 ++--- examples/puzzles/euler_03.zr | 4 ++-- examples/puzzles/euler_04.zr | 2 +- examples/puzzles/euler_05.zr | 2 +- examples/puzzles/euler_06.zr | 4 ++-- examples/puzzles/euler_07.zr | 4 ++-- examples/puzzles/euler_08.zr | 6 ++--- examples/puzzles/euler_10.zr | 2 +- examples/puzzles/euler_11.zr | 4 ++-- examples/puzzles/euler_12.zr | 6 ++--- examples/puzzles/euler_14.zr | 6 ++--- examples/puzzles/euler_15.zr | 6 ++--- examples/puzzles/euler_16.zr | 4 ++-- examples/puzzles/euler_17.zr | 2 +- examples/puzzles/euler_19.zr | 4 ++-- examples/puzzles/euler_20.zr | 4 ++-- examples/puzzles/euler_21.zr | 6 ++--- examples/raylib.zr | 4 ++-- examples/rule110.zr | 2 +- examples/sqlite_todo.zr | 6 ++--- src/codegen_x86_64.rs | 16 ++++++++++++- src/parser.rs | 16 ++++++++----- src/std.zr | 42 +++++++++++++++++----------------- 36 files changed, 118 insertions(+), 100 deletions(-) diff --git a/examples/brainfuck.zr b/examples/brainfuck.zr index 811d936..1f203ec 100644 --- a/examples/brainfuck.zr +++ b/examples/brainfuck.zr @@ -2,11 +2,11 @@ func main[] : i64 // https://brainfuck.org/sierpinski.b let src: str = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]" let src_len: i64 = str.len(src) - let i: i64 = 0 + let i = 0 let memory: ptr = mem.alloc(30000) mem.zero(memory, 30000) - let p: i64 = 0 + let p = 0 while i < src_len let op: u8 = src[i] @@ -26,7 +26,7 @@ func main[] : i64 else if op == '[' if !memory[p] i = i + 1 - let opened: i64 = 0 + let opened = 0 while i < src_len & !(src[i] == ']' & !opened) if src[i] == '[' opened = opened + 1 @@ -36,7 +36,7 @@ func main[] : i64 else if op == ']' if memory[p] i = i - 1 - let closed: i64 = 0 + let closed = 0 while i >= 0 & !(src[i] == '[' & !closed) if src[i] == ']' closed = closed + 1 diff --git a/examples/curl.zr b/examples/curl.zr index 9ab0438..ea79b3d 100644 --- a/examples/curl.zr +++ b/examples/curl.zr @@ -11,7 +11,7 @@ func main[argc: i64, argv: ptr] : i64 dbg.panic("invalid url scheme") let url_len: i64 = str.len(url) - let host_start: i64 = 7 + let host_start = 7 let i: i64 = host_start while i < url_len if url[i] == '/' @@ -37,7 +37,7 @@ func main[argc: i64, argv: ptr] : i64 mem.free(req) let header_buf: str = mem.alloc(8192) - let header_size: i64 = 0 + let header_size = 0 let found: bool = false let end_index: i64 = -1 diff --git a/examples/fib.zr b/examples/fib.zr index 0833c31..1bc9c60 100644 --- a/examples/fib.zr +++ b/examples/fib.zr @@ -1,6 +1,6 @@ func main[] : i64 - let a: i64 = 0 - let b: i64 = 1 + let a = 0 + let b = 1 while a < 100000 io.println_i64(a) diff --git a/examples/puzzles/aoc2024_01.zr b/examples/puzzles/aoc2024_01.zr index 79fc222..e4e7363 100644 --- a/examples/puzzles/aoc2024_01.zr +++ b/examples/puzzles/aoc2024_01.zr @@ -1,5 +1,5 @@ func part1[l1: array, l2: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(l1) out = out + math.abs(array.nth(l1, i) - array.nth(l2, i)) @@ -7,7 +7,7 @@ func part1[l1: array, l2: array] : void io.println_i64(out) func part2[l1: array, l2: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(l1) out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i)) diff --git a/examples/puzzles/aoc2024_02.zr b/examples/puzzles/aoc2024_02.zr index df0e22e..53a6238 100644 --- a/examples/puzzles/aoc2024_02.zr +++ b/examples/puzzles/aoc2024_02.zr @@ -14,7 +14,7 @@ func check[report: array] : bool return true func part1[data: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(data) if check(array.nth(data, i)) @@ -23,7 +23,7 @@ func part1[data: array] : void io.println_i64(out) func part2[data: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(data) if check(array.nth(data, i)) diff --git a/examples/puzzles/aoc2024_04.zr b/examples/puzzles/aoc2024_04.zr index 1e11672..70a97c1 100644 --- a/examples/puzzles/aoc2024_04.zr +++ b/examples/puzzles/aoc2024_04.zr @@ -14,7 +14,7 @@ func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool return true func part1[lines: array] : void - let out: i64 = 0 + let out = 0 for x in 0..array.size(lines) for y in 0..str.len(array.nth(lines, x)) @@ -38,7 +38,7 @@ func part1[lines: array] : void io.println_i64(out) func part2[lines: array] : void - let out: i64 = 0 + let out = 0 for x in 1..array.size(lines)-1 for y in 1..str.len(array.nth(lines, x))-1 diff --git a/examples/puzzles/aoc2024_05.zr b/examples/puzzles/aoc2024_05.zr index c85aa43..683f0b7 100644 --- a/examples/puzzles/aoc2024_05.zr +++ b/examples/puzzles/aoc2024_05.zr @@ -26,7 +26,7 @@ func sort_by_rules[update: array, rules_left: array, rules_right: array] : void swapped = true func part1[updates: array, rules_left: array, rules_right: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(updates) let update: array = array.nth(updates, i) @@ -36,7 +36,7 @@ func part1[updates: array, rules_left: array, rules_right: array] : void io.println_i64(out) func part2[updates: array, rules_left: array, rules_right: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(updates) let update: array = array.nth(updates, i) diff --git a/examples/puzzles/aoc2024_07.zr b/examples/puzzles/aoc2024_07.zr index 45dfc6d..57c2e43 100644 --- a/examples/puzzles/aoc2024_07.zr +++ b/examples/puzzles/aoc2024_07.zr @@ -44,7 +44,7 @@ func solve[ops: array, e: array] : i64 return 0 func part1[equations: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(equations) out = out + solve(["add", "mul"], array.nth(equations, i)) @@ -52,7 +52,7 @@ func part1[equations: array] : void io.println_i64(out) func part2[equations: array] : void - let out: i64 = 0 + let out = 0 for i in 0..array.size(equations) out = out + solve(["add", "mul", "concat"], array.nth(equations, i)) diff --git a/examples/puzzles/aoc2025_01.zr b/examples/puzzles/aoc2025_01.zr index fec511a..700d07c 100644 --- a/examples/puzzles/aoc2025_01.zr +++ b/examples/puzzles/aoc2025_01.zr @@ -1,6 +1,6 @@ func part1[lines: array] : void - let password: i64 = 0 - let dial: i64 = 50 + let password = 0 + let dial = 50 for i in 0..array.size(lines) let line: str = array.nth(lines, i) @@ -22,8 +22,8 @@ func part1[lines: array] : void io.println_i64(password) func part2[lines: array] : void - let password: i64 = 0 - let dial: i64 = 50 + let password = 0 + let dial = 50 for i in 0..array.size(lines) let line: str = array.nth(lines, i) diff --git a/examples/puzzles/aoc2025_02.zr b/examples/puzzles/aoc2025_02.zr index 1bf20a1..5f4fd4d 100644 --- a/examples/puzzles/aoc2025_02.zr +++ b/examples/puzzles/aoc2025_02.zr @@ -9,7 +9,7 @@ func part1_is_invalid_id[s: str] : bool return str.equal(first, second) func part1[ranges: array] : void - let sum: i64 = 0 + let sum = 0 for i in 0..array.size(ranges) let parts: array = array.nth(ranges, i) |> str.split("-") @@ -40,7 +40,7 @@ func part2_is_invalid_id[s: str] : bool return false func part2[ranges: array] : void - let sum: i64 = 0 + let sum = 0 for i in 0..array.size(ranges) let parts: array = array.nth(ranges, i) |> str.split("-") diff --git a/examples/puzzles/aoc2025_03.zr b/examples/puzzles/aoc2025_03.zr index 7591bb3..7c5d1a9 100644 --- a/examples/puzzles/aoc2025_03.zr +++ b/examples/puzzles/aoc2025_03.zr @@ -1,10 +1,10 @@ func part1[lines: array] : void - let sum: i64 = 0 + let sum = 0 for i in 0..array.size(lines) let line: str = array.nth(lines, i) - let largest: i64 = 0 + let largest = 0 for j in 0..str.len(line) for k in (j+1)..str.len(line) let s: str = mem.alloc(3) @@ -20,7 +20,7 @@ func part1[lines: array] : void // had to cheat this one func part2_rec[bank: str, start: i64, remaining: i64] : i64 - let largest: i64 = 0 + let largest = 0 let largest_idx: i64 = start let len: i64 = str.len(bank) @@ -36,7 +36,7 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64 return largest func part2[lines: array] : void - let sum: i64 = 0 + let sum = 0 for i in 0..array.size(lines) let line: str = array.nth(lines, i) diff --git a/examples/puzzles/euler_00.zr b/examples/puzzles/euler_00.zr index e8805ae..138412f 100644 --- a/examples/puzzles/euler_00.zr +++ b/examples/puzzles/euler_00.zr @@ -1,5 +1,5 @@ func main[] : i64 - let sum: i64 = 0 + let sum = 0 for i in 0..266000 if i % 2 != 0 diff --git a/examples/puzzles/euler_01.zr b/examples/puzzles/euler_01.zr index 6fbe47e..2f3eff5 100644 --- a/examples/puzzles/euler_01.zr +++ b/examples/puzzles/euler_01.zr @@ -1,5 +1,5 @@ func main[] : i64 - let sum: i64 = 0 + let sum = 0 for i in 0..1000 if i % 5 == 0 | i % 3 == 0 diff --git a/examples/puzzles/euler_02.zr b/examples/puzzles/euler_02.zr index 68bd32c..4078c53 100644 --- a/examples/puzzles/euler_02.zr +++ b/examples/puzzles/euler_02.zr @@ -1,7 +1,7 @@ func main[] : i64 - let sum: i64 = 0 - let a: i64 = 0 - let b: i64 = 1 + let sum = 0 + let a = 0 + let b = 1 while a < 4000000 if a % 2 == 0 diff --git a/examples/puzzles/euler_03.zr b/examples/puzzles/euler_03.zr index a4ed5ab..72928ba 100644 --- a/examples/puzzles/euler_03.zr +++ b/examples/puzzles/euler_03.zr @@ -1,6 +1,6 @@ func main[] : i64 - let n: i64 = 600851475143 - let f: i64 = 2 + let n = 600851475143 + let f = 2 while f * f <= n if n % f == 0 diff --git a/examples/puzzles/euler_04.zr b/examples/puzzles/euler_04.zr index 3e27193..866cee1 100644 --- a/examples/puzzles/euler_04.zr +++ b/examples/puzzles/euler_04.zr @@ -1,5 +1,5 @@ func main[] : i64 - let out: i64 = 0 + let out = 0 for a in 500..1000 for b in 500..1000 diff --git a/examples/puzzles/euler_05.zr b/examples/puzzles/euler_05.zr index 312d997..b911e6e 100644 --- a/examples/puzzles/euler_05.zr +++ b/examples/puzzles/euler_05.zr @@ -1,5 +1,5 @@ func main[] : i64 - let out: i64 = 1 + let out = 1 for i in 1..21 out = math.lcm(out, i) diff --git a/examples/puzzles/euler_06.zr b/examples/puzzles/euler_06.zr index 19ab320..d2c1733 100644 --- a/examples/puzzles/euler_06.zr +++ b/examples/puzzles/euler_06.zr @@ -1,9 +1,9 @@ func main[] : i64 - let sum_of_squares: i64 = 0 + let sum_of_squares = 0 for i in 1..101 sum_of_squares = sum_of_squares + i * i - let square_of_sum: i64 = 0 + let square_of_sum = 0 for i in 1..101 square_of_sum = square_of_sum + i square_of_sum = square_of_sum * square_of_sum diff --git a/examples/puzzles/euler_07.zr b/examples/puzzles/euler_07.zr index 6107143..b9fdfac 100644 --- a/examples/puzzles/euler_07.zr +++ b/examples/puzzles/euler_07.zr @@ -1,7 +1,7 @@ func main[] : i64 - let found: i64 = 0 + let found = 0 - let i: i64 = 1 + let i = 1 while true if math.is_prime(i) found = found + 1 diff --git a/examples/puzzles/euler_08.zr b/examples/puzzles/euler_08.zr index 65e0bba..0fd5bc5 100644 --- a/examples/puzzles/euler_08.zr +++ b/examples/puzzles/euler_08.zr @@ -1,11 +1,11 @@ func main[] : i64 let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" - let out: i64 = 0 + let out = 0 let max: i64 = str.len(n) - 13 for i in 0..max - let s: i64 = 1 - let j: i64 = 0 + let s = 1 + let j = 0 while j < 13 s = s * (n[i + j] - '0') j = j + 1 diff --git a/examples/puzzles/euler_10.zr b/examples/puzzles/euler_10.zr index 25176e1..bcda753 100644 --- a/examples/puzzles/euler_10.zr +++ b/examples/puzzles/euler_10.zr @@ -1,5 +1,5 @@ func main[] : i64 - let sum: i64 = 0 + let sum = 0 for i in 0..2000000 if math.is_prime(i) diff --git a/examples/puzzles/euler_11.zr b/examples/puzzles/euler_11.zr index b5f750d..9b6da28 100644 --- a/examples/puzzles/euler_11.zr +++ b/examples/puzzles/euler_11.zr @@ -1,5 +1,5 @@ func main[] : i64 - let N: i64 = 20 + let N = 20 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]) @@ -23,7 +23,7 @@ func main[] : i64 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]) - let out: i64 = 0 + let out = 0 for i in 0..N-3 for j in 0..N-3 diff --git a/examples/puzzles/euler_12.zr b/examples/puzzles/euler_12.zr index 2b02baa..2ba0bc0 100644 --- a/examples/puzzles/euler_12.zr +++ b/examples/puzzles/euler_12.zr @@ -1,7 +1,7 @@ func num_divisors[n: i64] : i64 let end: i64 = math.isqrt(n) - let out: i64 = 0 + let out = 0 for i in 1..end+1 if n % i == 0 out = out + 2 @@ -11,8 +11,8 @@ func num_divisors[n: i64] : i64 return out func main[] : i64 - let n: i64 = 0 - let i: i64 = 1 + let n = 0 + let i = 1 while true n = n + i if num_divisors(n) > 500 diff --git a/examples/puzzles/euler_14.zr b/examples/puzzles/euler_14.zr index 643d536..82fa9df 100644 --- a/examples/puzzles/euler_14.zr +++ b/examples/puzzles/euler_14.zr @@ -4,15 +4,15 @@ func collatz_num[n: i64] : i64 return n * 3 + 1 func collatz_seq[n: i64]: i64 - let i: i64 = 1 + let i = 1 while n != 1 n = collatz_num(n) i = i + 1 return i func main[] : i64 - let max: i64 = 0 - let max_index: i64 = 0 + let max = 0 + let max_index = 0 for i in 1..1000000 let seq: i64 = collatz_seq(i) diff --git a/examples/puzzles/euler_15.zr b/examples/puzzles/euler_15.zr index e2b43f8..40a0b40 100644 --- a/examples/puzzles/euler_15.zr +++ b/examples/puzzles/euler_15.zr @@ -1,7 +1,7 @@ func main[] : i64 - let n: i64 = 40 - let r: i64 = 20 - let out: i64 = 1 + let n = 40 + let r = 20 + let out = 1 for i in 1..r+1 out = out * (n - (r - i)) / i diff --git a/examples/puzzles/euler_16.zr b/examples/puzzles/euler_16.zr index 3c247ab..c8e6563 100644 --- a/examples/puzzles/euler_16.zr +++ b/examples/puzzles/euler_16.zr @@ -3,7 +3,7 @@ func main[] : i64 array.push(n, 1) for j in 0..1000 - let carry: i64 = 0 + let carry = 0 for i in 0..array.size(n) let tmp: i64 = array.nth(n, i) * 2 + carry array.set(n, i, tmp % 10) @@ -12,7 +12,7 @@ func main[] : i64 array.push(n, carry % 10) carry = carry / 10 - let sum: i64 = 0 + let sum = 0 for i in 0..array.size(n) sum = sum + array.nth(n, i) diff --git a/examples/puzzles/euler_17.zr b/examples/puzzles/euler_17.zr index c0248ac..5d9b0ad 100644 --- a/examples/puzzles/euler_17.zr +++ b/examples/puzzles/euler_17.zr @@ -3,7 +3,7 @@ func main[] : i64 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: i64 = 0 + let sum = 0 for i in 1..10 sum = sum + array.nth(s1, i) diff --git a/examples/puzzles/euler_19.zr b/examples/puzzles/euler_19.zr index 984d7ac..d679ba7 100644 --- a/examples/puzzles/euler_19.zr +++ b/examples/puzzles/euler_19.zr @@ -10,8 +10,8 @@ func days[y: i64, m: i64] : i64 return 31 func main[] : i64 - let wday: i64 = 0 - let sun: i64 = 0 + let wday = 0 + let sun = 0 for year in 1901..2001 for mon in 1..13 diff --git a/examples/puzzles/euler_20.zr b/examples/puzzles/euler_20.zr index ccd8d9b..116648a 100644 --- a/examples/puzzles/euler_20.zr +++ b/examples/puzzles/euler_20.zr @@ -1,5 +1,5 @@ func multiply[n: array, x: i64] : void - let carry: i64 = 0 + let carry = 0 for i in 0..array.size(n) let prod: i64 = array.nth(n, i) * x + carry array.set(n, i, prod % 10) @@ -14,7 +14,7 @@ func main[] : i64 for i in 2..101 multiply(n, i) - let sum: i64 = 0 + let sum = 0 for i in 0..array.size(n) sum = sum + array.nth(n, i) diff --git a/examples/puzzles/euler_21.zr b/examples/puzzles/euler_21.zr index 39b7b8d..eb6573b 100644 --- a/examples/puzzles/euler_21.zr +++ b/examples/puzzles/euler_21.zr @@ -1,9 +1,9 @@ func divisors_sum[n: i64] : i64 let k: i64 = n - let sum: i64 = 1 + let sum = 1 for i in 2..k+1 - let p: i64 = 1 + let p = 1 while k % i == 0 p = p * i k = k / i @@ -11,7 +11,7 @@ func divisors_sum[n: i64] : i64 return sum - n func main[] : i64 - let sum: i64 = 0 + let sum = 0 for i in 2..10000 let d: i64 = divisors_sum(i) diff --git a/examples/raylib.zr b/examples/raylib.zr index 9e87080..7b2d984 100644 --- a/examples/raylib.zr +++ b/examples/raylib.zr @@ -11,8 +11,8 @@ extern DrawRectangle extern IsKeyDown func main[] : i64 - let x: i64 = 200 - let y: i64 = 200 + let x = 200 + let y = 200 InitWindow(800, 600, "Hello, World!") SetTargetFPS(60) diff --git a/examples/rule110.zr b/examples/rule110.zr index a60c553..9c6f84c 100644 --- a/examples/rule110.zr +++ b/examples/rule110.zr @@ -24,7 +24,7 @@ func print_state[state: array]: void io.println("") func main[] : i64 - let SIZE: i64 = 60 + let SIZE = 60 let state: array = [] for i in 0..SIZE diff --git a/examples/sqlite_todo.zr b/examples/sqlite_todo.zr index 0e99cda..c721097 100644 --- a/examples/sqlite_todo.zr +++ b/examples/sqlite_todo.zr @@ -11,9 +11,9 @@ extern sqlite3_column_text extern sqlite3_finalize func main[] : i64 - let rc: i64 = 0 - let db: i64 = 0 - let stmt: i64 = 0 + let rc = 0 + let db = 0 + let stmt = 0 rc = sqlite3_open("todo.db", @db) if rc diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index ef04ba2..04363c8 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -158,8 +158,22 @@ _builtin_environ: ); } + let var_type: String = match var_type { + Some(t) => t.lexeme, + None => match &initializer { + Expr::Literal(token) => { + if token.token_type == TokenType::Number { + "i64".into() + } else { + return error!(&name.loc, "unable to infer variable type"); + } + } + _ => return error!(&name.loc, "unable to infer variable type"), + }, + }; + self.compile_expr(env, initializer)?; - let offset = env.define_var(name.lexeme.clone(), var_type.lexeme); + let offset = env.define_var(name.lexeme.clone(), var_type); emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset); } Stmt::Block(statements) => { diff --git a/src/parser.rs b/src/parser.rs index 38f8248..999fbce 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,7 +11,7 @@ pub enum Stmt { Expression(Expr), Let { name: Token, - var_type: Token, + var_type: Option, initializer: Expr, }, Block(Vec), @@ -172,12 +172,16 @@ impl Parser { fn let_declaration(&mut self) -> Result { let name = self.consume(TokenType::Identifier, "expected variable name")?; - self.consume(TokenType::Colon, "expected ':' after variable name")?; - let var_type = self.consume(TokenType::Identifier, "expected variable type")?; - if !TYPES.contains(&var_type.lexeme.as_str()) { - return error!(&name.loc, format!("unknown type: {}", var_type.lexeme)); - } + let var_type = if self.match_token(&[TokenType::Colon]) { + let token = self.consume(TokenType::Identifier, "expected variable type")?; + if !TYPES.contains(&token.lexeme.as_str()) { + return error!(&name.loc, format!("unknown type: {}", token.lexeme)); + } + Some(token) + } else { + None + }; self.consume(TokenType::Equal, "expected '=' after variable type")?; let initializer = self.expression()?; diff --git a/src/std.zr b/src/std.zr index e48591d..3cf7f43 100644 --- a/src/std.zr +++ b/src/std.zr @@ -65,7 +65,7 @@ func io.read_char[] : u8 return c func io.read_line[]: str - let MAX_SIZE: i64 = 60000 + let MAX_SIZE = 60000 let buffer: str = mem.alloc(MAX_SIZE + 1) let n: i64 = _builtin_syscall(0, 0, buffer, MAX_SIZE) // read if n < 0 @@ -96,7 +96,7 @@ func io.write_file[path: str, content: str] : void _builtin_syscall(3, fd) // close func str.len[s: str] : i64 - let i: i64 = 0 + let i = 0 while mem.read8(s + i) i = i + 1 return i @@ -112,7 +112,7 @@ func str.set[s: str, n: i64, c: u8] : void mem.write8(s + n, c) func str.equal[a: str, b: str] : bool - let i: i64 = 0 + let i = 0 while a[i] != 0 & b[i] != 0 if a[i] != b[i] return false @@ -165,7 +165,7 @@ func str.trim[s: str] : str if len == 0 return "" - let start: i64 = 0 + let start = 0 let end: i64 = len - 1 while start <= end & str.is_whitespace(s[start]) @@ -189,8 +189,8 @@ func str.split[haystack: str, needle: str]: array array.push(result, str.substr(haystack, i, 1)) return result - let start: i64 = 0 - let i: i64 = 0 + let start = 0 + let i = 0 while i < haystack_len if i <= haystack_len - needle_len let match: bool = true @@ -226,7 +226,7 @@ func str.from_i64[n: i64] : str if neg n = -n let buf: str = mem.alloc(21) // enough to fit -MAX_I64 - let i: i64 = 0 + let i = 0 while n > 0 let d: u8 = n % 10 str.set(buf, i, '0' + d) @@ -248,14 +248,14 @@ func str.from_char[c: u8] : str func str.parse_i64[s: str] : i64 let len: i64 = str.len(s) - let i: i64 = 0 + let i = 0 - let sign: i64 = 1 + let sign = 1 if i < len & s[i] == '-' sign = -1 i = i + 1 - let num: i64 = 0 + let num = 0 while i < len let d: u8 = s[i] if d < '0' | d > '9' @@ -267,7 +267,7 @@ func str.parse_i64[s: str] : i64 func str.hex_encode[s: str] : str let hex_chars: str = "0123456789abcdef" let s_len: i64 = str.len(s) - let j: i64 = 0 + let j = 0 let out: str = mem.alloc(s_len * 2 + 1) for i in 0..s_len @@ -289,8 +289,8 @@ func str._hex_digit_to_int[d: u8] : i64 func str.hex_decode[s: str] : str let s_len: i64 = str.len(s) - let i: i64 = 0 - let j: i64 = 0 + let i = 0 + let j = 0 let out: str = mem.alloc(s_len / 2 + 1) while i < s_len @@ -324,7 +324,7 @@ func math.abs[n: i64] : i64 return n func math.pow[b: i64, e: i64] : i64 - let out: i64 = 1 + let out = 1 for i in 0..e out = out * b return out @@ -355,7 +355,7 @@ func math.is_prime[n: i64]: bool if n % 2 == 0 | n % 3 == 0 return false - let i: i64 = 5 + let i = 5 while i * i <= n if n % i == 0 | n % (i + 2) == 0 return false @@ -386,7 +386,7 @@ func array.push[xs: array, x: i64] : void let size: i64 = mem.read64(xs + 16) if size == capacity - let new_capacity: i64 = 4 + let new_capacity = 4 if capacity != 0 new_capacity = capacity * 2 let new_data: ptr = realloc(data, new_capacity * 8) @@ -447,7 +447,7 @@ func alg._partition[arr: array, low: i64, high: i64] : i64 return i + 1 func alg.count[arr: array, item: i64] : i64 - let count: i64 = 0 + let count = 0 let size: i64 = array.size(arr) for i in 0..size if array.nth(arr, i) == item @@ -458,7 +458,7 @@ func os.exit[code: i64] : void _builtin_syscall(60, code) func os.urandom[]: i64 - let n: i64 = 0 + let n = 0 let fd: i64 = _builtin_syscall(257, -100, "/dev/urandom", 0, 0) // openat _builtin_syscall(0, fd, @n, 8) // read _builtin_syscall(3, fd) // close @@ -481,7 +481,7 @@ func os.shell[command: str] : i64 _builtin_syscall(59, "/bin/sh", mem.read64(argv), _builtin_environ()) // execve _builtin_syscall(60, 1) // exit else - let status: i64 = 0 + let status = 0 let wp: i64 = _builtin_syscall(61, pid, @status, 0, 0) // waitpid if wp == -1 return -1 @@ -504,7 +504,7 @@ func os.listdir[path: str] : array if n <= 0 break - let pos: i64 = 0 + let pos = 0 while pos < n let len: i64 = mem.read16(buf + pos + 16) let name: str = buf + pos + 19 @@ -530,7 +530,7 @@ func net.listen[packed_host: i64, port: i64] : i64 if s < 0 return -1 - let optval: i64 = 1 + let optval = 1 if _builtin_syscall(54, s, 1, 2, @optval, 8) < 0 // setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) _builtin_syscall(3, s) // close return -1