Make compiler as iterations on the assembler
This commit is contained in:
parent
4114812146
commit
2bb1166085
|
|
@ -27,7 +27,6 @@ typedef enum {
|
|||
typedef struct symbol_s Symbol;
|
||||
typedef struct symbol_tab_s SymbolTable;
|
||||
typedef struct scope_tab_s ScopeTable;
|
||||
typedef struct assembler_s Assembler;
|
||||
|
||||
#define MAX_SYMBOL_NAME_LENGTH 64
|
||||
struct symbol_s {
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -2,7 +2,9 @@
|
|||
#define UNDAR_COMPILER_H
|
||||
|
||||
#import "../../vm/common.h"
|
||||
#include "../../vm/opcodes.h"
|
||||
|
||||
typedef enum { GLOBAL, LOCAL, VAR } ScopeType;
|
||||
typedef enum {
|
||||
VOID,
|
||||
BOOL,
|
||||
|
|
@ -21,17 +23,12 @@ typedef enum {
|
|||
FUNCTION
|
||||
} SymbolType;
|
||||
|
||||
typedef struct value_type_s ValueType;
|
||||
typedef struct function_def_s FunctionDef;
|
||||
typedef struct function_tab_s FunctionTable;
|
||||
typedef struct plex_def_s PlexDef;
|
||||
typedef struct plex_tab_s PlexTable;
|
||||
typedef struct array_def_s ArrayDef;
|
||||
typedef struct array_tab_s ArrayTable;
|
||||
typedef struct symbol_s Symbol;
|
||||
typedef struct symbol_tab_s SymbolTable;
|
||||
typedef struct names_tab_s NamesTable;
|
||||
typedef struct value_type_s ValueType;
|
||||
typedef struct plex_fields_tab_s PlexFieldsTable;
|
||||
typedef struct plex_def_s PlexDef;
|
||||
typedef struct plex_tab_s PlexTable;
|
||||
typedef struct scope_s Scope;
|
||||
typedef struct scope_tab_s ScopeTable;
|
||||
|
||||
|
|
@ -42,13 +39,6 @@ struct value_type_s {
|
|||
u32 table_ref; // if it is a heap object
|
||||
};
|
||||
|
||||
struct function_def_s {
|
||||
u32 name;
|
||||
ValueType args[8];
|
||||
u8 arg_count;
|
||||
ValueType return_type;
|
||||
};
|
||||
|
||||
struct plex_def_s {
|
||||
u32 name;
|
||||
u32 size;
|
||||
|
|
@ -56,22 +46,6 @@ struct plex_def_s {
|
|||
u32 field_count;
|
||||
};
|
||||
|
||||
struct array_def_s {
|
||||
ValueType type;
|
||||
u32 length;
|
||||
u32 logical_size; // length of the array
|
||||
u32 physical_size; // logical_size * type_size + fat pointer
|
||||
};
|
||||
|
||||
struct symbol_s {
|
||||
u32 name;
|
||||
ValueType type;
|
||||
union {
|
||||
u32 local; // register
|
||||
u32 global; // address
|
||||
} ref;
|
||||
};
|
||||
|
||||
struct plex_fields_tab_s {
|
||||
u32 *plex_refs;
|
||||
ValueType *fields;
|
||||
|
|
@ -85,38 +59,30 @@ struct plex_tab_s {
|
|||
u32 capacity;
|
||||
};
|
||||
|
||||
struct array_tab_s {
|
||||
ArrayDef *symbols;
|
||||
u32 count;
|
||||
u32 capacity;
|
||||
};
|
||||
|
||||
struct function_tab_s {
|
||||
FunctionDef *symbols;
|
||||
u32 count;
|
||||
u32 capacity;
|
||||
};
|
||||
|
||||
struct names_tab_s {
|
||||
char **names;
|
||||
u32 count;
|
||||
u32 capacity;
|
||||
#define MAX_SYMBOL_NAME_LENGTH 64
|
||||
struct symbol_s {
|
||||
char name[MAX_SYMBOL_NAME_LENGTH];
|
||||
u8 name_length;
|
||||
SymbolType type;
|
||||
ScopeType scope;
|
||||
u32 ref; // vm->mp if global, vm->pc local, register if var
|
||||
u32 size; // size of symbol
|
||||
};
|
||||
|
||||
struct symbol_tab_s {
|
||||
Symbol *symbols;
|
||||
u32 count;
|
||||
u32 capacity;
|
||||
};
|
||||
|
||||
struct scope_s {
|
||||
SymbolTable table;
|
||||
Symbol symbols[256];
|
||||
u8 count;
|
||||
i32 parent;
|
||||
};
|
||||
|
||||
struct scope_tab_s {
|
||||
Scope *scopes;
|
||||
SymbolTable *scopes;
|
||||
u32 count;
|
||||
u32 capacity;
|
||||
i32 scope_ref;
|
||||
};
|
||||
|
||||
bool compile(VM *vm, ScopeTable *st, char *source);
|
||||
extern bool table_realloc(ScopeTable *table);/* implement this in arch/ not here */
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* Constants
|
||||
*/
|
||||
str term_namespace = "/dev/term/0";
|
||||
str nl = "\n";
|
||||
int x = 0;
|
||||
int y = 1;
|
||||
|
||||
plex Terminal {
|
||||
nat handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
function main() {
|
||||
int tmp0 = x;
|
||||
int tmp1 = y;
|
||||
int tmp2 = add(tmp0, tmp1);
|
||||
str tmp3 = tmp2 as str;
|
||||
pln(tmp3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two numbers together
|
||||
*/
|
||||
function add(int a, int b) int {
|
||||
int tmp0 = a + b;
|
||||
return tmp0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print with a newline
|
||||
*/
|
||||
function pln(str message) {
|
||||
str term_ns = term_namespace;
|
||||
int mode = 0;
|
||||
|
||||
Terminal term = open(term_ns, mode);
|
||||
|
||||
int msg_len = message.length;
|
||||
write(term, message, msg_len);
|
||||
|
||||
str nl_local = nl;
|
||||
int nl_len = nl.length;
|
||||
write(term, nl_local, nl_len);
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
/**
|
||||
* Constants
|
||||
*/
|
||||
str term_namespace = "/dev/term/0";
|
||||
str nl = "\n";
|
||||
|
||||
plex Terminal {
|
||||
nat handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
function main() {
|
||||
int fib = 35;
|
||||
int ans = fib(35);
|
||||
str ans_s = ans as str;
|
||||
pln(ans_s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively calculate fibonacci
|
||||
*/
|
||||
function fib(int n) int {
|
||||
int base_check = 2;
|
||||
|
||||
jump_lt_int base_case n base_check;
|
||||
jump end1;
|
||||
do base_case;
|
||||
return n;
|
||||
else base_case_end;
|
||||
|
||||
int tmp_c2 = 2;
|
||||
int tmp_c2_n = n - tmp_c2;
|
||||
int ans_c2 = fib(tmp_c2_n);
|
||||
|
||||
int tmp_c1 = 1;
|
||||
int tmp_c1_n = tmp_c1 - n;
|
||||
int ans_c1 = fib(tmp_c1_n);
|
||||
|
||||
int ans = tmp_c1_n + tmp_c2_n;
|
||||
return ans;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print with a newline
|
||||
*/
|
||||
function pln(str message) {
|
||||
str term_ns = term_namespace;
|
||||
int mode = 0;
|
||||
|
||||
Terminal term = open(term_ns, mode);
|
||||
|
||||
int msg_len = message.length;
|
||||
write(term, message, msg_len);
|
||||
|
||||
str nl_local = nl;
|
||||
int nl_len = nl.length;
|
||||
write(term, nl_local, nl_len);
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
str term_namespace = "/dev/term/0";
|
||||
str hello = "nuqneH 'u'?";
|
||||
str nl = "\n";
|
||||
|
||||
plex Terminal {
|
||||
nat handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
function main() {
|
||||
str msg = hello;
|
||||
pln(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print with a newline
|
||||
*/
|
||||
function pln(str message) {
|
||||
str term_ns = term_namespace;
|
||||
int mode = 0;
|
||||
|
||||
Terminal term = open(term_ns, mode);
|
||||
|
||||
int msg_len = message.length;
|
||||
write(term, message, msg_len);
|
||||
|
||||
str nl_local = nl;
|
||||
int nl_len = nl.length;
|
||||
write(term, nl_local, nl_len);
|
||||
}
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
/**
|
||||
* Constants
|
||||
* Plexes
|
||||
*/
|
||||
const str nl = "\n";
|
||||
|
||||
plex Terminal {
|
||||
nat handle;
|
||||
}
|
||||
|
|
@ -20,5 +18,6 @@ function main() {
|
|||
function pln(str message) {
|
||||
Terminal term = open("/dev/term/0", 0);
|
||||
write(term, message, message.length);
|
||||
const str nl = "\n";
|
||||
write(term, nl, nl.length);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
str term_namespace = "/dev/term/0";
|
||||
str prompt = "Enter a string:";
|
||||
str nl = "\n";
|
||||
|
||||
|
||||
plex Terminal {
|
||||
nat handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
function main() {
|
||||
str term_ns = term_namespace;
|
||||
int mode = 0;
|
||||
|
||||
Terminal term = open(term_ns, mode);
|
||||
|
||||
real a = 5.0;
|
||||
|
||||
// do (int i = 5000; i >= 0, i = i - 1)
|
||||
int i = 5000;
|
||||
int tmp0 = 0;
|
||||
int tmp1 = 1;
|
||||
int tmp2 = 5.0;
|
||||
loop loop_body {
|
||||
a = a + tmp2;
|
||||
i = i - tmp1;
|
||||
jump_ge_int loop_body i tmp0;
|
||||
}
|
||||
|
||||
nat b = a as nat;
|
||||
str local_prompt = prompt;
|
||||
pln(local_prompt);
|
||||
|
||||
nat size = 32;
|
||||
str user_string = malloc(size);
|
||||
read(term, user_string, size);
|
||||
|
||||
str a_str = a as str;
|
||||
pln(a_str);
|
||||
|
||||
str b_str = b as str;
|
||||
pln(b_str);
|
||||
|
||||
pln(user_string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print with a newline
|
||||
*/
|
||||
function pln(str message) {
|
||||
str term_ns = term_namespace;
|
||||
int mode = 0;
|
||||
|
||||
Terminal term = open(term_ns, mode);
|
||||
|
||||
int msg_len = message.length;
|
||||
write(term, message, msg_len);
|
||||
|
||||
str nl_local = nl;
|
||||
int nl_len = nl.length;
|
||||
write(term, nl_local, nl_len);
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* Constants
|
||||
*/
|
||||
const str nl = "\n";
|
||||
|
||||
plex Terminal {
|
||||
nat handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
function main() {
|
||||
Terminal term = open("/dev/term/0", 0);
|
||||
pln(term, "Enter a string: ");
|
||||
pln(term, term.read(32));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print with a newline
|
||||
*/
|
||||
function pln(Terminal term, str message) {
|
||||
write(term, message, message.length);
|
||||
write(term, nl, nl.length);
|
||||
}
|
||||
|
|
@ -0,0 +1,278 @@
|
|||
str screen_namespace = "/dev/screen/0";
|
||||
str mouse_namespace = "/dev/mouse/0";
|
||||
byte BLACK = 0;
|
||||
byte WHITE = 255;
|
||||
byte DARK_GRAY = 73;
|
||||
byte GRAY = 146;
|
||||
byte LIGHT_GRAY = 182;
|
||||
byte CHARCOAL = 36;
|
||||
byte DARK_RED = 128;
|
||||
byte RED = 224;
|
||||
byte DARK_YELLOW = 144;
|
||||
byte YELLOW = 252;
|
||||
byte DARK_TEAL = 9;
|
||||
byte TEAL = 18;
|
||||
byte DARK_GREEN = 12;
|
||||
byte GREEN = 16;
|
||||
byte LIME = 28;
|
||||
byte LIGHT_CYAN = 159;
|
||||
byte NAVY = 2;
|
||||
byte BLUE = 3;
|
||||
byte DEEP_SKY_BLUE = 10;
|
||||
byte LIGHT_BLUE = 19;
|
||||
byte PURPLE = 131;
|
||||
byte LIGHT_PURPLE = 147;
|
||||
byte DARK_MAGENTA = 130;
|
||||
byte MAGENTA = 227;
|
||||
byte PLUM = 129;
|
||||
byte PINK = 226;
|
||||
byte SADDLE_BROWN = 72;
|
||||
byte PERU = 141;
|
||||
byte SIENNA = 136;
|
||||
byte ORANGE = 241;
|
||||
byte DARK_ORANGE = 208;
|
||||
byte GOLD = 244;
|
||||
byte SELECTED_COLOR = 255;
|
||||
|
||||
plex Screen {
|
||||
nat handle;
|
||||
nat width;
|
||||
nat height;
|
||||
byte[] buffer;
|
||||
}
|
||||
|
||||
plex Mouse {
|
||||
nat handle;
|
||||
nat x;
|
||||
nat y;
|
||||
bool left;
|
||||
bool right;
|
||||
bool middle;
|
||||
bool btn4;
|
||||
}
|
||||
|
||||
function main () {
|
||||
str screen_name = screen_namespace;
|
||||
int mode = 0;
|
||||
Screen screen = open(screen_name, mode);
|
||||
|
||||
nat width = screen.width;
|
||||
nat size = screen.size;
|
||||
nat screen_offset = 16;
|
||||
nat screen_buffer = screen_buffer + screen_offset;
|
||||
|
||||
// open mouse
|
||||
str mouse_name = mouse_namespace;
|
||||
Mouse mouse = open(mouse_name, mode);
|
||||
|
||||
byte color = BLACK;
|
||||
nat x_pos = 1;
|
||||
nat y_pos = 1;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = WHITE;
|
||||
x_pos = 21;
|
||||
y_pos = 1;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = CHARCOAL;
|
||||
x_pos = 1;
|
||||
y_pos = 21;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = DARK_GRAY;
|
||||
x_pos = 21;
|
||||
y_pos = 21;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = RED;
|
||||
x_pos = 1;
|
||||
y_pos = 41;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = ORANGE;
|
||||
x_pos = 21;
|
||||
y_pos = 41;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = YELLOW;
|
||||
x_pos = 1;
|
||||
y_pos = 61;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = GREEN;
|
||||
x_pos = 21;
|
||||
y_pos = 61;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = BLUE;
|
||||
x_pos = 1;
|
||||
y_pos = 81;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
color = PURPLE;
|
||||
x_pos = 21;
|
||||
y_pos = 81;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
|
||||
// screen.draw
|
||||
write(screen, screen_buffer, size);
|
||||
|
||||
nat zero = 0;
|
||||
|
||||
loop draw {
|
||||
// load mouse click data
|
||||
refresh(mouse);
|
||||
|
||||
byte left_down = mouse.down;
|
||||
|
||||
jump_eq_nat draw left_down zero; // if (!btn1.left) continue;
|
||||
|
||||
nat mouse_x = mouse.x;
|
||||
nat mouse_y = mouse.y;
|
||||
|
||||
nat box_size = 20;
|
||||
|
||||
// first row
|
||||
color = BLACK;
|
||||
x_pos = 1;
|
||||
y_pos = 1;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = WHITE;
|
||||
x_pos = 21;
|
||||
y_pos = 1;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = CHARCOAL;
|
||||
x_pos = 1;
|
||||
y_pos = 21;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
DARK_GRAY -> color;
|
||||
x_pos = 21;
|
||||
y_pos = 21;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = RED;
|
||||
x_pos = 1;
|
||||
y_pos = 41;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = ORANGE;
|
||||
x_pos = 21;
|
||||
y_pos = 41;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = YELLOW;
|
||||
x_pos = 1;
|
||||
y_pos = 61;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = GREEN;
|
||||
x_pos = 21;
|
||||
y_pos = 61;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = BLUE;
|
||||
x_pos = 1;
|
||||
y_pos = 81;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
color = PURPLE;
|
||||
x_pos = 21;
|
||||
y_pos = 81;
|
||||
draw_outlined_swatch(screen_buffer, color, x_pos, y_pos, width);
|
||||
set_color(mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
||||
|
||||
write(screen, screen_buffer, size);
|
||||
|
||||
byte selected_color = SELECTED_COLOR;
|
||||
|
||||
nat brush_size = 5;
|
||||
|
||||
draw_box(screen_buffer, width, selected_color, mouse_x, mouse_y, brush_size, brush_size);
|
||||
|
||||
jump draw;
|
||||
}
|
||||
|
||||
// Flush and exit
|
||||
exit 0;
|
||||
}
|
||||
|
||||
function set_color (int click_x, int click_y, int box_x, int box_y, byte check_color, int size) {
|
||||
|
||||
// Compute right
|
||||
int right_edge = box_x + size;
|
||||
|
||||
// Compute bottom = box_y + size
|
||||
int bottom_edge = box_y + size;
|
||||
|
||||
// Bounds check: x in [box_x, right] and y in [box_y, bottom]
|
||||
jump_lt_int fail click_x box_x;
|
||||
jump_gt_int fail click_x right_edge;
|
||||
jump_lt_int fail click_y box_y;
|
||||
jump_gt_int fail click_y bottom_edge;
|
||||
|
||||
SELECTED_COLOR = check_color;
|
||||
|
||||
else fail
|
||||
return;
|
||||
}
|
||||
|
||||
function draw_outlined_swatch(nat base, byte swatch_color, int x, int y, int width) {
|
||||
|
||||
nat background_color = GRAY;
|
||||
byte selected_color = SELECTED_COLOR;
|
||||
|
||||
jump_eq_int set_selected swatch_color selected_color;
|
||||
jump end_set_selected;
|
||||
do set_selected
|
||||
background_color = DARK_GRAY;
|
||||
else end_set_selected
|
||||
|
||||
nat outline_size = 20;
|
||||
nat fill_size = 17;
|
||||
|
||||
draw_box(base, width, background_color, x, y, outline_size, outline_size);
|
||||
|
||||
nat offset = 2;
|
||||
int xO = x + offset; // x + 2
|
||||
int yO = y + offset; // y + 2
|
||||
|
||||
draw_box(base, width, swatch_color, xO, yO, fill_size, fill_size);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
function draw_box (nat base, nat screen_width, byte box_color,
|
||||
nat x, nat y, nat width, nat height) {
|
||||
|
||||
nat fat_ptr_size = 4;
|
||||
|
||||
// Compute start address: base + y*640 + x
|
||||
nat offset = y * screen_width;
|
||||
offset = offset + x;
|
||||
offset = offset + base;
|
||||
offset = offset + fat_ptr_size; // need to add offset for fat pointer size
|
||||
|
||||
int i = 1;
|
||||
int zero = 0;
|
||||
|
||||
loop draw_box_outer {
|
||||
memset(offset, width, box_color); // draw row
|
||||
offset = offset + screen_width; // next row += 640
|
||||
height = height - i; // decrement row count
|
||||
jump_gt_int draw_box_outer height zero;
|
||||
}
|
||||
return;
|
||||
}
|
||||
Loading…
Reference in New Issue