example fixes

This commit is contained in:
2025-07-29 18:53:38 +02:00
parent ddfa538a5c
commit 2c09d7bc21
9 changed files with 49 additions and 38 deletions

View File

@@ -43,4 +43,6 @@ func main[] : I64
closed = closed - 1
i = i - 1
i = i + 1
i = i + 1
c.free(memory)

View File

@@ -1,7 +1,24 @@
func main[] : I64
// TODO: parse url
let host: String = "devernay.free.fr"
let path: String = "/hacks/chip8/C8TECH10.HTM"
func main[argc: I64, argv: Ptr] : I64
if argc < 2
dbg.panic("url missing")
let url: String = _builtin_deref64(argv + 8)
if c.strncmp(url, "http://", 7) != 0
dbg.panic("invalid url scheme")
let url_len: I64 = c.strlen(url)
let host_start: I64 = 7
let i: I64 = host_start
while i < url_len
if str.nth(url, i) == '/'
break
i = i + 1
let host: String = str.substr(url, host_start, i - host_start)
let path: String = "/"
if i < url_len
path = str.substr(url, i, url_len - i)
let s: I64 = net.connect(host, 80)
if s < 0
@@ -22,7 +39,7 @@ func main[] : I64
if n <= 0
break
let current_size: I64 = header_size + n
let i: I64 = 0
i = 0
while i <= current_size - 4
let p: Ptr = header_buf + i
if str.nth(p, 0) == 13 & str.nth(p, 1) == 10 & str.nth(p, 2) == 13 & str.nth(p, 3) == 10

View File

@@ -1,4 +1,5 @@
func main[] : I64
// leaks a bit of memory but looks very cool
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)

View File

@@ -33,4 +33,6 @@ func main[] : I64
quicksort(arr)
for i in 0..array.size(arr)
io.print_i64(arr[i])
io.print_i64(arr[i])
array.free(arr)

View File

@@ -15,14 +15,13 @@ func rule110_step[state: Array] : Array
return new_state
func to_str[state: Array]: String
let out: String = malloc(array.size(state))
func print_state[state: Array]: Void
for i in 0..array.size(state)
if state[i]
str.set(out, i, '#')
c.putchar('#')
else
str.set(out, i, ' ')
return out
c.putchar(' ')
io.print("")
func main[] : I64
let SIZE: I64 = 60
@@ -32,7 +31,9 @@ func main[] : I64
array.push(state, false)
array.push(state, true)
io.print(to_str(state))
print_state(state)
for i in 0..SIZE
state = rule110_step(state)
io.print(to_str(state))
print_state(state)
array.free(state)

View File

@@ -1,13 +0,0 @@
func main[] : I64
let s: I64 = net.connect("devernay.free.fr", 80)
let req: String = "GET /hacks/chip8/C8TECH10.HTM HTTP/1.0\r\nHost: devernay.free.fr\r\nConnection: close\r\n\r\n"
c.send(s, req, c.strlen(req), 0)
let resp: String = c.malloc(60000)
let n: I64 = c.read(s, resp, 60000)
str.set(resp, n, 0)
io.print(resp)
c.free(resp)
c.close(s)

View File

@@ -103,7 +103,7 @@ section .text
);
// take that rustfmt
for name in "malloc,calloc,realloc,free,puts,printf,sprintf,snprintf,strtol,strlen,strcmp,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".split(",")
{
emit!(&mut self.output, "extern {}", name);
emit!(&mut self.output, "c.{} equ {}", name, name);

View File

@@ -280,13 +280,8 @@ impl Tokenizer {
fn count_indentation(&mut self) -> usize {
let mut count = 0;
while self.peek() == ' ' || self.peek() == '\t' {
if self.peek() == ' ' {
count += 1;
}
if self.peek() == '\t' {
count += 4;
}
while self.peek() == ' ' {
count += 1;
self.advance();
}
count

12
test.zr
View File

@@ -14,8 +14,12 @@ func run_test[x: String] : Void
c.printf("\033[93mSkipping %s...\033[0m\n", x)
else
let run_start_time: I64 = os.time()
if c.system("./out") != 0
c.exit(1)
if c.strcmp(x, "curl.zr") == 0
if c.system("./out http://devernay.free.fr/hacks/chip8/C8TECH10.HTM") != 0
c.exit(1)
else
if c.system("./out") != 0
c.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)
@@ -25,4 +29,6 @@ func main[] : I64
let files: Array = os.listdir("examples/")
for i in 0..array.size(files)
run_test(files[i])
run_test(files[i])
array.free(files)