ox

The Ox programming language, compiler and tools (WIP)
Log | Files | Refs | README | LICENSE

commit dd913db9e0967fcb53981fbec944dd82ea20d41b
parent 775a8293776b3e1703459e5b0ef3d5a8c4819a05
Author: citbl <citbl@citbl.org>
Date:   Sun, 12 Oct 2025 19:37:37 +1000

handle variables part 1

only handles strings for now and only from the one place, the first one
in the symbols list. I need to rethink further how I'm going to do this
so it's not such a mess and doesn't get complicated either.

Diffstat:
Mex2.ox | 10++++------
Msrc/gen/gen.c | 70++++++++++++++++++++++++++++++++++++++++------------------------------
Msrc/types.h | 2+-
3 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/ex2.ox b/ex2.ox @@ -1,8 +1,6 @@ -// example program -// ns main -// T add(T a, b) inline pure => a + b; - void main() { - string steve = "his name is steeeeeve"; - print(steve); + string steve = "this is the wrong name if you print(harry) and this is steve because we get the first one found!"; + string harry = "his name is harry"; + int bob = 4; + print(harry); } diff --git a/src/gen/gen.c b/src/gen/gen.c @@ -82,6 +82,33 @@ gen_init(Scope *scope, const char *src) }; } +static gcc_jit_rvalue * +handle_ident_call(Gen *gen, Node *node) +{ + // Look up the symbol in the current scope + for (size_t i = 0; i < gen->scope->len; i++) { + Symbol *sym = gen->scope->symbols[i]; + + // TODO @next right now we get the first one, which isn't good. + // we need to get the right one by name + // need to research what the best way is to store that and use it, right now, symbols are a mess + // and their relation between each other and the rest of the compiler is a mess. + return gcc_jit_lvalue_as_rvalue(sym->d.lvalue); + + // if (sym->name.start == node->data.var_decl.name.start && sym->name.end == node->data.var_decl.name.end) { + // gcc_jit_rvalue *p_r = gcc_jit_lvalue_as_rvalue(sym->d.lvalue); + // // dereference pointer -> lvalue of the pointee: + // gcc_jit_lvalue *pointee_lv = gcc_jit_rvalue_dereference(p_r, NULL); + // // read the pointee as an rvalue (e.g., for arithmetic): + // gcc_jit_rvalue *pointee_r = gcc_jit_lvalue_as_rvalue(pointee_lv); + // return pointee_r; + // } + } + + softpanic("undefined variable: %s\n", span_str(gen->src, node->data.ident.name, (char[IDENTSZ]) { 0 })); + return NULL; +} + static gcc_jit_rvalue *handle_expr(Gen *, Node *); static gcc_jit_rvalue * @@ -180,12 +207,6 @@ lower_builtin_print(Gen *gen, Node *node) // // TODO see todo below about linked list parameters... // } -static void -lookup_symbol(Gen *gen) -{ - // @next -} - static gcc_jit_rvalue * handle_func_call(Gen *gen, Node *node) { @@ -210,23 +231,6 @@ handle_func_call(Gen *gen, Node *node) } static gcc_jit_rvalue * -handle_ident_call(Gen *gen, Node *node) -{ - for (size_t i = 0; i < gen->scope->len; i++) { - Symbol *sym = gen->scope->symbols[i]; - if (sym->name.start == node->data.var_decl.name.start && sym->name.end == node->data.var_decl.name.end) { - gcc_jit_rvalue *p_r = gcc_jit_lvalue_as_rvalue(sym->d.lvalue); - // dereference pointer -> lvalue of the pointee: - gcc_jit_lvalue *pointee_lv = gcc_jit_rvalue_dereference(p_r, NULL); - // read the pointee as an rvalue (e.g., for arithmetic): - gcc_jit_rvalue *pointee_r = gcc_jit_lvalue_as_rvalue(pointee_lv); - return pointee_r; - } - } - return NULL; -} - -static gcc_jit_rvalue * handle_expr(Gen *gen, Node *node) { switch (node->type) { @@ -285,14 +289,20 @@ build_statement(Gen *gen, Node *node) gcc_jit_rvalue *rvalue = handle_expr(gen, node->data.var_decl.init); gcc_jit_block_add_assignment(gen->curr_block, loc, var_decl, rvalue); - for (size_t i = 0; i < gen->scope->len; i++) { - Symbol *sym = gen->scope->symbols[i]; - if (sym->name.start == node->data.var_decl.name.start && sym->name.end == node->data.var_decl.name.end) { - sym->ctype = declared_type; - sym->d.lvalue = var_decl; - break; - } + printf("adding 1 symbol: %s\n", span_str(gen->src, node->data.var_decl.name, (char[IDENTSZ]) { 0 })); + + Symbol *sym = (Symbol *)calloc(1, sizeof(Symbol)); + sym->name = node->data.var_decl.name; + sym->decl = node; + sym->ctype = declared_type; + sym->d.lvalue = var_decl; + + if (gen->scope->len == gen->scope->cap) { + gen->scope->cap *= 2; + gen->scope->symbols = (Symbol **)realloc(gen->scope->symbols, sizeof(Symbol *) * gen->scope->cap); } + + gen->scope->symbols[gen->scope->len++] = sym; } } break; case NODE_EXPR_STATEMENT: { diff --git a/src/types.h b/src/types.h @@ -271,7 +271,7 @@ typedef struct Type { typedef struct Symbol { Span name; Node *decl; - TypeInfo *type; + TypeInfo *type; // todo print all found in gen gcc_jit_type *ctype; union {