This commit is contained in:
2025-07-28 14:40:41 +02:00
parent dc3abc36a1
commit ddfa538a5c
2 changed files with 50 additions and 4 deletions

47
examples/curl.zr Normal file
View File

@@ -0,0 +1,47 @@
func main[] : I64
// TODO: parse url
let host: String = "devernay.free.fr"
let path: String = "/hacks/chip8/C8TECH10.HTM"
let s: I64 = net.connect(host, 80)
if s < 0
dbg.panic("failed to connect")
let req: String = c.malloc(2048)
c.snprintf(req, 2048, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host)
c.send(s, req, c.strlen(req), 0)
c.free(req)
let header_buf: Ptr = c.malloc(8192)
let header_size: I64 = 0
let found: Bool = false
let end_index: I64 = -1
while !found & header_size < 8192
let n: I64 = c.read(s, header_buf + header_size, 8192 - header_size)
if n <= 0
break
let current_size: I64 = header_size + n
let i: I64 = 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
found = true
end_index = i + 4
break
i = i + 1
header_size = current_size
if end_index < header_size
c.write(1, header_buf + end_index, header_size - end_index)
c.free(header_buf)
let buffer: Ptr = c.malloc(4096)
while true
let n: I64 = c.read(s, buffer, 4096)
if n <= 0
break
c.write(1, buffer, n)
c.free(buffer)
c.close(s)

View File

@@ -6,7 +6,7 @@ use crate::{
};
pub struct Var {
pub var_type: String,
// pub var_type: String,
pub stack_offset: usize,
}
@@ -35,11 +35,10 @@ impl Env {
self.scopes.pop();
}
pub fn define_var(&mut self, name: String, var_type: String) -> usize {
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 {
var_type,
stack_offset: offset,
});
offset
@@ -104,7 +103,7 @@ section .text
);
// take that rustfmt
for name in "malloc,calloc,realloc,free,puts,printf,sprintf,strtol,strlen,strcmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,read,close,bind,listen,accept,getchar,gethostbyname".split(",")
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(",")
{
emit!(&mut self.output, "extern {}", name);
emit!(&mut self.output, "c.{} equ {}", name, name);