io.is_a_directory, octal literal constants

This commit is contained in:
2026-03-20 15:35:35 +01:00
parent efe9dfe238
commit 3efbb3ecd1
10 changed files with 86 additions and 52 deletions

View File

@@ -1,4 +1,4 @@
// needs to be compiled with -m -C="-lraylib" // needs to be compiled with -m -C "-lraylib"
extern InitWindow extern InitWindow
extern SetTargetFPS extern SetTargetFPS
extern WindowShouldClose extern WindowShouldClose
@@ -45,6 +45,15 @@ func chip8_create[] : CHIP8
return c return c
func chip8_free[c: CHIP8] : void
mem.free(c->memory)
array.free(c->stack)
mem.free(c->reg)
array.free(c->keyboard)
mem.free(c->display)
array.free(c->keyboard_map)
mem.free(c)
func chip8_disassemble[c: CHIP8, ins_count: i64] : void func chip8_disassemble[c: CHIP8, ins_count: i64] : void
for i in 0..ins_count for i in 0..ins_count
io.printf("0x%x: ", c->pc) io.printf("0x%x: ", c->pc)
@@ -264,15 +273,6 @@ func chip8_step[c: CHIP8] : void
for i in 0..x+1 for i in 0..x+1
c->reg[i] = c->memory[c->I + i] c->reg[i] = c->memory[c->I + i]
func chip8_free[c: CHIP8] : void
mem.free(c->memory)
array.free(c->stack)
mem.free(c->reg)
array.free(c->keyboard)
mem.free(c->display)
array.free(c->keyboard_map)
mem.free(c)
func main[argc: i64, argv: ptr] : i64 func main[argc: i64, argv: ptr] : i64
let path: str = 0 as str let path: str = 0 as str
let disassemble: bool = false let disassemble: bool = false

View File

@@ -39,7 +39,7 @@ func main[argc: i64, argv: ptr] : i64
net.send(s, req as ptr, 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) as str
let header_size = 0 let header_size = 0
let found: bool = false let found: bool = false
let end_index: i64 = -1 let end_index: i64 = -1

View File

@@ -43,7 +43,7 @@ func part2[lines: Array] : void
for x in 1..lines->size-1 for x in 1..lines->size-1
for y in 1..str.len(array.nth(lines, x))-1 for y in 1..str.len(array.nth(lines, x))-1
if array.nth(lines, x)[y] == 'A' if array.nth(lines, x)[y] == 'A'
let s: str = mem.alloc(5) let s: str = mem.alloc(5) as str
s[0] = array.nth(lines, x - 1)[y - 1] s[0] = array.nth(lines, x - 1)[y - 1]
s[1] = array.nth(lines, x + 1)[y - 1] s[1] = array.nth(lines, x + 1)[y - 1]
s[2] = array.nth(lines, x + 1)[y + 1] s[2] = array.nth(lines, x + 1)[y + 1]

View File

@@ -7,7 +7,7 @@ func part1[lines: Array] : void
let largest = 0 let largest = 0
for j in 0..str.len(line) for j in 0..str.len(line)
for k in (j+1)..str.len(line) for k in (j+1)..str.len(line)
let s: str = mem.alloc(3) let s: str = mem.alloc(3) as str
s[0] = line[j] s[0] = line[j]
s[1] = line[k] s[1] = line[k]
s[2] = 0 s[2] = 0

View File

@@ -1,4 +1,4 @@
// needs to be compiled with -m -C="-lraylib" // needs to be compiled with -m -C "-lraylib"
extern InitWindow extern InitWindow
extern SetTargetFPS extern SetTargetFPS
extern WindowShouldClose extern WindowShouldClose

View File

@@ -1,4 +1,4 @@
// needs to be compiled with -m -C="-lsqlite3" // needs to be compiled with -m -C "-lsqlite3"
extern sqlite3_open extern sqlite3_open
extern sqlite3_exec extern sqlite3_exec
extern sqlite3_prepare_v2 extern sqlite3_prepare_v2

View File

@@ -132,7 +132,7 @@ impl Args {
match args.next() { match args.next() {
Some(s) => out.out = Some(s), Some(s) => out.out = Some(s),
None => { None => {
eprintln!("-o option requires a name"); eprintln!("\x1b[91mERROR\x1b[0m: -o option requires a name");
process::exit(1); process::exit(1);
} }
} }
@@ -146,7 +146,7 @@ impl Args {
match args.next() { match args.next() {
Some(s) => out.cflags = s, Some(s) => out.cflags = s,
None => { None => {
eprintln!("-C option requires a name"); eprintln!("\x1b[91mERROR\x1b[0m: -C option requires a name");
process::exit(1); process::exit(1);
} }
} }
@@ -154,16 +154,27 @@ impl Args {
println!("Usage: zern [-o path] [-S] [-r] [-m] [-C cflags] path"); println!("Usage: zern [-o path] [-S] [-r] [-m] [-C cflags] path");
process::exit(0); process::exit(0);
} else if arg.starts_with('-') { } else if arg.starts_with('-') {
eprintln!("unrecognized option: {}", arg); eprintln!("\x1b[91mERROR\x1b[0m: unrecognized option: {}", arg);
process::exit(1); process::exit(1);
} else if out.path.is_empty() { } else if out.path.is_empty() {
out.path = arg out.path = arg
} else { } else {
eprintln!("unrecognized argument: {}", arg); eprintln!("\x1b[91mERROR\x1b[0m: unrecognized argument: {}", arg);
process::exit(1); process::exit(1);
} }
} }
if out.path.is_empty() {
eprintln!("\x1b[91mERROR\x1b[0m: you must provide a path");
process::exit(1);
}
if !out.use_gcc && !out.cflags.is_empty() {
// no "ERROR:" since its not an error
eprintln!("You can't set CFLAGS if you're not using gcc. Add the -m flag.");
process::exit(1);
}
out out
} }
} }
@@ -173,11 +184,6 @@ fn main() {
_ = raw_args.next(); _ = raw_args.next();
let args = Args::parse(raw_args); let args = Args::parse(raw_args);
if !args.use_gcc && !args.cflags.is_empty() {
eprintln!("You can't set CFLAGS if you're not using gcc. Add the -m flag.");
process::exit(1);
}
if let Err(err) = compile_file(args) { if let Err(err) = compile_file(args) {
eprintln!("{}", err); eprintln!("{}", err);
process::exit(1); process::exit(1);

View File

@@ -137,7 +137,7 @@ func net.udp_receive[s: net.UDPSocket, size: i64] : net.UDPPacket
pkt->source_addr = mem.alloc(16) pkt->source_addr = mem.alloc(16)
mem.zero(pkt->source_addr, 16) mem.zero(pkt->source_addr, 16)
let addrlen: i64 = 16 let addrlen = 16
pkt->size = _builtin_syscall(SYS_recvfrom, s->fd, pkt->data, size, 0, pkt->source_addr, ^addrlen) pkt->size = _builtin_syscall(SYS_recvfrom, s->fd, pkt->data, size, 0, pkt->source_addr, ^addrlen)
return pkt return pkt
@@ -158,9 +158,9 @@ const DNS_RECURSION_DESIRED = 256
func net.encode_dns_name[domain: str] : io.Buffer func net.encode_dns_name[domain: str] : io.Buffer
let domain_len: i64 = str.len(domain) let domain_len: i64 = str.len(domain)
let buf: io.Buffer = must(io.Buffer.alloc?(domain_len + 2)) let buf: io.Buffer = must(io.Buffer.alloc?(domain_len + 2))
let out_pos: i64 = 0 let out_pos = 0
let part_start: i64 = 0 let part_start = 0
let i: i64 = 0 let i = 0
while i <= domain_len while i <= domain_len
if i == domain_len || domain[i] == '.' if i == domain_len || domain[i] == '.'
let part_len: i64 = i - part_start let part_len: i64 = i - part_start
@@ -212,7 +212,7 @@ func net.resolve?[domain: str] : i64
// TODO: do actual parsing // TODO: do actual parsing
let pos: i64 = 12 // skip header (12 bytes) let pos = 12 // skip header (12 bytes)
while pkt->data[pos] != 0 while pkt->data[pos] != 0
pos = pos + pkt->data[pos] + 1 // skip question pos = pos + pkt->data[pos] + 1 // skip question

View File

@@ -90,11 +90,11 @@ func mem._request_space?[size: i64] : mem.Block
mem.write64(_builtin_heap_tail(), blk) mem.write64(_builtin_heap_tail(), blk)
return blk return blk
func mem.alloc?[size: i64] : any func mem.alloc?[size: i64] : ptr
err.clear() err.clear()
if size <= 0 if size <= 0
err.set(ERR_ALLOC_FAILED, "mem.alloc? called with non-positive size") err.set(ERR_ALLOC_FAILED, "mem.alloc? called with non-positive size")
return 0 return 0 as ptr
size = mem._align(size) size = mem._align(size)
@@ -108,7 +108,7 @@ func mem.alloc?[size: i64] : any
let blk: mem.Block = mem._request_space?(size) let blk: mem.Block = mem._request_space?(size)
if err.check() if err.check()
return 0 return 0 as ptr
if !mem.read64(_builtin_heap_head()) if !mem.read64(_builtin_heap_head())
mem.write64(_builtin_heap_head(), blk) mem.write64(_builtin_heap_head(), blk)
@@ -117,7 +117,7 @@ func mem.alloc?[size: i64] : any
return blk as ptr + MEM_BLOCK_SIZE return blk as ptr + MEM_BLOCK_SIZE
func mem.alloc[size: i64] : any func mem.alloc[size: i64] : ptr
return must(mem.alloc?(size)) return must(mem.alloc?(size))
func mem.free[x: any] : void func mem.free[x: any] : void
@@ -330,7 +330,7 @@ func io.read_char[] : u8
func io.read_line[]: str func io.read_line[]: str
let MAX_SIZE = 60000 let MAX_SIZE = 60000
let buffer: str = mem.alloc(MAX_SIZE + 1) let buffer: str = mem.alloc(MAX_SIZE + 1) as str
let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE) let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
if n < 0 if n < 0
n = 0 n = 0
@@ -359,6 +359,22 @@ func io.Buffer.free[buf: io.Buffer] : void
func io.file_exists[path: str] : bool func io.file_exists[path: str] : bool
return _builtin_syscall(SYS_faccessat, -100, path, 0, 0) == 0 return _builtin_syscall(SYS_faccessat, -100, path, 0, 0) == 0
const S_IFDIR = 0o040000
const S_IFMT = 0o170000
func io.is_a_directory[path: str] : bool
let st: ptr = mem.alloc(256) // it has 21 mixed-size fields so `ptr` must do for now
let rc: i64 = _builtin_syscall(SYS_newfstatat, -100, path, st, 0)
if rc != 0
mem.free(st)
return false
let out: bool = (mem.read32(st + 24) & S_IFMT) == S_IFDIR
mem.free(st)
return out
func io.read_text_file?[path: str] : str func io.read_text_file?[path: str] : str
err.clear() err.clear()
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0) let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
@@ -436,7 +452,7 @@ func str.len[s: str] : i64
func str.make_copy[s: str] : str func str.make_copy[s: str] : str
let size: i64 = str.len(s) + 1 let size: i64 = str.len(s) + 1
let dup: str = mem.alloc(size) let dup: str = mem.alloc(size) as str
mem.copy(s as ptr, dup as ptr, size) mem.copy(s as ptr, dup as ptr, size)
return dup return dup
@@ -472,7 +488,7 @@ func str.is_alphanumeric[x: u8] : bool
func str.concat[a: str, b: str] : str func str.concat[a: str, b: str] : str
let a_len: i64 = str.len(a) let a_len: i64 = str.len(a)
let b_len: i64 = str.len(b) let b_len: i64 = str.len(b)
let out: str = mem.alloc(a_len + b_len + 1) let out: str = mem.alloc(a_len + b_len + 1) as str
mem.copy(a as ptr, out as ptr, a_len) mem.copy(a as ptr, out as ptr, a_len)
mem.copy(b as ptr, out as ptr + a_len, b_len) mem.copy(b as ptr, out as ptr + a_len, b_len)
out[a_len + b_len] = 0 out[a_len + b_len] = 0
@@ -505,7 +521,7 @@ func str.substr[s: str, start: i64, length: i64] : str
if start < 0 || length < 0 || start + length > str.len(s) if start < 0 || length < 0 || start + length > str.len(s)
panic("str.substr out of bounds") panic("str.substr out of bounds")
let out: str = mem.alloc(length + 1) let out: str = mem.alloc(length + 1) as str
mem.copy(s as ptr + start, out as ptr, length) mem.copy(s as ptr + start, out as ptr, length)
out[length] = 0 out[length] = 0
return out return out
@@ -513,7 +529,7 @@ func str.substr[s: str, start: i64, length: i64] : str
func str.trim[s: str] : str func str.trim[s: str] : str
let len: i64 = str.len(s) let len: i64 = str.len(s)
if len == 0 if len == 0
let out: str = mem.alloc(1) let out: str = mem.alloc(1) as str
out[0] = 0 out[0] = 0
return out return out
@@ -562,7 +578,7 @@ func str.split[haystack: str, needle: str]: Array
func str.reverse[s: str] : str func str.reverse[s: str] : str
let len: i64 = str.len(s) let len: i64 = str.len(s)
let out: str = mem.alloc(len + 1) let out: str = mem.alloc(len + 1) as str
for i in 0..len for i in 0..len
out[i] = s[len - i - 1] out[i] = s[len - i - 1]
@@ -571,7 +587,7 @@ func str.reverse[s: str] : str
func str.from_i64[n: i64] : str func str.from_i64[n: i64] : str
if n == 0 if n == 0
let out: str = mem.alloc(2) let out: str = mem.alloc(2) as str
out[0] = '0' out[0] = '0'
out[1] = 0 out[1] = 0
return out return out
@@ -582,7 +598,7 @@ func str.from_i64[n: i64] : str
if n == -9223372036854775808 if n == -9223372036854775808
return str.make_copy("-9223372036854775808") return str.make_copy("-9223372036854775808")
n = -n n = -n
let buf: str = mem.alloc(21) // enough to fit -MAX_I64 let buf: str = mem.alloc(21) as str // enough to fit -MAX_I64
let end = 20 let end = 20
buf[end] = 0 buf[end] = 0
end = end - 1 end = end - 1
@@ -601,13 +617,13 @@ func str.hex_from_i64[n: i64] : str
let hex_chars: str = "0123456789abcdef" let hex_chars: str = "0123456789abcdef"
if n == 0 if n == 0
let out: str = mem.alloc(2) let out: str = mem.alloc(2) as str
out[0] = '0' out[0] = '0'
out[1] = 0 out[1] = 0
return out return out
let mask: i64 = (1 << 60) - 1 let mask: i64 = (1 << 60) - 1
let buf: str = mem.alloc(17) let buf: str = mem.alloc(17) as str
let len = 0 let len = 0
while n != 0 while n != 0
@@ -615,7 +631,7 @@ func str.hex_from_i64[n: i64] : str
n = (n >> 4) & mask n = (n >> 4) & mask
len = len + 1 len = len + 1
let out: str = mem.alloc(len + 1) let out: str = mem.alloc(len + 1) as str
let j = 0 let j = 0
while j < len while j < len
out[j] = buf[len - 1 - j] out[j] = buf[len - 1 - j]
@@ -626,7 +642,7 @@ func str.hex_from_i64[n: i64] : str
return out return out
func str.from_char[c: u8] : str func str.from_char[c: u8] : str
let s: str = mem.alloc(2) let s: str = mem.alloc(2) as str
s[0] = c s[0] = c
s[1] = 0 s[1] = 0
return s return s
@@ -652,7 +668,7 @@ func str.parse_i64[s: str] : i64
func str.hex_encode[s: str, s_len: i64] : str func str.hex_encode[s: str, s_len: i64] : str
let hex_chars: str = "0123456789abcdef" let hex_chars: str = "0123456789abcdef"
let j = 0 let j = 0
let out: str = mem.alloc(s_len * 2 + 1) let out: str = mem.alloc(s_len * 2 + 1) as str
for i in 0..s_len for i in 0..s_len
let high: u8 = (s[i] >> 4) & 15 let high: u8 = (s[i] >> 4) & 15
@@ -678,7 +694,7 @@ func str.hex_decode[s: str] : str
let i = 0 let i = 0
let j = 0 let j = 0
let out: str = mem.alloc(s_len / 2 + 1) let out: str = mem.alloc(s_len / 2 + 1) as str
while i < s_len while i < s_len
out[j] = str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1]) out[j] = str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1])

View File

@@ -65,9 +65,7 @@ impl SymbolTable {
pub fn register_declaration(&mut self, stmt: &Stmt) -> Result<(), ZernError> { pub fn register_declaration(&mut self, stmt: &Stmt) -> Result<(), ZernError> {
match stmt { match stmt {
Stmt::Const { name, value } => { Stmt::Const { name, value } => {
if self.constants.contains_key(&name.lexeme) if self.is_name_defined(&name.lexeme) {
|| self.functions.contains_key(&name.lexeme)
{
return error!( return error!(
name.loc, name.loc,
format!("tried to redefine constant '{}'", name.lexeme) format!("tried to redefine constant '{}'", name.lexeme)
@@ -78,13 +76,18 @@ impl SymbolTable {
name.lexeme.clone(), name.lexeme.clone(),
u64::from_str_radix(&value.lexeme[2..], 16).unwrap(), u64::from_str_radix(&value.lexeme[2..], 16).unwrap(),
); );
} else if value.lexeme.starts_with("0o") {
self.constants.insert(
name.lexeme.clone(),
u64::from_str_radix(&value.lexeme[2..], 8).unwrap(),
);
} else { } else {
self.constants self.constants
.insert(name.lexeme.clone(), value.lexeme.parse().unwrap()); .insert(name.lexeme.clone(), value.lexeme.parse().unwrap());
} }
} }
Stmt::Extern(name) => { Stmt::Extern(name) => {
if self.functions.contains_key(&name.lexeme) { if self.is_name_defined(&name.lexeme) {
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme)); return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
} }
self.functions self.functions
@@ -97,7 +100,7 @@ impl SymbolTable {
body: _, body: _,
exported: _, exported: _,
} => { } => {
if self.functions.contains_key(&name.lexeme) { if self.is_name_defined(&name.lexeme) {
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme)); return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
} }
self.functions.insert( self.functions.insert(
@@ -109,6 +112,9 @@ impl SymbolTable {
); );
} }
Stmt::Struct { name, fields } => { Stmt::Struct { name, fields } => {
if self.is_name_defined(&name.lexeme) {
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
}
let mut fields_map: HashMap<String, StructField> = HashMap::new(); let mut fields_map: HashMap<String, StructField> = HashMap::new();
let mut offset: usize = 0; let mut offset: usize = 0;
@@ -129,4 +135,10 @@ impl SymbolTable {
} }
Ok(()) Ok(())
} }
fn is_name_defined(&self, s: &str) -> bool {
self.functions.contains_key(s)
|| self.constants.contains_key(s)
|| self.structs.contains_key(s)
}
} }