From 20499a8ee046573749275ca9842476f34650a7cf Mon Sep 17 00:00:00 2001 From: Toni Date: Sat, 27 Dec 2025 17:22:09 +0100 Subject: [PATCH] @ -> ^, functional example --- README.md | 14 ++++++++++++++ examples/fizzbuzz.zr | 2 +- examples/functional.zr | 11 +++++++++++ examples/sqlite_todo.zr | 8 ++++---- examples/tokenizer.zr | 4 +--- src/parser.rs | 2 +- src/std.zr | 36 +++++++++++++++++++++++++++++++----- src/tokenizer.rs | 2 -- 8 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 examples/functional.zr diff --git a/README.md b/README.md index 6cc221f..341c69d 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,20 @@ func main[] : i64 io.println("Too high!") ``` +```rust +func square[x: i64] : i64 + return x * x + +func sum[a: i64, b: i64] : i64 + return a + b + +func main[] : i64 + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] + |> alg.map(^square) + |> alg.reduce(^sum, 0) + |> io.println_i64() +``` + ## Quickstart ``` cargo install --git https://github.com/antpiasecki/zern diff --git a/examples/fizzbuzz.zr b/examples/fizzbuzz.zr index 624fd8f..bf989f6 100644 --- a/examples/fizzbuzz.zr +++ b/examples/fizzbuzz.zr @@ -4,7 +4,7 @@ func main[] : i64 io.println("FizzBuzz") else if i % 5 == 0 io.println("Buzz") - else if i %3 == 0 + else if i % 3 == 0 io.println("Fizz") else io.println_i64(i) \ No newline at end of file diff --git a/examples/functional.zr b/examples/functional.zr new file mode 100644 index 0000000..ee90c42 --- /dev/null +++ b/examples/functional.zr @@ -0,0 +1,11 @@ +func square[x: i64] : i64 + return x * x + +func sum[a: i64, b: i64] : i64 + return a + b + +func main[] : i64 + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] + |> alg.map(^square) + |> alg.reduce(^sum, 0) + |> io.println_i64() \ No newline at end of file diff --git a/examples/sqlite_todo.zr b/examples/sqlite_todo.zr index c721097..fe27004 100644 --- a/examples/sqlite_todo.zr +++ b/examples/sqlite_todo.zr @@ -15,7 +15,7 @@ func main[] : i64 let db = 0 let stmt = 0 - rc = sqlite3_open("todo.db", @db) + rc = sqlite3_open("todo.db", ^db) if rc dbg.panic("failed to open db") @@ -36,7 +36,7 @@ func main[] : i64 break else if choice == 1 io.println("============") - sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, @stmt, 0) + sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, ^stmt, 0) while sqlite3_step(stmt) == 100 let id: i64 = sqlite3_column_int(stmt, 0) @@ -51,7 +51,7 @@ func main[] : i64 io.print("\nEnter new task: ") let task: str = io.read_line() |> str.trim() - sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, @stmt, 0) + sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0) sqlite3_bind_text(stmt, 1, task, -1, 0) if sqlite3_step(stmt) != 101 dbg.panic(sqlite3_errmsg(db)) @@ -61,7 +61,7 @@ func main[] : i64 io.print("\nEnter task id: ") let id: i64 = io.read_line() |> str.parse_i64() - sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, @stmt, 0) + sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0) sqlite3_bind_int(stmt, 1, id, -1, 0) if sqlite3_step(stmt) != 101 dbg.panic(sqlite3_errmsg(db)) diff --git a/examples/tokenizer.zr b/examples/tokenizer.zr index 16a0523..d862441 100644 --- a/examples/tokenizer.zr +++ b/examples/tokenizer.zr @@ -151,8 +151,6 @@ func scan_token[tokens: array, current: ptr, line: ptr, column: ptr, source: str add_token("Xor", tokens, source, start, mem.read64(current), mem.read64(line), mem.read64(column)) else if c == ':' add_token("Colon", tokens, source, start, mem.read64(current), mem.read64(line), mem.read64(column)) - else if c == '@' - add_token("At", tokens, source, start, mem.read64(current), mem.read64(line), mem.read64(column)) else if c == '.' if match_char('.', current, column, source, source_len) add_token("DoubleDot", tokens, source, start, mem.read64(current), mem.read64(line), mem.read64(column)) @@ -241,7 +239,7 @@ func tokenize[source: str, filename: str] : array let tokens: array = [] while !eof(current, source_len) - scan_token(tokens, @current, @line, @column, source, source_len, filename, indent_stack, @current_indent) + scan_token(tokens, ^current, ^line, ^column, source, source_len, filename, indent_stack, ^current_indent) add_token_with_lexeme("Eof", tokens, "", line, column) return tokens diff --git a/src/parser.rs b/src/parser.rs index 9c6e8f7..a7e2095 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -441,7 +441,7 @@ impl Parser { } fn unary(&mut self) -> Result { - if self.match_token(&[TokenType::At]) { + if self.match_token(&[TokenType::Xor]) { let op = self.previous().clone(); let right = self.unary()?; return Ok(Expr::AddrOf { diff --git a/src/std.zr b/src/std.zr index 2ab705b..82b0ce0 100644 --- a/src/std.zr +++ b/src/std.zr @@ -47,7 +47,7 @@ func io.println[x: str] : void io.print("\n") func io.print_char[x: u8] : void - io.print_sized(@x, 1) + io.print_sized(^x, 1) func io.print_i64[x: i64] : void let s: str = str.from_i64(x) @@ -61,7 +61,7 @@ func io.println_i64[x: i64] : void func io.read_char[] : u8 let c: u8 = 0 - _builtin_syscall(0, 0, @c, 1) // read + _builtin_syscall(0, 0, ^c, 1) // read return c func io.read_line[]: str @@ -341,6 +341,14 @@ func math.abs[n: i64] : i64 return -n return n +func math.sign[n: i64] : i64 + if n < 0 + return -1 + else if n > 0 + return 1 + else + return 0 + func math.pow[b: i64, e: i64] : i64 let out = 1 for i in 0..e @@ -480,13 +488,31 @@ func alg.count[arr: array, item: i64] : i64 count = count + 1 return count +func alg.map[arr: array, fn: ptr] : array + let out: array = [] + for i in 0..array.size(arr) + array.push(out, fn(array.nth(arr, i))) + return out + +func alg.filter[arr: array, fn: ptr] : array + let out: array = [] + for i in 0..array.size(arr) + if fn(array.nth(arr, i)) + array.push(out, array.nth(arr, i)) + return out + +func alg.reduce[arr: array, fn: ptr, acc: i64] : i64 + for i in 0..array.size(arr) + acc = fn(acc, array.nth(arr, i)) + return acc + func os.exit[code: i64] : void _builtin_syscall(60, code) func os.urandom[]: i64 let n = 0 let fd: i64 = _builtin_syscall(257, -100, "/dev/urandom", 0, 0) // openat - _builtin_syscall(0, fd, @n, 8) // read + _builtin_syscall(0, fd, ^n, 8) // read _builtin_syscall(3, fd) // close return n @@ -508,7 +534,7 @@ func os.shell[command: str] : i64 _builtin_syscall(60, 1) // exit else let status = 0 - let wp: i64 = _builtin_syscall(61, pid, @status, 0, 0) // waitpid + let wp: i64 = _builtin_syscall(61, pid, ^status, 0, 0) // waitpid if wp == -1 return -1 let st: i64 = status & 0xffffffff @@ -557,7 +583,7 @@ func net.listen[packed_host: i64, port: i64] : i64 return -1 let optval = 1 - if _builtin_syscall(54, s, 1, 2, @optval, 8) < 0 // setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) + if _builtin_syscall(54, s, 1, 2, ^optval, 8) < 0 // setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) _builtin_syscall(3, s) // close return -1 diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 3dd93e8..441db72 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -20,7 +20,6 @@ pub enum TokenType { LogicalAnd, LogicalOr, Pipe, - At, DoubleDot, ShiftLeft, ShiftRight, @@ -158,7 +157,6 @@ impl Tokenizer { '%' => self.add_token(TokenType::Mod), '^' => self.add_token(TokenType::Xor), ':' => self.add_token(TokenType::Colon), - '@' => self.add_token(TokenType::At), '.' => { if self.match_char('.') { self.add_token(TokenType::DoubleDot)