Compare commits

..

2 Commits

Author SHA1 Message Date
af08d1888a real relative imports 2026-07-15 18:26:28 +02:00
9bbd382e10 http server 2026-07-07 19:40:25 +02:00
50 changed files with 276 additions and 218 deletions

43
Cargo.lock generated
View File

@@ -2,49 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "cc"
version = "1.2.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e228eec9be7c17ccb640b59b36a5cd805ea2a564a4c5e162c2f659fea30d3b96"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "libmimalloc-sys"
version = "0.1.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9"
dependencies = [
"cc",
]
[[package]]
name = "mimalloc"
version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d4139bb28d14ad1facf21d5eb8825051b326e172d216b39f6d31df53cc97862"
dependencies = [
"libmimalloc-sys",
]
[[package]]
name = "shlex"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
[[package]] [[package]]
name = "zern" name = "zern"
version = "0.3.0" version = "0.3.0"
dependencies = [
"mimalloc",
]

View File

@@ -3,9 +3,3 @@ name = "zern"
version = "0.3.0" version = "0.3.0"
edition = "2024" edition = "2024"
license = "BSD-2-Clause" license = "BSD-2-Clause"
[dependencies]
mimalloc = { version = "0.1.52", optional = true }
[features]
mimalloc = ["dep:mimalloc"]

View File

@@ -11,6 +11,8 @@ A very cool language
## Syntax ## Syntax
```rust ```rust
include "$/io.zr"
func main[] : i64 func main[] : i64
answer := os.urandom_i64()->abs() % 100 answer := os.urandom_i64()->abs() % 100

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
// https://brainfuck.org/sierpinski.b // https://brainfuck.org/sierpinski.b

View File

@@ -1,6 +1,6 @@
// needs to be compiled with -m -C "-lraylib" // needs to be compiled with -m -C "-lraylib"
include "std/io.zr" include "$/io.zr"
include "std/os.zr" include "$/os.zr"
extern InitWindow extern InitWindow
extern SetTargetFPS extern SetTargetFPS
@@ -278,7 +278,7 @@ func main[argc: i64, argv: ptr] : i64
disassemble := false disassemble := false
for i in 1..argc for i in 1..argc
arg := mem.read64(argv + i * 8) as str arg := os.arg(argv, i)
if arg->equal("-d") if arg->equal("-d")
disassemble = true disassemble = true
else else

View File

@@ -1,12 +1,12 @@
include "std/io.zr" include "$/io.zr"
include "std/net.zr" include "$/net.zr"
func main[argc: i64, argv: ptr] : i64 func main[argc: i64, argv: ptr] : i64
if argc < 2 if argc < 2
io.println("ERROR: url is missing") io.println("ERROR: url is missing")
return 1 return 1
url := mem.read64(argv + 8) as str url := os.arg(argv, 1)
if url->len() <= 7 if url->len() <= 7
io.println("ERROR: invalid url (Did you forget \"http://\"?)") io.println("ERROR: invalid url (Did you forget \"http://\"?)")
@@ -34,9 +34,9 @@ func main[argc: i64, argv: ptr] : i64
panic("failed to connect") panic("failed to connect")
// seems reasonable // seems reasonable
req := _stackalloc(4096) req := _stackalloc(4096) as str
req_size := str.format_into(req as str, 4096, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host) req_size := req->format_into(4096, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host)
net.send(s, req, req_size) net.send(s, req as ptr, req_size)
header_buf := mem.alloc(8192) as str header_buf := mem.alloc(8192) as str
header_size := 0 header_size := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
sum := 0 sum := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
sum := 0 sum := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
sum := 0 sum := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
n := 600851475143 n := 600851475143

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
out := 0 out := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
out := 1 out := 1

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
sum_of_squares := 0 sum_of_squares := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
found := 0 found := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
n := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" n := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
for a in 1..1000 for a in 1..1000

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
sum := 0 sum := 0

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/containers.zr" include "$/containers.zr"
func main[] : i64 func main[] : i64
N := 20 N := 20

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func num_divisors[n: i64] : i64 func num_divisors[n: i64] : i64
end := n->isqrt() end := n->isqrt()

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
n := 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 n := 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

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func collatz_num[n: i64] : i64 func collatz_num[n: i64] : i64
if n % 2 == 0 if n % 2 == 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
n := 40 n := 40

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/containers.zr" include "$/containers.zr"
func main[] : i64 func main[] : i64
n := [1] n := [1]

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/containers.zr" include "$/containers.zr"
func main[] : i64 func main[] : i64
s1 := [0, 3, 3, 5, 4, 4, 3, 5, 5, 4] s1 := [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/containers.zr" include "$/containers.zr"
func findmax[triangle: Array, row: i64, col: i64] : i64 func findmax[triangle: Array, row: i64, col: i64] : i64
if row == 14 if row == 14

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func days[y: i64, m: i64] : i64 func days[y: i64, m: i64] : i64
if m == 2 if m == 2

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/containers.zr" include "$/containers.zr"
func multiply[n: Array, x: i64] : void func multiply[n: Array, x: i64] : void
carry := 0 carry := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func divisors_sum[n: i64] : i64 func divisors_sum[n: i64] : i64
k := n k := n

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
a := 0 a := 0

View File

@@ -1,4 +1,4 @@
include "std/io.zr" include "$/io.zr"
func main[] : i64 func main[] : i64
for i in 1..40 for i in 1..40

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/os.zr" include "$/os.zr"
func main[] : i64 func main[] : i64
answer := os.urandom_i64()->abs() % 100 answer := os.urandom_i64()->abs() % 100

View File

@@ -1,5 +1,5 @@
// needs to be compiled with -m -C "-lraylib" // needs to be compiled with -m -C "-lraylib"
include "std/io.zr" include "$/io.zr"
extern InitWindow extern InitWindow
extern SetTargetFPS extern SetTargetFPS

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/containers.zr" include "$/containers.zr"
func rule110_step[state: Array] : void func rule110_step[state: Array] : void
new_state := [] new_state := []

96
examples/serve.zr Normal file
View File

@@ -0,0 +1,96 @@
// needs to be compiled with -m -C "-lpthread"
include "$/net.zr"
extern pthread_create
extern pthread_detach
struct ConnData
sock: i64
addr: str
func handle_connection[conn: ConnData] : void
req := _stackalloc(10001) as str
n := net.read(conn->sock, req as ptr, 10000)
if n <= 0
net.close(conn->sock)
conn->addr->free()
(conn as ptr)->free()
return
req[n] = 0
pos := req->find("\r\n")
if pos == -1
panic("TODO: bad request")
first_line := req->substr_n(0, pos)
parts := first_line->split(" ")
method := parts->nth(0) as str
path := parts->nth(1) as str
if !method->equal("GET")
// TODO: method not supported
return
if path->equal("/")
// TODO: index
return
// TODO: ANY validation
file_path := "."->concat(path)
file_size, ok := os.get_file_size(file_path)
if !ok
// TODO: not found
return
fd := _builtin_syscall(SYS_openat, AT_FDCWD, file_path, O_RDONLY, 0)
if fd < 0
// TODO: not found
return
resp := "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n"
net.send(conn->sock, resp as ptr, resp->len())
offset := 0
while offset < file_size
sent := _builtin_syscall(SYS_sendfile, conn->sock, fd, ^offset, IP_MAXPACKET)
if sent <= 0
break
net.close(conn->sock)
_builtin_syscall(SYS_close, fd)
file_path->free()
first_line->free()
parts->free_with_items()
conn->addr->free()
(conn as ptr)->free()
func main[argc: i64, argv: ptr] : i64
port := 8000
if argc > 1
port = os.arg(argv, 1)->parse_i64()
server_sock, ok := net.listen("0.0.0.0", port)
if !ok
panic("failed to listen")
io.printf("Listening on :%d...\n", port)
addr := _stackalloc(16)
while true
conn := net.accept(server_sock, addr)
if conn < 0
io.println("ERROR: failed to accept connection")
continue
conn_data := new* ConnData
conn_data->sock = conn
conn_data->addr = mem.alloc(30) as str
conn_data->addr->format_into(30, "%d.%d.%d.%d:%d", addr[4], addr[5], addr[6], addr[7], mem.read16be(addr + 2))
thread := 0
pthread_create(^thread, 0, ^handle_connection, conn_data)
pthread_detach(thread)

View File

@@ -1,5 +1,5 @@
// needs to be compiled with -m -C "-lsqlite3" // needs to be compiled with -m -C "-lsqlite3"
include "std/io.zr" include "$/io.zr"
extern sqlite3_open extern sqlite3_open
extern sqlite3_exec extern sqlite3_exec

View File

@@ -1,26 +0,0 @@
include "std/io.zr"
include "std/net.zr"
func main[] : i64
s, ok := net.listen("127.0.0.1", 8000)
if !ok
panic("failed to listen")
io.println("Listening on port 8000...")
resp := mem.alloc(IP_MAXPACKET)
addr := _stackalloc(16)
while true
conn := net.accept(s, addr)
if conn < 0
continue
io.printf("%d.%d.%d.%d:%d\n", addr[4], addr[5], addr[6], addr[7], mem.read16be(addr + 2))
n := net.read(conn, resp, IP_MAXPACKET)
io.print_sized(resp, n)
io.println("")
net.send(conn, resp, n)
net.close(conn)

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/net.zr" include "$/net.zr"
func main[] : i64 func main[] : i64
s, ok := net.create_udp_server("127.0.0.1", 8000) s, ok := net.create_udp_server("127.0.0.1", 8000)

View File

@@ -7,16 +7,11 @@ mod typechecker;
use std::{ use std::{
collections::HashSet, collections::HashSet,
fs, fs,
path::Path,
process::{self, Command}, process::{self, Command},
}; };
use tokenizer::ZernError; use tokenizer::ZernError;
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn compile_file(args: Args) -> Result<(), ZernError> { fn compile_file(args: Args) -> Result<(), ZernError> {
let source = match fs::read_to_string(&args.path) { let source = match fs::read_to_string(&args.path) {
Ok(x) => x, Ok(x) => x,
@@ -26,10 +21,8 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
} }
}; };
let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap();
let mut included_paths = HashSet::new(); let mut included_paths = HashSet::new();
let tokenizer = tokenizer::Tokenizer::new(filename.to_owned(), source, &mut included_paths); let tokenizer = tokenizer::Tokenizer::new(args.path, source, &mut included_paths);
let parser = parser::Parser::new(tokenizer.tokenize()?); let parser = parser::Parser::new(tokenizer.tokenize()?);
let statements = parser.parse()?; let statements = parser.parse()?;

View File

@@ -449,28 +449,40 @@ impl<'a> Tokenizer<'a> {
self.include_file(path) self.include_file(path)
} }
fn include_file(&mut self, path: String) -> Result<(), ZernError> { fn include_file(&mut self, mut path: String) -> Result<(), ZernError> {
let canonical = match fs::canonicalize(&path) { if path.starts_with("$/") {
path = find_std_path()
.join(&path[2..])
.to_string_lossy()
.into_owned();
}
let base_dir = Path::new(&self.loc.filename).parent().unwrap();
let resolved_path = base_dir.join(&path);
let canonical = match fs::canonicalize(&resolved_path) {
Ok(p) => p, Ok(p) => p,
Err(e) => { Err(e) => {
return error!(self.loc, format!("failed to resolve {}: {}", path, e)); return error!(self.loc, format!("failed to resolve {}: {}", path, e));
} }
}; };
if !self.included_paths.insert(canonical) { if !self.included_paths.insert(canonical.clone()) {
return Ok(()); return Ok(());
} }
let source = match fs::read_to_string(&path) { let source = match fs::read_to_string(&canonical) {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(_) => {
return error!(self.loc, format!("failed to include {}", path)); return error!(self.loc, format!("failed to include {}", path));
} }
}; };
let filename = Path::new(&path).file_name().unwrap().to_str().unwrap(); let tokenizer = Tokenizer::new(
canonical.to_string_lossy().into_owned(),
let tokenizer = Tokenizer::new(filename.to_owned(), source, &mut *self.included_paths); source,
&mut *self.included_paths,
);
self.tokens.extend(tokenizer.tokenize()?); self.tokens.extend(tokenizer.tokenize()?);
self.tokens.pop(); // remove inner Eof self.tokens.pop(); // remove inner Eof
@@ -550,3 +562,15 @@ impl<'a> Tokenizer<'a> {
self.current >= self.source.len() self.current >= self.source.len()
} }
} }
fn find_std_path() -> PathBuf {
let path = std::env::current_exe().unwrap();
for dir in path.ancestors() {
let candidate = dir.join("std");
if candidate.is_dir() {
return candidate;
}
}
panic!("could not find zern std directory");
}

View File

@@ -1,4 +1,4 @@
include "std/mem.zr" include "mem.zr"
struct Array struct Array
data: ptr data: ptr

View File

@@ -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)

View File

@@ -1,4 +1,15 @@
include "std/str.zr" include "str.zr"
include "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 assert[cond: bool, msg: str] : void
if !cond
panic(msg)
func io.printf[..] : void func io.printf[..] : void
s := _var_arg(0) as ptr s := _var_arg(0) as ptr
@@ -126,7 +137,7 @@ func io.read_binary_file[path: str] : Blob, bool
return buf, true return buf, true
func io.write_file[path: str, content: str] : bool func io.write_text_file[path: str, content: str] : bool
return io.write_binary_file(path, content as ptr, content->len()) return io.write_binary_file(path, content as ptr, content->len())
func io.write_binary_file[path: str, content: ptr, size: i64] : bool func io.write_binary_file[path: str, content: ptr, size: i64] : bool

View File

@@ -1,11 +1,11 @@
include "std/containers.zr" include "containers.zr"
const JSON_OBJECT = 0 const json.OBJECT = 0
const JSON_ARRAY = 1 const json.ARRAY = 1
const JSON_STRING = 2 const json.STRING = 2
const JSON_NUMBER = 3 const json.NUMBER = 3
const JSON_NULL = 4 const json.NULL = 4
const JSON_BOOL = 5 const json.BOOL = 5
struct json.Value struct json.Value
type: i64 type: i64
@@ -14,7 +14,7 @@ struct json.Value
func json.Value.dump[v: json.Value] : str func json.Value.dump[v: json.Value] : str
s := new str.Builder s := new str.Builder
if v->type == JSON_OBJECT if v->type == json.OBJECT
map := v->value as HashMap map := v->value as HashMap
keys := map->keys() keys := map->keys()
s->append_char('{') s->append_char('{')
@@ -29,7 +29,7 @@ func json.Value.dump[v: json.Value] : str
s->append((value as json.Value)->dump()) s->append((value as json.Value)->dump())
s->append_char('}') s->append_char('}')
keys->free() keys->free()
else if v->type == JSON_ARRAY else if v->type == json.ARRAY
arr := v->value as Array arr := v->value as Array
s->append_char('[') s->append_char('[')
for i in 0..arr->size for i in 0..arr->size
@@ -37,16 +37,16 @@ func json.Value.dump[v: json.Value] : str
s->append_char(',') s->append_char(',')
s->append((arr->nth(i) as json.Value)->dump()) s->append((arr->nth(i) as json.Value)->dump())
s->append_char(']') s->append_char(']')
else if v->type == JSON_STRING else if v->type == json.STRING
// TODO: escape sequences // TODO: escape sequences
s->append_char('"') s->append_char('"')
s->append(v->value as str) s->append(v->value as str)
s->append_char('"') s->append_char('"')
else if v->type == JSON_NUMBER else if v->type == json.NUMBER
s->append((v->value as i64)->to_str()) s->append((v->value as i64)->to_str())
else if v->type == JSON_NULL else if v->type == json.NULL
s->append("null") s->append("null")
else if v->type == JSON_BOOL else if v->type == json.BOOL
if v->value as bool if v->value as bool
s->append("true") s->append("true")
else else
@@ -57,7 +57,7 @@ func json.Value.dump[v: json.Value] : str
return s->build_and_free_buffer() return s->build_and_free_buffer()
func json.Value.free[v: json.Value] : void func json.Value.free[v: json.Value] : void
if v->type == JSON_OBJECT if v->type == json.OBJECT
map := v->value as HashMap map := v->value as HashMap
keys := map->keys() keys := map->keys()
for i in 0..keys->size for i in 0..keys->size
@@ -66,16 +66,19 @@ func json.Value.free[v: json.Value] : void
keys->free() keys->free()
map->free_with_keys() map->free_with_keys()
else if v->type == JSON_ARRAY else if v->type == json.ARRAY
arr := v->value as Array arr := v->value as Array
for i in 0..arr->size for i in 0..arr->size
(arr->nth(i) as json.Value)->free() (arr->nth(i) as json.Value)->free()
arr->free() arr->free()
else if v->type == JSON_STRING else if v->type == json.STRING
(v->value as str)->free() (v->value as str)->free()
(v as ptr)->free() (v as ptr)->free()
func json.val[x: any] : any
return (x as json.Value)->value
func json.parse[s: str] : json.Value func json.parse[s: str] : json.Value
i := 0 i := 0
return json._parse_value(s, ^i) return json._parse_value(s, ^i)
@@ -92,7 +95,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
mem.write64(i, mem.read64(i) + 1) mem.write64(i, mem.read64(i) + 1)
json._skip_whitespace(s, i) json._skip_whitespace(s, i)
out->type = JSON_OBJECT out->type = json.OBJECT
out->value = HashMap.new() out->value = HashMap.new()
if s[mem.read64(i)] != '}' if s[mem.read64(i)] != '}'
@@ -124,7 +127,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
mem.write64(i, mem.read64(i) + 1) mem.write64(i, mem.read64(i) + 1)
json._skip_whitespace(s, i) json._skip_whitespace(s, i)
out->type = JSON_ARRAY out->type = json.ARRAY
out->value = [] out->value = []
while s[mem.read64(i)] != ']' while s[mem.read64(i)] != ']'
@@ -143,7 +146,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
panic("json.parse: expected ']'") panic("json.parse: expected ']'")
mem.write64(i, mem.read64(i) + 1) mem.write64(i, mem.read64(i) + 1)
else if s[mem.read64(i)] == '"' else if s[mem.read64(i)] == '"'
out->type = JSON_STRING out->type = json.STRING
out->value = json._parse_string(s, i) out->value = json._parse_string(s, i)
else if s[mem.read64(i)]->is_digit() || s[mem.read64(i)] == '-' else if s[mem.read64(i)]->is_digit() || s[mem.read64(i)] == '-'
start := mem.read64(i) start := mem.read64(i)
@@ -160,18 +163,18 @@ func json._parse_value[s: str, i: ptr] : json.Value
len := mem.read64(i) - start len := mem.read64(i) - start
num_str := s->substr_n(start, len) num_str := s->substr_n(start, len)
out->type = JSON_NUMBER out->type = json.NUMBER
out->value = num_str->parse_i64() out->value = num_str->parse_i64()
num_str->free() 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' 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) 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' 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 out->value = true
mem.write64(i, mem.read64(i) + 4) 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' 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 out->value = false
mem.write64(i, mem.read64(i) + 5) mem.write64(i, mem.read64(i) + 5)
else else

View File

@@ -1,5 +1,5 @@
include "std/linux_syscalls.zr" include "linux_syscalls.zr"
include "std/num.zr" include "num.zr"
const MEM_BLOCK_SIZE = 32 const MEM_BLOCK_SIZE = 32
@@ -33,7 +33,7 @@ func mem._request_space[size: i64] : mem.Block
alloc_size := (needed + 4095) & -4096 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 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") panic("mem._request_space: failed to mmap")
blk->size = alloc_size - MEM_BLOCK_SIZE blk->size = alloc_size - MEM_BLOCK_SIZE

View File

@@ -1,4 +1,4 @@
include "std/os.zr" include "os.zr"
const AF_INET = 2 const AF_INET = 2
const SOCK_STREAM = 1 const SOCK_STREAM = 1

View File

@@ -1,4 +1,4 @@
include "std/core.zr" include "io.zr"
func i64.abs[n: i64] : i64 func i64.abs[n: i64] : i64
if n == -9223372036854775808 if n == -9223372036854775808
@@ -97,6 +97,16 @@ func i64.to_hex_str_buf[n: i64, buf: ptr] : void
buf[len] = 0 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 func f64.to_str_buf[x: i64, buf: ptr] : void
bits := x bits := x
sign := (bits >> 63) & 1 sign := (bits >> 63) & 1

View File

@@ -1,7 +1,7 @@
include "std/posix.zr" include "posix.zr"
include "std/linux_syscalls.zr" include "linux_syscalls.zr"
include "std/str.zr" include "str.zr"
include "std/containers.zr" include "containers.zr"
func os.exit[code: i64] : void func os.exit[code: i64] : void
_builtin_syscall(SYS_exit, code) _builtin_syscall(SYS_exit, code)
@@ -9,6 +9,9 @@ func os.exit[code: i64] : void
func os.getpid[] : i64 func os.getpid[] : i64
return _builtin_syscall(SYS_getpid) 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 func os.getenv[name: str] : str, bool
name_len := name->len() name_len := name->len()
i := 0 i := 0
@@ -96,6 +99,15 @@ func os.run_shell_command[command: str] : i64, bool
func os.file_exists[path: str] : bool func os.file_exists[path: str] : bool
return _builtin_syscall(SYS_faccessat, AT_FDCWD, path, F_OK, 0) == 0 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 func os.is_a_directory[path: str] : bool
st := _stackalloc(256) // it has 21 mixed-size fields so ptr for now st := _stackalloc(256) // it has 21 mixed-size fields so ptr for now

View File

@@ -8,10 +8,15 @@ const AT_FDCWD = -100
const S_IFDIR = 16384 const S_IFDIR = 16384
const S_IFMT = 61440 const S_IFMT = 61440
const PROT_NONE = 0
const PROT_READ = 1 const PROT_READ = 1
const PROT_WRITE = 2 const PROT_WRITE = 2
const PROT_EXEC = 4
const MAP_FAILED = -1
const MAP_SHARED = 1
const MAP_PRIVATE = 2 const MAP_PRIVATE = 2
const MAP_FIXED = 16
const MAP_ANONYMOUS = 32 const MAP_ANONYMOUS = 32
const O_RDONLY = 0 const O_RDONLY = 0

View File

@@ -1,5 +1,5 @@
include "std/num.zr" include "num.zr"
include "std/mem.zr" include "mem.zr"
func u8.is_whitespace[x: u8] : bool func u8.is_whitespace[x: u8] : bool
return x == ' ' || x == '\n' || x == '\t' || x == '\r' return x == ' ' || x == '\n' || x == '\t' || x == '\r'
@@ -297,13 +297,3 @@ func str.Builder.free_buffer[b: str.Builder] : void
b->size = 0 b->size = 0
b->capacity = 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

View File

@@ -1,5 +1,5 @@
include "std/io.zr" include "$/io.zr"
include "std/os.zr" include "$/os.zr"
struct TestRunner struct TestRunner
build_blacklist: Array build_blacklist: Array
@@ -67,11 +67,12 @@ func main[] : i64
tr := new TestRunner tr := new TestRunner
tr->build_blacklist = [] tr->build_blacklist = []
tr->run_blacklist = ["raylib.zr", "sqlite_todo.zr", "guess_number.zr", "udp_server.zr", "chip8.zr", "tcp_server.zr"] tr->run_blacklist = ["raylib.zr", "sqlite_todo.zr", "guess_number.zr", "udp_server.zr", "chip8.zr", "serve.zr"]
tr->custom_build_flags = HashMap.new() tr->custom_build_flags = HashMap.new()
tr->custom_build_flags->insert("raylib.zr", "-m -C \"-lraylib\"") tr->custom_build_flags->insert("raylib.zr", "-m -C \"-lraylib\"")
tr->custom_build_flags->insert("chip8.zr", "-m -C \"-lraylib\"") tr->custom_build_flags->insert("chip8.zr", "-m -C \"-lraylib\"")
tr->custom_build_flags->insert("sqlite_todo.zr", "-m -C \"-lsqlite3\"") tr->custom_build_flags->insert("sqlite_todo.zr", "-m -C \"-lsqlite3\"")
tr->custom_build_flags->insert("serve.zr", "-m -C \"-lpthread\"")
tr->custom_run_args = HashMap.new() tr->custom_run_args = HashMap.new()
tr->custom_run_args->insert("curl.zr", "http://example.com") tr->custom_run_args->insert("curl.zr", "http://example.com")