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.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;
}
Scope*
Scope *
scope_new()
{
Scope *current;
@ -31,9 +31,9 @@ scope_new()
child.parent = parser.current_scope;
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.scope_idx++;
parser.scope_idx++;
parser.depth++;
return current;
}
@ -41,7 +41,7 @@ scope_new()
void
scope_pop()
{
if (parser.current_scope) {
if(parser.current_scope) {
parser.current_scope = parser.current_scope->parent;
parser.depth--;
}
@ -50,10 +50,10 @@ scope_pop()
void
scope_push()
{
Scope *scope = List_get(parser.scopes, parser.scope_idx);
parser.current_scope = scope;
Scope *scope = List_get(parser.scopes, parser.scope_idx);
parser.current_scope = scope;
parser.depth++;
parser.scope_idx++;
parser.scope_idx++;
}
Symbol *
@ -65,7 +65,8 @@ scope_get_symbol(Scope *scope, const char *name, u32 name_length)
count = scope->symbols->count;
for(i = 0; i < count; 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);
@ -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)
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
@ -227,7 +229,7 @@ define_function()
while(!check(TOKEN_LBRACE)) {
size += variable_declaration(fn);
advance();
if (check(TOKEN_LBRACE)) break;
if(check(TOKEN_LBRACE)) break;
advance();
}
@ -294,15 +296,35 @@ variable_declaration(Symbol *def)
TokenType tt = parser.previous.type;
SymbolType st = token_type_to_sym_type(tt);
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) {
size = emitter.get_size(st);
variable =
scope_add_symbol(parser.current.start, parser.current.length, st, size);
if (!def && (parser.pass == 0)) {
return size;
}
if(!def && (parser.pass == 0)) return size;
if(def) {
List_push(arena, def->args, variable, sizeof(Symbol));
@ -321,7 +343,7 @@ variable_declaration(Symbol *def)
if(def)
emitter.error(parser.previous.start, parser.previous.length,
parser.previous.line);
if (parser.pass == 0) return size;
if(parser.pass == 0) return size;
if(match(TOKEN_EQ)) {
emitter.emit_set_value();
@ -431,7 +453,7 @@ variable()
parser.previous.line);
}
if (sym->type == SYMBOL_FUNCTION) {
if(sym->type == SYMBOL_FUNCTION) {
parser.call_fn = sym;
return;
}
@ -450,8 +472,8 @@ cast_type()
{
SymbolType st = parser.current_type;
TokenType cast_type;
if (!(parser.current.type >= TOKEN_TYPE_I8 &&
parser.current.type <= TOKEN_TYPE_PTR)) {
if(!(parser.current.type >= TOKEN_TYPE_I8 &&
parser.current.type <= TOKEN_TYPE_PTR)) {
emitter.error("Expect type name after 'as'.", 26, parser.current.line);
return;
}
@ -545,11 +567,10 @@ cast_type()
void
declaration()
{
if(is_type() && parser.current.type == TOKEN_IDENTIFIER) {
if(is_type() && parser.current.type == TOKEN_IDENTIFIER)
variable_declaration(nil);
}else{
else
statement();
}
}
void
@ -605,21 +626,21 @@ function()
Symbol *sym = scope_get_symbol(parser.current_scope, parser.previous.start,
parser.previous.length);
emitter.emit_function(sym);
scope_push();
while(!check(TOKEN_RPAREN)) {
advance();
}
consume(TOKEN_RPAREN);
consume(TOKEN_LBRACE);
scope_push();
while(!check(TOKEN_RPAREN)) advance();
consume(TOKEN_RPAREN);
consume(TOKEN_LBRACE);
block();
emitter.emit_arena_fn_return();
}
void call() {
if (!check(TOKEN_RPAREN)) {
void
call()
{
if(!check(TOKEN_RPAREN)) {
do {
expression();
} while (match(TOKEN_COMMA));
expression();
} while(match(TOKEN_COMMA));
}
consume(TOKEN_RPAREN);
@ -631,13 +652,13 @@ void call() {
void
return_statement()
{
if (match(TOKEN_SEMICOLON)) {
emitter.emit_early_return();
} else {
expression();
consume(TOKEN_SEMICOLON);
emitter.emit_early_return();
}
if(match(TOKEN_SEMICOLON)) {
emitter.emit_early_return();
} else {
expression();
consume(TOKEN_SEMICOLON);
emitter.emit_early_return();
}
}
void
@ -651,14 +672,14 @@ statement()
advance();
function();
} else if(match(TOKEN_KEYWORD_RETURN)) {
return_statement();
return_statement();
} else if(match(TOKEN_KEYWORD_IF)) {
if_statement();
} else if(match(TOKEN_KEYWORD_WHILE)) {
while_statement();
} else if(match(TOKEN_KEYWORD_PRINT)) {
print_statement();
} else if (match(TOKEN_KEYWORD_HALT)) {
} else if(match(TOKEN_KEYWORD_HALT)) {
emitter.emit_halt();
} else {
expression();
@ -676,14 +697,16 @@ grouping()
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) {
int current_stride = 1;
for (int i = num_dims - 1; i >= 0; i--) {
out_strides[i] = current_stride;
current_stride *= dims[i];
}
for(i = num_dims - 1; i >= 0; i--) {
out_strides[i] = current_stride;
current_stride *= dims[i];
}
}
void
@ -691,9 +714,9 @@ define_array()
{
/* example
i32 dims[3] = {3, 3, 3};
i32 strides[3];
calculate_strides(dims, 3, strides);
i32 strides[3];
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);
halt;

View File

@ -1,4 +1,7 @@
print("Enter a string: ");
str msg = read(32);
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 DARK_GRAY = 73;
const byte GRAY = 146;
const byte LIGHT_GRAY = 182;
byte selected_color = 255;
trait Device {
nat handle;
plex System {
ref function vector;
byte wst;
byte rst;
u32 pad;
nat r;
nat g;
nat b;
byte debug;
byte halt;
}
plex Screen implements Device {
nat handle;
plex Screen {
ref function vector;
nat width;
nat height;
byte[] buffer;
draw() {
write(this, this.buffer, this.buffer.length);
}
}
plex Mouse implements Device {
nat handle;
nat auto;
nat x;
nat y;
bool left;
bool right;
bool middle;
bool btn4;
nat addr;
byte pixel;
byte sprite;
}
Screen screen = open("/dev/screen/0", 0);
Mouse mouse = open("/dev/mouse/0", 0);
plex Mouse {
ref function vector;
nat x;
nat y;
nat state;
nat z;
nat scrollx;
nat scrolly;
nat unused_;
}
outline_swatch(screen, BLACK, 1, 1);
outline_swatch(screen, WHITE, 21, 1);
screen.draw();
System system = mmap(0x00, System);
Screen screen = mmap(0x20, Screen);
screen.vector = ref render_screen;
Mouse mouse = mmap(0x90, Mouse);
mouse.vector = ref render_screen;
halt;
loop {
mouse.refresh();
if (!mouse.left) continue;
function on_mouse_down() {
int box_size = 20;
int x = 1;
int y = 1;
@ -56,12 +62,14 @@ loop {
x = 21;
outlined_swatch(screen, color, x, y);
set_color(box_size, x, y, mouse.x, mouse.y, color);
screen.draw();
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.
@ -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
*/
function outline_swatch(Device screen, byte color, int x, int y) {
function outline_swatch(byte color, int x, int y) {
byte bg_color = GRAY;
if (selected_color == color) {
bg_color = DARK_GRAY;
}
rectangle(screen, bg_color, x, y, 20, 20);
rectangle(screen, color, x + 2, y + 2, 17, 17);
rectangle(bg_color, x, y, 20, 20);
rectangle(color, x + 2, y + 2, 17, 17);
}
/**
* Draw a rectangle
*/
function rectangle(Device screen, byte color, int x, int y, int width, int height) {
int base = y * screen.width + x + screen.buffer.ptr + 4;
function rectangle(byte color, int x, int y, int width, int height) {
int base = y * screen.width + x;
for (int i = height; i > 0; i--) {
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;
}
}