diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-11 03:41:08 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-11 03:41:08 +0200 |
commit | 0fc731ad0f750e944513b77abfa0a8938667a31c (patch) | |
tree | 3ad25676d825fab54947ce3eec45b2d5090db46f | |
parent | 617d9d0d7344c8079a1f028ce1288cc589223366 (diff) | |
download | golem-0fc731ad0f750e944513b77abfa0a8938667a31c.tar.gz |
bad define implementation
-rw-r--r-- | samples/helloworld.golem | 4 | ||||
-rw-r--r-- | src/main.c | 36 |
2 files changed, 38 insertions, 2 deletions
diff --git a/samples/helloworld.golem b/samples/helloworld.golem index 9ec0086..473331f 100644 --- a/samples/helloworld.golem +++ b/samples/helloworld.golem @@ -1,5 +1,7 @@ global str[32]; +define iterations = 9; + main() { local i; @@ -9,7 +11,7 @@ main() { i = 0; loop { i = i + 1; - if (i > 9) + if (i > iterations) break; print(str); uprint(str); @@ -186,6 +186,7 @@ typedef enum { NOD_WORD, NOD_FNCALL, NOD_GLOBAL, + NOD_DEFINE, NOD_LOCAL, } NodeType; @@ -240,6 +241,14 @@ node_find(const Node *node, Node *op) } static Node * +node_get(Node *node, int idx) +{ + while (idx--) + node = node->next; + return node; +} + +static Node * new_node(NodeType type) { Node *const node = calloc(1, sizeof(Node)); @@ -285,6 +294,7 @@ new_num(int val) static Node *stmt(Token **rest, Token *tok); static Node *function(Token **rest, Token *tok); +void define(Token **rest, Token *tok); static Node *global(Token **rest, Token *tok); static Node *local(Token **rest, Token *tok); static Node *expr(Token **rest, Token *tok); @@ -306,6 +316,7 @@ static Node *primary(Token **rest, Token *tok); static Node *locals; static int locals_size; static Node *strings; +static Node *defines; static int strings_size; static Node * @@ -333,6 +344,20 @@ function(Token **rest, Token *tok) return node; } +void +define(Token **rest, Token *tok) +{ + tok = skip(tok, "define"); + Node *store = new_word(tok); + tok = skip(tok->next, "="); + store->val = get_number(tok); + *rest = skip(tok->next, ";"); + + store->type = NOD_DEFINE; + store->next = defines; + defines = store; +} + static Node * global(Token **rest, Token *tok) { @@ -783,8 +808,12 @@ gen_fncall(Node *node) static void gen_variableget(Node *node) { + const int define_idx = node_find(defines, node); const int found = node_find(locals, node); - if (found != -1) { + + if (define_idx != -1) + printf("\tLIT %04x\n", node_get(defines, define_idx)->val); + else if (found != -1) { printf("\tLIT ,__stack_ptr LDA\n"); if (locals_size - found - 1) printf("\tLIT %04x SUB\n", locals_size - found - 1); @@ -995,6 +1024,11 @@ parse(Token *tok) Node head = {0}; Node *cur = &head; while (tok->type != TOK_EOF) { + if (equal(tok, "define")) { + define(&tok, tok); + continue; + } + if (equal(tok, "global")) { cur = cur->next = global(&tok, tok); continue; |