WIP arrays, fix formatting

This commit is contained in:
zongor 2026-05-24 09:54:25 -07:00
parent cbb904abd9
commit deed734dc9
5 changed files with 156 additions and 86 deletions

View File

@ -18,11 +18,11 @@ scopes_init()
child.symbols = List_init(arena); child.symbols = List_init(arena);
child.locals_offset = 0; child.locals_offset = 0;
current = (Scope*)List_push(arena, parser.scopes, &child, sizeof(Scope)); current = (Scope *)List_push(arena, parser.scopes, &child, sizeof(Scope));
parser.current_scope = current; parser.current_scope = current;
} }
Scope* Scope *
scope_new() scope_new()
{ {
Scope *current; Scope *current;
@ -31,9 +31,9 @@ scope_new()
child.parent = parser.current_scope; child.parent = parser.current_scope;
child.locals_offset = 0; child.locals_offset = 0;
current = (Scope*)List_push(arena, parser.scopes, &child, sizeof(Scope)); current = (Scope *)List_push(arena, parser.scopes, &child, sizeof(Scope));
parser.current_scope = current; parser.current_scope = current;
parser.scope_idx++; parser.scope_idx++;
parser.depth++; parser.depth++;
return current; return current;
} }
@ -41,7 +41,7 @@ scope_new()
void void
scope_pop() scope_pop()
{ {
if (parser.current_scope) { if(parser.current_scope) {
parser.current_scope = parser.current_scope->parent; parser.current_scope = parser.current_scope->parent;
parser.depth--; parser.depth--;
} }
@ -50,10 +50,10 @@ scope_pop()
void void
scope_push() scope_push()
{ {
Scope *scope = List_get(parser.scopes, parser.scope_idx); Scope *scope = List_get(parser.scopes, parser.scope_idx);
parser.current_scope = scope; parser.current_scope = scope;
parser.depth++; parser.depth++;
parser.scope_idx++; parser.scope_idx++;
} }
Symbol * Symbol *
@ -65,7 +65,8 @@ scope_get_symbol(Scope *scope, const char *name, u32 name_length)
count = scope->symbols->count; count = scope->symbols->count;
for(i = 0; i < count; i++) { for(i = 0; i < count; i++) {
Symbol *sym = List_get(scope->symbols, i); Symbol *sym = List_get(scope->symbols, i);
if(sym->name_length == name_length && sleq(sym->name, name, name_length)) return sym; if(sym->name_length == name_length && sleq(sym->name, name, name_length))
return sym;
} }
return scope_get_symbol(scope->parent, name, name_length); return scope_get_symbol(scope->parent, name, name_length);
@ -99,7 +100,8 @@ scope_add_symbol(const char *name, u32 name_length, SymbolType type, u32 size)
if(type == SYMBOL_PLEX || type == SYMBOL_METHOD || type == SYMBOL_TRAIT) if(type == SYMBOL_PLEX || type == SYMBOL_METHOD || type == SYMBOL_TRAIT)
new_sym.fields = List_init(arena); new_sym.fields = List_init(arena);
return List_push(arena, parser.current_scope->symbols, &new_sym, sizeof(Symbol)); return List_push(arena, parser.current_scope->symbols, &new_sym,
sizeof(Symbol));
} }
bool bool
@ -227,7 +229,7 @@ define_function()
while(!check(TOKEN_LBRACE)) { while(!check(TOKEN_LBRACE)) {
size += variable_declaration(fn); size += variable_declaration(fn);
advance(); advance();
if (check(TOKEN_LBRACE)) break; if(check(TOKEN_LBRACE)) break;
advance(); advance();
} }
@ -294,15 +296,35 @@ variable_declaration(Symbol *def)
TokenType tt = parser.previous.type; TokenType tt = parser.previous.type;
SymbolType st = token_type_to_sym_type(tt); SymbolType st = token_type_to_sym_type(tt);
Symbol *variable; Symbol *variable;
bool is_array = false;
if(parser.current.type == TOKEN_LBRACE) {
is_array = true;
while(!check(TOKEN_RBRACKET)) {
if(check(TOKEN_LITERAL_NAT)) {
/* consume dimension if exists */
} else if (check(TOKEN_LBRACKET)) {
/* it is a unbounded array */
}
advance();
if(check(TOKEN_LBRACKET)) break;
advance();
if(check(TOKEN_COMMA)) {
/* it is a multidimensional array */
}
}
}
USED(is_array);
if(st != SYMBOL_UNDEFINED) { if(st != SYMBOL_UNDEFINED) {
size = emitter.get_size(st); size = emitter.get_size(st);
variable = variable =
scope_add_symbol(parser.current.start, parser.current.length, st, size); scope_add_symbol(parser.current.start, parser.current.length, st, size);
if (!def && (parser.pass == 0)) { if(!def && (parser.pass == 0)) return size;
return size;
}
if(def) { if(def) {
List_push(arena, def->args, variable, sizeof(Symbol)); List_push(arena, def->args, variable, sizeof(Symbol));
@ -321,7 +343,7 @@ variable_declaration(Symbol *def)
if(def) if(def)
emitter.error(parser.previous.start, parser.previous.length, emitter.error(parser.previous.start, parser.previous.length,
parser.previous.line); parser.previous.line);
if (parser.pass == 0) return size; if(parser.pass == 0) return size;
if(match(TOKEN_EQ)) { if(match(TOKEN_EQ)) {
emitter.emit_set_value(); emitter.emit_set_value();
@ -431,7 +453,7 @@ variable()
parser.previous.line); parser.previous.line);
} }
if (sym->type == SYMBOL_FUNCTION) { if(sym->type == SYMBOL_FUNCTION) {
parser.call_fn = sym; parser.call_fn = sym;
return; return;
} }
@ -450,8 +472,8 @@ cast_type()
{ {
SymbolType st = parser.current_type; SymbolType st = parser.current_type;
TokenType cast_type; TokenType cast_type;
if (!(parser.current.type >= TOKEN_TYPE_I8 && if(!(parser.current.type >= TOKEN_TYPE_I8 &&
parser.current.type <= TOKEN_TYPE_PTR)) { parser.current.type <= TOKEN_TYPE_PTR)) {
emitter.error("Expect type name after 'as'.", 26, parser.current.line); emitter.error("Expect type name after 'as'.", 26, parser.current.line);
return; return;
} }
@ -545,11 +567,10 @@ cast_type()
void void
declaration() declaration()
{ {
if(is_type() && parser.current.type == TOKEN_IDENTIFIER) { if(is_type() && parser.current.type == TOKEN_IDENTIFIER)
variable_declaration(nil); variable_declaration(nil);
}else{ else
statement(); statement();
}
} }
void void
@ -606,20 +627,20 @@ function()
parser.previous.length); parser.previous.length);
emitter.emit_function(sym); emitter.emit_function(sym);
scope_push(); scope_push();
while(!check(TOKEN_RPAREN)) { while(!check(TOKEN_RPAREN)) advance();
advance(); consume(TOKEN_RPAREN);
} consume(TOKEN_LBRACE);
consume(TOKEN_RPAREN);
consume(TOKEN_LBRACE);
block(); block();
emitter.emit_arena_fn_return(); emitter.emit_arena_fn_return();
} }
void call() { void
if (!check(TOKEN_RPAREN)) { call()
{
if(!check(TOKEN_RPAREN)) {
do { do {
expression(); expression();
} while (match(TOKEN_COMMA)); } while(match(TOKEN_COMMA));
} }
consume(TOKEN_RPAREN); consume(TOKEN_RPAREN);
@ -631,13 +652,13 @@ void call() {
void void
return_statement() return_statement()
{ {
if (match(TOKEN_SEMICOLON)) { if(match(TOKEN_SEMICOLON)) {
emitter.emit_early_return(); emitter.emit_early_return();
} else { } else {
expression(); expression();
consume(TOKEN_SEMICOLON); consume(TOKEN_SEMICOLON);
emitter.emit_early_return(); emitter.emit_early_return();
} }
} }
void void
@ -658,7 +679,7 @@ statement()
while_statement(); while_statement();
} else if(match(TOKEN_KEYWORD_PRINT)) { } else if(match(TOKEN_KEYWORD_PRINT)) {
print_statement(); print_statement();
} else if (match(TOKEN_KEYWORD_HALT)) { } else if(match(TOKEN_KEYWORD_HALT)) {
emitter.emit_halt(); emitter.emit_halt();
} else { } else {
expression(); expression();
@ -676,14 +697,16 @@ grouping()
consume(TOKEN_RPAREN); consume(TOKEN_RPAREN);
} }
void
calculate_strides(i32 *dims, i32 num_dims, i32 *out_strides)
{
i32 i;
i32 current_stride = 1;
void calculate_strides(i32* dims, i32 num_dims, i32* out_strides) { for(i = num_dims - 1; i >= 0; i--) {
int current_stride = 1; out_strides[i] = current_stride;
current_stride *= dims[i];
for (int i = num_dims - 1; i >= 0; i--) { }
out_strides[i] = current_stride;
current_stride *= dims[i];
}
} }
void void
@ -691,9 +714,9 @@ define_array()
{ {
/* example /* example
i32 dims[3] = {3, 3, 3}; i32 dims[3] = {3, 3, 3};
i32 strides[3]; i32 strides[3];
calculate_strides(dims, 3, strides); calculate_strides(dims, 3, strides);
*/ */
} }

29
test/array.ul Normal file
View File

@ -0,0 +1,29 @@
nat[6] fixed_size = [1, 2, 3, 4, 5, 6];
nat[] variable_size = [1, 2, 3];
nat[] other_vsize = [4, 5, 6]
nat i = 0;
nat len = array.length;
while (i < len) {
print(array[i] as str);
print("\n");
i = i + 1;
}
print("\n");
for (i in array) {
print(i as str);
print("\n");
}
for (i in combine_arrays(variable_size, other_vsize)) {
print(i as str);
print("\n");
}
halt;
function combine_arrays(nat[] a, nat[] b) {
}

View File

@ -1,3 +1,6 @@
/**
* calculate fibonacci number 23
*/
print(fib(23) as str); print(fib(23) as str);
halt; halt;

View File

@ -1,4 +1,7 @@
print("Enter a string: "); print("Enter a string: ");
str msg = read(32); str msg = read(32);
print(msg); print(msg);
return 0;
nat[32] test = read(32);
halt;

View File

@ -5,46 +5,52 @@ const byte BLACK = 0;
const byte WHITE = 255; const byte WHITE = 255;
const byte DARK_GRAY = 73; const byte DARK_GRAY = 73;
const byte GRAY = 146; const byte GRAY = 146;
const byte LIGHT_GRAY = 182;
byte selected_color = 255; byte selected_color = 255;
trait Device { plex System {
nat handle; ref function vector;
byte wst;
byte rst;
u32 pad;
nat r;
nat g;
nat b;
byte debug;
byte halt;
} }
plex Screen implements Device { plex Screen {
nat handle; ref function vector;
nat width; nat width;
nat height; nat height;
byte[] buffer; nat auto;
draw() {
write(this, this.buffer, this.buffer.length);
}
}
plex Mouse implements Device {
nat handle;
nat x; nat x;
nat y; nat y;
bool left; nat addr;
bool right; byte pixel;
bool middle; byte sprite;
bool btn4;
} }
Screen screen = open("/dev/screen/0", 0); plex Mouse {
Mouse mouse = open("/dev/mouse/0", 0); ref function vector;
nat x;
nat y;
nat state;
nat z;
nat scrollx;
nat scrolly;
nat unused_;
}
outline_swatch(screen, BLACK, 1, 1); System system = mmap(0x00, System);
outline_swatch(screen, WHITE, 21, 1); Screen screen = mmap(0x20, Screen);
screen.draw(); screen.vector = ref render_screen;
Mouse mouse = mmap(0x90, Mouse);
loop { mouse.vector = ref render_screen;
mouse.refresh(); halt;
if (!mouse.left) continue;
function on_mouse_down() {
int box_size = 20; int box_size = 20;
int x = 1; int x = 1;
int y = 1; int y = 1;
@ -56,12 +62,14 @@ loop {
x = 21; x = 21;
outlined_swatch(screen, color, x, y); outlined_swatch(screen, color, x, y);
set_color(box_size, x, y, mouse.x, mouse.y, color); set_color(box_size, x, y, mouse.x, mouse.y, color);
screen.draw();
rectangle(screen, selected_color, x, y, 5, 5); rectangle(screen, selected_color, x, y, 5, 5);
} }
halt;
function render_screen() {
outline_swatch(screen, BLACK, 1, 1);
outline_swatch(screen, WHITE, 21, 1);
}
/** /**
* Checks if the click is within the bound and update the selected color if so. * Checks if the click is within the bound and update the selected color if so.
@ -81,25 +89,29 @@ function set_color(int box_size, int bx, int by, int mx, int my, byte color) {
/** /**
* Draw a color box with a grey outline, if selected use a darker 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) { function outline_swatch(byte color, int x, int y) {
byte bg_color = GRAY; byte bg_color = GRAY;
if (selected_color == color) { if (selected_color == color) {
bg_color = DARK_GRAY; bg_color = DARK_GRAY;
} }
rectangle(screen, bg_color, x, y, 20, 20); rectangle(bg_color, x, y, 20, 20);
rectangle(screen, color, x + 2, y + 2, 17, 17); rectangle(color, x + 2, y + 2, 17, 17);
} }
/** /**
* Draw a rectangle * Draw a rectangle
*/ */
function rectangle(Device screen, byte color, int x, int y, int width, int height) { function rectangle(byte color, int x, int y, int width, int height) {
int base = y * screen.width + x + screen.buffer.ptr + 4; int base = y * screen.width + x;
for (int i = height; i > 0; i--) { for (int i = height; i > 0; i--) {
int row = base + width; int row = base + width;
memset(screen.buffer, row, color, width); for (int j = width; j < row; j++) {
screen.x = i;
screen.y = j;
screen.pixel = color;
}
base += screen.width; base += screen.width;
} }
} }