infer var type when initializer is a number constant

This commit is contained in:
2025-12-23 18:10:56 +01:00
parent 5682318915
commit c527aceecd
36 changed files with 118 additions and 100 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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))

View File

@@ -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))

View File

@@ -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

View File

@@ -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)

View File

@@ -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))

View File

@@ -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)

View File

@@ -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("-")

View File

@@ -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)

View File

@@ -1,5 +1,5 @@
func main[] : i64
let sum: i64 = 0
let sum = 0
for i in 0..266000
if i % 2 != 0

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,5 +1,5 @@
func main[] : i64
let out: i64 = 0
let out = 0
for a in 500..1000
for b in 500..1000

View File

@@ -1,5 +1,5 @@
func main[] : i64
let out: i64 = 1
let out = 1
for i in 1..21
out = math.lcm(out, i)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,5 +1,5 @@
func main[] : i64
let sum: i64 = 0
let sum = 0
for i in 0..2000000
if math.is_prime(i)

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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) => {

View File

@@ -11,7 +11,7 @@ pub enum Stmt {
Expression(Expr),
Let {
name: Token,
var_type: Token,
var_type: Option<Token>,
initializer: Expr,
},
Block(Vec<Stmt>),
@@ -172,12 +172,16 @@ impl Parser {
fn let_declaration(&mut self) -> Result<Stmt, ZernError> {
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()?;

View File

@@ -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