Add semicolon

This commit is contained in:
zongor 2025-11-29 09:21:12 -08:00
parent 32ae0eeb8f
commit 55b6145064
10 changed files with 502 additions and 490 deletions

View File

@ -216,6 +216,7 @@ bool define_global(VM *vm, SymbolTable *st) {
default: default:
return false; return false;
} }
next_token_is(TOKEN_SEMICOLON);
symbol_table_add(st, s); symbol_table_add(st, s);
return true; return true;
@ -297,6 +298,8 @@ void define_var(VM *vm, SymbolTable *st, Token regType) {
Token reg_num = next_token_is(TOKEN_LITERAL_INT); Token reg_num = next_token_is(TOKEN_LITERAL_INT);
s.ref = atoi(reg_num.start); s.ref = atoi(reg_num.start);
next_token_is(TOKEN_SEMICOLON);
symbol_table_add(st, s); symbol_table_add(st, s);
} }
@ -403,6 +406,8 @@ void build_symbol_table(VM *vm, char *source, SymbolTable *st) {
next_token_is(TOKEN_LITERAL_NAT); next_token_is(TOKEN_LITERAL_NAT);
vm->pc+=4; vm->pc+=4;
next_token_is(TOKEN_SEMICOLON);
} else if (streq(token.start, "call")) { } else if (streq(token.start, "call")) {
vm->pc++; vm->pc++;
@ -418,7 +423,12 @@ void build_symbol_table(VM *vm, char *source, SymbolTable *st) {
} }
/* return type */ /* return type */
next = next_token(); next = next_token();
vm->pc++; vm->pc++; /* we emit a value regardless, a void is register 255 */
if (next.type == TOKEN_SEMICOLON) {
continue;
}
/* if it is not void, then it was the value */
next_token_is(TOKEN_SEMICOLON);
} else if (streq(token.start, "syscall")) { } else if (streq(token.start, "syscall")) {
} else if (streq(token.start, "load_immediate")) { } else if (streq(token.start, "load_immediate")) {
} else if (streq(token.start, "load_indirect_8")) { } else if (streq(token.start, "load_indirect_8")) {

View File

@ -1,35 +1,35 @@
global str terminal_namespace = "/dev/term/0" global str terminal_namespace = "/dev/term/0";
global str new_line = "\n" global str new_line = "\n";
global int x = 1 global int x = 1;
global int y = 1 global int y = 1;
function main () function main ()
int ans $2 int ans $2;
str ans_string $3 str ans_string $3;
load_absolute_32 x -> $0 load_absolute_32 x -> $0;
load_absolute_32 y -> $1 load_absolute_32 y -> $1;
call add $0 $1 -> ans call add $0 $1 -> ans;
int_to_string ans -> ans_string int_to_string ans -> ans_string;
call pln ans_string -> void call pln ans_string -> void;
exit 0 exit 0;
function add (int a $0, int b $1) function add (int a $0, int b $1)
int result $2 int result $2;
add_int a b -> result add_int a b -> result;
return result return result;
function pln (str message $0) function pln (str message $0)
str term $1 str term $1;
int msg_length $2 int msg_length $2;
str nl $3 str nl $3;
int nl_length $4 int nl_length $4;
int mode $5 int mode $5;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN terminal_namespace mode -> term syscall OPEN terminal_namespace mode -> term;
strlen message -> msg_length strlen message -> msg_length;
syscall WRITE term message msg_length syscall WRITE term message msg_length;
strlen new_line -> nl_length strlen new_line -> nl_length;
syscall WRITE term nl nl_length syscall WRITE term nl nl_length;
return return;

View File

@ -1,48 +1,49 @@
global str terminal_namespace = "/dev/term/0" global str terminal_namespace = "/dev/term/0";
global str new_line = "\n" global str new_line = "\n";
function main () function main ()
int n $0 int n $0;
int str_n $1 int str_n $1;
load_immediate 35 -> n load_immediate 35 -> n;
call fib n -> n call fib n -> n;
int_to_string n -> str_n int_to_string n -> str_n;
call pln str_n -> void call pln str_n -> void;
exit 0 exit 0;
function fib (int n $0) function fib (int n $0)
load_immediate 2 -> $1 load_immediate 2 -> $1;
jump_lt_int base_case n $1 jump_lt_int base_case n $1;
load_immediate 2 -> $3 load_immediate 2 -> $3;
sub_int n $3 -> $4 sub_int n $3 -> $4;
call fib $4 -> $5 call fib $4 -> $5;
load_immediate 1 -> $3 load_immediate 1 -> $3;
sub_int n $3 -> $4 sub_int n $3 -> $4;
call fib $4 -> $6 call fib $4 -> $6;
add_int $6 $5 -> $7 add_int $6 $5 -> $7;
return $7 return $7;
else base_case else base_case;
return n return n;
function pln (str message $0) function pln (str message $0)
str ts $1 str ts $1;
int mode $5 int mode $5;
int msg_length $2 int msg_length $2;
str nl $3 str nl $3;
int nl_length $4 int nl_length $4;
load_immediate terminal_namespace -> ts load_immediate terminal_namespace -> ts;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN ts mode -> ts syscall OPEN ts mode -> ts;
strlen message -> msg_length strlen message -> msg_length;
syscall WRITE ts message msg_length syscall WRITE ts message msg_length;
load_immediate new_line -> nl load_immediate new_line -> nl;
strlen nl -> nl_length strlen nl -> nl_length;
syscall WRITE ts nl nl_length syscall WRITE ts nl nl_length;
return return;

View File

@ -3,25 +3,25 @@ global str new_line = "\n"
global str message = "nuqneH 'u'?" global str message = "nuqneH 'u'?"
function main () function main ()
str hello $0 str hello $0;
load_immediate message -> hello load_immediate message -> hello;
call pln hello -> void call pln hello -> void;
exit 0 exit 0;
function pln (str message $0) function pln (str message $0)
str ts $1 str ts $1;
int mode $5 int mode $5;
int msg_length $2 int msg_length $2;
str nl $3 str nl $3;
int nl_length $4 int nl_length $4;
load_immediate terminal_namespace -> ts load_immediate terminal_namespace -> ts;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN ts mode -> ts syscall OPEN ts mode -> ts;
strlen message -> msg_length strlen message -> msg_length;
syscall WRITE ts message msg_length syscall WRITE ts message msg_length;
load_immediate new_line -> nl load_immediate new_line -> nl;
strlen nl -> nl_length strlen nl -> nl_length;
syscall WRITE ts nl nl_length syscall WRITE ts nl nl_length;
return return;

View File

@ -1,58 +1,58 @@
global str terminal_namespace = "/dev/term/0" global str terminal_namespace = "/dev/term/0";
global str prompt = "Enter a string:" global str prompt = "Enter a string:";
global str new_line = "\n" global str new_line = "\n";
function main () function main ();
real a $0 real a $0;
int i $1 int i $1;
int mode $11 int mode $11;
str term $10 str term $10;
load_immediate 5.0 -> a load_immediate 5.0 -> a;
load_immediate 5000 -> i load_immediate 5000 -> i;
load_immediate 0 -> $2 load_immediate 0 -> $2;
load_immediate -1 -> $3 load_immediate -1 -> $3;
load_immediate 5.0 -> $5 load_immediate 5.0 -> $5;
loop loop_body loop loop_body;
add_real a $5 -> a add_real a $5 -> a;
add_int i $3 -> i add_int i $3 -> i;
jump_ge_int loop_body i $2 jump_ge_int loop_body i $2;
load_immediate terminal_namespace -> term
load_immediate 0 -> mode
syscall OPEN term mode -> term // Terminal term = open("/dev/term/0", 0);
nat b $1 load_immediate terminal_namespace -> term;
real_to_nat a -> b load_immediate 0 -> mode;
load_immediate prompt -> $7 syscall OPEN term mode -> term; // Terminal term = open("/dev/term/0", 0);
string_length $7 -> $8
syscall WRITE term $7 $8 // print prompt
str user_string $9 nat b $1;
load_immediate 32 -> $8 real_to_nat a -> b;
malloc $8 -> user_string load_immediate prompt -> $7;
syscall READ term user_string $8 // read in max 32 byte string string_length $7 -> $8;
syscall WRITE term $7 $8; // print prompt
call pln user_string -> void str user_string $9;
nat_to_string b -> $4 load_immediate 32 -> $8;
call pln $4 -> void malloc $8 -> user_string;
real_to_string a -> $3 syscall READ term user_string; $8 // read in max 32 byte string
call pln $3 -> void
exit 0
function pln (str message $0) call pln user_string -> void;
str ts $1 nat_to_string b -> $4;
int mode $5 call pln $4 -> void;
int msg_length $2 real_to_string a -> $3;
str nl $3 call pln $3 -> void;
int nl_length $4 exit 0;
load_immediate terminal_namespace -> ts function pln (str message $0);
load_immediate 0 -> mode str ts $1;
syscall OPEN ts mode -> ts int mode $5;
strlen message -> msg_length int msg_length $2;
syscall WRITE ts message msg_length str nl $3;
load_immediate new_line -> nl int nl_length $4;
strlen nl -> nl_length
syscall WRITE ts nl nl_length load_immediate terminal_namespace -> ts;
return load_immediate 0 -> mode;
syscall OPEN ts mode -> ts;
strlen message -> msg_length;
syscall WRITE ts message msg_length ;
load_immediate new_line -> nl;
strlen nl -> nl_length;
syscall WRITE ts nl nl_length;
return;

View File

@ -1,40 +1,40 @@
global str terminal_namespace = "/dev/term/0" global str terminal_namespace = "/dev/term/0";
global str prompt = "Enter a string:" global str prompt = "Enter a string:";
global str new_line = "\n" global str new_line = "\n";
function main () function main ()
int mode $11; int mode $11;
str term $10; str term $10;
load_immediate terminal_namespace -> term load_immediate terminal_namespace -> term;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN term mode -> term // Terminal term = open("/dev/term/0", 0); syscall OPEN term mode -> term; // Terminal term = open("/dev/term/0", 0);
load_immediate prompt -> $7 load_immediate prompt -> $7;
string_length $7 -> $8 string_length $7 -> $8;
syscall WRITE term $7 $8 // print prompt syscall WRITE term $7 $8; // print prompt
str user_string $9 str user_string $9;
load_immediate 32 -> $8 load_immediate 32 -> $8;
malloc $8 -> user_string malloc $8 -> user_string;
syscall READ term user_string $8 // read in max 32 byte string syscall READ term user_string $8; // read in max 32 byte string
call pln user_string -> void call pln user_string -> void;
exit 0 exit 0;
function pln (str message $0) function pln (str message $0)
str ts $1 str ts $1;
int mode $5 int mode $5;
int msg_length $2 int msg_length $2;
str nl $3 str nl $3;
int nl_length $4 int nl_length $4;
load_immediate terminal_namespace -> ts load_immediate terminal_namespace -> ts;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN ts mode -> ts syscall OPEN ts mode -> ts;
strlen message -> msg_length strlen message -> msg_length;
syscall WRITE ts message msg_length syscall WRITE ts message msg_length;
load_immediate new_line -> nl load_immediate new_line -> nl;
strlen nl -> nl_length strlen nl -> nl_length;
syscall WRITE ts nl nl_length syscall WRITE ts nl nl_length;
return return;

View File

@ -1,185 +1,185 @@
global const str screen_namespace = "/dev/screen/0" global const str screen_namespace = "/dev/screen/0";
global const str mouse_namespace = "/dev/mouse/0" global const str mouse_namespace = "/dev/mouse/0";
global const byte BLACK = 0 global const byte BLACK = 0;
global const byte WHITE = 255 global const byte WHITE = 255;
global const byte DARK_GRAY = 73 global const byte DARK_GRAY = 73;
global const byte GRAY = 146 global const byte GRAY = 146;
global const byte LIGHT_GRAY = 182 global const byte LIGHT_GRAY = 182;
global byte SELECTED_COLOR = 255 global byte SELECTED_COLOR = 255;
function main () function main ()
// Open screen // Open screen
plex screen $0 plex screen $0;
str screen_name $18 str screen_name $18;
int mode $11 int mode $11;
nat screen_buffer $21 nat screen_buffer $21;
// use load immediate because it a pointer to a string, not a value // use load immediate because it a pointer to a string, not a value
load_address screen_namespace -> screen_name load_address screen_namespace -> screen_name;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN screen_name mode -> screen // Screen screen = open("/dev/screen/0", 0); syscall OPEN screen_name mode -> screen; // Screen screen = open("/dev/screen/0", 0);
nat width $20 nat width $20;
nat size $22 nat size $22;
load_offset_32 screen 8 -> width // load width load_offset_32 screen 8 -> width; // load width
load_offset_32 screen 12 -> size // load size load_offset_32 screen 12 -> size; // load size
load_immediate 16 -> $1 // offset for screen buffer load_immediate 16 -> $1; // offset for screen buffer
add_nat screen $1 -> screen_buffer add_nat screen $1 -> screen_buffer;
// open mouse // open mouse
plex mouse $15 plex mouse $15;
str mouse_name $16 str mouse_name $16;
load_address mouse_namespace -> mouse_name load_address mouse_namespace -> mouse_name;
syscall OPEN mouse_name mode -> mouse // Mouse mouse = open("/dev/mouse/0", 0); syscall OPEN mouse_name mode -> mouse; // Mouse mouse = open("/dev/mouse/0", 0);
byte color $1 byte color $1;
nat x_pos $12 nat x_pos $12;
nat y_pos $13 nat y_pos $13;
load_absolute_32 BLACK -> color load_absolute_32 BLACK -> color;
load_immediate 1 -> x_pos load_immediate 1 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
load_absolute_32 WHITE -> color load_absolute_32 WHITE -> color;
load_immediate 21 -> x_pos load_immediate 21 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
// screen.draw // screen.draw
syscall WRITE screen screen_buffer size syscall WRITE screen screen_buffer size;
nat zero $11 nat zero $11;
loop draw_loop loop draw_loop
// load mouse click data // load mouse click data
syscall REFRESH mouse syscall REFRESH mouse;
byte left_down $9 byte left_down $9;
load_offset_8 mouse 16 -> left_down // load btn1 pressed load_offset_8 mouse 16 -> left_down; // load btn1 pressed
jump_eq_nat draw_loop left_down zero jump_eq_nat draw_loop left_down zero;
nat mouse_x $7 nat mouse_x $7;
nat mouse_y $8 nat mouse_y $8;
load_offset_32 mouse 8 -> mouse_x // load x load_offset_32 mouse 8 -> mouse_x; // load x
load_offset_32 mouse 12 -> mouse_y // load y load_offset_32 mouse 12 -> mouse_y; // load y
nat box_size $14 nat box_size $14;
load_immediate 20 -> box_size load_immediate 20 -> box_size;
// first row // first row
load_absolute_32 BLACK -> color load_absolute_32 BLACK -> color;
load_immediate 1 -> x_pos load_immediate 1 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void;
load_absolute_32 WHITE -> color load_absolute_32 WHITE -> color;
load_immediate 21 -> x_pos load_immediate 21 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void;
syscall WRITE screen screen_buffer size syscall WRITE screen screen_buffer size;
byte selected_color $25 byte selected_color $25;
load_absolute_32 SELECTED_COLOR -> selected_color load_absolute_32 SELECTED_COLOR -> selected_color;
nat brush_size $19 nat brush_size $19;
load_immediate 5 -> brush_size load_immediate 5 -> brush_size;
call draw_box screen_buffer width selected_color mouse_x mouse_y brush_size brush_size -> void call draw_box screen_buffer width selected_color mouse_x mouse_y brush_size brush_size -> void;
jump draw_loop jump draw_loop;
// Flush and exit // Flush and exit
exit 0 exit 0;
function set_color_if_clicked (int click_x $0, int click_y $1, function set_color_if_clicked (int click_x $0, int click_y $1,
int box_x $2, int box_y $3, byte color $4, int box_size $5) int box_x $2, int box_y $3, byte color $4, int box_size $5)
// Compute right // Compute right
int right_edge $6 int right_edge $6;
add_int box_x box_size -> right_edge add_int box_x box_size -> right_edge;
// Compute bottom = box_y + box_size // Compute bottom = box_y + box_size
int bottom_edge $7 int bottom_edge $7;
add_int box_y box_size -> bottom_edge add_int box_y box_size -> bottom_edge;
// Bounds check: x in [box_x, right] and y in [box_y, bottom] // Bounds check: x in [box_x, right] and y in [box_y, bottom]
jump_lt_int fail click_x box_x jump_lt_int fail click_x box_x;
jump_ge_int fail click_x right_edge jump_ge_int fail click_x right_edge;
jump_lt_int fail click_y box_y jump_lt_int fail click_y box_y;
jump_ge_int fail click_y bottom_edge jump_ge_int fail click_y bottom_edge;
store_absolute_8 SELECTED_COLOR color store_absolute_8 SELECTED_COLOR color;
else fail else fail
return return;
function draw_outlined_swatch(nat base $0, function draw_outlined_swatch(nat base $0,
byte color $1, int x $2, int y $3, int width $4) byte color $1, int x $2, int y $3, int width $4)
// Constants // Constants
nat background_color $5 nat background_color $5;
load_absolute_32 GRAY -> background_color load_absolute_32 GRAY -> background_color;
byte selected_color $10 byte selected_color $10;
load_absolute_32 SELECTED_COLOR -> selected_color load_absolute_32 SELECTED_COLOR -> selected_color;
jump_eq_int set_selected selected_color color jump_eq_int set_selected selected_color color;
jump end_set_selected jump end_set_selected;
set_selected: do set_selected
load_absolute_32 DARK_GRAY -> background_color load_absolute_32 DARK_GRAY -> background_color;
end_set_selected: else end_set_selected
nat outline_size $6 nat outline_size $6;
load_immediate 20 -> outline_size load_immediate 20 -> outline_size;
nat fill_size $7 nat fill_size $7;
load_immediate 17 -> fill_size load_immediate 17 -> fill_size;
nat offset $8 nat offset $8;
load_immediate 2 -> offset load_immediate 2 -> offset;
call draw_box base width background_color x y outline_size outline_size -> void call draw_box base width background_color x y outline_size outline_size -> void;
add_int x offset -> $9 // x + 2 add_int x offset -> $9; // x + 2
add_int y offset -> $10 // y + 2 add_int y offset -> $10; // y + 2
call draw_box base width color $9 $10 fill_size fill_size -> void call draw_box base width color $9 $10 fill_size fill_size -> void;
return return;
function draw_box (nat base $0, nat screen_width $1, function draw_box (nat base $0, nat screen_width $1,
byte color $2, nat x_start $3, nat y_start $4, byte color $2, nat x_start $3, nat y_start $4,
nat width $5, nat height $6) nat width $5, nat height $6)
// Compute start address: base + y*640 + x // Compute start address: base + y*640 + x
nat offset $15 nat offset $15;
mul_int y_start screen_width -> offset mul_int y_start screen_width -> offset;
add_int offset x_start -> offset add_int offset x_start -> offset;
add_nat offset base -> offset add_nat offset base -> offset;
nat fat_ptr_size $25 nat fat_ptr_size $25;
load_immediate 4 -> fat_ptr_size load_immediate 4 -> fat_ptr_size;
add_nat offset fat_ptr_size -> offset // need to add offset for fat pointer size add_nat offset fat_ptr_size -> offset; // need to add offset for fat pointer size
int i $30 int i $30;
load_immediate 1 -> i load_immediate 1 -> i;
int zero $26 int zero $26;
load_immediate 0 -> zero load_immediate 0 -> zero;
int row_end $27 int row_end $27;
nat pixel_ptr $29 nat pixel_ptr $29;
loop draw_box_outer loop draw_box_outer
add_int offset width -> row_end // current + width add_int offset width -> row_end; // current + width
register_move offset -> pixel_ptr // set pixel point register_move offset -> pixel_ptr; // set pixel point
memset_8 pixel_ptr color width // draw row memset_8 pixel_ptr color width; // draw row
add_int offset screen_width -> offset // next row += 640 add_int offset screen_width -> offset; // next row += 640
sub_int height i -> height // decrement row count sub_int height i -> height; // decrement row count
jump_gt_int draw_box_outer height zero jump_gt_int draw_box_outer height zero;
return return;

View File

@ -1,184 +1,185 @@
global const str screen_namespace = "/dev/screen/0" global const str screen_namespace = "/dev/screen/0";
global const str mouse_namespace = "/dev/mouse/0" global const str mouse_namespace = "/dev/mouse/0";
global const byte BLACK = 0 global const byte BLACK = 0;
global const byte WHITE = 255 global const byte WHITE = 255;
global const byte DARK_GRAY = 73 global const byte DARK_GRAY = 73;
global const byte GRAY = 146 global const byte GRAY = 146;
global const byte LIGHT_GRAY = 182 global const byte LIGHT_GRAY = 182;
global byte SELECTED_COLOR = 255 global byte SELECTED_COLOR = 255;
function main () function main ()
// Open screen // Open screen
plex screen $0 plex screen $0;
str screen_name $18 str screen_name $18;
int mode $11 int mode $11;
nat screen_buffer $21 nat screen_buffer $21;
// use load immediate because it a pointer to a string, not a value // use load immediate because it a pointer to a string, not a value
load_address screen_namespace -> screen_name load_address screen_namespace -> screen_name;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN screen_name mode -> screen // Screen screen = open("/dev/screen/0", 0); syscall OPEN screen_name mode -> screen; // Screen screen = open("/dev/screen/0", 0);
nat width $20 nat width $20;
nat size $22 nat size $22;
load_offset_32 screen 8 -> width // load width load_offset_32 screen 8 -> width; // load width
load_offset_32 screen 12 -> size // load size load_offset_32 screen 12 -> size; // load size
load_immediate 16 -> $1 // offset for screen buffer load_immediate 16 -> $1; // offset for screen buffer
add_nat screen $1 -> screen_buffer add_nat screen $1 -> screen_buffer;
// open mouse // open mouse
plex mouse $15 plex mouse $15;
str mouse_name $16 str mouse_name $16;
load_address mouse_namespace -> mouse_name load_address mouse_namespace -> mouse_name;
syscall OPEN mouse_name mode -> mouse // Mouse mouse = open("/dev/mouse/0", 0); syscall OPEN mouse_name mode -> mouse; // Mouse mouse = open("/dev/mouse/0", 0);
byte color $1 byte color $1;
nat x_pos $12 nat x_pos $12;
nat y_pos $13 nat y_pos $13;
load_absolute_32 BLACK -> color load_absolute_32 BLACK -> color;
load_immediate 1 -> x_pos load_immediate 1 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch [screen_buffer color x_pos y_pos width] -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
load_absolute_32 WHITE -> color load_absolute_32 WHITE -> color;
load_immediate 21 -> x_pos load_immediate 21 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
// screen.draw// // screen.draw
syscall WRITE screen screen_buffer size syscall WRITE screen screen_buffer size;
nat zero $11 nat zero $11;
loop draw_loop loop draw_loop
// load mouse click data // load mouse click data
syscall REFRESH mouse syscall REFRESH mouse;
byte left_down $9 byte left_down $9;
load_offset_8 mouse 16 -> left_down // load btn1 pressed load_offset_8 mouse 16 -> left_down; // load btn1 pressed
jump_eq_nat draw_loop left_down zero jump_eq_nat draw_loop left_down zero;
nat mouse_x $7 nat mouse_x $7;
nat mouse_y $8 nat mouse_y $8;
load_offset_32 mouse 8 -> mouse_x // load x load_offset_32 mouse 8 -> mouse_x; // load x
load_offset_32 mouse 12 -> mouse_y // load y load_offset_32 mouse 12 -> mouse_y; // load y
nat box_size $14 nat box_size $14;
load_immediate 20 -> box_size load_immediate 20 -> box_size;
// first row // first row
load_absolute_32 BLACK -> color load_absolute_32 BLACK -> color;
load_immediate 1 -> x_pos load_immediate 1 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void;
load_absolute_32 WHITE -> color load_absolute_32 WHITE -> color;
load_immediate 21 -> x_pos load_immediate 21 -> x_pos;
load_immediate 1 -> y_pos load_immediate 1 -> y_pos;
call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void call draw_outlined_swatch screen_buffer color x_pos y_pos width -> void;
call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void call set_color_if_clicked mouse_x mouse_y x_pos y_pos color box_size -> void;
syscall WRITE screen screen_buffer size syscall WRITE screen screen_buffer size;
byte selected_color $25 byte selected_color $25;
load_absolute_32 SELECTED_COLOR -> selected_color load_absolute_32 SELECTED_COLOR -> selected_color;
nat brush_size $19 nat brush_size $19;
load_immediate 5 -> brush_size load_immediate 5 -> brush_size;
call draw_box screen_buffer width selected_color mouse_x mouse_y brush_size brush_size -> void call draw_box screen_buffer width selected_color mouse_x mouse_y brush_size brush_size -> void;
jump draw_loop jump draw_loop;
// Flush and exit // Flush and exit
exit 0 exit 0;
function set_color_if_clicked (int click_x $0, int click_y $1, function set_color_if_clicked (int click_x $0, int click_y $1,
int box_x $2, int box_y $3, byte color $4, int box_size $5) int box_x $2, int box_y $3, byte color $4, int box_size $5)
// Compute right // Compute right
int right_edge $6 int right_edge $6;
add_int box_x box_size -> right_edge add_int box_x box_size -> right_edge;
// Compute bottom = box_y + box_size // Compute bottom = box_y + box_size
int bottom_edge $7 int bottom_edge $7;
add_int box_y box_size -> bottom_edge add_int box_y box_size -> bottom_edge;
// Bounds check: x in [box_x, right] and y in [box_y, bottom] // Bounds check: x in [box_x, right] and y in [box_y, bottom]
jump_lt_int fail click_x box_x jump_lt_int fail click_x box_x;
jump_ge_int fail click_x right_edge jump_ge_int fail click_x right_edge;
jump_lt_int fail click_y box_y jump_lt_int fail click_y box_y;
jump_ge_int fail click_y bottom_edge jump_ge_int fail click_y bottom_edge;
store_absolute_8 SELECTED_COLOR color store_absolute_8 SELECTED_COLOR color;
else fail else fail
return return;
function draw_outlined_swatch(nat base $0, function draw_outlined_swatch(nat base $0,
byte color $1, int x $2, int y $3, int width $4) byte color $1, int x $2, int y $3, int width $4)
// Constants // Constants
nat background_color $5 nat background_color $5;
load_absolute_32 GRAY -> background_color load_absolute_32 GRAY -> background_color;
byte selected_color $10 byte selected_color $10;
load_absolute_32 SELECTED_COLOR -> selected_color load_absolute_32 SELECTED_COLOR -> selected_color;
jump_eq_int set_selected selected_color color jump_eq_int set_selected selected_color color;
jump end_set_selected jump end_set_selected;
do set_selected do set_selected
load_absolute_32 DARK_GRAY -> background_color load_absolute_32 DARK_GRAY -> background_color;
else end_set_selected else end_set_selected
nat outline_size $6 nat outline_size $6;
load_immediate 20 -> outline_size load_immediate 20 -> outline_size;
nat fill_size $7 nat fill_size $7;
load_immediate 17 -> fill_size load_immediate 17 -> fill_size;
nat offset $8 nat offset $8;
load_immediate 2 -> offset load_immediate 2 -> offset;
call draw_box base width background_color x y outline_size outline_size -> void call draw_box base width background_color x y outline_size outline_size -> void;
add_int x offset -> $9 // x + 2 add_int x offset -> $9; // x + 2
add_int y offset -> $10 // y + 2 add_int y offset -> $10; // y + 2
call draw_box base width color $9 $10 fill_size fill_size -> void call draw_box base width color $9 $10 fill_size fill_size -> void;
return return;
function draw_box (nat base $0, nat screen_width $1, function draw_box (nat base $0, nat screen_width $1,
byte color $2, nat x_start $3, nat y_start $4, nat width $5, nat height $6) byte color $2, nat x_start $3, nat y_start $4,
nat width $5, nat height $6)
// Compute start address: base + y*640 + x // Compute start address: base + y*640 + x
nat offset $15 nat offset $15;
mul_int y_start screen_width -> offset mul_int y_start screen_width -> offset;
add_int offset x_start -> offset add_int offset x_start -> offset;
add_nat offset base -> offset add_nat offset base -> offset;
nat fat_ptr_size $25 nat fat_ptr_size $25;
load_immediate 4 -> fat_ptr_size load_immediate 4 -> fat_ptr_size;
add_nat offset fat_ptr_size -> offset // need to add offset for fat pointer size add_nat offset fat_ptr_size -> offset; // need to add offset for fat pointer size
int i $30 int i $30;
load_immediate 1 -> i load_immediate 1 -> i;
int zero $26 int zero $26;
load_immediate 0 -> zero load_immediate 0 -> zero;
int row_end $27 int row_end $27;
nat pixel_ptr $29 nat pixel_ptr $29;
loop draw_box_outer loop draw_box_outer
add_int offset width -> row_end // current + width add_int offset width -> row_end; // current + width
register_move offset -> pixel_ptr // set pixel point register_move offset -> pixel_ptr; // set pixel point
memset_8 pixel_ptr color width // draw row memset_8 pixel_ptr color width; // draw row
add_int offset screen_width -> offset // next row += 640 add_int offset screen_width -> offset; // next row += 640
sub_int height i -> height // decrement row count sub_int height i -> height; // decrement row count
jump_gt_int draw_box_outer height zero jump_gt_int draw_box_outer height zero;
return return;

View File

@ -1,31 +1,31 @@
global str terminal_namespace = "/dev/term/0" global str terminal_namespace = "/dev/term/0";
global real x = 1.0 global real x = 1.0;
global real y = 1.0 global real y = 1.0;
function main () function main ()
real x $0 real x $0;
load_absolute_32 &x -> x load_absolute_32 x -> x;
real y $1 real y $1;
load_absolute_32 &y -> y load_absolute_32 y -> y;
real result $2 real result $2;
add_real x y -> result add_real x y -> result;
str result_str $3 str result_str $3;
real_to_string result -> result_str real_to_string result -> result_str;
call &pln result_str -> void call pln result_str -> void;
exit 0 exit 0;
function pln (str message $0) function pln (str message $0)
str term $1 str term $1;
int msg_length $2 int msg_length $2;
str nl $3 str nl $3;
int nl_length $4 int nl_length $4;
int mode $5 int mode $5;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN &terminal_namespace mode -> term syscall OPEN terminal_namespace mode -> term;
strlen message -> msg_length strlen message -> msg_length;
syscall WRITE term message msg_length syscall WRITE term message msg_length;
load_address new_line -> nl load_address new_line -> nl;
strlen nl -> nl_length strlen nl -> nl_length;
syscall WRITE term nl nl_length syscall WRITE term nl nl_length;
return return;

View File

@ -1,89 +1,89 @@
global str screen_namespace = "/dev/screen/0" global str screen_namespace = "/dev/screen/0";
global str mouse_namespace = "/dev/mouse/0" global str mouse_namespace = "/dev/mouse/0";
global str terminal_namespace = "/dev/term/0" global str terminal_namespace = "/dev/term/0";
global str new_line = "\n" global str new_line = "\n";
global byte white = 255 global byte white = 255;
function main () function main ()
plex screen $0 plex screen $0;
plex mouse $1 plex mouse $1;
str tmp_str $2 str tmp_str $2;
byte color $3 byte color $3;
bool left_down $4 bool left_down $4;
int mode $5 int mode $5;
nat offset_temp $6 nat offset_temp $6;
nat x $7 nat x $7;
nat y $8 nat y $8;
nat width $9 nat width $9;
nat screen_buffer $10 nat screen_buffer $10;
nat buffer_size $11 nat buffer_size $11;
nat pixel_pos $12 nat pixel_pos $12;
load_immediate screen_namespace -> screen load_immediate screen_namespace -> screen;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN screen mode -> screen syscall OPEN screen mode -> screen;
nat_to_string screen -> tmp_str nat_to_string screen -> tmp_str;
call pln tmp_str -> void call pln tmp_str -> void;
load_offset_32 screen 8 -> width load_offset_32 screen 8 -> width;
nat_to_string width -> tmp_str nat_to_string width -> tmp_str;
call pln tmp_str -> void call pln tmp_str -> void;
load_offset_32 screen 12 -> buffer_size load_offset_32 screen 12 -> buffer_size;
nat_to_string buffer_size -> tmp_str nat_to_string buffer_size -> tmp_str;
call pln tmp_str -> void call pln tmp_str -> void;
load_immediate 16 -> offset_temp load_immediate 16 -> offset_temp;
add_nat screen offset_temp -> screen_buffer add_nat screen offset_temp -> screen_buffer;
nat_to_string screen_buffer -> tmp_str nat_to_string screen_buffer -> tmp_str;
call pln tmp_str -> void call pln tmp_str -> void;
// open mouse // open mouse
load_immediate mouse_namespace -> mouse load_immediate mouse_namespace -> mouse;
syscall OPEN mouse mode -> mouse syscall OPEN mouse mode -> mouse;
syscall WRITE screen screen_buffer buffer_size // redraw syscall WRITE screen screen_buffer buffer_size; // redraw
loop draw_loop loop draw_loop
// load mouse click data // load mouse click data
syscall STAT mouse syscall STAT mouse;
load_offset_8 mouse 16 -> left_down load_offset_8 mouse 16 -> left_down;
jump_eq_nat draw_loop left_down mode // mode = 0 / false jump_eq_nat draw_loop left_down mode; // mode = 0 / false
load_offset_32 mouse 8 -> x load_offset_32 mouse 8 -> x;
load_offset_32 mouse 12 -> y load_offset_32 mouse 12 -> y;
// Compute start address: y *width + x // Compute start address: y *width + x
mul_nat y width -> pixel_pos mul_nat y width -> pixel_pos;
add_nat x pixel_pos -> pixel_pos add_nat x pixel_pos -> pixel_pos;
add_nat screen_buffer pixel_pos -> pixel_pos add_nat screen_buffer pixel_pos -> pixel_pos;
load_immediate 4 -> fat_ptr_size load_immediate 4 -> fat_ptr_size;
add_nat pixel_pos fat_ptr_size -> pixel_pos add_nat pixel_pos fat_ptr_size -> pixel_pos;
load_absolute_32 white -> color load_absolute_32 white -> color;
store_absolute_8 pixel_pos color // draw color at screen [x,y] store_absolute_8 pixel_pos color; // draw color at screen [x,y]
syscall WRITE screen screen_buffer buffer_size // redraw syscall WRITE screen screen_buffer buffer_size; // redraw
jump draw_loop jump draw_loop;
exit 0 exit 0;
function pln (str message $0) function pln (str message $0)
str ts $1 str ts $1;
int mode $5 int mode $5;
int msg_length $2 int msg_length $2;
str nl $3 str nl $3;
int nl_length $4 int nl_length $4;
load_immediate terminal_namespace -> ts load_immediate terminal_namespace -> ts;
load_immediate 0 -> mode load_immediate 0 -> mode;
syscall OPEN ts mode -> ts syscall OPEN ts mode -> ts;
strlen message -> msg_length strlen message -> msg_length;
syscall WRITE ts message msg_length syscall WRITE ts message msg_length ;
load_immediate new_line -> nl load_immediate new_line -> nl;
strlen nl -> nl_length strlen nl -> nl_length;
syscall WRITE ts nl nl_length syscall WRITE ts nl nl_length;
return return;