Setup demos for new compiler.

This commit is contained in:
zongor 2025-10-25 13:59:05 -07:00
parent de9ab028b5
commit 47d5bdd1d9
17 changed files with 419 additions and 108 deletions

4
.gitattributes vendored
View File

@ -1,2 +1,2 @@
*.ul linguist-language=fortran *.ul linguist-language=Fortran
*.zl linguist-language=zig *.zl linguist-language=Zig

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -1,11 +1,13 @@
use "common.ztl"; use <str.ul>; // from stdlib
use "common.ul"; // from local
function main(int argc, str[] argv) { function main(int argc, str[] argv) {
nat screen_width = 800; nat screen_width = 800;
nat screen_height = 450; nat screen_height = 450;
if (argv < 2) { if (argv < 2) {
exits("usage: zre client.ul <username> <password>"); pln("usage: undar client.ul <username> <password>");
exit(-1);
} }
str username = argv[1]; str username = argv[1];
str password = argv[2]; str password = argv[2];

View File

@ -1,14 +1,20 @@
!! /**
! Note that these look like classes but act like structs * Note that these look like classes but act like structs
! the methods actually have a implied struct as their first argument * the methods actually have a implied struct as their first argument
!! */
!! /**
! Camera. * Camera.
!! */
plex Camera { plex Camera {
int setting;
real pov;
real[3] up;
real[3] pos;
real[3] look;
init(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.pov = 45.0;
this.up = [0.0, 1.0, 0.0]; this.up = [0.0, 1.0, 0.0];
this.pos = pos; this.pos = pos;
@ -16,10 +22,16 @@ plex Camera {
} }
} }
!! /**
! Player. * Player.
!! */
plex Player { plex Player {
Client client;
str username;
real[3] pos;
nat color;
Camera camera;
init(str username, real[3] pos, Color color) { init(str username, real[3] pos, Color color) {
this.client = Client("tcp://localhost:25565"); this.client = Client("tcp://localhost:25565");
this.username = username; 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); 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.client.attach(this.username, password);
this.players = client.open("players"); this.players = client.open("players");
return players.read(); return players.read();
@ -64,6 +76,6 @@ plex Player {
} }
} }
const RED is [255, 0, 0]; const nat RED = rgb332([255, 0, 0]);
const WHITE is [0, 0, 0]; const nat WHITE = rgb332([0, 0, 0]);
const PURPLE is [255, 255, 0]; const nat PURPLE = rgb332([255, 255, 0]);

View File

@ -1,4 +1,4 @@
use "common.ztl"; use "common.ul";
function main(int argc, str[] argv) { function main(int argc, str[] argv) {
Server s("tcp://0.0.0.0:25565"); Server s("tcp://0.0.0.0:25565");

35
test/add.ul Normal file
View File

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

36
test/fib.ul Normal file
View File

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

28
test/hello.ul Normal file
View File

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

37
test/loop.ul Normal file
View File

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

View File

@ -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() { function main() {
Terminal term(terminal_namespace, 0); Terminal term();
pln(prompt); pln(term, prompt);
pln(terminal.read(32)); pln(term, term.read(32));
return 0; 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);
} }

118
test/paint-bw.ul Normal file
View File

@ -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;
}
}
}

28
test/simple.ul Normal file
View File

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

78
test/window.ul Normal file
View File

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