diff --git a/README.md b/README.md index fb35c7b..7682727 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,10 @@ A very cool language ## Features * Clean indentation-based syntax -* Compiles to x86_64 Assembly +* Compiles to x86-64 Assembly * No libc required! * Produces tiny static executables (11KB for `hello.zr`) -* Has type inference, [UFCS](https://en.wikipedia.org/wiki/Uniform_function_call_syntax), variadics, dynamic arrays, hashmaps, DNS resolver, etc. -* Growing [standard library](https://git.ton1.dev/toni/zern/src/branch/main/std) +* Has static typing, [UFCS](https://en.wikipedia.org/wiki/Uniform_function_call_syntax), variadics, dynamic arrays, hashmaps, DNS resolver, etc. ## Syntax ```rust diff --git a/examples/brainfuck.zr b/examples/brainfuck.zr index 4f5d521..192be86 100644 --- a/examples/brainfuck.zr +++ b/examples/brainfuck.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 // https://brainfuck.org/sierpinski.b src := "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]" diff --git a/examples/chip8.zr b/examples/chip8.zr index da61804..735f898 100644 --- a/examples/chip8.zr +++ b/examples/chip8.zr @@ -1,4 +1,7 @@ // needs to be compiled with -m -C "-lraylib" +include "std/io.zr" +include "std/os.zr" + extern InitWindow extern SetTargetFPS extern WindowShouldClose diff --git a/examples/curl.zr b/examples/curl.zr index 4114b80..db447d2 100644 --- a/examples/curl.zr +++ b/examples/curl.zr @@ -1,3 +1,4 @@ +include "std/io.zr" include "std/net.zr" func main[argc: i64, argv: ptr] : i64 diff --git a/examples/euler/euler_00.zr b/examples/euler/euler_00.zr index b5786e9..7cc459e 100644 --- a/examples/euler/euler_00.zr +++ b/examples/euler/euler_00.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 sum := 0 diff --git a/examples/euler/euler_01.zr b/examples/euler/euler_01.zr index f0636d2..6895dd0 100644 --- a/examples/euler/euler_01.zr +++ b/examples/euler/euler_01.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 sum := 0 diff --git a/examples/euler/euler_02.zr b/examples/euler/euler_02.zr index 3e7a325..0c846ee 100644 --- a/examples/euler/euler_02.zr +++ b/examples/euler/euler_02.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 sum := 0 a := 0 diff --git a/examples/euler/euler_03.zr b/examples/euler/euler_03.zr index e235491..3c2378f 100644 --- a/examples/euler/euler_03.zr +++ b/examples/euler/euler_03.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 n := 600851475143 f := 2 diff --git a/examples/euler/euler_04.zr b/examples/euler/euler_04.zr index a4c3991..96c02ba 100644 --- a/examples/euler/euler_04.zr +++ b/examples/euler/euler_04.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 out := 0 diff --git a/examples/euler/euler_05.zr b/examples/euler/euler_05.zr index 69374c8..e2a5be5 100644 --- a/examples/euler/euler_05.zr +++ b/examples/euler/euler_05.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 out := 1 diff --git a/examples/euler/euler_06.zr b/examples/euler/euler_06.zr index dcb5768..ffd866e 100644 --- a/examples/euler/euler_06.zr +++ b/examples/euler/euler_06.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 sum_of_squares := 0 for i in 1..101 diff --git a/examples/euler/euler_07.zr b/examples/euler/euler_07.zr index a485c67..eaa4fb8 100644 --- a/examples/euler/euler_07.zr +++ b/examples/euler/euler_07.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 found := 0 diff --git a/examples/euler/euler_08.zr b/examples/euler/euler_08.zr index d268413..dfdecd3 100644 --- a/examples/euler/euler_08.zr +++ b/examples/euler/euler_08.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 n := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" diff --git a/examples/euler/euler_09.zr b/examples/euler/euler_09.zr index 4384e1f..0c05505 100644 --- a/examples/euler/euler_09.zr +++ b/examples/euler/euler_09.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 for a in 1..1000 for b in 1..1000 diff --git a/examples/euler/euler_10.zr b/examples/euler/euler_10.zr index 4ef7488..8bd384e 100644 --- a/examples/euler/euler_10.zr +++ b/examples/euler/euler_10.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 sum := 0 diff --git a/examples/euler/euler_11.zr b/examples/euler/euler_11.zr index 6dc68a0..4eae779 100644 --- a/examples/euler/euler_11.zr +++ b/examples/euler/euler_11.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/containers.zr" + func main[] : i64 N := 20 diff --git a/examples/euler/euler_12.zr b/examples/euler/euler_12.zr index 33910d8..0518eea 100644 --- a/examples/euler/euler_12.zr +++ b/examples/euler/euler_12.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func num_divisors[n: i64] : i64 end := n->isqrt() diff --git a/examples/euler/euler_13.zr b/examples/euler/euler_13.zr index 0efb7fb..ce082d1 100644 --- a/examples/euler/euler_13.zr +++ b/examples/euler/euler_13.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + 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 io.println(n->to_str()->substr(0, 10)) diff --git a/examples/euler/euler_14.zr b/examples/euler/euler_14.zr index 9d3454e..e1591cb 100644 --- a/examples/euler/euler_14.zr +++ b/examples/euler/euler_14.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func collatz_num[n: i64] : i64 if n % 2 == 0 return n / 2 diff --git a/examples/euler/euler_15.zr b/examples/euler/euler_15.zr index 4008465..789de35 100644 --- a/examples/euler/euler_15.zr +++ b/examples/euler/euler_15.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 n := 40 r := 20 diff --git a/examples/euler/euler_16.zr b/examples/euler/euler_16.zr index ef07e6e..547b041 100644 --- a/examples/euler/euler_16.zr +++ b/examples/euler/euler_16.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/containers.zr" + func main[] : i64 n := [1] diff --git a/examples/euler/euler_17.zr b/examples/euler/euler_17.zr index 7dcfeba..2ed1734 100644 --- a/examples/euler/euler_17.zr +++ b/examples/euler/euler_17.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/containers.zr" + func main[] : i64 s1 := [0, 3, 3, 5, 4, 4, 3, 5, 5, 4] s2 := [3, 6, 6, 8, 8, 7, 7, 9, 8, 8] diff --git a/examples/euler/euler_18.zr b/examples/euler/euler_18.zr index 69979b7..b91ee93 100644 --- a/examples/euler/euler_18.zr +++ b/examples/euler/euler_18.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/containers.zr" + func findmax[triangle: Array, row: i64, col: i64] : i64 if row == 14 return (triangle->nth(row) as Array)->nth(col) diff --git a/examples/euler/euler_19.zr b/examples/euler/euler_19.zr index fef3eda..5078545 100644 --- a/examples/euler/euler_19.zr +++ b/examples/euler/euler_19.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func days[y: i64, m: i64] : i64 if m == 2 if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) diff --git a/examples/euler/euler_20.zr b/examples/euler/euler_20.zr index fc1e5cb..c0c2d8d 100644 --- a/examples/euler/euler_20.zr +++ b/examples/euler/euler_20.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/containers.zr" + func multiply[n: Array, x: i64] : void carry := 0 for i in 0..n->size diff --git a/examples/euler/euler_21.zr b/examples/euler/euler_21.zr index 1bb3b97..35bbce4 100644 --- a/examples/euler/euler_21.zr +++ b/examples/euler/euler_21.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func divisors_sum[n: i64] : i64 k := n sum := 1 diff --git a/examples/fib.zr b/examples/fib.zr index b0ec677..d45650f 100644 --- a/examples/fib.zr +++ b/examples/fib.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 a := 0 b := 1 diff --git a/examples/fizzbuzz.zr b/examples/fizzbuzz.zr index 2c8dfd3..6118691 100644 --- a/examples/fizzbuzz.zr +++ b/examples/fizzbuzz.zr @@ -1,3 +1,5 @@ +include "std/io.zr" + func main[] : i64 for i in 1..40 if i % 15 == 0 diff --git a/examples/guess_number.zr b/examples/guess_number.zr index d1d4a2d..21ca36d 100644 --- a/examples/guess_number.zr +++ b/examples/guess_number.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/os.zr" + func main[] : i64 answer := os.urandom_i64()->abs() % 100 diff --git a/examples/hello.zr b/examples/hello.zr index e8a8c1f..8bdc52a 100644 --- a/examples/hello.zr +++ b/examples/hello.zr @@ -1,2 +1,4 @@ +include "std/io.zr" + func main[] : i64 io.println("Hello, World!") diff --git a/examples/quicksort.zr b/examples/quicksort.zr index fc6b203..c4ec09b 100644 --- a/examples/quicksort.zr +++ b/examples/quicksort.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/os.zr" + func main[] : i64 arr := [] for i in 0..10 diff --git a/examples/raylib.zr b/examples/raylib.zr index 04c974b..3061855 100644 --- a/examples/raylib.zr +++ b/examples/raylib.zr @@ -1,4 +1,6 @@ // needs to be compiled with -m -C "-lraylib" +include "std/io.zr" + extern InitWindow extern SetTargetFPS extern WindowShouldClose diff --git a/examples/rule110.zr b/examples/rule110.zr index 1a2179a..6ce2638 100644 --- a/examples/rule110.zr +++ b/examples/rule110.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/containers.zr" + func rule110_step[state: Array] : void new_state := [] diff --git a/examples/sqlite_todo.zr b/examples/sqlite_todo.zr index 99ffe0b..91b86e5 100644 --- a/examples/sqlite_todo.zr +++ b/examples/sqlite_todo.zr @@ -1,4 +1,6 @@ // needs to be compiled with -m -C "-lsqlite3" +include "std/io.zr" + extern sqlite3_open extern sqlite3_exec extern sqlite3_prepare_v2 diff --git a/examples/tcp_server.zr b/examples/tcp_server.zr index 7f52814..6efc520 100644 --- a/examples/tcp_server.zr +++ b/examples/tcp_server.zr @@ -1,3 +1,4 @@ +include "std/io.zr" include "std/net.zr" func main[] : i64 diff --git a/examples/udp_server.zr b/examples/udp_server.zr index f5ba8ca..154cadf 100644 --- a/examples/udp_server.zr +++ b/examples/udp_server.zr @@ -1,3 +1,4 @@ +include "std/io.zr" include "std/net.zr" func main[] : i64 diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index 98738d9..d1e6c3f 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -147,8 +147,8 @@ _builtin_cvttsd2si: cvttsd2si rax, xmm0 ret -.section .text._builtin_f64_to_float -_builtin_f64_to_float: +.section .text._builtin_f64_to_f32 +_builtin_f64_to_f32: cvtsd2ss xmm0, xmm0 movd eax, xmm0 ret @@ -648,10 +648,10 @@ _builtin_environ: emit!(&mut self.output, " lea rax, [rip + {}]", label); } - TokenType::True => { + TokenType::KeywordTrue => { emit!(&mut self.output, " mov rax, 1"); } - TokenType::False => { + TokenType::KeywordFalse => { emit!(&mut self.output, " mov rax, 0"); } _ => unreachable!(), diff --git a/src/main.rs b/src/main.rs index 336cd40..078d40f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod tokenizer; mod typechecker; use std::{ + collections::HashSet, fs, path::Path, process::{self, Command}, @@ -27,8 +28,8 @@ fn compile_file(args: Args) -> Result<(), ZernError> { let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap(); - let mut tokenizer = tokenizer::Tokenizer::new(filename.to_owned(), source); - tokenizer.include_file("std/std.zr".into())?; + let mut included_paths = HashSet::new(); + let tokenizer = tokenizer::Tokenizer::new(filename.to_owned(), source, &mut included_paths); let parser = parser::Parser::new(tokenizer.tokenize()?); let statements = parser.parse()?; diff --git a/src/parser.rs b/src/parser.rs index 51caad8..865f68b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -665,8 +665,8 @@ impl Parser { TokenType::FloatLiteral, TokenType::CharLiteral, TokenType::StringLiteral, - TokenType::True, - TokenType::False, + TokenType::KeywordTrue, + TokenType::KeywordFalse, ]) { Ok(Expr::new(ExprKind::Literal(self.previous().clone()))) } else if self.match_token(&[TokenType::LeftParen]) { diff --git a/src/symbol_table.rs b/src/symbol_table.rs index 82a4187..16af961 100644 --- a/src/symbol_table.rs +++ b/src/symbol_table.rs @@ -58,8 +58,8 @@ impl SymbolTable { ("_builtin_cvtsi2sd".into(), FnType::new("f64", vec!["i64"])), ("_builtin_cvttsd2si".into(), FnType::new("i64", vec!["f64"])), ( - "_builtin_f64_to_float".into(), - FnType::new("i64", vec!["f64"]), + "_builtin_f64_to_f32".into(), + FnType::new("any", vec!["f64"]), ), ("_builtin_syscall".into(), FnType::new_variadic("i64")), ("_builtin_environ".into(), FnType::new("ptr", vec![])), diff --git a/src/tokenizer.rs b/src/tokenizer.rs index a080dbd..b3f57f6 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -1,4 +1,9 @@ -use std::{cmp::Ordering, fmt, fs, path::Path}; +use std::{ + cmp::Ordering, + collections::HashSet, + fmt, fs, + path::{Path, PathBuf}, +}; #[derive(Debug, Clone, PartialEq)] pub enum TokenType { @@ -40,8 +45,6 @@ pub enum TokenType { CharLiteral, IntLiteral, FloatLiteral, - True, - False, KeywordConst, KeywordIf, @@ -58,6 +61,8 @@ pub enum TokenType { KeywordStruct, KeywordNew, KeywordAs, + KeywordTrue, + KeywordFalse, Indent, Dedent, @@ -109,7 +114,7 @@ pub struct Token { pub loc: Loc, } -pub struct Tokenizer { +pub struct Tokenizer<'a> { source: Vec, tokens: Vec, indent_stack: Vec, @@ -117,10 +122,15 @@ pub struct Tokenizer { start: usize, current: usize, loc: Loc, + included_paths: &'a mut HashSet, } -impl Tokenizer { - pub fn new(filename: String, source: String) -> Tokenizer { +impl<'a> Tokenizer<'a> { + pub fn new( + filename: String, + source: String, + included_paths: &'a mut HashSet, + ) -> Tokenizer<'a> { Tokenizer { source: source.chars().collect(), tokens: vec![], @@ -133,6 +143,7 @@ impl Tokenizer { line: 1, column: 1, }, + included_paths, } } @@ -357,6 +368,9 @@ impl Tokenizer { self.advance(); } } else { + if self.source[self.current - 1] == '0' && self.peek().is_ascii_digit() { + return error!(self.loc, "octal literals are not allowed"); + } while self.peek().is_ascii_digit() { self.advance(); } @@ -406,8 +420,8 @@ impl Tokenizer { "struct" => TokenType::KeywordStruct, "new" => TokenType::KeywordNew, "as" => TokenType::KeywordAs, - "true" => TokenType::True, - "false" => TokenType::False, + "true" => TokenType::KeywordTrue, + "false" => TokenType::KeywordFalse, _ => TokenType::Identifier, }) } @@ -437,8 +451,18 @@ impl Tokenizer { self.include_file(path) } - // TODO: circular includes lead to "fatal runtime error: stack overflow, aborting" - pub fn include_file(&mut self, path: String) -> Result<(), ZernError> { + fn include_file(&mut self, path: String) -> Result<(), ZernError> { + let canonical = match fs::canonicalize(&path) { + Ok(p) => p, + Err(e) => { + return error!(self.loc, format!("failed to resolve {}: {}", path, e)); + } + }; + + if !self.included_paths.insert(canonical) { + return Ok(()); + } + let source = match fs::read_to_string(&path) { Ok(x) => x, Err(_) => { @@ -448,7 +472,7 @@ impl Tokenizer { let filename = Path::new(&path).file_name().unwrap().to_str().unwrap(); - let tokenizer = Tokenizer::new(filename.to_owned(), source); + let tokenizer = Tokenizer::new(filename.to_owned(), source, &mut *self.included_paths); self.tokens.extend(tokenizer.tokenize()?); self.tokens.pop(); // remove inner Eof diff --git a/src/typechecker.rs b/src/typechecker.rs index d667b8d..372c846 100644 --- a/src/typechecker.rs +++ b/src/typechecker.rs @@ -428,8 +428,8 @@ impl<'a> TypeChecker<'a> { TokenType::FloatLiteral => Ok("f64".into()), TokenType::CharLiteral => Ok("u8".into()), TokenType::StringLiteral => Ok("str".into()), - TokenType::True => Ok("bool".into()), - TokenType::False => Ok("bool".into()), + TokenType::KeywordTrue => Ok("bool".into()), + TokenType::KeywordFalse => Ok("bool".into()), _ => unreachable!(), }, ExprKind::Unary { op, right } => { diff --git a/std/containers.zr b/std/containers.zr new file mode 100644 index 0000000..97dda67 --- /dev/null +++ b/std/containers.zr @@ -0,0 +1,240 @@ +include "std/mem.zr" + +struct Array + data: ptr + size: i64 + capacity: i64 + +func Array.new_preallocated_and_zeroed[size: i64] : Array + xs := new* Array + if size > 0 + xs->data = mem.alloc(size * 8) + mem.zero(xs->data, size * 8) + xs->size = size + xs->capacity = size + return xs + +func Array.nth[xs: Array, n: i64] : any + if n < 0 || n >= xs->size + panic("Array.nth out of bounds") + return mem.read64(xs->data + n * 8) + +func Array.set[xs: Array, n: i64, x: any] : void + if n < 0 || n >= xs->size + panic("Array.set out of bounds") + mem.write64(xs->data + n * 8, x) + +func Array.push[xs: Array, x: any] : void + if xs->size == xs->capacity + new_capacity := 4 + if xs->capacity != 0 + new_capacity = xs->capacity * 2 + xs->data = xs->data->realloc(new_capacity * 8) + xs->capacity = new_capacity + + mem.write64(xs->data + xs->size * 8, x) + xs->size += 1 + +func Array.free[xs: Array] : void + if xs->data != 0 + xs->data->free() + mem.free(xs) + +func Array.free_with_items[xs: Array] : void + if xs->data != 0 + for i in 0..xs->size + mem.free(xs->nth(i)) + xs->data->free() + mem.free(xs) + +func Array.pop[xs: Array] : any + if xs->size == 0 + panic("Array.pop on empty array") + x : any = Array.last(xs) + xs->size = xs->size - 1 + return x + +func Array.last[xs: Array] : any + if xs->size == 0 + panic("Array.last on empty array") + return xs->nth(xs->size - 1) + +func Array.slice[xs: Array, start: i64, length: i64] : Array + if start < 0 || length < 0 || start + length > xs->size + panic("Array.slice out of bounds") + + new_array := Array.new_preallocated_and_zeroed(length) + mem.copy(xs->data + start * 8, new_array->data, length * 8) + return new_array + +func Array.concat[a: Array, b: Array] : Array + new_array := Array.new_preallocated_and_zeroed(a->size + b->size) + mem.copy(a->data, new_array->data, a->size * 8) + mem.copy(b->data, new_array->data + a->size * 8, b->size * 8) + return new_array + +func Array.quicksort[arr: Array] : void + arr->_do_quicksort(0, arr->size - 1) + +func Array._do_quicksort[arr: Array, low: i64, high: i64] : void + if low < high + i := arr->_partition(low, high) + arr->_do_quicksort(low, i - 1) + arr->_do_quicksort(i + 1, high) + +func Array._partition[arr: Array, low: i64, high: i64] : i64 + pivot : i64 = arr->nth(high) + i := low - 1 + for j in (low)..high + if arr->nth(j) as i64 <= pivot + i += 1 + temp : i64 = arr->nth(i) + arr->set(i, arr->nth(j)) + arr->set(j, temp) + temp : i64 = arr->nth(i + 1) + arr->set(i + 1, arr->nth(high)) + arr->set(high, temp) + return i + 1 + +func Array.contains_str[arr: Array, s: str] : bool + for i in 0..arr->size + if (arr->nth(i) as str)->equal(s) + return true + return false + +func Array.count[arr: Array, item: any] : i64 + count := 0 + for i in 0..arr->size + if arr->nth(i) == item + count += 1 + return count + +func Array.map[arr: Array, fn: ptr] : Array + out := Array.new_preallocated_and_zeroed(arr->size) + for i in 0..arr->size + out->set(i, fn(arr->nth(i))) + return out + +func Array.filter[arr: Array, fn: ptr] : Array + out := [] + for i in 0..arr->size + if fn(arr->nth(i)) + out->push(arr->nth(i)) + return out + +func Array.reduce[arr: Array, fn: ptr, acc: any] : any + for i in 0..arr->size + acc = fn(acc, arr->nth(i)) + return acc + +func str.split[haystack: str, needle: str]: Array + haystack_len := haystack->len() + needle_len := needle->len() + result := [] + + if !needle_len + if !haystack_len + return result + else + for i in 0..haystack_len + result->push(haystack->substr(i, 1)) + return result + + start := 0 + i := 0 + while i < haystack_len + if i <= haystack_len - needle_len + match := true + for j in 0..needle_len + if haystack[i + j] != needle[j] + match = false + break + if match + result->push(haystack->substr(start, i - start)) + start = i + needle_len + i += needle_len + continue + i += 1 + + result->push(haystack->substr(start, haystack_len - start)) + return result + +const HashMap._TABLE_SIZE = 100 + +struct HashMap + table: Array + +struct HashMap._Node + key: str + value: any + next: HashMap._Node + +func HashMap.new[] : HashMap + map := new* HashMap + map->table = Array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE) + return map + +func HashMap.insert[map: HashMap, key: str, value: any] : void + index := HashMap._djb2(key) + current : HashMap._Node = map->table->nth(index) + + while current as ptr + if current->key->equal(key) + current->value = value + return + current = current->next + + new_node := new* HashMap._Node + new_node->key = key->make_copy() + new_node->value = value + new_node->next = map->table->nth(index) + map->table->set(index, new_node) + +func HashMap.get[map: HashMap, key: str] : any, bool + index := HashMap._djb2(key) + current : HashMap._Node = map->table->nth(index) + + while current as ptr + if current->key->equal(key) + return current->value, true + current = current->next + + return 0 as any, false + +func HashMap.delete[map: HashMap, key: str] : void + index := HashMap._djb2(key) + current : HashMap._Node = map->table->nth(index) + prev := 0 as HashMap._Node + + while current as ptr + if current->key->equal(key) + if prev as ptr + prev->next = current->next + else + map->table->set(index, current->next) + + current->key->free() + mem.free(current) + return + + prev = current + current = current->next + +func HashMap._djb2[key: str] : i64 + hash := 5381 + for i in 0..key->len() + hash = ((hash << 5) + hash) + key[i] + hash = (hash & 0x7fffffffffffffff) // prevent negative + return hash % HashMap._TABLE_SIZE + +func HashMap.free[map: HashMap] : void + for i in 0..HashMap._TABLE_SIZE + current : HashMap._Node = map->table->nth(i) + while current as ptr + tmp := current + current = current->next + tmp->key->free() + mem.free(tmp) + + map->table->free() + mem.free(map) diff --git a/std/core.zr b/std/core.zr new file mode 100644 index 0000000..bf89977 --- /dev/null +++ b/std/core.zr @@ -0,0 +1,14 @@ +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) diff --git a/std/io.zr b/std/io.zr new file mode 100644 index 0000000..6f4e6ea --- /dev/null +++ b/std/io.zr @@ -0,0 +1,143 @@ +include "std/str.zr" + +func io.printf[..] : void + s := _var_arg(0) as ptr + i := 1 + + while s[0] + if s[0] == '%' + s += 1 + + if s[0] == 'd' + io.print_i64(_var_arg(i) as i64) + i += 1 + else if s[0] == 'x' + io.print_i64_hex(_var_arg(i) as i64) + i += 1 + else if s[0] == 's' + io.print(_var_arg(i) as str) + i += 1 + else if s[0] == 'c' + io.print_char(_var_arg(i) as u8) + i += 1 + else if s[0] == 'f' + io.print_f64(_var_arg(i) as i64) + i += 1 + else if s[0] == '%' + io.print_char('%') + else if s[0] == 0 + break + else + panic("io.printf: unrecognized format") + else + io.print_char(s[0]) + s += 1 + +func io.print_sized[x: ptr, size: i64] : void + _builtin_syscall(SYS_write, STDOUT_FILENO, x, size) + +func io.print[x: str] : void + io.print_sized(x as ptr, x->len()) + +func io.println[x: str] : void + io.print(x) + io.print("\n") + +func io.print_char[x: u8] : void + io.print_sized(^x, 1) + +func io.print_bool[x: bool] : void + if x + io.print("true") + else + io.print("false") + +func io.print_i64[x: i64] : void + s := _stackalloc(21) + x->to_str_buf(s) + io.print(s as str) + +func io.print_i64_hex[x: i64] : void + s := _stackalloc(17) + x->to_hex_str_buf(s) + io.print(s as str) + +func io.println_i64[x: i64] : void + s := _stackalloc(21) + x->to_str_buf(s) + io.println(s as str) + +// TODO: fix when we implement f64 params +func io.print_f64[x: i64] : void + s := _stackalloc(64) + f64.to_str_buf(x, s) + io.print(s as str) + +func io.read_char[] : u8 + c := 0 as u8 + _builtin_syscall(SYS_read, STDIN_FILENO, ^c, 1) + return c + +func io.read_line[] : str + b := new str.Builder + while true + c := io.read_char() + if c == '\n' + break + b->append_char(c) + s := b->build() + b->destroy() + return s + +func io.read_text_file[path: str] : str, bool + fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0) + if fd < 0 + return 0 as str, false + + size := _builtin_syscall(SYS_lseek, fd, 0, SEEK_END) + _builtin_syscall(SYS_lseek, fd, 0, SEEK_SET) + + buffer := mem.alloc(size + 1) + n := _builtin_syscall(SYS_read, fd, buffer, size) + _builtin_syscall(SYS_close, fd) + + if n != size + buffer->free() + return 0 as str, false + + buffer[n] = 0 + return buffer as str, true + +func io.read_binary_file[path: str] : Blob, bool + fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0) + if fd < 0 + return 0 as Blob, false + + buf := new* Blob + buf->size = _builtin_syscall(SYS_lseek, fd, 0, SEEK_END) + _builtin_syscall(SYS_lseek, fd, 0, SEEK_SET) + + buf->data = mem.alloc(buf->size) + + n := _builtin_syscall(SYS_read, fd, buf->data, buf->size) + _builtin_syscall(SYS_close, fd) + + if n != buf->size + buf->free() + return 0 as Blob, false + + return buf, true + +func io.write_file[path: str, content: str] : bool + return io.write_binary_file(path, content as ptr, content->len()) + +func io.write_binary_file[path: str, content: ptr, size: i64] : bool + fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_WRONLY|O_CREAT|O_TRUNC, math.octal_to_decimal(644)) + if fd < 0 + return false + + n := _builtin_syscall(SYS_write, fd, content, size) + _builtin_syscall(SYS_close, fd) + if n != size + return false + return true diff --git a/std/linux.zr b/std/linux_syscalls.zr similarity index 99% rename from std/linux.zr rename to std/linux_syscalls.zr index 4b12050..0949488 100644 --- a/std/linux.zr +++ b/std/linux_syscalls.zr @@ -1,5 +1,3 @@ -include "std/posix.zr" - const SYS_read = 0 const SYS_write = 1 const SYS_open = 2 diff --git a/std/mem.zr b/std/mem.zr new file mode 100644 index 0000000..972d1e4 --- /dev/null +++ b/std/mem.zr @@ -0,0 +1,240 @@ +include "std/linux_syscalls.zr" +include "std/num.zr" + +const MEM_BLOCK_SIZE = 32 + +struct mem.Block + size: i64 + free: bool + next: mem.Block + prev: mem.Block + +func mem.align[x: i64] : i64 + return (x + 7) & -8 + +func mem.Block._split[blk: mem.Block, needed: i64] : void + if blk->size >= needed + MEM_BLOCK_SIZE + 8 + new_blk := (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block + new_blk->size = blk->size - needed - MEM_BLOCK_SIZE + new_blk->free = true + new_blk->next = blk->next + new_blk->prev = blk + + if blk->next as ptr + blk->next->prev = new_blk + else + mem.write64(_builtin_heap_tail(), new_blk) + + blk->size = needed + blk->next = new_blk + +func mem._request_space[size: i64] : mem.Block + needed := size + MEM_BLOCK_SIZE + 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 + panic("mem._request_space: failed to mmap") + + blk->size = alloc_size - MEM_BLOCK_SIZE + blk->free = false + blk->next = 0 as mem.Block + blk->prev = 0 as mem.Block + + tail := mem.read64(_builtin_heap_tail()) as mem.Block + if tail as ptr + tail->next = blk + blk->prev = tail + + mem.write64(_builtin_heap_tail(), blk) + return blk + +func mem.alloc[size: i64] : ptr + if size <= 0 + panic("mem.alloc: called with nonpositive size") + + size = mem.align(size) + + cur := mem.read64(_builtin_heap_head()) as mem.Block + while cur as ptr + if cur->free && cur->size >= size + cur->_split(size) + cur->free = false + return cur as ptr + MEM_BLOCK_SIZE + cur = cur->next + + blk := mem._request_space(size) + + if !mem.read64(_builtin_heap_head()) + mem.write64(_builtin_heap_head(), blk) + + blk->_split(size) + + return blk as ptr + MEM_BLOCK_SIZE + +func mem.free[x: any] : void + ptr.free(x as ptr) + +func ptr.free[x: ptr] : void + if x == 0 + return + + blk := (x - MEM_BLOCK_SIZE) as mem.Block + if blk->free + return + + blk->free = true + + next := blk->next + if next as ptr && next->free + expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block + if expected_next as ptr == next as ptr + blk->size += MEM_BLOCK_SIZE + next->size + blk->next = next->next + if next->next as ptr + next->next->prev = blk + if mem.read64(_builtin_heap_tail()) == next as ptr + mem.write64(_builtin_heap_tail(), blk) + + prev := blk->prev + if prev as ptr && prev->free + expected_blk := (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block + if expected_blk as ptr == blk as ptr + prev->size += MEM_BLOCK_SIZE + blk->size + prev->next = blk->next + if blk->next as ptr + blk->next->prev = prev + if mem.read64(_builtin_heap_tail()) == blk as ptr + mem.write64(_builtin_heap_tail(), prev) + blk = prev + + block_total := blk->size + MEM_BLOCK_SIZE + if (blk as i64 & 4095) == 0 && (block_total & 4095) == 0 + if blk->prev as ptr + blk->prev->next = blk->next + else + mem.write64(_builtin_heap_head(), blk->next) + if blk->next as ptr + blk->next->prev = blk->prev + else + mem.write64(_builtin_heap_tail(), blk->prev) + _builtin_syscall(SYS_munmap, blk, block_total) + +func ptr.realloc[x: ptr, new_size: i64] : ptr + if !x + return mem.alloc(new_size) + + if new_size < 0 + panic("mem.realloc: called with negative new_size") + + if new_size == 0 + x->free() + return 0 as ptr + + new_size = mem.align(new_size) + + blk := (x - MEM_BLOCK_SIZE) as mem.Block + if blk->size >= new_size + blk->_split(new_size) + return x + + next := blk->next + expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block + + if next as ptr && next->free && expected_next as ptr == next as ptr + combined := blk->size + MEM_BLOCK_SIZE + next->size + if combined >= new_size + blk->size = combined + blk->next = next->next + if next->next as ptr + next->next->prev = blk + if mem.read64(_builtin_heap_tail()) == next as ptr + mem.write64(_builtin_heap_tail(), blk) + blk->_split(new_size) + return x + + new_ptr := mem.alloc(new_size) + + mem.copy(x, new_ptr, math.min(blk->size, new_size)) + x->free() + return new_ptr + +func ptr.zero_and_free[x: ptr, size: i64] : void + mem.zero(x, size) + x->free() + +func mem.zero[x: ptr, size: i64] : void + i := 0 + while i + 8 <= size + mem.write64(x + i, 0) + i += 8 + while i < size + x[i] = 0 as u8 + i += 1 + +func mem.copy[src: ptr, dst: ptr, n: i64] : void + if dst > src && dst < src + n + i := n + while i - 8 >= 0 + i -= 8 + mem.write64(dst + i, mem.read64(src + i)) + while i > 0 + i -= 1 + dst[i] = src[i] + else + i := 0 + while i + 8 <= n + mem.write64(dst + i, mem.read64(src + i)) + i += 8 + while i < n + dst[i] = src[i] + i += 1 + +func mem.read8[x: ptr] : u8 + return x[0] + +func mem.read16[x: ptr] : i64 + return x[0] as i64 | (x[1] << 8) + +func mem.read16be[x: ptr] : i64 + return (x[0] << 8) as i64 | x[1] + +func mem.read32[x: ptr] : i64 + return x[0] as i64 | (x[1] << 8) | (x[2] << 16) | (x[3] << 24) + +func mem.read32be[x: ptr] : i64 + return (x[0] as i64 << 24) | (x[1] << 16) | (x[2] << 8) | x[3] + +func mem.read64[x: ptr] : i64 + return _builtin_read64(x) + +func mem.write32[x: ptr, d: i64] : void + x[0] = d & 0xff + x[1] = (d >> 8) & 0xff + x[2] = (d >> 16) & 0xff + x[3] = (d >> 24) & 0xff + +func mem.write16[x: ptr, d: i64] : void + x[0] = d & 0xff + x[1] = (d >> 8) & 0xff + +func mem.write16be[x: ptr, d: i64] : void + x[0] = (d >> 8) & 0xff + x[1] = d & 0xff + +func mem.write64[x: ptr, d: any] : void + _builtin_set64(x, d) + +struct Blob + data: ptr + size: i64 + +func Blob.alloc[size: i64] : Blob + buffer := new* Blob + buffer->size = size + buffer->data = mem.alloc(size) + return buffer + +func Blob.free[buf: Blob] : void + buf->data->free() + mem.free(buf) diff --git a/std/net.zr b/std/net.zr index 582cf07..c53aa13 100644 --- a/std/net.zr +++ b/std/net.zr @@ -1,3 +1,5 @@ +include "std/os.zr" + const AF_INET = 2 const SOCK_STREAM = 1 const SOCK_DGRAM = 2 diff --git a/std/num.zr b/std/num.zr new file mode 100644 index 0000000..5a45023 --- /dev/null +++ b/std/num.zr @@ -0,0 +1,210 @@ +include "std/core.zr" + +func i64.abs[n: i64] : i64 + if n == -9223372036854775808 + panic("MIN_I64 passed to math.abs") + if n < 0 + return -n + return n + +func i64.sign[n: i64] : i64 + if n < 0 + return -1 + else if n > 0 + return 1 + else + return 0 + +func i64.isqrt[n: i64] : i64 + if n < 0 + panic("negative number passed to math.isqrt") + if n == 0 || n == 1 + return n + + guess := n + next_guess := (guess + n / guess) / 2 + + while next_guess < guess + guess = next_guess + next_guess = (guess + n / guess) / 2 + + return guess + +func i64.is_prime[n: i64]: bool + if n <= 1 + return false + if n == 2 || n == 3 + return true + if n % 2 == 0 || n % 3 == 0 + return false + + i := 5 + while i * i <= n + if n % i == 0 || n % (i + 2) == 0 + return false + i += 6 + return true + +func i64.to_str_buf[n: i64, buf: ptr] : void + if n == 0 + buf[0] = '0' + buf[1] = 0 + return + + neg : bool = n < 0 + if neg + // MIN_I64 will fail but i so dont care + n = -n + tmp := _stackalloc(21) + end := 20 + tmp[end] = 0 + end -= 1 + for i in 0..19 + if n == 0 + break + tmp[end] = '0' + (n % 10) + n = n / 10 + end -= 1 + if neg + tmp[end] = '-' + end -= 1 + len := 19 - end + for i in 0..len + buf[i] = tmp[end + 1 + i] + buf[len] = 0 + +func i64.to_hex_str_buf[n: i64, buf: ptr] : void + hex_chars := "0123456789abcdef" + + if n == 0 + buf[0] = '0' + buf[1] = 0 + return + + mask := (1 << 60) - 1 + tmp := _stackalloc(17) + len := 0 + + for i in 0..16 + if n == 0 + break + tmp[len] = hex_chars[n & 15] + n = (n >> 4) & mask + len += 1 + + for j in 0..len + buf[j] = tmp[len - 1 - j] + + buf[len] = 0 + +func f64.to_str_buf[x: i64, buf: ptr] : void + bits := x + sign := (bits >> 63) & 1 + exp := (bits >> 52) & 0x7ff + mant := bits & 0x000fffffffffffff + + if exp == 0x7ff + panic("invalid float") + + p := 0 + if sign + buf[p] = '-' + p += 1 + bits = bits & 0x7fffffffffffffff + + if exp == 0 && mant == 0 + buf[p] = '0' + buf[p+1] = '.' + buf[p+2] = '0' + buf[p+3] = 0 + return + + sig := mant + if exp != 0 + sig = sig | 0x0010000000000000 + + shift := exp - 1075 + + if shift >= 0 + (sig << shift)->to_str_buf(buf + p) + while buf[p] + p += 1 + buf[p] = '.' + buf[p+1] = '0' + buf[p+2] = 0 + return + + ns := -shift + + if ns <= 52 + int_part := sig >> ns + int_part->to_str_buf(buf + p) + while buf[p] + p += 1 + else + buf[p] = '0' + p += 1 + + buf[p] = '.' + p += 1 + + mask := (1 << ns) - 1 + frac := sig & mask + + for i in 0..6 + if frac == 0 + if i > 0 + break + buf[p] = '0' + p += 1 + break + frac = frac * 10 + digit := frac >> ns + buf[p] = '0' + digit as u8 + p += 1 + frac = frac & mask + buf[p] = 0 + +func math.gcd[a: i64, b: i64] : i64 + a = a->abs() + b = b->abs() + while b != 0 + tmp := b + b = a % b + a = tmp + return a + +func math.min[a: i64, b: i64] : i64 + if a < b + return a + return b + +func math.max[a: i64, b: i64] : i64 + if a > b + return a + return b + +func math.pow[b: i64, e: i64] : i64 + if e < 0 + panic("negative exponent passed to math.pow") + out := 1 + while e > 0 + if e & 1 + out = out * b + b = b * b + e = e >> 1 + return out + +func math.lcm[a: i64, b: i64] : i64 + return (a / math.gcd(a, b)) * b + +func math.octal_to_decimal[octal: i64] : i64 + decimal := 0 + base := 1 + + while octal > 0 + digit := octal % 10 + decimal += digit * base + base = base * 8 + octal = octal / 10 + return decimal diff --git a/std/os.zr b/std/os.zr new file mode 100644 index 0000000..dbaed96 --- /dev/null +++ b/std/os.zr @@ -0,0 +1,133 @@ +include "std/posix.zr" +include "std/linux_syscalls.zr" +include "std/str.zr" +include "std/containers.zr" + +func os.exit[code: i64] : void + _builtin_syscall(SYS_exit, code) + +func os.getpid[] : i64 + return _builtin_syscall(SYS_getpid) + +func os.getenv[name: str] : str + name_len := name->len() + i := 0 + while true + entry := mem.read64(_builtin_environ() + i * 8) as str + if entry as ptr == 0 + break + if entry->equal_n(name, name_len) && entry[name_len] == '=' + return (entry as ptr + name_len + 1) as str + i += 1 + return "" + +func os.basename[path: str] : str + i := path->len() - 1 + while i >= 0 && path[i] != '/' + i -= 1 + return path->substr(i + 1, path->len() - i - 1) + +struct os.Timespec + tv_sec: i64 + tv_nsec: i64 + +func os.sleep[ms: i64] : void + if ms < 0 + panic("negative time passed to os.sleep") + req := new os.Timespec + req->tv_sec = ms / 1000 + req->tv_nsec = (ms % 1000) * 1000000 + _builtin_syscall(SYS_nanosleep, req, 0) + +func os.time[] : i64 + ts := new os.Timespec + _builtin_syscall(SYS_clock_gettime, CLOCK_REALTIME, ts) + out := ts->tv_sec * 1000 + ts->tv_nsec / 1000000 + return out + +func os.urandom_buf[buf: ptr, n: i64] : void + pos := 0 + while pos < n + ret := _builtin_syscall(SYS_getrandom, buf + pos, n - pos, 0) + if ret <= 0 + panic("os.urandom_buf: syscall failed") + pos += ret + +func os.urandom[n: i64]: ptr + buf := mem.alloc(n) + os.urandom_buf(buf, n) + return buf + +func os.urandom_i64[] : i64 + n := 0 + os.urandom_buf(^n, 8) + return n + +func os.run_shell_command[command: str] : i64, bool + pid := _builtin_syscall(SYS_fork) + if pid < 0 + return -1, false + + if pid == 0 + argv := ["sh", "-c", command, 0] // gets freed after child process exits + _builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ()) + os.exit(1) + else + status := 0 + wp := _builtin_syscall(SYS_wait4, pid, ^status, 0, 0) + if wp < 0 + return -1, false + st := status & 0xffffffff + + if (st & 0x7f) == 0 + return (st >> 8) & 0xff, true + else + return -(st & 0x7f), true + +func os.file_exists[path: str] : bool + return _builtin_syscall(SYS_faccessat, AT_FDCWD, path, F_OK, 0) == 0 + +func os.is_a_directory[path: str] : 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 false + + return (mem.read32(st + 24) & S_IFMT) == S_IFDIR + +func os.list_directory[path: str] : Array, bool + fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0) + if fd < 0 + return 0 as Array, false + + files := [] + buf := _stackalloc(1024) + while true + n := _builtin_syscall(SYS_getdents64, fd, buf, 1024) + if n < 0 + files->free_with_items() + _builtin_syscall(SYS_close, fd) + return 0 as Array, false + else if n == 0 + break + + pos := 0 + while pos < n + len := mem.read16(buf + pos + 16) + name : ptr = buf + pos + 19 + if name[0] + skip := false + // skip if name is exactly '.' or '..' + if name[0] == '.' + if name[1] == 0 + skip = true + else if name[1] == '.' + if name[2] == 0 + skip = true + if !skip + files->push((name as str)->make_copy()) + pos += len + + _builtin_syscall(SYS_close, fd) + return files, true diff --git a/std/std.zr b/std/std.zr deleted file mode 100644 index 6f6ed0c..0000000 --- a/std/std.zr +++ /dev/null @@ -1,1251 +0,0 @@ -include "std/linux.zr" - -func panic[msg: str] : void - io.print("PANIC: ") - io.println(msg) - // for gdb backtrace - _builtin_syscall(SYS_kill, os.getpid(), SIGABRT) - os.exit(1) - -const MEM_BLOCK_SIZE = 32 - -struct mem.Block - size: i64 - free: bool - next: mem.Block - prev: mem.Block - -func mem.align[x: i64] : i64 - return (x + 7) & -8 - -func mem.Block._split[blk: mem.Block, needed: i64] : void - if blk->size >= needed + MEM_BLOCK_SIZE + 8 - new_blk := (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block - new_blk->size = blk->size - needed - MEM_BLOCK_SIZE - new_blk->free = true - new_blk->next = blk->next - new_blk->prev = blk - - if blk->next as ptr - blk->next->prev = new_blk - else - mem.write64(_builtin_heap_tail(), new_blk) - - blk->size = needed - blk->next = new_blk - -func mem._request_space[size: i64] : mem.Block - needed := size + MEM_BLOCK_SIZE - 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 - panic("mem._request_space: failed to mmap") - - blk->size = alloc_size - MEM_BLOCK_SIZE - blk->free = false - blk->next = 0 as mem.Block - blk->prev = 0 as mem.Block - - tail := mem.read64(_builtin_heap_tail()) as mem.Block - if tail as ptr - tail->next = blk - blk->prev = tail - - mem.write64(_builtin_heap_tail(), blk) - return blk - -func mem.alloc[size: i64] : ptr - if size <= 0 - panic("mem.alloc: called with nonpositive size") - - size = mem.align(size) - - cur := mem.read64(_builtin_heap_head()) as mem.Block - while cur as ptr - if cur->free && cur->size >= size - cur->_split(size) - cur->free = false - return cur as ptr + MEM_BLOCK_SIZE - cur = cur->next - - blk := mem._request_space(size) - - if !mem.read64(_builtin_heap_head()) - mem.write64(_builtin_heap_head(), blk) - - blk->_split(size) - - return blk as ptr + MEM_BLOCK_SIZE - -func mem.free[x: any] : void - ptr.free(x as ptr) - -func ptr.free[x: ptr] : void - if x == 0 - return - - blk := (x - MEM_BLOCK_SIZE) as mem.Block - if blk->free - return - - blk->free = true - - next := blk->next - if next as ptr && next->free - expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block - if expected_next as ptr == next as ptr - blk->size += MEM_BLOCK_SIZE + next->size - blk->next = next->next - if next->next as ptr - next->next->prev = blk - if mem.read64(_builtin_heap_tail()) == next as ptr - mem.write64(_builtin_heap_tail(), blk) - - prev := blk->prev - if prev as ptr && prev->free - expected_blk := (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block - if expected_blk as ptr == blk as ptr - prev->size += MEM_BLOCK_SIZE + blk->size - prev->next = blk->next - if blk->next as ptr - blk->next->prev = prev - if mem.read64(_builtin_heap_tail()) == blk as ptr - mem.write64(_builtin_heap_tail(), prev) - blk = prev - - block_total := blk->size + MEM_BLOCK_SIZE - if (blk as i64 & 4095) == 0 && (block_total & 4095) == 0 - if blk->prev as ptr - blk->prev->next = blk->next - else - mem.write64(_builtin_heap_head(), blk->next) - if blk->next as ptr - blk->next->prev = blk->prev - else - mem.write64(_builtin_heap_tail(), blk->prev) - _builtin_syscall(SYS_munmap, blk, block_total) - -func ptr.realloc[x: ptr, new_size: i64] : ptr - if !x - return mem.alloc(new_size) - - if new_size < 0 - panic("mem.realloc: called with negative new_size") - - if new_size == 0 - x->free() - return 0 as ptr - - new_size = mem.align(new_size) - - blk := (x - MEM_BLOCK_SIZE) as mem.Block - if blk->size >= new_size - blk->_split(new_size) - return x - - next := blk->next - expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block - - if next as ptr && next->free && expected_next as ptr == next as ptr - combined := blk->size + MEM_BLOCK_SIZE + next->size - if combined >= new_size - blk->size = combined - blk->next = next->next - if next->next as ptr - next->next->prev = blk - if mem.read64(_builtin_heap_tail()) == next as ptr - mem.write64(_builtin_heap_tail(), blk) - blk->_split(new_size) - return x - - new_ptr := mem.alloc(new_size) - - mem.copy(x, new_ptr, math.min(blk->size, new_size)) - x->free() - return new_ptr - -func ptr.zero_and_free[x: ptr, size: i64] : void - mem.zero(x, size) - x->free() - -func mem.zero[x: ptr, size: i64] : void - i := 0 - while i + 8 <= size - mem.write64(x + i, 0) - i += 8 - while i < size - x[i] = 0 as u8 - i += 1 - -func mem.copy[src: ptr, dst: ptr, n: i64] : void - if dst > src && dst < src + n - i := n - while i - 8 >= 0 - i -= 8 - mem.write64(dst + i, mem.read64(src + i)) - while i > 0 - i -= 1 - dst[i] = src[i] - else - i := 0 - while i + 8 <= n - mem.write64(dst + i, mem.read64(src + i)) - i += 8 - while i < n - dst[i] = src[i] - i += 1 - -func mem.read8[x: ptr] : u8 - return x[0] - -func mem.read16[x: ptr] : i64 - return x[0] as i64 | (x[1] << 8) - -func mem.read16be[x: ptr] : i64 - return (x[0] << 8) as i64 | x[1] - -func mem.read32[x: ptr] : i64 - return x[0] as i64 | (x[1] << 8) | (x[2] << 16) | (x[3] << 24) - -func mem.read32be[x: ptr] : i64 - return (x[0] as i64 << 24) | (x[1] << 16) | (x[2] << 8) | x[3] - -func mem.read64[x: ptr] : i64 - return _builtin_read64(x) - -func mem.write32[x: ptr, d: i64] : void - x[0] = d & 0xff - x[1] = (d >> 8) & 0xff - x[2] = (d >> 16) & 0xff - x[3] = (d >> 24) & 0xff - -func mem.write16[x: ptr, d: i64] : void - x[0] = d & 0xff - x[1] = (d >> 8) & 0xff - -func mem.write16be[x: ptr, d: i64] : void - x[0] = (d >> 8) & 0xff - x[1] = d & 0xff - -func mem.write64[x: ptr, d: any] : void - _builtin_set64(x, d) - -func io.printf[..] : void - s := _var_arg(0) as ptr - i := 1 - - while s[0] - if s[0] == '%' - s += 1 - - if s[0] == 'd' - io.print_i64(_var_arg(i) as i64) - i += 1 - else if s[0] == 'x' - io.print_i64_hex(_var_arg(i) as i64) - i += 1 - else if s[0] == 's' - io.print(_var_arg(i) as str) - i += 1 - else if s[0] == 'c' - io.print_char(_var_arg(i) as u8) - i += 1 - else if s[0] == 'f' - io.print_f64(_var_arg(i) as i64) - i += 1 - else if s[0] == '%' - io.print_char('%') - else if s[0] == 0 - break - else - panic("io.printf: unrecognized format") - else - io.print_char(s[0]) - s += 1 - -func io.print_sized[x: ptr, size: i64] : void - _builtin_syscall(SYS_write, STDOUT_FILENO, x, size) - -func io.print[x: str] : void - io.print_sized(x as ptr, x->len()) - -func io.println[x: str] : void - io.print(x) - io.print("\n") - -func io.print_char[x: u8] : void - io.print_sized(^x, 1) - -func io.print_bool[x: bool] : void - if x - io.print("true") - else - io.print("false") - -func io.print_i64[x: i64] : void - s := _stackalloc(21) - x->to_str_buf(s) - io.print(s as str) - -func io.print_i64_hex[x: i64] : void - s := _stackalloc(17) - x->to_hex_str_buf(s) - io.print(s as str) - -func io.println_i64[x: i64] : void - s := _stackalloc(21) - x->to_str_buf(s) - io.println(s as str) - -// TODO: fix when we implement f64 params -func io.print_f64[x: i64] : void - s := _stackalloc(64) - f64.to_str_buf(x, s) - io.print(s as str) - -func f64.to_str_buf[x: i64, buf: ptr] : void - bits := x - sign := (bits >> 63) & 1 - exp := (bits >> 52) & 0x7ff - mant := bits & 0x000fffffffffffff - - if exp == 0x7ff - if mant == 0 - if sign - mem.copy("-inf" as ptr, buf, 5) - else - mem.copy("inf" as ptr, buf, 4) - else - mem.copy("nan" as ptr, buf, 4) - return - - p := 0 - if sign - buf[p] = '-' - p += 1 - bits = bits & 0x7fffffffffffffff - - if exp == 0 && mant == 0 - buf[p] = '0' - buf[p+1] = '.' - buf[p+2] = '0' - buf[p+3] = 0 - return - - sig := mant - if exp != 0 - sig = sig | 0x0010000000000000 - - shift := exp - 1075 - - if shift >= 0 - (sig << shift)->to_str_buf(buf + p) - while buf[p] - p += 1 - buf[p] = '.' - buf[p+1] = '0' - buf[p+2] = 0 - return - - ns := -shift - - if ns <= 52 - int_part := sig >> ns - int_part->to_str_buf(buf + p) - while buf[p] - p += 1 - else - buf[p] = '0' - p += 1 - - buf[p] = '.' - p += 1 - - mask := (1 << ns) - 1 - frac := sig & mask - - for i in 0..6 - if frac == 0 - if i > 0 - break - buf[p] = '0' - p += 1 - break - frac = frac * 10 - digit := frac >> ns - buf[p] = '0' + digit as u8 - p += 1 - frac = frac & mask - buf[p] = 0 - -func io.read_char[] : u8 - c := 0 as u8 - _builtin_syscall(SYS_read, STDIN_FILENO, ^c, 1) - return c - -func io.read_line[] : str - b := new str.Builder - while true - c := io.read_char() - if c == '\n' - break - b->append_char(c) - s := b->build() - b->destroy() - return s - -func io.read_text_file[path: str] : str, bool - fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0) - if fd < 0 - return 0 as str, false - - size := _builtin_syscall(SYS_lseek, fd, 0, SEEK_END) - _builtin_syscall(SYS_lseek, fd, 0, SEEK_SET) - - buffer := mem.alloc(size + 1) - n := _builtin_syscall(SYS_read, fd, buffer, size) - _builtin_syscall(SYS_close, fd) - - if n != size - buffer->free() - return 0 as str, false - - buffer[n] = 0 - return buffer as str, true - -func io.read_binary_file[path: str] : Blob, bool - fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0) - if fd < 0 - return 0 as Blob, false - - buf := new* Blob - buf->size = _builtin_syscall(SYS_lseek, fd, 0, SEEK_END) - _builtin_syscall(SYS_lseek, fd, 0, SEEK_SET) - - buf->data = mem.alloc(buf->size) - - n := _builtin_syscall(SYS_read, fd, buf->data, buf->size) - _builtin_syscall(SYS_close, fd) - - if n != buf->size - buf->free() - return 0 as Blob, false - - return buf, true - -func io.write_file[path: str, content: str] : bool - return io.write_binary_file(path, content as ptr, content->len()) - -func io.write_binary_file[path: str, content: ptr, size: i64] : bool - fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_WRONLY|O_CREAT|O_TRUNC, math.octal_to_decimal(644)) - if fd < 0 - return false - - n := _builtin_syscall(SYS_write, fd, content, size) - _builtin_syscall(SYS_close, fd) - if n != size - return false - return true - -func str.free[s: str] : void - mem.free(s) - -func str.len[s: str] : i64 - i := 0 - while s[i] - i += 1 - return i - -func str.make_copy[s: str] : str - size := s->len() + 1 - dup := mem.alloc(size) as str - mem.copy(s as ptr, dup as ptr, size) - return dup - -func str.equal[a: str, b: str] : bool - i := 0 - while a[i] != 0 && b[i] != 0 - if a[i] != b[i] - return false - i += 1 - return a[i] == b[i] - -func str.format_into[..] : i64 - buf := _var_arg(0) as ptr - size := _var_arg(1) as i64 - if size <= 0 - return 0 - s := _var_arg(2) as ptr - i := 3 - n := 0 - - tmp := _stackalloc(64) as str - - while s[0] - if s[0] == '%' - s += 1 - - if s[0] == 'd' - (_var_arg(i) as i64)->to_str_buf(tmp as ptr) - tmp_len := tmp->len() - remaining := size - n - 1 - mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) - n += tmp_len - i += 1 - else if s[0] == 'x' - (_var_arg(i) as i64)->to_hex_str_buf(tmp as ptr) - tmp_len := tmp->len() - remaining := size - n - 1 - mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) - n += tmp_len - i += 1 - else if s[0] == 's' - tmp_str := _var_arg(i) as str - tmp_len := tmp_str->len() - remaining := size - n - 1 - mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining)) - n += tmp_len - i += 1 - else if s[0] == 'c' - if n < size - 1 - buf[n] = _var_arg(i) as u8 - n += 1 - i += 1 - else if s[0] == 'f' - // TODO: fix when we implement f64 params - f64.to_str_buf(_var_arg(i), tmp as ptr) - tmp_len := tmp->len() - remaining := size - n - 1 - mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) - n += tmp_len - i += 1 - else if s[0] == '%' - if n < size - 1 - buf[n] = '%' - n += 1 - else if s[0] == 0 - break - else - panic("str.format_into: unrecognized format") - else - if n < size - 1 - buf[n] = s[0] - n += 1 - s += 1 - - if n < size - buf[n] = 0 - else if size > 0 - buf[size - 1] = 0 - return n - -func str.concat[a: str, b: str] : str - a_len := a->len() - b_len := b->len() - out := mem.alloc(a_len + b_len + 1) as str - mem.copy(a as ptr, out as ptr, a_len) - mem.copy(b as ptr, out as ptr + a_len, b_len) - out[a_len + b_len] = 0 - return out - -func str.contains[haystack: str, needle: str] : bool - return haystack->find(needle) != -1 - -func str.find[haystack: str, needle: str] : i64 - haystack_len := haystack->len() - needle_len := needle->len() - - if needle_len == 0 - return 0 - - if needle_len > haystack_len - return -1 - - for i in 0..(haystack_len - needle_len + 1) - if haystack[i] != needle[0] - continue - if needle_len == 1 - return i - match := true - for j in 1..needle_len - if haystack[i + j] != needle[j] - match = false - break - if match - return i - return -1 - -func str.substr[s: str, start: i64, length: i64] : str - out := mem.alloc(length + 1) as str - mem.copy(s as ptr + start, out as ptr, length) - out[length] = 0 - return out - -func str.trim[s: str] : str - len := s->len() - if len == 0 - out := mem.alloc(1) as str - out[0] = 0 - return out - - start := 0 - end := len - 1 - - while start <= end && s[start]->is_whitespace() - start += 1 - - while end >= start && s[end]->is_whitespace() - end -= 1 - - return s->substr(start, end - start + 1) - -func str.split[haystack: str, needle: str]: Array - haystack_len := haystack->len() - needle_len := needle->len() - result := [] - - if !needle_len - if !haystack_len - return result - else - for i in 0..haystack_len - result->push(haystack->substr(i, 1)) - return result - - start := 0 - i := 0 - while i < haystack_len - if i <= haystack_len - needle_len - match := true - for j in 0..needle_len - if haystack[i + j] != needle[j] - match = false - break - if match - result->push(haystack->substr(start, i - start)) - start = i + needle_len - i += needle_len - continue - i += 1 - - result->push(haystack->substr(start, haystack_len - start)) - return result - -func str.reverse[s: str] : str - len := s->len() - out := mem.alloc(len + 1) as str - - for i in 0..len - out[i] = s[len - i - 1] - out[len] = 0 - return out - - -func str.parse_i64[s: str] : i64 - len := s->len() - i := 0 - - sign := 1 - if i < len && s[i] == '-' - sign = -1 - i += 1 - - num := 0 - while i < len - d := s[i] - if !d->is_digit() - break - num = num * 10 + (d - '0') - i += 1 - return num * sign - -func str.hex_encode[s: str, s_len: i64] : str - hex_chars := "0123456789abcdef" - out := mem.alloc(s_len * 2 + 1) as str - - for i in 0..s_len - b := s[i] - out[i * 2] = hex_chars[(b >> 4) & 15] - out[i * 2 + 1] = hex_chars[b & 15] - - out[s_len * 2] = 0 - return out - -func str._hex_digit_to_int[d: u8] : u8 - if d->is_digit() - return d - '0' - lower : u8 = d | 32 - if lower >= 'a' && lower <= 'f' - return lower - 'a' + 10 - panic("invalid hex digit passed to str._hex_digit_to_int") - -func str.hex_decode[s: str] : str - s_len := s->len() - if s_len % 2 != 0 - panic("invalid hex string passed to str.hex_decode") - - out_len := s_len / 2 - out := mem.alloc(out_len + 1) as str - - for i in 0..out_len - high := str._hex_digit_to_int(s[i * 2]) - low := str._hex_digit_to_int(s[i * 2 + 1]) - out[i] = (high << 4) | low - - out[out_len] = 0 - return out - -struct str.Builder - data: ptr - size: i64 - capacity: i64 - -func str.Builder._grow[b: str.Builder, needed: i64] : void - if b->size + needed > b->capacity - new_capacity := 64 - if b->capacity != 0 - new_capacity = b->capacity * 2 - while new_capacity < b->size + needed - new_capacity = new_capacity * 2 - b->data = b->data->realloc(new_capacity) - b->capacity = new_capacity - -func str.Builder.append_char[b: str.Builder, c: u8] : void - b->_grow(1) - b->data[b->size] = c - b->size += 1 - -func str.Builder.append[b: str.Builder, s: str] : void - len := s->len() - b->_grow(len) - mem.copy(s as ptr, b->data + b->size, len) - b->size += len - -func str.Builder.build[b: str.Builder] : str - s := mem.alloc(b->size + 1) as str - mem.copy(b->data, s as ptr, b->size) - s[b->size] = 0 - return s - -func str.Builder.destroy[b: str.Builder] : void - if b->data != 0 - b->data->free() - b->size = 0 - b->capacity = 0 - -func u8.is_whitespace[x: u8] : bool - return x == ' ' || x == '\n' || x == '\t' || x == '\r' - -func u8.is_digit[x: u8] : bool - return x >= '0' && x <= '9' - -func u8.is_hex_digit[x: u8] : bool - return (x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F') - -func u8.is_lowercase[x: u8] : bool - return x >= 'a' && x <= 'z' - -func u8.is_uppercase[x: u8] : bool - return x >= 'A' && x <= 'Z' - -func u8.is_letter[x: u8] : bool - return x->is_uppercase() || x->is_lowercase() - -func u8.is_alphanumeric[x: u8] : bool - return x->is_letter() || x->is_digit() - -func u8.to_str[c: u8] : str - s := mem.alloc(2) as str - s[0] = c - s[1] = 0 - return s - -func i64.abs[n: i64] : i64 - if n == -9223372036854775808 - panic("MIN_I64 passed to math.abs") - if n < 0 - return -n - return n - -func i64.sign[n: i64] : i64 - if n < 0 - return -1 - else if n > 0 - return 1 - else - return 0 - -func i64.isqrt[n: i64] : i64 - if n < 0 - panic("negative number passed to math.isqrt") - if n == 0 || n == 1 - return n - - guess := n - next_guess := (guess + n / guess) / 2 - - while next_guess < guess - guess = next_guess - next_guess = (guess + n / guess) / 2 - - return guess - -func i64.is_prime[n: i64]: bool - if n <= 1 - return false - if n == 2 || n == 3 - return true - if n % 2 == 0 || n % 3 == 0 - return false - - i := 5 - while i * i <= n - if n % i == 0 || n % (i + 2) == 0 - return false - i += 6 - return true - -func i64.to_str[n: i64] : str - out := mem.alloc(21) - n->to_str_buf(out) - return out as str - -func i64.to_str_buf[n: i64, buf: ptr] : void - if n == 0 - buf[0] = '0' - buf[1] = 0 - return - - neg : bool = n < 0 - if neg - // MIN_I64 will fail but i so dont care - n = -n - tmp := _stackalloc(21) - end := 20 - tmp[end] = 0 - end -= 1 - for i in 0..19 - if n == 0 - break - tmp[end] = '0' + (n % 10) - n = n / 10 - end -= 1 - if neg - tmp[end] = '-' - end -= 1 - len := 19 - end - for i in 0..len - buf[i] = tmp[end + 1 + i] - buf[len] = 0 - -func i64.to_hex_str[n: i64] : str - out := mem.alloc(17) - n->to_hex_str_buf(out) - return out as str - -func i64.to_hex_str_buf[n: i64, buf: ptr] : void - hex_chars := "0123456789abcdef" - - if n == 0 - buf[0] = '0' - buf[1] = 0 - return - - mask := (1 << 60) - 1 - tmp := _stackalloc(17) - len := 0 - - for i in 0..16 - if n == 0 - break - tmp[len] = hex_chars[n & 15] - n = (n >> 4) & mask - len += 1 - - for j in 0..len - buf[j] = tmp[len - 1 - j] - - buf[len] = 0 - -func math.gcd[a: i64, b: i64] : i64 - a = a->abs() - b = b->abs() - while b != 0 - tmp := b - b = a % b - a = tmp - return a - -func math.min[a: i64, b: i64] : i64 - if a < b - return a - return b - -func math.max[a: i64, b: i64] : i64 - if a > b - return a - return b - -func math.pow[b: i64, e: i64] : i64 - if e < 0 - panic("negative exponent passed to math.pow") - out := 1 - while e > 0 - if e & 1 - out = out * b - b = b * b - e = e >> 1 - return out - -func math.lcm[a: i64, b: i64] : i64 - return (a / math.gcd(a, b)) * b - -func math.octal_to_decimal[octal: i64] : i64 - decimal := 0 - base := 1 - - while octal > 0 - digit := octal % 10 - decimal += digit * base - base = base * 8 - octal = octal / 10 - return decimal - -struct Blob - data: ptr - size: i64 - -func Blob.alloc[size: i64] : Blob - buffer := new* Blob - buffer->size = size - buffer->data = mem.alloc(size) - return buffer - -func Blob.free[buf: Blob] : void - buf->data->free() - mem.free(buf) - -struct Array - data: ptr - size: i64 - capacity: i64 - -func Array.new_preallocated_and_zeroed[size: i64] : Array - xs := new* Array - if size > 0 - xs->data = mem.alloc(size * 8) - mem.zero(xs->data, size * 8) - xs->size = size - xs->capacity = size - return xs - -func Array.nth[xs: Array, n: i64] : any - if n < 0 || n >= xs->size - panic("Array.nth out of bounds") - return mem.read64(xs->data + n * 8) - -func Array.set[xs: Array, n: i64, x: any] : void - if n < 0 || n >= xs->size - panic("array.set out of bounds") - mem.write64(xs->data + n * 8, x) - -func Array.push[xs: Array, x: any] : void - if xs->size == xs->capacity - new_capacity := 4 - if xs->capacity != 0 - new_capacity = xs->capacity * 2 - xs->data = xs->data->realloc(new_capacity * 8) - xs->capacity = new_capacity - - mem.write64(xs->data + xs->size * 8, x) - xs->size += 1 - -func Array.free[xs: Array] : void - if xs->data != 0 - xs->data->free() - mem.free(xs) - -func Array.free_with_items[xs: Array] : void - if xs->data != 0 - for i in 0..xs->size - mem.free(xs->nth(i)) - xs->data->free() - mem.free(xs) - -func Array.pop[xs: Array] : any - if xs->size == 0 - panic("Array.pop on empty array") - x : any = Array.last(xs) - xs->size = xs->size - 1 - return x - -func Array.last[xs: Array] : any - if xs->size == 0 - panic("Array.last on empty array") - return xs->nth(xs->size - 1) - -func Array.slice[xs: Array, start: i64, length: i64] : Array - if start < 0 || length < 0 || start + length > xs->size - panic("Array.slice out of bounds") - - new_array := Array.new_preallocated_and_zeroed(length) - mem.copy(xs->data + start * 8, new_array->data, length * 8) - return new_array - -func Array.concat[a: Array, b: Array] : Array - new_array := Array.new_preallocated_and_zeroed(a->size + b->size) - mem.copy(a->data, new_array->data, a->size * 8) - mem.copy(b->data, new_array->data + a->size * 8, b->size * 8) - return new_array - -func Array.quicksort[arr: Array] : void - arr->_do_quicksort(0, arr->size - 1) - -func Array._do_quicksort[arr: Array, low: i64, high: i64] : void - if low < high - i := arr->_partition(low, high) - arr->_do_quicksort(low, i - 1) - arr->_do_quicksort(i + 1, high) - -func Array._partition[arr: Array, low: i64, high: i64] : i64 - pivot : i64 = arr->nth(high) - i := low - 1 - for j in (low)..high - if arr->nth(j) as i64 <= pivot - i += 1 - temp : i64 = arr->nth(i) - arr->set(i, arr->nth(j)) - arr->set(j, temp) - temp : i64 = arr->nth(i + 1) - arr->set(i + 1, arr->nth(high)) - arr->set(high, temp) - return i + 1 - -func Array.contains_str[arr: Array, s: str] : bool - for i in 0..arr->size - if (arr->nth(i) as str)->equal(s) - return true - return false - -func Array.count[arr: Array, item: any] : i64 - count := 0 - for i in 0..arr->size - if arr->nth(i) == item - count += 1 - return count - -func Array.map[arr: Array, fn: ptr] : Array - out := Array.new_preallocated_and_zeroed(arr->size) - for i in 0..arr->size - out->set(i, fn(arr->nth(i))) - return out - -func Array.filter[arr: Array, fn: ptr] : Array - out := [] - for i in 0..arr->size - if fn(arr->nth(i)) - out->push(arr->nth(i)) - return out - -func Array.reduce[arr: Array, fn: ptr, acc: any] : any - for i in 0..arr->size - acc = fn(acc, arr->nth(i)) - return acc - -const HashMap._TABLE_SIZE = 100 - -struct HashMap - table: Array - -struct HashMap._Node - key: str - value: any - next: HashMap._Node - -func HashMap.new[] : HashMap - map := new* HashMap - map->table = Array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE) - return map - -func HashMap.insert[map: HashMap, key: str, value: any] : void - index := HashMap._djb2(key) - current : HashMap._Node = map->table->nth(index) - - while current as ptr - if current->key->equal(key) - current->value = value - return - current = current->next - - new_node := new* HashMap._Node - new_node->key = key->make_copy() - new_node->value = value - new_node->next = map->table->nth(index) - map->table->set(index, new_node) - -func HashMap.get[map: HashMap, key: str] : any, bool - index := HashMap._djb2(key) - current : HashMap._Node = map->table->nth(index) - - while current as ptr - if current->key->equal(key) - return current->value, true - current = current->next - - return 0 as any, false - -func HashMap.delete[map: HashMap, key: str] : void - index := HashMap._djb2(key) - current : HashMap._Node = map->table->nth(index) - prev := 0 as HashMap._Node - - while current as ptr - if current->key->equal(key) - if prev as ptr - prev->next = current->next - else - map->table->set(index, current->next) - - current->key->free() - mem.free(current) - return - - prev = current - current = current->next - -func HashMap._djb2[key: str] : i64 - hash := 5381 - for i in 0..key->len() - hash = ((hash << 5) + hash) + key[i] - hash = (hash & 0x7fffffffffffffff) // prevent negative - return hash % HashMap._TABLE_SIZE - -func HashMap.free[map: HashMap] : void - for i in 0..HashMap._TABLE_SIZE - current : HashMap._Node = map->table->nth(i) - while current as ptr - tmp := current - current = current->next - tmp->key->free() - mem.free(tmp) - - map->table->free() - mem.free(map) - -func os.exit[code: i64] : void - _builtin_syscall(SYS_exit, code) - -func os.getpid[] : i64 - return _builtin_syscall(SYS_getpid) - -func os.basename[path: str] : str - i := path->len() - 1 - while i >= 0 && path[i] != '/' - i -= 1 - return path->substr(i + 1, path->len() - i - 1) - -struct os.Timespec - tv_sec: i64 - tv_nsec: i64 - -func os.sleep[ms: i64] : void - if ms < 0 - panic("negative time passed to os.sleep") - req := new os.Timespec - req->tv_sec = ms / 1000 - req->tv_nsec = (ms % 1000) * 1000000 - _builtin_syscall(SYS_nanosleep, req, 0) - -func os.time[] : i64 - ts := new os.Timespec - _builtin_syscall(SYS_clock_gettime, CLOCK_REALTIME, ts) - out := ts->tv_sec * 1000 + ts->tv_nsec / 1000000 - return out - -func os.urandom_buf[buf: ptr, n: i64] : void - pos := 0 - while pos < n - ret := _builtin_syscall(SYS_getrandom, buf + pos, n - pos, 0) - if ret <= 0 - panic("os.urandom_buf: syscall failed") - pos += ret - -func os.urandom[n: i64]: ptr - buf := mem.alloc(n) - os.urandom_buf(buf, n) - return buf - -func os.urandom_i64[] : i64 - n := 0 - os.urandom_buf(^n, 8) - return n - -func os.run_shell_command[command: str] : i64, bool - pid := _builtin_syscall(SYS_fork) - if pid < 0 - return -1, false - - if pid == 0 - argv := ["sh", "-c", command, 0] // gets freed after child process exits - _builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ()) - os.exit(1) - else - status := 0 - wp := _builtin_syscall(SYS_wait4, pid, ^status, 0, 0) - if wp < 0 - return -1, false - st := status & 0xffffffff - - if (st & 0x7f) == 0 - return (st >> 8) & 0xff, true - else - return -(st & 0x7f), true - -func os.file_exists[path: str] : bool - return _builtin_syscall(SYS_faccessat, AT_FDCWD, path, F_OK, 0) == 0 - -func os.is_a_directory[path: str] : 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 false - - return (mem.read32(st + 24) & S_IFMT) == S_IFDIR - -func os.list_directory[path: str] : Array, bool - fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0) - if fd < 0 - return 0 as Array, false - - files := [] - buf := _stackalloc(1024) - while true - n := _builtin_syscall(SYS_getdents64, fd, buf, 1024) - if n < 0 - files->free_with_items() - _builtin_syscall(SYS_close, fd) - return 0 as Array, false - else if n == 0 - break - - pos := 0 - while pos < n - len := mem.read16(buf + pos + 16) - name : ptr = buf + pos + 19 - if name[0] - skip := false - // skip if name is exactly '.' or '..' - if name[0] == '.' - if name[1] == 0 - skip = true - else if name[1] == '.' - if name[2] == 0 - skip = true - if !skip - files->push((name as str)->make_copy()) - pos += len - - _builtin_syscall(SYS_close, fd) - return files, true diff --git a/std/str.zr b/std/str.zr new file mode 100644 index 0000000..0a24cab --- /dev/null +++ b/std/str.zr @@ -0,0 +1,304 @@ +include "std/num.zr" +include "std/mem.zr" + +func u8.is_whitespace[x: u8] : bool + return x == ' ' || x == '\n' || x == '\t' || x == '\r' + +func u8.is_digit[x: u8] : bool + return x >= '0' && x <= '9' + +func u8.is_hex_digit[x: u8] : bool + return (x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F') + +func u8.is_lowercase[x: u8] : bool + return x >= 'a' && x <= 'z' + +func u8.is_uppercase[x: u8] : bool + return x >= 'A' && x <= 'Z' + +func u8.is_letter[x: u8] : bool + return x->is_uppercase() || x->is_lowercase() + +func u8.is_alphanumeric[x: u8] : bool + return x->is_letter() || x->is_digit() + +func u8.to_str[c: u8] : str + s := mem.alloc(2) as str + s[0] = c + s[1] = 0 + return s + +func str.free[s: str] : void + mem.free(s) + +func str.len[s: str] : i64 + i := 0 + while s[i] + i += 1 + return i + +func str.make_copy[s: str] : str + size := s->len() + 1 + dup := mem.alloc(size) as str + mem.copy(s as ptr, dup as ptr, size) + return dup + +func str.equal[a: str, b: str] : bool + i := 0 + while a[i] != 0 && b[i] != 0 + if a[i] != b[i] + return false + i += 1 + return a[i] == b[i] + +func str.equal_n[a: str, b: str, n: i64] : bool + i := 0 + while i < n + if a[i] == 0 || b[i] == 0 + return false + if a[i] != b[i] + return false + i += 1 + return true + +func str.format_into[..] : i64 + buf := _var_arg(0) as ptr + size := _var_arg(1) as i64 + if size <= 0 + return 0 + s := _var_arg(2) as ptr + i := 3 + n := 0 + + tmp := _stackalloc(64) as str + + while s[0] + if s[0] == '%' + s += 1 + + if s[0] == 'd' + (_var_arg(i) as i64)->to_str_buf(tmp as ptr) + tmp_len := tmp->len() + remaining := size - n - 1 + mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) + n += tmp_len + i += 1 + else if s[0] == 'x' + (_var_arg(i) as i64)->to_hex_str_buf(tmp as ptr) + tmp_len := tmp->len() + remaining := size - n - 1 + mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) + n += tmp_len + i += 1 + else if s[0] == 's' + tmp_str := _var_arg(i) as str + tmp_len := tmp_str->len() + remaining := size - n - 1 + mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining)) + n += tmp_len + i += 1 + else if s[0] == 'c' + if n < size - 1 + buf[n] = _var_arg(i) as u8 + n += 1 + i += 1 + else if s[0] == 'f' + // TODO: fix when we implement f64 params + f64.to_str_buf(_var_arg(i), tmp as ptr) + tmp_len := tmp->len() + remaining := size - n - 1 + mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining)) + n += tmp_len + i += 1 + else if s[0] == '%' + if n < size - 1 + buf[n] = '%' + n += 1 + else if s[0] == 0 + break + else + panic("str.format_into: unrecognized format") + else + if n < size - 1 + buf[n] = s[0] + n += 1 + s += 1 + + if n < size + buf[n] = 0 + else if size > 0 + buf[size - 1] = 0 + return n + +func str.concat[a: str, b: str] : str + a_len := a->len() + b_len := b->len() + out := mem.alloc(a_len + b_len + 1) as str + mem.copy(a as ptr, out as ptr, a_len) + mem.copy(b as ptr, out as ptr + a_len, b_len) + out[a_len + b_len] = 0 + return out + +func str.contains[haystack: str, needle: str] : bool + return haystack->find(needle) != -1 + +func str.find[haystack: str, needle: str] : i64 + haystack_len := haystack->len() + needle_len := needle->len() + + if needle_len == 0 + return 0 + + if needle_len > haystack_len + return -1 + + for i in 0..(haystack_len - needle_len + 1) + if haystack[i] != needle[0] + continue + if needle_len == 1 + return i + match := true + for j in 1..needle_len + if haystack[i + j] != needle[j] + match = false + break + if match + return i + return -1 + +func str.substr[s: str, start: i64, length: i64] : str + out := mem.alloc(length + 1) as str + mem.copy(s as ptr + start, out as ptr, length) + out[length] = 0 + return out + +func str.trim[s: str] : str + len := s->len() + if len == 0 + out := mem.alloc(1) as str + out[0] = 0 + return out + + start := 0 + end := len - 1 + + while start <= end && s[start]->is_whitespace() + start += 1 + + while end >= start && s[end]->is_whitespace() + end -= 1 + + return s->substr(start, end - start + 1) + +func str.reverse[s: str] : str + len := s->len() + out := mem.alloc(len + 1) as str + + for i in 0..len + out[i] = s[len - i - 1] + out[len] = 0 + return out + + +func str.parse_i64[s: str] : i64 + len := s->len() + i := 0 + + sign := 1 + if i < len && s[i] == '-' + sign = -1 + i += 1 + + num := 0 + while i < len + d := s[i] + if !d->is_digit() + break + num = num * 10 + (d - '0') + i += 1 + return num * sign + +func str.hex_encode[s: str, s_len: i64] : str + hex_chars := "0123456789abcdef" + out := mem.alloc(s_len * 2 + 1) as str + + for i in 0..s_len + b := s[i] + out[i * 2] = hex_chars[(b >> 4) & 15] + out[i * 2 + 1] = hex_chars[b & 15] + + out[s_len * 2] = 0 + return out + +func str._hex_digit_to_int[d: u8] : u8 + if d->is_digit() + return d - '0' + lower : u8 = d | 32 + if lower >= 'a' && lower <= 'f' + return lower - 'a' + 10 + panic("invalid hex digit passed to str._hex_digit_to_int") + +func str.hex_decode[s: str] : str + s_len := s->len() + if s_len % 2 != 0 + panic("invalid hex string passed to str.hex_decode") + + out_len := s_len / 2 + out := mem.alloc(out_len + 1) as str + + for i in 0..out_len + high := str._hex_digit_to_int(s[i * 2]) + low := str._hex_digit_to_int(s[i * 2 + 1]) + out[i] = (high << 4) | low + + out[out_len] = 0 + return out + +struct str.Builder + data: ptr + size: i64 + capacity: i64 + +func str.Builder._grow[b: str.Builder, needed: i64] : void + if b->size + needed > b->capacity + new_capacity := 64 + if b->capacity != 0 + new_capacity = b->capacity * 2 + while new_capacity < b->size + needed + new_capacity = new_capacity * 2 + b->data = b->data->realloc(new_capacity) + b->capacity = new_capacity + +func str.Builder.append_char[b: str.Builder, c: u8] : void + b->_grow(1) + b->data[b->size] = c + b->size += 1 + +func str.Builder.append[b: str.Builder, s: str] : void + len := s->len() + b->_grow(len) + mem.copy(s as ptr, b->data + b->size, len) + b->size += len + +func str.Builder.build[b: str.Builder] : str + s := mem.alloc(b->size + 1) as str + mem.copy(b->data, s as ptr, b->size) + s[b->size] = 0 + return s + +func str.Builder.destroy[b: str.Builder] : void + if b->data != 0 + b->data->free() + 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 diff --git a/test.zr b/test.zr index 90fdbdf..3cff57c 100644 --- a/test.zr +++ b/test.zr @@ -1,3 +1,6 @@ +include "std/io.zr" +include "std/os.zr" + struct TestRunner build_blacklist: Array run_blacklist: Array