drop some libc functions, octal numerals, io.println, mem.read32

This commit is contained in:
2025-11-22 17:31:36 +01:00
parent 5a41163ca1
commit 7cffd73406
28 changed files with 165 additions and 126 deletions

View File

@@ -14,16 +14,16 @@ func main[] : I64
let answer: I64 = math.abs(math.urandom()) % 100
while true
io.print("Guess a number: ")
io.println("Guess a number: ")
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
if guess == answer
io.print("You win!")
io.println("You win!")
break
else if guess < answer
io.print("Too low!")
io.println("Too low!")
else
io.print("Too high!")
io.println("Too high!")
```
## Quickstart

View File

@@ -20,7 +20,7 @@ func main[] : I64
else if op == '-'
str.set(memory, p, memory[p]-1)
else if op == '.'
c.printf("%c", memory[p])
io.print_char(memory[p])
else if op == ','
str.set(memory, p, c.getchar())
else if op == '['

View File

@@ -4,4 +4,4 @@ func main[] : I64
for i in 0..1000
if i % 5 == 0 | i % 3 == 0
sum = sum + i
io.print_i64(sum)
io.println_i64(sum)

View File

@@ -4,4 +4,4 @@ func main[] : I64
for i in 0..2000000
if math.is_prime(i)
sum = sum + i
io.print_i64(sum)
io.println_i64(sum)

View File

@@ -16,6 +16,6 @@ func main[] : I64
while true
n = n + i
if num_divisors(n) > 500
io.print_i64(n)
io.println_i64(n)
break
i = i + 1

View File

@@ -3,4 +3,4 @@ func main[] : I64
37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226
|> str.from_i64()
|> str.substr(0, 10)
|> io.print()
|> io.println()

View File

@@ -19,4 +19,4 @@ func main[] : I64
if seq > max
max = seq
max_index = i
io.print_i64(max_index)
io.println_i64(max_index)

View File

@@ -6,4 +6,4 @@ func main[] : I64
for i in 1..r+1
out = out * (n - (r - i)) / i
io.print_i64(out)
io.println_i64(out)

View File

@@ -10,4 +10,4 @@ func main[] : I64
b = a + b
a = temp
io.print_i64(sum)
io.println_i64(sum)

View File

@@ -8,4 +8,4 @@ func main[] : I64
else
f = f + 1
io.print_i64(n)
io.println_i64(n)

View File

@@ -10,4 +10,4 @@ func main[] : I64
out = a * b
mem.free(s)
mem.free(s_rev)
io.print_i64(out)
io.println_i64(out)

View File

@@ -3,4 +3,4 @@ func main[] : I64
for i in 1..21
out = math.lcm(out, i)
io.print_i64(out)
io.println_i64(out)

View File

@@ -8,4 +8,4 @@ func main[] : I64
square_of_sum = square_of_sum + i
square_of_sum = square_of_sum * square_of_sum
io.print_i64(square_of_sum - sum_of_squares)
io.println_i64(square_of_sum - sum_of_squares)

View File

@@ -6,6 +6,6 @@ func main[] : I64
if math.is_prime(i)
found = found + 1
if found == 10001
io.print_i64(i)
io.println_i64(i)
break
i = i + 1

View File

@@ -11,4 +11,4 @@ func main[] : I64
j = j + 1
if s > out
out = s
io.print_i64(out)
io.println_i64(out)

View File

@@ -3,5 +3,5 @@ func main[] : I64
for b in 1..1000
let c: I64 = 1000 - b - a
if a * a + b * b == c * c
io.print_i64(a * b * c)
io.println_i64(a * b * c)
return 0

View File

@@ -3,7 +3,7 @@ func main[] : I64
let b: I64 = 1
while a < 100000
io.print_i64(a)
io.println_i64(a)
let temp: I64 = b
b = a + b
a = temp

View File

@@ -2,13 +2,13 @@ func main[] : I64
let answer: I64 = math.abs(math.urandom()) % 100
while true
io.print("Guess a number: ")
io.println("Guess a number: ")
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
if guess == answer
io.print("You win!")
io.println("You win!")
break
else if guess < answer
io.print("Too low!")
io.println("Too low!")
else
io.print("Too high!")
io.println("Too high!")

View File

@@ -1,2 +1,2 @@
func main[] : I64
io.print("Hello, World!")
io.println("Hello, World!")

View File

@@ -4,12 +4,12 @@ func main[] : I64
array.push(arr, math.abs(math.urandom() % 1000))
for i in 0..array.size(arr)
io.print_i64(array.nth(arr, i))
io.print("------------")
io.println_i64(array.nth(arr, i))
io.println("------------")
alg.quicksort(arr)
for i in 0..array.size(arr)
io.print_i64(array.nth(arr, i))
io.println_i64(array.nth(arr, i))
array.free(arr)

View File

@@ -1,4 +1,4 @@
// musl doesnt like dlopen, needs to be compiled with -m -C="/usr/local/lib/libraylib.a -lm"
// needs to be compiled with -m -C="/usr/local/lib/libraylib.a -lm"
func main[] : I64
extern InitWindow
@@ -11,11 +11,6 @@ func main[] : I64
extern DrawRectangle
extern IsKeyDown
let KEY_W: I64 = 87
let KEY_S: I64 = 83
let KEY_A: I64 = 65
let KEY_D: I64 = 68
let x: I64 = 200
let y: I64 = 200
@@ -23,13 +18,13 @@ func main[] : I64
SetTargetFPS(60)
while !WindowShouldClose()
if IsKeyDown(KEY_W) & 255
if IsKeyDown(87) & 255 // W
y = y - 10
if IsKeyDown(KEY_S) & 255
if IsKeyDown(83) & 255 // S
y = y + 10
if IsKeyDown(KEY_A) & 255
if IsKeyDown(65) & 255 // A
x = x - 10
if IsKeyDown(KEY_D) & 255
if IsKeyDown(68) & 255 // D
x = x + 10
BeginDrawing()

View File

@@ -18,10 +18,10 @@ func rule110_step[state: Array] : Array
func print_state[state: Array]: Void
for i in 0..array.size(state)
if array.nth(state, i)
c.putchar('#')
io.print_char('#')
else
c.putchar(' ')
io.print("")
io.print_char(' ')
io.println("")
func main[] : I64
let SIZE: I64 = 60

View File

@@ -1,20 +1,18 @@
// musl doesnt like dlopen, needs to be compiled with -m
// needs to be compiled with -m -C="-lX11"
func main[] : I64
let x11: Ptr = c.dlopen("libX11.so", 2)
let XOpenDisplay: Ptr = c.dlsym(x11, "XOpenDisplay")
let XDefaultRootWindow: Ptr = c.dlsym(x11, "XDefaultRootWindow")
let XCreateSimpleWindow: Ptr = c.dlsym(x11, "XCreateSimpleWindow")
let XMapWindow: Ptr = c.dlsym(x11, "XMapWindow")
let XSelectInput: Ptr = c.dlsym(x11, "XSelectInput")
let XNextEvent: Ptr = c.dlsym(x11, "XNextEvent")
let XBlackPixel: Ptr = c.dlsym(x11, "XBlackPixel")
let XWhitePixel: Ptr = c.dlsym(x11, "XWhitePixel")
let XSetForeground: Ptr = c.dlsym(x11, "XSetForeground")
let XCreateGC: Ptr = c.dlsym(x11, "XCreateGC")
let XDefaultScreen: Ptr = c.dlsym(x11, "XDefaultScreen")
let XDrawString: Ptr = c.dlsym(x11, "XDrawString")
extern XOpenDisplay
extern XDefaultRootWindow
extern XCreateSimpleWindow
extern XMapWindow
extern XSelectInput
extern XNextEvent
extern XBlackPixel
extern XWhitePixel
extern XSetForeground
extern XCreateGC
extern XDefaultScreen
extern XDrawString
let dpy: Ptr = XOpenDisplay(0)
let screen: Ptr = XDefaultScreen(dpy)

View File

@@ -106,7 +106,7 @@ section .text
);
// take that rustfmt
for name in "malloc,memset,realloc,free,puts,putchar,printf,snprintf,strtol,strlen,strcmp,strcat,strcpy,strncpy,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".split(",")
for name in "syscall,malloc,realloc,free,snprintf,strtol,system,opendir,readdir,closedir,gettimeofday,send,write,read,close,getchar,gethostbyname".split(",")
{
emit!(&mut self.output, "extern {}", name);
emit!(&mut self.output, "c.{} equ {}", name, name);
@@ -121,6 +121,11 @@ _builtin_read8:
mov al, byte [rdi]
ret
section .text._builtin_read32
_builtin_read32:
mov eax, dword [rdi]
ret
section .text._builtin_read64
_builtin_read64:
mov rax, qword [rdi]

View File

@@ -276,7 +276,7 @@ impl Parser {
}
fn pipe(&mut self) -> Result<Expr, ZernError> {
let mut expr = self.logical_or()?;
let mut expr = self.or_and()?;
while self.match_token(&[TokenType::Pipe]) {
let pipe = self.previous().clone();
@@ -305,26 +305,10 @@ impl Parser {
Ok(expr)
}
fn logical_or(&mut self) -> Result<Expr, ZernError> {
let mut expr = self.logical_and()?;
while self.match_token(&[TokenType::BitOr]) {
let op = self.previous().clone();
let right = self.logical_and()?;
expr = Expr::Binary {
left: Box::new(expr),
op,
right: Box::new(right),
}
}
Ok(expr)
}
fn logical_and(&mut self) -> Result<Expr, ZernError> {
fn or_and(&mut self) -> Result<Expr, ZernError> {
let mut expr = self.equality()?;
while self.match_token(&[TokenType::BitAnd]) {
while self.match_token(&[TokenType::BitOr, TokenType::BitAnd]) {
let op = self.previous().clone();
let right = self.equality()?;
expr = Expr::Binary {

View File

@@ -1,6 +1,7 @@
func dbg.panic[msg: String] : Void
c.printf("PANIC: %s\n", msg)
c.exit(1)
io.print("PANIC:")
io.println(msg)
os.exit(1)
func mem.alloc[x: I64] : Ptr
return c.malloc(x)
@@ -9,11 +10,15 @@ func mem.free[x: Ptr] : Void
c.free(x)
func mem.zero[x: I64, size: I64] : Void
c.memset(x, 0, size)
for i in 0..size
mem.write8(x + i, 0)
func mem.read8[x: Ptr] : U8
return _builtin_read8(x)
func mem.read32[x: Ptr] : I64
return _builtin_read32(x)
func mem.read64[x: Ptr] : I64
return _builtin_read64(x)
@@ -24,46 +29,63 @@ func mem.write64[x: Ptr, d: I64] : Void
_builtin_set64(x, d)
func io.print[x: String] : Void
c.puts(x)
c.syscall(1, 1, x, str.len(x))
func io.println[x: String] : Void
io.print(x)
io.print("\n")
func io.print_char[x: U8] : Void
let s: String = mem.alloc(1)
str.set(s, 0, x)
c.syscall(1, 1, s, 1)
mem.free(s)
func io.print_i64[x: I64] : Void
c.printf("%ld\n", x)
let s: String = str.from_i64(x)
io.print(s)
mem.free(s)
func io.println_i64[x: I64] : Void
let s: String = str.from_i64(x)
io.println(s)
mem.free(s)
func io.read_stdin[]: String
let buffer: String = mem.alloc(1025)
let n: I64 = c.read(0, buffer, 1024)
let n: I64 = c.syscall(0, 0, buffer, 1024)
if n < 0
return ""
str.set(buffer, n, 0)
return buffer
func io.read_file[path: String]: String
let file: Ptr = c.fopen(path, "rb")
if !file
let fd: I64 = c.syscall(2, path, 0, 0) // open
if fd <= 0
dbg.panic("failed to open file")
c.fseek(file, 0, 2)
let size: I64 = c.ftell(file)
c.rewind(file)
let size: I64 = c.syscall(8, fd, 0, 2) // lseek to the end
c.syscall(8, fd, 0, 0) // lseek back to start
let buffer: String = mem.alloc(size + 1)
// TODO: check if works with huge files
let n: I64 = c.fread(buffer, 1, size, file)
let n: I64 = c.syscall(0, fd, buffer, size) // read
str.set(buffer, n, 0)
c.fclose(file)
c.syscall(3, fd) // close
return buffer
func io.write_file[path: String, content: String] : Void
let file: Ptr = c.fopen(path, "wb")
if !file
let fd: Ptr = c.syscall(2, path, 0x241, 0o644) // open
if fd < 0
dbg.panic("failed to open file")
c.fwrite(content, 1, str.len(content), file)
c.fclose(file)
c.syscall(1, fd, content, str.len(content)) // write
c.syscall(3, fd) // close
func str.len[s: String] : I64
return c.strlen(s)
let i: I64 = 0
while mem.read8(s + i)
i = i + 1
return i
func str.copy[s: String] : String
let size: I64 = str.len(s) + 1
@@ -76,15 +98,25 @@ func str.set[s: String, n: I64, c: U8] : Void
mem.write8(s+n, c)
func str.equal[a: String, b: String] : Bool
return c.strcmp(a, b) == 0
let i: I64 = 0
while a[i] != 0 & b[i] != 0
if a[i] != b[i]
return false
i = i + 1
return a[i] == b[i]
func str.is_whitespace[x: U8] : Bool
return x == ' ' | x == 10 | x == 13 | x == 9
func str.concat[a: String, b: String] : String
let out: String = mem.alloc(str.len(a) + str.len(b) + 1)
c.strcpy(out, a)
c.strcat(out, b)
let a_len: I64 = str.len(a)
let b_len: I64 = str.len(b)
let out: String = mem.alloc(a_len + b_len + 1)
for i in 0..a_len
str.set(out, i, a[i])
for i in 0..b_len
str.set(out, a_len + i, b[i])
str.set(out, a_len + b_len, 0)
return out
func str.find[s: String, c: U8] : I64
@@ -99,7 +131,8 @@ func str.substr[s: String, start: I64, length: I64] : String
dbg.panic("str.substr out of bounds")
let out: String = mem.alloc(length + 1)
c.strncpy(out, s + start, length)
for i in 0..length
str.set(out, i, s[start + i])
str.set(out, length, 0)
return out
@@ -264,9 +297,9 @@ func math.is_prime[n: I64]: Bool
func math.urandom[]: I64
let buffer: Ptr = mem.alloc(8)
let file: Ptr = c.fopen("/dev/urandom", "rb")
c.fread(buffer, 8, 1, file)
c.fclose(file)
let fd: I64 = c.syscall(2, "/dev/urandom", 0, 0) // open
c.syscall(0, fd, buffer, 8) // read
c.syscall(3, fd) // close
let n: I64 = mem.read64(buffer)
mem.free(buffer)
return n
@@ -337,7 +370,7 @@ func alg._partition[arr: Array, low: I64, high: I64] : I64
return i + 1
func os.exit[code: I64] : Void
c.exit(code)
c.syscall(60, code)
func os.time[] : I64
let tv: Ptr = mem.alloc(16)
@@ -370,7 +403,7 @@ func os.listdir[path: String] : Array
return files
func net.listen[port: I64] : I64
let s: I64 = c.socket(2, 1, 0)
let s: I64 = c.syscall(41, 2, 1, 0) // socket
if s < 0
return -1
@@ -381,13 +414,13 @@ func net.listen[port: I64] : I64
mem.write8(sa + 2, (port >> 8) & 255)
mem.write8(sa + 3, port & 255)
if c.bind(s, sa, 16) < 0
c.close(s)
if c.syscall(49, s, sa, 16) < 0 // bind
c.syscall(3, s) // close
return -1
mem.free(sa)
if c.listen(s, 1) < 0
c.close(s)
if c.syscall(50, s, 1) < 0 // listen
c.syscall(3, s) // close
return -1
return s
@@ -399,7 +432,7 @@ func net.connect[host: String, port: I64] : I64
let ip_ptr: Ptr = mem.read64(mem.read64(he + 24))
let s: I64 = c.socket(2, 1, 0)
let s: I64 = c.syscall(41, 2, 1, 0) // socket
if s < 0
return -1
@@ -413,9 +446,9 @@ func net.connect[host: String, port: I64] : I64
mem.write8(sa + 6, ip_ptr[2])
mem.write8(sa + 7, ip_ptr[3])
if c.connect(s, sa, 16) < 0
if c.syscall(42, s, sa, 16) < 0 // connect
mem.free(sa)
c.close(s)
c.syscall(3, s) // close
return -1
mem.free(sa)

View File

@@ -299,6 +299,10 @@ impl Tokenizer {
while self.peek().is_ascii_hexdigit() {
self.advance();
}
} else if self.match_char('o') {
while matches!(self.peek(), '0'..='7') {
self.advance();
}
} else {
while self.peek().is_ascii_digit() {
self.advance();

46
test.zr
View File

@@ -1,5 +1,13 @@
func run_test[x: String] : Void
c.printf("\033[93mBuilding %s...\033[0m", x)
if str.equal(x, "raylib.zr") | str.equal(x, "x11.zr")
io.print("\033[93mSkipping ")
io.print(x)
io.println("...\033[0m")
return 0
io.print("\033[93mBuilding ")
io.print(x)
io.print("...\033[0m ")
let cmd: String = str.concat("./target/release/zern examples/", x)
let build_start_time: I64 = os.time()
@@ -8,21 +16,33 @@ func run_test[x: String] : Void
let build_end_time: I64 = os.time()
mem.free(cmd)
c.printf(" %ldms\n", build_end_time - build_start_time)
io.print_i64(build_end_time - build_start_time)
io.println("ms")
if str.equal(x, "guess_number.zr") | str.equal(x, "tcp_server.zr") | str.equal(x, "raylib.zr") | str.equal(x, "x11.zr")
c.printf("\033[93mSkipping %s...\033[0m\n", x)
if str.equal(x, "guess_number.zr") | str.equal(x, "tcp_server.zr")
io.print("\033[93mSkipping ")
io.print(x)
io.println("...\033[0m")
return 0
io.print("\033[93mRunning ")
io.print(x)
io.println("...\033[0m")
let run_start_time: I64 = os.time()
if str.equal(x, "curl.zr")
if c.system("./out http://example.com") != 0
os.exit(1)
else
let run_start_time: I64 = os.time()
if str.equal(x, "curl.zr")
if c.system("./out http://example.com") != 0
os.exit(1)
else
if c.system("./out") != 0
os.exit(1)
let run_end_time: I64 = os.time()
if c.system("./out") != 0
os.exit(1)
let run_end_time: I64 = os.time()
c.printf("\033[93mRunning %s...\033[0m %ldms\n", x, run_end_time - run_start_time)
io.print("\033[93mRunning ")
io.print(x)
io.print(" took\033[0m ")
io.print_i64(run_end_time - run_start_time)
io.println("ms")
func main[] : I64
c.system("cargo build --release")