From 47d5bdd1d9e93a37974aa9bdd494480a8b84f14b Mon Sep 17 00:00:00 2001 From: zongor Date: Sat, 25 Oct 2025 13:59:05 -0700 Subject: [PATCH] Setup demos for new compiler. --- .gitattributes | 4 +- bench/add.ul | 17 ---- bench/fib.ul | 18 ---- bench/hello.ul | 12 --- bench/loop.ul | 21 ----- bench/simple.ul | 13 --- docs/project-syntax-example/client.ul | 6 +- docs/project-syntax-example/common.ul | 42 +++++---- docs/project-syntax-example/server.ul | 2 +- test/add.ul | 35 ++++++++ test/fib.ul | 36 ++++++++ test/hello.ul | 28 ++++++ test/loop.ul | 37 ++++++++ test/malloc.ul | 32 +++++-- test/paint-bw.ul | 118 ++++++++++++++++++++++++++ test/simple.ul | 28 ++++++ test/window.ul | 78 +++++++++++++++++ 17 files changed, 419 insertions(+), 108 deletions(-) delete mode 100644 bench/add.ul delete mode 100644 bench/fib.ul delete mode 100644 bench/hello.ul delete mode 100644 bench/loop.ul delete mode 100644 bench/simple.ul create mode 100644 test/add.ul create mode 100644 test/fib.ul create mode 100644 test/hello.ul create mode 100644 test/loop.ul create mode 100644 test/paint-bw.ul create mode 100644 test/simple.ul create mode 100644 test/window.ul diff --git a/.gitattributes b/.gitattributes index 54386f4..3b447a2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,2 @@ -*.ul linguist-language=fortran -*.zl linguist-language=zig \ No newline at end of file +*.ul linguist-language=Fortran +*.zl linguist-language=Zig \ No newline at end of file diff --git a/bench/add.ul b/bench/add.ul deleted file mode 100644 index 5396bb5..0000000 --- a/bench/add.ul +++ /dev/null @@ -1,17 +0,0 @@ -Device term("/dev/term/0"); -str new-line = "\n"; - -fn1 main() { - int sum = add(1, 1); - pln(sum.str); -} - -fn1 add(int a, int b) int { - return a + b; -} - -fn1 pln(str string) { - term.write(string); - term.write(new-line); - return; -} diff --git a/bench/fib.ul b/bench/fib.ul deleted file mode 100644 index 35107e8..0000000 --- a/bench/fib.ul +++ /dev/null @@ -1,18 +0,0 @@ -Device term("/dev/term/0"); -str new-line = "\n"; - -fn1 main() { - print(fib(35).str); - exit(0); -} - -fn1 fib(int n) int { - if (n < 2) return n; - return fib(n - 2) + fib(n - 1); -} - -fn1 pln(str string) { - term.write(string); - term.write(new-line); - return; -} diff --git a/bench/hello.ul b/bench/hello.ul deleted file mode 100644 index aa8983c..0000000 --- a/bench/hello.ul +++ /dev/null @@ -1,12 +0,0 @@ -Device term("/dev/term/0"); -str new-line = "\n"; - -fn1 main(int argc, str[] argv) { - print("nuqneH 'u'?"); -} - -fn1 pln(str string) { - term.write(string); - term.write(new-line); - return; -} diff --git a/bench/loop.ul b/bench/loop.ul deleted file mode 100644 index 696647f..0000000 --- a/bench/loop.ul +++ /dev/null @@ -1,21 +0,0 @@ -Device term("/dev/term/0"); -str new-line = "\n"; - -fn1 main() { - real a = 5.0; - do (int i = 5000, 0, -1) { - a = a + 5.0; - } - nat b = a as nat; - pln("Enter a string:"); - str32 user_string = term.read(); - pln(a.str); - pln(b.str); - pln(user_string); -} - -fn1 pln(str string) { - term.write(string); - term.write(new-line); - return; -} diff --git a/bench/simple.ul b/bench/simple.ul deleted file mode 100644 index b8d5e92..0000000 --- a/bench/simple.ul +++ /dev/null @@ -1,13 +0,0 @@ -Device term("/dev/term/0"); -str new-line = "\n"; - -fn1 main(int argc, str[] argv) { - int sum = 1 + 2; - pln(sum.str); -} - -fn1 pln(str string) { - term.write(string); - term.write(new-line); - return; -} diff --git a/docs/project-syntax-example/client.ul b/docs/project-syntax-example/client.ul index 3c1be45..c876ade 100644 --- a/docs/project-syntax-example/client.ul +++ b/docs/project-syntax-example/client.ul @@ -1,11 +1,13 @@ -use "common.ztl"; +use ; // from stdlib +use "common.ul"; // from local function main(int argc, str[] argv) { nat screen_width = 800; nat screen_height = 450; if (argv < 2) { - exits("usage: zre client.ul "); + pln("usage: undar client.ul "); + exit(-1); } str username = argv[1]; str password = argv[2]; diff --git a/docs/project-syntax-example/common.ul b/docs/project-syntax-example/common.ul index 6eb7070..3867eb6 100644 --- a/docs/project-syntax-example/common.ul +++ b/docs/project-syntax-example/common.ul @@ -1,14 +1,20 @@ -!! -! Note that these look like classes but act like structs -! the methods actually have a implied struct as their first argument -!! +/** + * Note that these look like classes but act like structs + * the methods actually have a implied struct as their first argument + */ -!! -! Camera. -!! +/** + * Camera. + */ plex Camera { + int setting; + real pov; + real[3] up; + real[3] pos; + real[3] look; + init(real[3] pos, real[3] look) { - this.setting = "CAMERA_PERSPECTIVE"; + this.setting = CAMERA_PERSPECTIVE; this.pov = 45.0; this.up = [0.0, 1.0, 0.0]; this.pos = pos; @@ -16,10 +22,16 @@ plex Camera { } } -!! -! Player. -!! +/** + * Player. + */ plex Player { + Client client; + str username; + real[3] pos; + nat color; + Camera camera; + init(str username, real[3] pos, Color color) { this.client = Client("tcp://localhost:25565"); this.username = username; @@ -29,7 +41,7 @@ plex Player { Camera([this.pos.x + 10.0, this.pos.y + 10.0, this.pos.z], this.pos); } - login(str password) Player[] { ! looks like a method but really it just has an implied "Player this" as the first argument + login(str password) Player[] { // looks like a method but really it just has an implied "Player this" as the first argument this.client.attach(this.username, password); this.players = client.open("players"); return players.read(); @@ -64,6 +76,6 @@ plex Player { } } -const RED is [255, 0, 0]; -const WHITE is [0, 0, 0]; -const PURPLE is [255, 255, 0]; +const nat RED = rgb332([255, 0, 0]); +const nat WHITE = rgb332([0, 0, 0]); +const nat PURPLE = rgb332([255, 255, 0]); diff --git a/docs/project-syntax-example/server.ul b/docs/project-syntax-example/server.ul index 7b24a72..677b743 100644 --- a/docs/project-syntax-example/server.ul +++ b/docs/project-syntax-example/server.ul @@ -1,4 +1,4 @@ -use "common.ztl"; +use "common.ul"; function main(int argc, str[] argv) { Server s("tcp://0.0.0.0:25565"); diff --git a/test/add.ul b/test/add.ul new file mode 100644 index 0000000..b947588 --- /dev/null +++ b/test/add.ul @@ -0,0 +1,35 @@ +/** + * Constants + */ +const str nl = "\n"; + +plex Terminal { + nat handle; + + init() { + handle = open("/dev/term/0", 0); + } +} + +/** + * Main function + */ +function main() { + pln(add(1, 1).str); +} + +/** + * Add two numbers together + */ +function add(int a, int b) int { + return a + b; +} + +/** + * Print with a newline + */ +function pln(str message) { + Terminal term(); + write(term, message, message.length); + write(term, nl, nl.length); +} diff --git a/test/fib.ul b/test/fib.ul new file mode 100644 index 0000000..5e3ef6c --- /dev/null +++ b/test/fib.ul @@ -0,0 +1,36 @@ +/** + * Constants + */ +const str nl = "\n"; + +plex Terminal { + nat handle; + + init() { + handle = open("/dev/term/0", 0); + } +} + +/** + * Main function + */ +function main() { + pln(fib(35).str); +} + +/** + * Recursively calculate fibonacci + */ +function fib(int n) int { + if (n < 2) return n; + return fib(n - 2) + fib(n - 1); +} + +/** + * Print with a newline + */ +function pln(str message) { + Terminal term(); + write(term, message, message.length); + write(term, nl, nl.length); +} diff --git a/test/hello.ul b/test/hello.ul new file mode 100644 index 0000000..c16d46f --- /dev/null +++ b/test/hello.ul @@ -0,0 +1,28 @@ +/** + * Constants + */ +const str nl = "\n"; + +plex Terminal { + nat handle; + + init() { + handle = open("/dev/term/0", 0); + } +} + +/** + * Main function + */ +function main() { + pln("nuqneH 'u'?"); +} + +/** + * Print with a newline + */ +function pln(str message) { + Terminal term(); + write(term, message, message.length); + write(term, nl, nl.length); +} diff --git a/test/loop.ul b/test/loop.ul new file mode 100644 index 0000000..2d1602f --- /dev/null +++ b/test/loop.ul @@ -0,0 +1,37 @@ +/** + * Constants + */ +const str nl = "\n"; + +plex Terminal { + nat handle; + + init() { + handle = open("/dev/term/0", 0); + } +} + +/** + * Main function + */ +function main() { + Terminal term(); + real a = 5.0; + do (int i = 5000, 0, -1) { + a = a + 5.0; + } + nat b = a as nat; + pln(term, "Enter a string:"); + str user_string = read(term, 32); + pln(term, a.str); + pln(term, b.str); + pln(term, user_string); +} + +/** + * Print with a newline + */ +function pln(Terminal term, str message) { + write(term, message, message.length); + write(term, nl, nl.length); +} diff --git a/test/malloc.ul b/test/malloc.ul index f1bee58..1014ca2 100644 --- a/test/malloc.ul +++ b/test/malloc.ul @@ -1,13 +1,31 @@ -str terminal_namespace = ""; -str prompt = "Enter a string: "; +/** + * Constants + */ +const str prompt = "Enter a string: "; +const str nl = "\n"; +plex Terminal { + nat handle; + + init() { + handle = open("/dev/term/0", 0); + } +} + +/** + * Main function + */ function main() { - Terminal term(terminal_namespace, 0); - pln(prompt); - pln(terminal.read(32)); + Terminal term(); + pln(term, prompt); + pln(term, term.read(32)); return 0; } -function pln(ref Terminal term, str message) { - term.write(message); +/** + * Print with a newline + */ +function pln(Terminal term, str message) { + write(term, message, message.length); + write(term, nl, nl.length); } diff --git a/test/paint-bw.ul b/test/paint-bw.ul new file mode 100644 index 0000000..fc5e8f1 --- /dev/null +++ b/test/paint-bw.ul @@ -0,0 +1,118 @@ +/** + * Constants + */ +const byte BLACK = 0; +const byte WHITE = 255; +const byte DARK_GRAY = 73; +const byte GRAY = 146; +const byte LIGHT_GRAY = 182; + +byte selected_color = 255; + +interface Device { + nat handle; +} + +plex Screen implements Device { + nat handle; + nat width; + nat height; + nat buffer_size; + byte[] screen_buffer; + + init() { + this.handle = open("/dev/screen/0", 0); + } +} + +plex Mouse implements Device { + nat handle; + nat x; + nat y; + bool left; + bool right; + bool middle; + bool btn4; + nat size; + + init() { + this.handle = open("/dev/mouse/0", 0); + } +} + +/** + * Main function + */ +function main() { + Screen screen(); + Mouse mouse(); + + outline_swatch(screen, BLACK, 1, 1, 8); + outline_swatch(screen, WHITE, 21, 1, 8); + screen.draw(); + + loop { + mouse.read(); + if (not mouse.left) continue; + + int box_size = 20; + int x = 1; + int y = 1; + byte color = BLACK; + outlined_swatch(screen, color, x, y); + set_color(box_size, x, y, mouse.x, mouse.y, color); + + color = WHITE; + x = 21; + outlined_swatch(screen, color, x, y); + set_color(box_size, x, y, mouse.x, mouse.y, color); + screen.draw(); + + draw_box(screen, selected_color, x, y, 5, 5); + } + exit(0); +} + +/** + * Checks if the click is within the bound and update the selected color if so. + */ +function set_color(int mouse_x, int mouse_y, int box_x, int box_y, nat color, nat box_size) { + int right = box_x + box_size; + int bottom = box_y + box_size; + + if (mouse_x < box_x) return; + if (mouse_x > right) return; + if (mouse_y < box_y) return; + if (mouse_y > bottom) return; + + selected_color = color; +} + +/** + * Draw a color box with a grey outline, if selected use a darker color + */ +function outline_swatch(Device screen, byte color, int x, int y) { + byte bg_color = GRAY; + if (selected_color == color) { + bg_color = DARK_GRAY; + } + + draw_box(screen, bg_color, x, y, 20, 20); + draw_box(screen, color, x + 2, y + 2, 17, 17); +} + +/** + * Draw a box + */ +function draw_box(Device screen, int screen_width, byte color, int x, int y, int width, int height) { + // we need unsafe because we are using `ptr` and `memset` directly + // unsafe takes the guardrails off and allows you to access/modify memory directly + unsafe { + int pixel = y * screen_width + x + screen.buffer.ptr + 4; + do (int i = height; i > 0; i--) { + int row = pixel + width; + memset(screen.buffer.ptr, row, color, width); + pixel += screen_width; + } + } +} diff --git a/test/simple.ul b/test/simple.ul new file mode 100644 index 0000000..5b26383 --- /dev/null +++ b/test/simple.ul @@ -0,0 +1,28 @@ +/** + * Constants + */ +const str nl = "\n"; + +plex Terminal { + nat handle; + + init() { + handle = open("/dev/term/0", 0); + } +} + +/** + * Main function + */ +function main() { + pln((1 + 2).str); +} + +/** + * Print with a newline + */ +function pln(str message) { + Terminal term(); + write(term, message, message.length); + write(term, nl, nl.length); +} diff --git a/test/window.ul b/test/window.ul new file mode 100644 index 0000000..22e6fa9 --- /dev/null +++ b/test/window.ul @@ -0,0 +1,78 @@ +/** + * Constants + */ +const str nl = "\n"; +const nat WHITE = 255; + +/** + * Devices + */ +plex Terminal { + nat handle; + + init() { + handle = open("/dev/term/0", 0); + } +} + +plex Screen { + nat handle; + nat width; + nat height; + nat buffer_size; + byte[] screen_buffer; + + init() { + this.handle = open("/dev/screen/0", 0); + } + + draw() { + write(this.handle, this.screen_buffer, this.buffer_size); + } +} + +plex Mouse { + nat handle; + nat x; + nat y; + bool left; + bool right; + bool middle; + bool btn4; + nat size; + + init() { + this.handle = open("/dev/mouse/0", 0); + } +} + +/** + * Main function + */ +function main() { + Screen screen(); + pln(screen.handle.str); + pln(screen.width.str); + pln(screen.size.str); + pln(screen.screen_buffer.ptr.str); + + Mouse mouse(); + screen.draw(); + + loop { + if (mouse.btn1) { + screen.screen_buffer[mouse.y * width + mouse.x + + screen.screen_buffer.ptr + 4] = WHITE; + screen.draw(); + } + } +} + +/** + * Print with a newline + */ +function pln(str message) { + Terminal term(); + write(term, message, message.length); + write(term, nl, nl.length); +}