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

@@ -3,7 +3,7 @@ func main[argc: i64, argv: ptr] : i64
io.println("ERROR: url is missing") io.println("ERROR: url is missing")
return 1 return 1
let url: str = mem.read64(argv + 8) let url: str = mem.read64(argv + 8) as str
if str.len(url) <= 7 if str.len(url) <= 7
io.println("ERROR: invalid url (Did you forget \"http://\"?)") io.println("ERROR: invalid url (Did you forget \"http://\"?)")
@@ -36,7 +36,7 @@ func main[argc: i64, argv: ptr] : i64
req = str.concat(req, " HTTP/1.0\r\nHost: ") req = str.concat(req, " HTTP/1.0\r\nHost: ")
req = str.concat(req, host) req = str.concat(req, host)
req = str.concat(req, "\r\nConnection: close\r\n\r\n") req = str.concat(req, "\r\nConnection: close\r\n\r\n")
net.send(s, req, str.len(req)) net.send(s, req as ptr, str.len(req))
mem.free(req) mem.free(req)
let header_buf: str = mem.alloc(8192) let header_buf: str = mem.alloc(8192)
@@ -45,7 +45,7 @@ func main[argc: i64, argv: ptr] : i64
let end_index: i64 = -1 let end_index: i64 = -1
while !found && header_size < 8192 while !found && header_size < 8192
let n: i64 = net.read(s, header_buf + header_size, 8192 - header_size) let n: i64 = net.read(s, header_buf as ptr + header_size, 8192 - header_size)
if n <= 0 if n <= 0
break break
let current_size: i64 = header_size + n let current_size: i64 = header_size + n
@@ -59,7 +59,7 @@ func main[argc: i64, argv: ptr] : i64
header_size = current_size header_size = current_size
if end_index < header_size if end_index < header_size
io.print_sized(header_buf + end_index, header_size - end_index) io.print_sized(header_buf as ptr + end_index, header_size as i64 - end_index)
mem.free(header_buf) mem.free(header_buf)
let buffer: ptr = mem.alloc(4096) let buffer: ptr = mem.alloc(4096)

View File

@@ -25,7 +25,7 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64
let len: i64 = str.len(bank) let len: i64 = str.len(bank)
for i in (start)..(len-remaining+1) for i in (start)..(len-remaining+1)
let v: i64 = bank[i] - '0' let v: i64 = (bank[i] - '0') as i64
if v > largest if v > largest
largest = v largest = v
largest_idx = i largest_idx = i

View File

@@ -2,7 +2,7 @@ func main[] : i64
let s: i64 = must(net.listen?(net.pack_addr(127, 0, 0, 1), 8000)) let s: i64 = must(net.listen?(net.pack_addr(127, 0, 0, 1), 8000))
io.println("Listening on port 8000...") io.println("Listening on port 8000...")
let resp: str = mem.alloc(60000) let resp: ptr = mem.alloc(60000)
while true while true
let conn: i64 = net.accept(s) let conn: i64 = net.accept(s)
if conn < 0 if conn < 0

View File

@@ -1,5 +1,5 @@
func main[] : i64 func main[] : i64
let s: i64 = must(net.udp_listen?(net.pack_addr(127, 0, 0, 1), 8000)) let s: net.UDPSocket = must(net.udp_listen?(net.pack_addr(127, 0, 0, 1), 8000))
io.println("Listening on port 8000...") io.println("Listening on port 8000...")
while true while true

View File

@@ -289,18 +289,18 @@ func io._printf_impl[s: ptr, args: ptr] : void
io.print_char(s[0]) io.print_char(s[0])
s = s + 1 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) _builtin_syscall(SYS_write, 1, x, size)
func io.print[x: str] : void 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 func io.println[x: str] : void
io.print(x) io.print(x)
io.print("\n") io.print("\n")
func io.print_char[x: u8] : void 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 func io.print_bool[x: bool] : void
if x if x
@@ -860,7 +860,7 @@ func alg.count[arr: Array, item: any] : i64
count = count + 1 count = count + 1
return count 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) let out: Array = array.new_with_size_zeroed(arr->size)
for i in 0..arr->size for i in 0..arr->size
array.set(out, i, fn(array.nth(arr, i))) 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"]; static BUILTIN_TYPES: [&str; 8] = ["void", "u8", "i64", "str", "bool", "ptr", "fnptr", "any"];
pub struct Env { pub struct Env {
@@ -245,7 +244,7 @@ impl TypeChecker {
Ok("i64".into()) Ok("i64".into())
} }
TokenType::Bang => { TokenType::Bang => {
expect_types!(right_type, ["bool", "i64", "ptr"], op.loc); expect_types!(right_type, ["bool", "i64", "ptr", "u8"], op.loc);
Ok("bool".into()) Ok("bool".into())
} }
_ => unreachable!(), _ => unreachable!(),
@@ -371,10 +370,18 @@ impl TypeChecker {
expect_types!(self.typecheck_expr(env, index)?, ["i64", "u8"], bracket.loc); expect_types!(self.typecheck_expr(env, index)?, ["i64", "u8"], bracket.loc);
Ok("u8".into()) Ok("u8".into())
} }
Expr::AddrOf { op, expr } => { Expr::AddrOf { op, expr } => match *expr.clone() {
self.typecheck_expr(env, expr)?; Expr::Variable(name) => {
if self.analyzer.borrow().functions.contains_key(&name.lexeme) {
Ok("fnptr".into())
} else {
Ok("ptr".into()) Ok("ptr".into())
} }
}
_ => {
error!(&op.loc, "can only take address of variables and functions")
}
},
Expr::New(struct_name) => Ok(struct_name.lexeme.clone()), Expr::New(struct_name) => Ok(struct_name.lexeme.clone()),
Expr::MemberAccess { left, field } => { Expr::MemberAccess { left, field } => {
let left_type = self.typecheck_expr(env, left)?; let left_type = self.typecheck_expr(env, left)?;