raylib example

This commit is contained in:
2025-11-11 13:33:40 +01:00
parent ca8ae6e110
commit 46045af4fa
3 changed files with 54 additions and 10 deletions

20
examples/raylib.zr Normal file
View File

@@ -0,0 +1,20 @@
// musl doesnt like dlopen, needs to be compiled with -m
func main[] : I64
let rl: Ptr = c.dlopen("libraylib.so", 2)
let rl.InitWindow: Ptr = c.dlsym(rl, "InitWindow")
let rl.WindowShouldClose: Ptr = c.dlsym(rl, "WindowShouldClose")
let rl.BeginDrawing: Ptr = c.dlsym(rl, "BeginDrawing")
let rl.EndDrawing: Ptr = c.dlsym(rl, "EndDrawing")
let rl.ClearBackground: Ptr = c.dlsym(rl, "ClearBackground")
let rl.CloseWindow: Ptr = c.dlsym(rl, "CloseWindow")
rl.InitWindow(800, 600, "Hello, World!")
while !rl.WindowShouldClose()
rl.BeginDrawing()
rl.ClearBackground(4278190335) // 0xff0000ff
rl.EndDrawing()
rl.CloseWindow()

View File

@@ -38,9 +38,12 @@ impl Env {
pub fn define_var(&mut self, name: String, _var_type: String) -> usize {
let offset = self.next_offset;
self.next_offset += 8;
self.scopes.last_mut().unwrap().insert(name, Var {
stack_offset: offset,
});
self.scopes.last_mut().unwrap().insert(
name,
Var {
stack_offset: offset,
},
);
offset
}
@@ -103,7 +106,7 @@ section .text
);
// take that rustfmt
for name in "malloc,calloc,realloc,free,puts,putchar,printf,sprintf,snprintf,strtol,strlen,strcmp,strncmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname".split(",")
for name in "malloc,calloc,realloc,free,puts,putchar,printf,sprintf,snprintf,strtol,strlen,strcmp,strncmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname,dlopen,dlsym,dlerror".split(",")
{
emit!(&mut self.output, "extern {}", name);
emit!(&mut self.output, "c.{} equ {}", name, name);
@@ -471,7 +474,19 @@ _builtin_set64:
emit!(&mut self.output, " pop {}", reg);
}
emit!(&mut self.output, " call {}", callee);
match env.get_var(&callee) {
Some(var) => {
emit!(
&mut self.output,
" mov rax, QWORD [rbp-{}]",
var.stack_offset,
);
emit!(&mut self.output, " call rax");
}
None => {
emit!(&mut self.output, " call {}", callee);
}
};
}
Expr::ArrayLiteral(exprs) => {
emit!(&mut self.output, " call array.new");

View File

@@ -77,11 +77,17 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out));
// TODO: drop libc entirely
run_command(format!(
"./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
args.out, args.out, args.cflags
));
if args.no_musl {
run_command(format!(
"gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}",
args.out, args.out, args.cflags
));
} else {
run_command(format!(
"./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
args.out, args.out, args.cflags
));
}
if args.run_exe {
run_command(
@@ -112,6 +118,9 @@ struct Args {
#[arg(short = 'r', help = "Run the compiled executable")]
run_exe: bool,
#[arg(short = 'm', help = "Don't use musl")]
no_musl: bool,
#[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")]
cflags: String,
}