drop some libc functions, octal numerals, io.println, mem.read32
This commit is contained in:
@@ -14,16 +14,16 @@ func main[] : I64
|
|||||||
let answer: I64 = math.abs(math.urandom()) % 100
|
let answer: I64 = math.abs(math.urandom()) % 100
|
||||||
|
|
||||||
while true
|
while true
|
||||||
io.print("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.print("You win!")
|
io.println("You win!")
|
||||||
break
|
break
|
||||||
else if guess < answer
|
else if guess < answer
|
||||||
io.print("Too low!")
|
io.println("Too low!")
|
||||||
else
|
else
|
||||||
io.print("Too high!")
|
io.println("Too high!")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func main[] : I64
|
|||||||
else if op == '-'
|
else if op == '-'
|
||||||
str.set(memory, p, memory[p]-1)
|
str.set(memory, p, memory[p]-1)
|
||||||
else if op == '.'
|
else if op == '.'
|
||||||
c.printf("%c", memory[p])
|
io.print_char(memory[p])
|
||||||
else if op == ','
|
else if op == ','
|
||||||
str.set(memory, p, c.getchar())
|
str.set(memory, p, c.getchar())
|
||||||
else if op == '['
|
else if op == '['
|
||||||
|
|||||||
@@ -4,4 +4,4 @@ func main[] : I64
|
|||||||
for i in 0..1000
|
for i in 0..1000
|
||||||
if i % 5 == 0 | i % 3 == 0
|
if i % 5 == 0 | i % 3 == 0
|
||||||
sum = sum + i
|
sum = sum + i
|
||||||
io.print_i64(sum)
|
io.println_i64(sum)
|
||||||
@@ -4,4 +4,4 @@ func main[] : I64
|
|||||||
for i in 0..2000000
|
for i in 0..2000000
|
||||||
if math.is_prime(i)
|
if math.is_prime(i)
|
||||||
sum = sum + i
|
sum = sum + i
|
||||||
io.print_i64(sum)
|
io.println_i64(sum)
|
||||||
@@ -16,6 +16,6 @@ func main[] : I64
|
|||||||
while true
|
while true
|
||||||
n = n + i
|
n = n + i
|
||||||
if num_divisors(n) > 500
|
if num_divisors(n) > 500
|
||||||
io.print_i64(n)
|
io.println_i64(n)
|
||||||
break
|
break
|
||||||
i = i + 1
|
i = i + 1
|
||||||
@@ -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
|
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.from_i64()
|
||||||
|> str.substr(0, 10)
|
|> str.substr(0, 10)
|
||||||
|> io.print()
|
|> io.println()
|
||||||
@@ -19,4 +19,4 @@ func main[] : I64
|
|||||||
if seq > max
|
if seq > max
|
||||||
max = seq
|
max = seq
|
||||||
max_index = i
|
max_index = i
|
||||||
io.print_i64(max_index)
|
io.println_i64(max_index)
|
||||||
@@ -6,4 +6,4 @@ func main[] : I64
|
|||||||
for i in 1..r+1
|
for i in 1..r+1
|
||||||
out = out * (n - (r - i)) / i
|
out = out * (n - (r - i)) / i
|
||||||
|
|
||||||
io.print_i64(out)
|
io.println_i64(out)
|
||||||
@@ -10,4 +10,4 @@ func main[] : I64
|
|||||||
b = a + b
|
b = a + b
|
||||||
a = temp
|
a = temp
|
||||||
|
|
||||||
io.print_i64(sum)
|
io.println_i64(sum)
|
||||||
@@ -8,4 +8,4 @@ func main[] : I64
|
|||||||
else
|
else
|
||||||
f = f + 1
|
f = f + 1
|
||||||
|
|
||||||
io.print_i64(n)
|
io.println_i64(n)
|
||||||
@@ -10,4 +10,4 @@ func main[] : I64
|
|||||||
out = a * b
|
out = a * b
|
||||||
mem.free(s)
|
mem.free(s)
|
||||||
mem.free(s_rev)
|
mem.free(s_rev)
|
||||||
io.print_i64(out)
|
io.println_i64(out)
|
||||||
@@ -3,4 +3,4 @@ func main[] : I64
|
|||||||
|
|
||||||
for i in 1..21
|
for i in 1..21
|
||||||
out = math.lcm(out, i)
|
out = math.lcm(out, i)
|
||||||
io.print_i64(out)
|
io.println_i64(out)
|
||||||
@@ -8,4 +8,4 @@ func main[] : I64
|
|||||||
square_of_sum = square_of_sum + i
|
square_of_sum = square_of_sum + i
|
||||||
square_of_sum = square_of_sum * square_of_sum
|
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)
|
||||||
@@ -6,6 +6,6 @@ func main[] : I64
|
|||||||
if math.is_prime(i)
|
if math.is_prime(i)
|
||||||
found = found + 1
|
found = found + 1
|
||||||
if found == 10001
|
if found == 10001
|
||||||
io.print_i64(i)
|
io.println_i64(i)
|
||||||
break
|
break
|
||||||
i = i + 1
|
i = i + 1
|
||||||
@@ -11,4 +11,4 @@ func main[] : I64
|
|||||||
j = j + 1
|
j = j + 1
|
||||||
if s > out
|
if s > out
|
||||||
out = s
|
out = s
|
||||||
io.print_i64(out)
|
io.println_i64(out)
|
||||||
@@ -3,5 +3,5 @@ func main[] : I64
|
|||||||
for b in 1..1000
|
for b in 1..1000
|
||||||
let c: I64 = 1000 - b - a
|
let c: I64 = 1000 - b - a
|
||||||
if a * a + b * b == c * c
|
if a * a + b * b == c * c
|
||||||
io.print_i64(a * b * c)
|
io.println_i64(a * b * c)
|
||||||
return 0
|
return 0
|
||||||
@@ -3,7 +3,7 @@ func main[] : I64
|
|||||||
let b: I64 = 1
|
let b: I64 = 1
|
||||||
|
|
||||||
while a < 100000
|
while a < 100000
|
||||||
io.print_i64(a)
|
io.println_i64(a)
|
||||||
let temp: I64 = b
|
let temp: I64 = b
|
||||||
b = a + b
|
b = a + b
|
||||||
a = temp
|
a = temp
|
||||||
@@ -2,13 +2,13 @@ func main[] : I64
|
|||||||
let answer: I64 = math.abs(math.urandom()) % 100
|
let answer: I64 = math.abs(math.urandom()) % 100
|
||||||
|
|
||||||
while true
|
while true
|
||||||
io.print("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.print("You win!")
|
io.println("You win!")
|
||||||
break
|
break
|
||||||
else if guess < answer
|
else if guess < answer
|
||||||
io.print("Too low!")
|
io.println("Too low!")
|
||||||
else
|
else
|
||||||
io.print("Too high!")
|
io.println("Too high!")
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
func main[] : I64
|
func main[] : I64
|
||||||
io.print("Hello, World!")
|
io.println("Hello, World!")
|
||||||
@@ -4,12 +4,12 @@ func main[] : I64
|
|||||||
array.push(arr, math.abs(math.urandom() % 1000))
|
array.push(arr, math.abs(math.urandom() % 1000))
|
||||||
|
|
||||||
for i in 0..array.size(arr)
|
for i in 0..array.size(arr)
|
||||||
io.print_i64(array.nth(arr, i))
|
io.println_i64(array.nth(arr, i))
|
||||||
io.print("------------")
|
io.println("------------")
|
||||||
|
|
||||||
alg.quicksort(arr)
|
alg.quicksort(arr)
|
||||||
|
|
||||||
for i in 0..array.size(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)
|
array.free(arr)
|
||||||
@@ -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
|
func main[] : I64
|
||||||
extern InitWindow
|
extern InitWindow
|
||||||
@@ -11,11 +11,6 @@ func main[] : I64
|
|||||||
extern DrawRectangle
|
extern DrawRectangle
|
||||||
extern IsKeyDown
|
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 x: I64 = 200
|
||||||
let y: I64 = 200
|
let y: I64 = 200
|
||||||
|
|
||||||
@@ -23,13 +18,13 @@ func main[] : I64
|
|||||||
SetTargetFPS(60)
|
SetTargetFPS(60)
|
||||||
|
|
||||||
while !WindowShouldClose()
|
while !WindowShouldClose()
|
||||||
if IsKeyDown(KEY_W) & 255
|
if IsKeyDown(87) & 255 // W
|
||||||
y = y - 10
|
y = y - 10
|
||||||
if IsKeyDown(KEY_S) & 255
|
if IsKeyDown(83) & 255 // S
|
||||||
y = y + 10
|
y = y + 10
|
||||||
if IsKeyDown(KEY_A) & 255
|
if IsKeyDown(65) & 255 // A
|
||||||
x = x - 10
|
x = x - 10
|
||||||
if IsKeyDown(KEY_D) & 255
|
if IsKeyDown(68) & 255 // D
|
||||||
x = x + 10
|
x = x + 10
|
||||||
|
|
||||||
BeginDrawing()
|
BeginDrawing()
|
||||||
|
|||||||
@@ -18,10 +18,10 @@ func rule110_step[state: Array] : Array
|
|||||||
func print_state[state: Array]: Void
|
func print_state[state: Array]: Void
|
||||||
for i in 0..array.size(state)
|
for i in 0..array.size(state)
|
||||||
if array.nth(state, i)
|
if array.nth(state, i)
|
||||||
c.putchar('#')
|
io.print_char('#')
|
||||||
else
|
else
|
||||||
c.putchar(' ')
|
io.print_char(' ')
|
||||||
io.print("")
|
io.println("")
|
||||||
|
|
||||||
func main[] : I64
|
func main[] : I64
|
||||||
let SIZE: I64 = 60
|
let SIZE: I64 = 60
|
||||||
|
|||||||
@@ -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
|
func main[] : I64
|
||||||
let x11: Ptr = c.dlopen("libX11.so", 2)
|
extern XOpenDisplay
|
||||||
|
extern XDefaultRootWindow
|
||||||
let XOpenDisplay: Ptr = c.dlsym(x11, "XOpenDisplay")
|
extern XCreateSimpleWindow
|
||||||
let XDefaultRootWindow: Ptr = c.dlsym(x11, "XDefaultRootWindow")
|
extern XMapWindow
|
||||||
let XCreateSimpleWindow: Ptr = c.dlsym(x11, "XCreateSimpleWindow")
|
extern XSelectInput
|
||||||
let XMapWindow: Ptr = c.dlsym(x11, "XMapWindow")
|
extern XNextEvent
|
||||||
let XSelectInput: Ptr = c.dlsym(x11, "XSelectInput")
|
extern XBlackPixel
|
||||||
let XNextEvent: Ptr = c.dlsym(x11, "XNextEvent")
|
extern XWhitePixel
|
||||||
let XBlackPixel: Ptr = c.dlsym(x11, "XBlackPixel")
|
extern XSetForeground
|
||||||
let XWhitePixel: Ptr = c.dlsym(x11, "XWhitePixel")
|
extern XCreateGC
|
||||||
let XSetForeground: Ptr = c.dlsym(x11, "XSetForeground")
|
extern XDefaultScreen
|
||||||
let XCreateGC: Ptr = c.dlsym(x11, "XCreateGC")
|
extern XDrawString
|
||||||
let XDefaultScreen: Ptr = c.dlsym(x11, "XDefaultScreen")
|
|
||||||
let XDrawString: Ptr = c.dlsym(x11, "XDrawString")
|
|
||||||
|
|
||||||
let dpy: Ptr = XOpenDisplay(0)
|
let dpy: Ptr = XOpenDisplay(0)
|
||||||
let screen: Ptr = XDefaultScreen(dpy)
|
let screen: Ptr = XDefaultScreen(dpy)
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ section .text
|
|||||||
);
|
);
|
||||||
|
|
||||||
// take that rustfmt
|
// 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, "extern {}", name);
|
||||||
emit!(&mut self.output, "c.{} equ {}", name, name);
|
emit!(&mut self.output, "c.{} equ {}", name, name);
|
||||||
@@ -121,6 +121,11 @@ _builtin_read8:
|
|||||||
mov al, byte [rdi]
|
mov al, byte [rdi]
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
section .text._builtin_read32
|
||||||
|
_builtin_read32:
|
||||||
|
mov eax, dword [rdi]
|
||||||
|
ret
|
||||||
|
|
||||||
section .text._builtin_read64
|
section .text._builtin_read64
|
||||||
_builtin_read64:
|
_builtin_read64:
|
||||||
mov rax, qword [rdi]
|
mov rax, qword [rdi]
|
||||||
|
|||||||
@@ -276,7 +276,7 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn pipe(&mut self) -> Result<Expr, ZernError> {
|
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]) {
|
while self.match_token(&[TokenType::Pipe]) {
|
||||||
let pipe = self.previous().clone();
|
let pipe = self.previous().clone();
|
||||||
@@ -305,26 +305,10 @@ impl Parser {
|
|||||||
Ok(expr)
|
Ok(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn logical_or(&mut self) -> Result<Expr, ZernError> {
|
fn or_and(&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> {
|
|
||||||
let mut expr = self.equality()?;
|
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 op = self.previous().clone();
|
||||||
let right = self.equality()?;
|
let right = self.equality()?;
|
||||||
expr = Expr::Binary {
|
expr = Expr::Binary {
|
||||||
|
|||||||
107
src/std.zr
107
src/std.zr
@@ -1,6 +1,7 @@
|
|||||||
func dbg.panic[msg: String] : Void
|
func dbg.panic[msg: String] : Void
|
||||||
c.printf("PANIC: %s\n", msg)
|
io.print("PANIC:")
|
||||||
c.exit(1)
|
io.println(msg)
|
||||||
|
os.exit(1)
|
||||||
|
|
||||||
func mem.alloc[x: I64] : Ptr
|
func mem.alloc[x: I64] : Ptr
|
||||||
return c.malloc(x)
|
return c.malloc(x)
|
||||||
@@ -9,11 +10,15 @@ func mem.free[x: Ptr] : Void
|
|||||||
c.free(x)
|
c.free(x)
|
||||||
|
|
||||||
func mem.zero[x: I64, size: I64] : Void
|
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
|
func mem.read8[x: Ptr] : U8
|
||||||
return _builtin_read8(x)
|
return _builtin_read8(x)
|
||||||
|
|
||||||
|
func mem.read32[x: Ptr] : I64
|
||||||
|
return _builtin_read32(x)
|
||||||
|
|
||||||
func mem.read64[x: Ptr] : I64
|
func mem.read64[x: Ptr] : I64
|
||||||
return _builtin_read64(x)
|
return _builtin_read64(x)
|
||||||
|
|
||||||
@@ -24,46 +29,63 @@ func mem.write64[x: Ptr, d: I64] : Void
|
|||||||
_builtin_set64(x, d)
|
_builtin_set64(x, d)
|
||||||
|
|
||||||
func io.print[x: String] : Void
|
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
|
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
|
func io.read_stdin[]: String
|
||||||
let buffer: String = mem.alloc(1025)
|
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
|
if n < 0
|
||||||
return ""
|
return ""
|
||||||
str.set(buffer, n, 0)
|
str.set(buffer, n, 0)
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.read_file[path: String]: String
|
func io.read_file[path: String]: String
|
||||||
let file: Ptr = c.fopen(path, "rb")
|
let fd: I64 = c.syscall(2, path, 0, 0) // open
|
||||||
if !file
|
if fd <= 0
|
||||||
dbg.panic("failed to open file")
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
c.fseek(file, 0, 2)
|
let size: I64 = c.syscall(8, fd, 0, 2) // lseek to the end
|
||||||
let size: I64 = c.ftell(file)
|
c.syscall(8, fd, 0, 0) // lseek back to start
|
||||||
c.rewind(file)
|
|
||||||
|
|
||||||
let buffer: String = mem.alloc(size + 1)
|
let buffer: String = mem.alloc(size + 1)
|
||||||
|
let n: I64 = c.syscall(0, fd, buffer, size) // read
|
||||||
// TODO: check if works with huge files
|
|
||||||
let n: I64 = c.fread(buffer, 1, size, file)
|
|
||||||
str.set(buffer, n, 0)
|
str.set(buffer, n, 0)
|
||||||
c.fclose(file)
|
c.syscall(3, fd) // close
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.write_file[path: String, content: String] : Void
|
func io.write_file[path: String, content: String] : Void
|
||||||
let file: Ptr = c.fopen(path, "wb")
|
let fd: Ptr = c.syscall(2, path, 0x241, 0o644) // open
|
||||||
if !file
|
if fd < 0
|
||||||
dbg.panic("failed to open file")
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
c.fwrite(content, 1, str.len(content), file)
|
c.syscall(1, fd, content, str.len(content)) // write
|
||||||
c.fclose(file)
|
c.syscall(3, fd) // close
|
||||||
|
|
||||||
func str.len[s: String] : I64
|
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
|
func str.copy[s: String] : String
|
||||||
let size: I64 = str.len(s) + 1
|
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)
|
mem.write8(s+n, c)
|
||||||
|
|
||||||
func str.equal[a: String, b: String] : Bool
|
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
|
func str.is_whitespace[x: U8] : Bool
|
||||||
return x == ' ' | x == 10 | x == 13 | x == 9
|
return x == ' ' | x == 10 | x == 13 | x == 9
|
||||||
|
|
||||||
func str.concat[a: String, b: String] : String
|
func str.concat[a: String, b: String] : String
|
||||||
let out: String = mem.alloc(str.len(a) + str.len(b) + 1)
|
let a_len: I64 = str.len(a)
|
||||||
c.strcpy(out, a)
|
let b_len: I64 = str.len(b)
|
||||||
c.strcat(out, 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
|
return out
|
||||||
|
|
||||||
func str.find[s: String, c: U8] : I64
|
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")
|
dbg.panic("str.substr out of bounds")
|
||||||
|
|
||||||
let out: String = mem.alloc(length + 1)
|
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)
|
str.set(out, length, 0)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -264,9 +297,9 @@ func math.is_prime[n: I64]: Bool
|
|||||||
|
|
||||||
func math.urandom[]: I64
|
func math.urandom[]: I64
|
||||||
let buffer: Ptr = mem.alloc(8)
|
let buffer: Ptr = mem.alloc(8)
|
||||||
let file: Ptr = c.fopen("/dev/urandom", "rb")
|
let fd: I64 = c.syscall(2, "/dev/urandom", 0, 0) // open
|
||||||
c.fread(buffer, 8, 1, file)
|
c.syscall(0, fd, buffer, 8) // read
|
||||||
c.fclose(file)
|
c.syscall(3, fd) // close
|
||||||
let n: I64 = mem.read64(buffer)
|
let n: I64 = mem.read64(buffer)
|
||||||
mem.free(buffer)
|
mem.free(buffer)
|
||||||
return n
|
return n
|
||||||
@@ -337,7 +370,7 @@ func alg._partition[arr: Array, low: I64, high: I64] : I64
|
|||||||
return i + 1
|
return i + 1
|
||||||
|
|
||||||
func os.exit[code: I64] : Void
|
func os.exit[code: I64] : Void
|
||||||
c.exit(code)
|
c.syscall(60, code)
|
||||||
|
|
||||||
func os.time[] : I64
|
func os.time[] : I64
|
||||||
let tv: Ptr = mem.alloc(16)
|
let tv: Ptr = mem.alloc(16)
|
||||||
@@ -370,7 +403,7 @@ func os.listdir[path: String] : Array
|
|||||||
return files
|
return files
|
||||||
|
|
||||||
func net.listen[port: I64] : I64
|
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
|
if s < 0
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
@@ -381,13 +414,13 @@ func net.listen[port: I64] : I64
|
|||||||
mem.write8(sa + 2, (port >> 8) & 255)
|
mem.write8(sa + 2, (port >> 8) & 255)
|
||||||
mem.write8(sa + 3, port & 255)
|
mem.write8(sa + 3, port & 255)
|
||||||
|
|
||||||
if c.bind(s, sa, 16) < 0
|
if c.syscall(49, s, sa, 16) < 0 // bind
|
||||||
c.close(s)
|
c.syscall(3, s) // close
|
||||||
return -1
|
return -1
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
|
|
||||||
if c.listen(s, 1) < 0
|
if c.syscall(50, s, 1) < 0 // listen
|
||||||
c.close(s)
|
c.syscall(3, s) // close
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
return s
|
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 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
|
if s < 0
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
@@ -413,9 +446,9 @@ func net.connect[host: String, port: I64] : I64
|
|||||||
mem.write8(sa + 6, ip_ptr[2])
|
mem.write8(sa + 6, ip_ptr[2])
|
||||||
mem.write8(sa + 7, ip_ptr[3])
|
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)
|
mem.free(sa)
|
||||||
c.close(s)
|
c.syscall(3, s) // close
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
|
|||||||
@@ -299,6 +299,10 @@ impl Tokenizer {
|
|||||||
while self.peek().is_ascii_hexdigit() {
|
while self.peek().is_ascii_hexdigit() {
|
||||||
self.advance();
|
self.advance();
|
||||||
}
|
}
|
||||||
|
} else if self.match_char('o') {
|
||||||
|
while matches!(self.peek(), '0'..='7') {
|
||||||
|
self.advance();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
while self.peek().is_ascii_digit() {
|
while self.peek().is_ascii_digit() {
|
||||||
self.advance();
|
self.advance();
|
||||||
|
|||||||
46
test.zr
46
test.zr
@@ -1,5 +1,13 @@
|
|||||||
func run_test[x: String] : Void
|
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 cmd: String = str.concat("./target/release/zern examples/", x)
|
||||||
|
|
||||||
let build_start_time: I64 = os.time()
|
let build_start_time: I64 = os.time()
|
||||||
@@ -8,21 +16,33 @@ func run_test[x: String] : Void
|
|||||||
let build_end_time: I64 = os.time()
|
let build_end_time: I64 = os.time()
|
||||||
|
|
||||||
mem.free(cmd)
|
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")
|
if str.equal(x, "guess_number.zr") | str.equal(x, "tcp_server.zr")
|
||||||
c.printf("\033[93mSkipping %s...\033[0m\n", x)
|
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
|
else
|
||||||
let run_start_time: I64 = os.time()
|
if c.system("./out") != 0
|
||||||
if str.equal(x, "curl.zr")
|
os.exit(1)
|
||||||
if c.system("./out http://example.com") != 0
|
let run_end_time: I64 = os.time()
|
||||||
os.exit(1)
|
|
||||||
else
|
|
||||||
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
|
func main[] : I64
|
||||||
c.system("cargo build --release")
|
c.system("cargo build --release")
|
||||||
|
|||||||
Reference in New Issue
Block a user