real relative imports

This commit is contained in:
2026-07-15 18:26:28 +02:00
parent 9bbd382e10
commit af08d1888a
47 changed files with 101 additions and 124 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

View File

@@ -1,5 +1,5 @@
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

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 := []

View File

@@ -1,5 +1,5 @@
// needs to be compiled with -m -C "-lpthread" // needs to be compiled with -m -C "-lpthread"
include "std/net.zr" include "$/net.zr"
extern pthread_create extern pthread_create
extern pthread_detach extern pthread_detach

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,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,5 +1,5 @@
include "std/str.zr" include "str.zr"
include "std/os.zr" include "os.zr"
func panic[msg: str] : void func panic[msg: str] : void
io.printf("PANIC: %s\n", msg) io.printf("PANIC: %s\n", msg)
@@ -7,6 +7,10 @@ func panic[msg: str] : void
_builtin_syscall(SYS_kill, os.getpid(), SIGABRT) _builtin_syscall(SYS_kill, os.getpid(), SIGABRT)
os.exit(1) 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
i := 1 i := 1
@@ -133,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,4 +1,4 @@
include "std/containers.zr" include "containers.zr"
const json.OBJECT = 0 const json.OBJECT = 0
const json.ARRAY = 1 const json.ARRAY = 1
@@ -76,6 +76,9 @@ func json.Value.free[v: json.Value] : void
(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)

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

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/io.zr" include "io.zr"
func i64.abs[n: i64] : i64 func i64.abs[n: i64] : i64
if n == -9223372036854775808 if n == -9223372036854775808

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)

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'

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