From 165b5844dea2f6c947a74c9c70eba4dda53db803 Mon Sep 17 00:00:00 2001 From: Toni Date: Fri, 17 Jul 2026 16:14:08 +0200 Subject: [PATCH] remove type hints from declarations --- examples/chip8.zr | 6 +++--- examples/euler/euler_11.zr | 8 ++++---- examples/euler/euler_16.zr | 2 +- examples/euler/euler_20.zr | 2 +- examples/rule110.zr | 2 +- src/codegen_x86_64.rs | 15 ++++----------- src/parser.rs | 15 ++------------- src/tokenizer.rs | 1 - src/typechecker.rs | 23 +++-------------------- std/containers.zr | 22 +++++++++++----------- std/num.zr | 2 +- std/os.zr | 2 +- std/str.zr | 2 +- 13 files changed, 33 insertions(+), 69 deletions(-) diff --git a/examples/chip8.zr b/examples/chip8.zr index 1fc45b1..eb98819 100644 --- a/examples/chip8.zr +++ b/examples/chip8.zr @@ -201,7 +201,7 @@ func CHIP8.step[c: CHIP8] : void else if n == 0x3 c->reg[x] = c->reg[x] ^ c->reg[y] else if n == 0x4 - res : u8 = c->reg[x] + c->reg[y] + res := c->reg[x] + c->reg[y] c->reg[0xf] = (res > 0xff) as u8 c->reg[x] = res else if n == 0x5 @@ -230,8 +230,8 @@ func CHIP8.step[c: CHIP8] : void for row in 0..n for col in 0..8 if (c->memory[c->I + row] & (0x80 >> col)) != 0 - pixel_x : u8 = (c->reg[x] + col) % 64 - pixel_y : u8 = (c->reg[y] + row) % 32 + pixel_x := (c->reg[x] + col) % 64 + pixel_y := (c->reg[y] + row) % 32 offset := pixel_x as i64 + (pixel_y * 64) if c->display[offset] == 1 diff --git a/examples/euler/euler_11.zr b/examples/euler/euler_11.zr index 1f90f80..7b98516 100644 --- a/examples/euler/euler_11.zr +++ b/examples/euler/euler_11.zr @@ -30,10 +30,10 @@ func main[] : i64 for i in 0..N-3 for j in 0..N-3 - h : i64 = grid->nth(i)->nth(j) * grid->nth(i)->nth(j+1) * grid->nth(i)->nth(j+2) * grid->nth(i)->nth(j+3) - v : i64 = grid->nth(j)->nth(i) * grid->nth(j+1)->nth(i) * grid->nth(j+2)->nth(i) * grid->nth(j+3)->nth(i) - d1 : i64 = grid->nth(i)->nth(j) * grid->nth(i+1)->nth(j+1) * grid->nth(i+2)->nth(j+2) * grid->nth(i+3)->nth(j+3) - d2 : i64 = grid->nth(i)->nth(N-j-1) * grid->nth(i+1)->nth(N-j-2) * grid->nth(i+2)->nth(N-j-3) * grid->nth(i+3)->nth(N-j-4) + h := grid->nth(i)->nth(j) * grid->nth(i)->nth(j+1) * grid->nth(i)->nth(j+2) * grid->nth(i)->nth(j+3) + v := grid->nth(j)->nth(i) * grid->nth(j+1)->nth(i) * grid->nth(j+2)->nth(i) * grid->nth(j+3)->nth(i) + d1 := grid->nth(i)->nth(j) * grid->nth(i+1)->nth(j+1) * grid->nth(i+2)->nth(j+2) * grid->nth(i+3)->nth(j+3) + d2 := grid->nth(i)->nth(N-j-1) * grid->nth(i+1)->nth(N-j-2) * grid->nth(i+2)->nth(N-j-3) * grid->nth(i+3)->nth(N-j-4) out = math.max(out, math.max(h, math.max(v, math.max(d1, d2)))) io.println_i64(out) diff --git a/examples/euler/euler_16.zr b/examples/euler/euler_16.zr index 81b66ac..5432a11 100644 --- a/examples/euler/euler_16.zr +++ b/examples/euler/euler_16.zr @@ -7,7 +7,7 @@ func main[] : i64 for j in 0..1000 carry := 0 for i in 0..n->size - tmp : i64 = n->nth(i) * 2 + carry + tmp := n->nth(i) * 2 + carry n->set(i, tmp % 10) carry = tmp / 10 while carry > 0 diff --git a/examples/euler/euler_20.zr b/examples/euler/euler_20.zr index d3c6721..dd6d0fb 100644 --- a/examples/euler/euler_20.zr +++ b/examples/euler/euler_20.zr @@ -4,7 +4,7 @@ include "$/containers.zr" func multiply[n: Array, x: i64] : void carry := 0 for i in 0..n->size - prod : i64 = n->nth(i) * x + carry + prod := n->nth(i) * x + carry n->set(i, prod % 10) carry = prod / 10 while carry > 0 diff --git a/examples/rule110.zr b/examples/rule110.zr index e4b62ed..9b326c2 100644 --- a/examples/rule110.zr +++ b/examples/rule110.zr @@ -8,7 +8,7 @@ func rule110_step[state: Array] : void left := false if i - 1 >= 0 left = state->nth(i - 1) - center : bool = state->nth(i) + center := state->nth(i) right := false if i + 1 < state->size right = state->nth(i + 1) diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index 0f8430c..2aee33e 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -215,11 +215,7 @@ _builtin_environ: pub fn compile_stmt(&mut self, env: &mut Env, stmt: &Stmt) -> Result<(), ZernError> { match stmt { Stmt::Expression(expr) => self.compile_expr(env, expr)?, - Stmt::Declare { - name, - var_type, - initializer, - } => { + Stmt::Declare { name, initializer } => { // TODO: move to typechecker? if env.get_var(&name.lexeme).is_some() { return error!( @@ -228,12 +224,9 @@ _builtin_environ: ); } - let var_type: String = match var_type { - Some(t) => t.lexeme.clone(), - None => match self.expr_types[&initializer.id].as_str() { - "opaque" => return error!(name.loc, "cannot infer type from opaque"), - t => t.into(), - }, + let var_type: String = match self.expr_types[&initializer.id].as_str() { + "opaque" => return error!(name.loc, "cannot infer type from opaque"), + t => t.into(), }; self.compile_expr(env, initializer)?; diff --git a/src/parser.rs b/src/parser.rs index 3568a5a..215c354 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -23,7 +23,6 @@ pub enum Stmt { Expression(Expr), Declare { name: Token, - var_type: Option, initializer: Expr, }, Assign { @@ -371,20 +370,10 @@ impl Parser { let name = self.consume(TokenType::Identifier, "expected variable name")?; self.consume(TokenType::Colon, "expected ':'")?; - let var_type = if self.match_token(&[TokenType::Equal]) { - None - } else { - let var_type = self.parse_type_ref()?; - self.consume(TokenType::Equal, "expected '=' after variable type")?; - Some(var_type) - }; + self.consume(TokenType::Equal, "expected '=' after ':'")?; let initializer = self.expression()?; - Ok(Stmt::Declare { - name, - var_type, - initializer, - }) + Ok(Stmt::Declare { name, initializer }) } else if self.match_token(&[TokenType::KeywordIf]) { self.if_statement() } else if self.match_token(&[TokenType::KeywordWhile]) { diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 706d69e..fa94a6b 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -2,7 +2,6 @@ use std::{ cmp::Ordering, collections::HashSet, fmt, fs, - os::unix::fs::FileTypeExt, path::{Path, PathBuf}, }; diff --git a/src/typechecker.rs b/src/typechecker.rs index cb7261e..94f50ff 100644 --- a/src/typechecker.rs +++ b/src/typechecker.rs @@ -91,31 +91,14 @@ impl<'a> TypeChecker<'a> { Stmt::Expression(expr) => { self.typecheck_expr(env, expr)?; } - Stmt::Declare { - name, - var_type, - initializer, - } => { - let mut actual_type = self.typecheck_expr(env, initializer)?; + Stmt::Declare { name, initializer } => { + let actual_type = self.typecheck_expr(env, initializer)?; if actual_type.contains(',') { return error!( &name.loc, "cannot assign multi-return call to a single variable" ); } - if let Some(var_type) = var_type { - if !self.is_valid_type_name(&var_type.lexeme) { - return error!( - &name.loc, - "unrecognized type: ".to_owned() + &var_type.lexeme - ); - } - expect_type!(actual_type.clone(), var_type.lexeme, var_type.loc); - - if actual_type == "opaque" { - actual_type = var_type.lexeme.clone(); - } - } env.define_var(name.lexeme.clone(), actual_type); } @@ -447,7 +430,7 @@ impl<'a> TypeChecker<'a> { let right_type = self.typecheck_expr(env, right)?; match op.token_type { TokenType::Minus => { - expect_types!(right_type, ["i64"], op.loc); + expect_type!(right_type.clone(), "i64", op.loc); Ok(right_type) } TokenType::Bang => { diff --git a/std/containers.zr b/std/containers.zr index d5b5057..6940d11 100644 --- a/std/containers.zr +++ b/std/containers.zr @@ -73,25 +73,25 @@ func Array.concat[a: Array<$>, b: Array<$>] : Array<$> mem.copy(b->data, new_array->data + a->size * 8, b->size * 8) return new_array -func Array.quicksort[arr: Array<$>] : void +func Array.quicksort[arr: Array] : void arr->_do_quicksort(0, arr->size - 1) -func Array._do_quicksort[arr: Array<$>, low: i64, high: i64] : void +func Array._do_quicksort[arr: Array, low: i64, high: i64] : void if low < high i := arr->_partition(low, high) arr->_do_quicksort(low, i - 1) arr->_do_quicksort(i + 1, high) -func Array._partition[arr: Array<$>, low: i64, high: i64] : i64 - pivot : i64 = arr->nth(high) +func Array._partition[arr: Array, low: i64, high: i64] : i64 + pivot := arr->nth(high) i := low - 1 for j in (low)..high if arr->nth(j) as i64 <= pivot i += 1 - temp : i64 = arr->nth(i) + temp := arr->nth(i) arr->set(i, arr->nth(j)) arr->set(j, temp) - temp : i64 = arr->nth(i + 1) + temp := arr->nth(i + 1) arr->set(i + 1, arr->nth(high)) arr->set(high, temp) return i + 1 @@ -178,7 +178,7 @@ func HashMap.new[] : HashMap func HashMap.insert[map: HashMap<$>, key: str, value: $] : void index := HashMap._djb2(key) - current : HashMap._Node = map->table->nth(index) + current := map->table->nth(index) while current as ptr if current->key->equal(key) @@ -194,7 +194,7 @@ func HashMap.insert[map: HashMap<$>, key: str, value: $] : void func HashMap.get[map: HashMap<$>, key: str] : $, bool index := HashMap._djb2(key) - current : HashMap._Node = map->table->nth(index) + current := map->table->nth(index) while current as ptr if current->key->equal(key) @@ -205,7 +205,7 @@ func HashMap.get[map: HashMap<$>, key: str] : $, bool func HashMap.delete[map: HashMap<$>, key: str] : void index := HashMap._djb2(key) - current : HashMap._Node = map->table->nth(index) + current := map->table->nth(index) prev := 0 as HashMap._Node while current as ptr @@ -225,7 +225,7 @@ func HashMap.delete[map: HashMap<$>, key: str] : void func HashMap.keys[map: HashMap<$>] : Array out := [] as Array for i in 0..HashMap._TABLE_SIZE - current : HashMap._Node = map->table->nth(i) + current := map->table->nth(i) while current as ptr out->push(current->key) current = current->next @@ -233,7 +233,7 @@ func HashMap.keys[map: HashMap<$>] : Array func HashMap.free_with_keys[map: HashMap<$>] : void for i in 0..HashMap._TABLE_SIZE - current : HashMap._Node = map->table->nth(i) + current := map->table->nth(i) while current as ptr tmp := current current = current->next diff --git a/std/num.zr b/std/num.zr index 2565aed..19ae2d1 100644 --- a/std/num.zr +++ b/std/num.zr @@ -51,7 +51,7 @@ func i64.to_str_buf[n: i64, buf: ptr] : void buf[1] = 0 return - neg : bool = n < 0 + neg := n < 0 if neg // MIN_I64 will fail but i so dont care n = -n diff --git a/std/os.zr b/std/os.zr index 8297dbe..ea1f23c 100644 --- a/std/os.zr +++ b/std/os.zr @@ -136,7 +136,7 @@ func os.list_directory[path: str] : Array, bool pos := 0 while pos < n len := mem.read16(buf + pos + 16) - name : ptr = buf + pos + 19 + name := buf + pos + 19 if name[0] skip := false // skip if name is exactly '.' or '..' diff --git a/std/str.zr b/std/str.zr index e4be7a2..e1d03b6 100644 --- a/std/str.zr +++ b/std/str.zr @@ -233,7 +233,7 @@ func str.hex_encode[s: str, s_len: i64] : str func str._hex_digit_to_int[d: u8] : u8 if d->is_digit() return d - '0' - lower : u8 = d | 32 + lower := d | 32 if lower >= 'a' && lower <= 'f' return lower - 'a' + 10 panic("invalid hex digit passed to str._hex_digit_to_int")