http server
This commit is contained in:
14
std/core.zr
14
std/core.zr
@@ -1,14 +0,0 @@
|
||||
include "std/linux_syscalls.zr"
|
||||
include "std/posix.zr"
|
||||
|
||||
// weird like that so num doesnt depend on io
|
||||
func panic[msg: str] : void
|
||||
len := 0
|
||||
while msg[len]
|
||||
len += 1
|
||||
_builtin_syscall(SYS_write, 2, "PANIC: ", 7)
|
||||
_builtin_syscall(SYS_write, 2, msg, len)
|
||||
_builtin_syscall(SYS_write, 2, "\n", 1)
|
||||
// for gdb backtrace
|
||||
_builtin_syscall(SYS_kill, _builtin_syscall(SYS_getpid), SIGABRT)
|
||||
_builtin_syscall(SYS_exit, 1)
|
||||
@@ -1,4 +1,11 @@
|
||||
include "std/str.zr"
|
||||
include "std/os.zr"
|
||||
|
||||
func panic[msg: str] : void
|
||||
io.printf("PANIC: %s\n", msg)
|
||||
// for gdb backtrace
|
||||
_builtin_syscall(SYS_kill, os.getpid(), SIGABRT)
|
||||
os.exit(1)
|
||||
|
||||
func io.printf[..] : void
|
||||
s := _var_arg(0) as ptr
|
||||
|
||||
44
std/json.zr
44
std/json.zr
@@ -1,11 +1,11 @@
|
||||
include "std/containers.zr"
|
||||
|
||||
const JSON_OBJECT = 0
|
||||
const JSON_ARRAY = 1
|
||||
const JSON_STRING = 2
|
||||
const JSON_NUMBER = 3
|
||||
const JSON_NULL = 4
|
||||
const JSON_BOOL = 5
|
||||
const json.OBJECT = 0
|
||||
const json.ARRAY = 1
|
||||
const json.STRING = 2
|
||||
const json.NUMBER = 3
|
||||
const json.NULL = 4
|
||||
const json.BOOL = 5
|
||||
|
||||
struct json.Value
|
||||
type: i64
|
||||
@@ -14,7 +14,7 @@ struct json.Value
|
||||
func json.Value.dump[v: json.Value] : str
|
||||
s := new str.Builder
|
||||
|
||||
if v->type == JSON_OBJECT
|
||||
if v->type == json.OBJECT
|
||||
map := v->value as HashMap
|
||||
keys := map->keys()
|
||||
s->append_char('{')
|
||||
@@ -29,7 +29,7 @@ func json.Value.dump[v: json.Value] : str
|
||||
s->append((value as json.Value)->dump())
|
||||
s->append_char('}')
|
||||
keys->free()
|
||||
else if v->type == JSON_ARRAY
|
||||
else if v->type == json.ARRAY
|
||||
arr := v->value as Array
|
||||
s->append_char('[')
|
||||
for i in 0..arr->size
|
||||
@@ -37,16 +37,16 @@ func json.Value.dump[v: json.Value] : str
|
||||
s->append_char(',')
|
||||
s->append((arr->nth(i) as json.Value)->dump())
|
||||
s->append_char(']')
|
||||
else if v->type == JSON_STRING
|
||||
else if v->type == json.STRING
|
||||
// TODO: escape sequences
|
||||
s->append_char('"')
|
||||
s->append(v->value as str)
|
||||
s->append_char('"')
|
||||
else if v->type == JSON_NUMBER
|
||||
else if v->type == json.NUMBER
|
||||
s->append((v->value as i64)->to_str())
|
||||
else if v->type == JSON_NULL
|
||||
else if v->type == json.NULL
|
||||
s->append("null")
|
||||
else if v->type == JSON_BOOL
|
||||
else if v->type == json.BOOL
|
||||
if v->value as bool
|
||||
s->append("true")
|
||||
else
|
||||
@@ -57,7 +57,7 @@ func json.Value.dump[v: json.Value] : str
|
||||
return s->build_and_free_buffer()
|
||||
|
||||
func json.Value.free[v: json.Value] : void
|
||||
if v->type == JSON_OBJECT
|
||||
if v->type == json.OBJECT
|
||||
map := v->value as HashMap
|
||||
keys := map->keys()
|
||||
for i in 0..keys->size
|
||||
@@ -66,12 +66,12 @@ func json.Value.free[v: json.Value] : void
|
||||
keys->free()
|
||||
|
||||
map->free_with_keys()
|
||||
else if v->type == JSON_ARRAY
|
||||
else if v->type == json.ARRAY
|
||||
arr := v->value as Array
|
||||
for i in 0..arr->size
|
||||
(arr->nth(i) as json.Value)->free()
|
||||
arr->free()
|
||||
else if v->type == JSON_STRING
|
||||
else if v->type == json.STRING
|
||||
(v->value as str)->free()
|
||||
|
||||
(v as ptr)->free()
|
||||
@@ -92,7 +92,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
mem.write64(i, mem.read64(i) + 1)
|
||||
json._skip_whitespace(s, i)
|
||||
|
||||
out->type = JSON_OBJECT
|
||||
out->type = json.OBJECT
|
||||
out->value = HashMap.new()
|
||||
|
||||
if s[mem.read64(i)] != '}'
|
||||
@@ -124,7 +124,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
mem.write64(i, mem.read64(i) + 1)
|
||||
json._skip_whitespace(s, i)
|
||||
|
||||
out->type = JSON_ARRAY
|
||||
out->type = json.ARRAY
|
||||
out->value = []
|
||||
|
||||
while s[mem.read64(i)] != ']'
|
||||
@@ -143,7 +143,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
panic("json.parse: expected ']'")
|
||||
mem.write64(i, mem.read64(i) + 1)
|
||||
else if s[mem.read64(i)] == '"'
|
||||
out->type = JSON_STRING
|
||||
out->type = json.STRING
|
||||
out->value = json._parse_string(s, i)
|
||||
else if s[mem.read64(i)]->is_digit() || s[mem.read64(i)] == '-'
|
||||
start := mem.read64(i)
|
||||
@@ -160,18 +160,18 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
len := mem.read64(i) - start
|
||||
|
||||
num_str := s->substr_n(start, len)
|
||||
out->type = JSON_NUMBER
|
||||
out->type = json.NUMBER
|
||||
out->value = num_str->parse_i64()
|
||||
num_str->free()
|
||||
else if s[mem.read64(i)] == 'n' && s[mem.read64(i)+1] == 'u' && s[mem.read64(i)+2] == 'l' && s[mem.read64(i)+3] == 'l'
|
||||
out->type = JSON_NULL
|
||||
out->type = json.NULL
|
||||
mem.write64(i, mem.read64(i) + 4)
|
||||
else if s[mem.read64(i)] == 't' && s[mem.read64(i)+1] == 'r' && s[mem.read64(i)+2] == 'u' && s[mem.read64(i)+3] == 'e'
|
||||
out->type = JSON_BOOL
|
||||
out->type = json.BOOL
|
||||
out->value = true
|
||||
mem.write64(i, mem.read64(i) + 4)
|
||||
else if s[mem.read64(i)] == 'f' && s[mem.read64(i)+1] == 'a' && s[mem.read64(i)+2] == 'l' && s[mem.read64(i)+3] == 's' && s[mem.read64(i)+4] == 'e'
|
||||
out->type = JSON_BOOL
|
||||
out->type = json.BOOL
|
||||
out->value = false
|
||||
mem.write64(i, mem.read64(i) + 5)
|
||||
else
|
||||
|
||||
@@ -33,7 +33,7 @@ func mem._request_space[size: i64] : mem.Block
|
||||
alloc_size := (needed + 4095) & -4096
|
||||
|
||||
blk := _builtin_syscall(SYS_mmap, 0, alloc_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) as mem.Block
|
||||
if (blk as ptr as i64) == -1
|
||||
if (blk as ptr as i64) == MAP_FAILED
|
||||
panic("mem._request_space: failed to mmap")
|
||||
|
||||
blk->size = alloc_size - MEM_BLOCK_SIZE
|
||||
|
||||
12
std/num.zr
12
std/num.zr
@@ -1,4 +1,4 @@
|
||||
include "std/core.zr"
|
||||
include "std/io.zr"
|
||||
|
||||
func i64.abs[n: i64] : i64
|
||||
if n == -9223372036854775808
|
||||
@@ -97,6 +97,16 @@ func i64.to_hex_str_buf[n: i64, buf: ptr] : void
|
||||
|
||||
buf[len] = 0
|
||||
|
||||
func i64.to_str[n: i64] : str
|
||||
out := mem.alloc(21)
|
||||
n->to_str_buf(out)
|
||||
return out as str
|
||||
|
||||
func i64.to_hex_str[n: i64] : str
|
||||
out := mem.alloc(17)
|
||||
n->to_hex_str_buf(out)
|
||||
return out as str
|
||||
|
||||
func f64.to_str_buf[x: i64, buf: ptr] : void
|
||||
bits := x
|
||||
sign := (bits >> 63) & 1
|
||||
|
||||
12
std/os.zr
12
std/os.zr
@@ -9,6 +9,9 @@ func os.exit[code: i64] : void
|
||||
func os.getpid[] : i64
|
||||
return _builtin_syscall(SYS_getpid)
|
||||
|
||||
func os.arg[argv: ptr, n: i64] : str
|
||||
return mem.read64(argv + n * 8) as str
|
||||
|
||||
func os.getenv[name: str] : str, bool
|
||||
name_len := name->len()
|
||||
i := 0
|
||||
@@ -96,6 +99,15 @@ func os.run_shell_command[command: str] : i64, bool
|
||||
func os.file_exists[path: str] : bool
|
||||
return _builtin_syscall(SYS_faccessat, AT_FDCWD, path, F_OK, 0) == 0
|
||||
|
||||
func os.get_file_size[path: str] : i64, bool
|
||||
st := _stackalloc(256) // it has 21 mixed-size fields so ptr for now
|
||||
|
||||
rc := _builtin_syscall(SYS_newfstatat, AT_FDCWD, path, st, 0)
|
||||
if rc != 0
|
||||
return 0, false
|
||||
|
||||
return mem.read64(st + 48), true
|
||||
|
||||
func os.is_a_directory[path: str] : bool
|
||||
st := _stackalloc(256) // it has 21 mixed-size fields so ptr for now
|
||||
|
||||
|
||||
39
std/posix.zr
39
std/posix.zr
@@ -1,27 +1,32 @@
|
||||
const STDIN_FILENO = 0
|
||||
const STDOUT_FILENO = 1
|
||||
const STDIN_FILENO = 0
|
||||
const STDOUT_FILENO = 1
|
||||
|
||||
const SIGABRT = 6
|
||||
const SIGABRT = 6
|
||||
|
||||
const AT_FDCWD = -100
|
||||
const AT_FDCWD = -100
|
||||
|
||||
const S_IFDIR = 16384
|
||||
const S_IFMT = 61440
|
||||
const S_IFDIR = 16384
|
||||
const S_IFMT = 61440
|
||||
|
||||
const PROT_READ = 1
|
||||
const PROT_WRITE = 2
|
||||
const PROT_NONE = 0
|
||||
const PROT_READ = 1
|
||||
const PROT_WRITE = 2
|
||||
const PROT_EXEC = 4
|
||||
|
||||
const MAP_PRIVATE = 2
|
||||
const MAP_ANONYMOUS = 32
|
||||
const MAP_FAILED = -1
|
||||
const MAP_SHARED = 1
|
||||
const MAP_PRIVATE = 2
|
||||
const MAP_FIXED = 16
|
||||
const MAP_ANONYMOUS = 32
|
||||
|
||||
const O_RDONLY = 0
|
||||
const O_WRONLY = 1
|
||||
const O_CREAT = 64
|
||||
const O_TRUNC = 512
|
||||
const O_RDONLY = 0
|
||||
const O_WRONLY = 1
|
||||
const O_CREAT = 64
|
||||
const O_TRUNC = 512
|
||||
|
||||
const SEEK_SET = 0
|
||||
const SEEK_END = 2
|
||||
const SEEK_SET = 0
|
||||
const SEEK_END = 2
|
||||
|
||||
const F_OK = 0
|
||||
const F_OK = 0
|
||||
|
||||
const CLOCK_REALTIME = 0
|
||||
|
||||
10
std/str.zr
10
std/str.zr
@@ -297,13 +297,3 @@ func str.Builder.free_buffer[b: str.Builder] : void
|
||||
b->size = 0
|
||||
b->capacity = 0
|
||||
|
||||
// here to prevent num depending on mem
|
||||
func i64.to_str[n: i64] : str
|
||||
out := mem.alloc(21)
|
||||
n->to_str_buf(out)
|
||||
return out as str
|
||||
|
||||
func i64.to_hex_str[n: i64] : str
|
||||
out := mem.alloc(17)
|
||||
n->to_hex_str_buf(out)
|
||||
return out as str
|
||||
|
||||
Reference in New Issue
Block a user