everything works except for weird issue where add.ul and fib.ul are duplicating the first function call #1

Merged
zongor merged 2 commits from feature/stdlib-print into main 2026-07-11 01:12:51 -04:00
3 changed files with 39 additions and 5 deletions
Showing only changes of commit e1c68b8e80 - Show all commits

View File

@ -10,6 +10,7 @@ Emitter *emitter;
Arena *arena; Arena *arena;
List *code; List *code;
List *memory; List *memory;
CallStack *call_stack;
/**************************************************** /****************************************************
* Scope * Scope
@ -107,6 +108,23 @@ scope_add_symbol(const char *name, u32 name_length, SymbolType type, u32 size)
sizeof(Symbol)); sizeof(Symbol));
} }
void CallStack_push(CallStack *stack, Symbol *symbol) {
if (stack->count >= stack->capacity) {
emitter->error("call stack full", 16, parser->previous.line);
}
stack->symbols[stack->count] = symbol;
stack->count++;
}
Symbol *CallStack_pop(CallStack *stack) {
if (stack->count == 0) {
emitter->error("call stack empty", 17, parser->previous.line);
}
stack->count--;
return stack->symbols[stack->count];
}
bool bool
is_global(void) is_global(void)
{ {
@ -590,7 +608,7 @@ variable(void)
} }
if(sym->type == SYMBOL_FUNCTION) { if(sym->type == SYMBOL_FUNCTION) {
parser->call_fn = sym; CallStack_push(call_stack, sym);
return; return;
} }
@ -862,6 +880,8 @@ function(void)
void void
call(void) call(void)
{ {
Symbol *call_fn;
if(!check(TOKEN_RPAREN)) { if(!check(TOKEN_RPAREN)) {
do { do {
expression(); expression();
@ -869,8 +889,9 @@ call(void)
} }
consume(TOKEN_RPAREN); consume(TOKEN_RPAREN);
emitter->emit_arena_fn_call(arena, code, parser->call_fn); call_fn = CallStack_pop(call_stack);
parser->current_type = parser->call_fn->secondary_type; emitter->emit_arena_fn_call(arena, code, call_fn);
parser->current_type = call_fn->secondary_type;
return; return;
} }
@ -1120,6 +1141,13 @@ bool
compile(Arena *a, List *c, List *m, Emitter *e, char *source) compile(Arena *a, List *c, List *m, Emitter *e, char *source)
{ {
Parser p = {0}; Parser p = {0};
Symbol *syms[50] = {0};
CallStack cs;
cs.symbols = syms;
cs.count = 0;
cs.capacity = 50;
call_stack = &cs;
arena = a; arena = a;
emitter = e; emitter = e;
code = c; code = c;

View File

@ -15,6 +15,13 @@ struct scope_s {
u32 locals_offset; u32 locals_offset;
}; };
typedef struct call_stack_s CallStack;
struct call_stack_s {
Symbol **symbols;
u32 count;
u32 capacity;
};
typedef enum { typedef enum {
PREC_NONE, PREC_NONE,
PREC_ASSIGNMENT, /* = */ PREC_ASSIGNMENT, /* = */
@ -39,7 +46,6 @@ struct parse_rule_s {
}; };
struct parser_s { struct parser_s {
Symbol *call_fn;
Scope *current_scope; Scope *current_scope;
List *scopes; List *scopes;
Token current; Token current;

View File

@ -1,7 +1,7 @@
|100 |100
LIT2r 0000 LIT2r 0000
#0001 #0001 add_ nat_to_str_ add_ #0001 #0001 add_ nat_to_str_ print_
POP2r BRK POP2r BRK
@add_ ( a b -- res ) @add_ ( a b -- res )
OVR2r LIT2r 0004 SUB2r OVR2r LIT2r 0004 SUB2r