add casts to examples

This commit is contained in:
2026-03-17 16:34:20 +01:00
parent 15a68a41d1
commit 664a51c88d
6 changed files with 24 additions and 17 deletions

View File

@@ -289,18 +289,18 @@ func io._printf_impl[s: ptr, args: ptr] : void
io.print_char(s[0])
s = s + 1
func io.print_sized[x: str, size: i64] : void
func io.print_sized[x: ptr, size: i64] : void
_builtin_syscall(SYS_write, 1, x, size)
func io.print[x: str] : void
io.print_sized(x, str.len(x))
io.print_sized(x as ptr, str.len(x))
func io.println[x: str] : void
io.print(x)
io.print("\n")
func io.print_char[x: u8] : void
io.print_sized(^x as str, 1)
io.print_sized(^x, 1)
func io.print_bool[x: bool] : void
if x
@@ -860,7 +860,7 @@ func alg.count[arr: Array, item: any] : i64
count = count + 1
return count
func alg.map[arr: Array, fn: ptr] : Array
func alg.map[arr: Array, fn: fnptr] : Array
let out: Array = array.new_with_size_zeroed(arr->size)
for i in 0..arr->size
array.set(out, i, fn(array.nth(arr, i)))

View File

@@ -35,7 +35,6 @@ macro_rules! expect_types {
};
}
// TODO: currently they are all just 64 bit values
static BUILTIN_TYPES: [&str; 8] = ["void", "u8", "i64", "str", "bool", "ptr", "fnptr", "any"];
pub struct Env {
@@ -245,7 +244,7 @@ impl TypeChecker {
Ok("i64".into())
}
TokenType::Bang => {
expect_types!(right_type, ["bool", "i64", "ptr"], op.loc);
expect_types!(right_type, ["bool", "i64", "ptr", "u8"], op.loc);
Ok("bool".into())
}
_ => unreachable!(),
@@ -371,10 +370,18 @@ impl TypeChecker {
expect_types!(self.typecheck_expr(env, index)?, ["i64", "u8"], bracket.loc);
Ok("u8".into())
}
Expr::AddrOf { op, expr } => {
self.typecheck_expr(env, expr)?;
Ok("ptr".into())
}
Expr::AddrOf { op, expr } => match *expr.clone() {
Expr::Variable(name) => {
if self.analyzer.borrow().functions.contains_key(&name.lexeme) {
Ok("fnptr".into())
} else {
Ok("ptr".into())
}
}
_ => {
error!(&op.loc, "can only take address of variables and functions")
}
},
Expr::New(struct_name) => Ok(struct_name.lexeme.clone()),
Expr::MemberAccess { left, field } => {
let left_type = self.typecheck_expr(env, left)?;