diff options
author | kdx <kdx@42l.fr> | 2023-01-22 23:01:16 +0100 |
---|---|---|
committer | kdx <kdx@42l.fr> | 2023-01-22 23:01:16 +0100 |
commit | 2a3dccf7ad817aede812b6474e9d14dabe750050 (patch) | |
tree | 4e3af60c1980aae2cf684bdbb691b75b1232f5d4 | |
parent | a2097e53d48b3779d92d8de1ae8b923ff8b79811 (diff) | |
download | golem-legacy.tar.gz |
insert values in root scopelegacy
-rw-r--r-- | Node.h | 10 | ||||
-rw-r--r-- | Scope.c | 2 | ||||
-rw-r--r-- | Scope.h | 1 | ||||
-rw-r--r-- | baby.golem | 4 | ||||
-rw-r--r-- | parse.c | 32 |
5 files changed, 37 insertions, 12 deletions
@@ -0,0 +1,10 @@ +#pragma once +#include "Token.h" + +/* AST element */ +typedef struct Node Node; +struct Node { + const Token *tok; + Node *lhs; + Node *rhs; +}; @@ -31,6 +31,8 @@ int scope_insert(Scope *scope, const char *word, int value) scope->words[scope->used] = word; scope->values[scope->used] = value; scope->words_hash[scope->used] = hash; + fprintf(stderr, "inserted %s = %d in scope\n", + scope->words[scope->used], scope->values[scope->used]); scope->used += 1; return 0; } @@ -13,5 +13,6 @@ struct Scope { const Scope *parent; }; +/* created pointer shall be manualy freed by caller */ Scope *scope_create(const Scope *parent); int scope_insert(Scope *scope, const char *word, int value); @@ -5,8 +5,8 @@ const dwidth = 396; const dheight = 224; // simple function -fn hello() +fn hello(coucou) { - var x = 10; + var x = coucou * 10; return x; } @@ -1,34 +1,39 @@ #include "parse.h" #include "Token.h" +#include "Scope.h" #include <stdio.h> +#include <stdlib.h> static void *unexpected(const Token *tok, int n); static const Token *expression(const Token *tok); static const Token *keyword_fn(const Token *tok); -static const Token *keyword_var(const Token *tok); -static const Token *keyword_const(const Token *tok); +static const Token *keyword_var(Scope *scope, const Token *tok); +static const Token *keyword_const(Scope *scope, const Token *tok); int parse(const Token *tok) { + Scope *scope = scope_create(NULL); + if (scope == NULL) + return 1; while (tok != NULL && tok->type != TOK_NONE) { switch (tok->type) { case TOK_KW_FN: tok = keyword_fn(tok); break; case TOK_KW_VAR: - tok = keyword_var(tok); + tok = keyword_var(scope, tok); break; case TOK_KW_CONST: - tok = keyword_const(tok); + tok = keyword_const(scope, tok); break; default: unexpected(tok, -1); - return 1; + return free(scope), 1; } if (tok == NULL) - return 1; + return free(scope), 1; } - return 0; + return free(scope), 0; } /* write unexpected token error to stderr before returning NULL, @@ -81,7 +86,7 @@ static const Token *keyword_fn(const Token *tok) } /* mutable variable declaration */ -static const Token *keyword_var(const Token *tok) +static const Token *keyword_var(Scope *scope, const Token *tok) { tok += 1; if (tok->type != TOK_WORD) @@ -90,8 +95,11 @@ static const Token *keyword_var(const Token *tok) (void)name; tok += 1; /* no initialization */ - if (tok->type == TOK_SEMICOLON) + if (tok->type == TOK_SEMICOLON) { + if (scope_insert(scope, name->s, 0)) + return NULL; return tok + 1; + } if (tok->type != TOK_ASSIGN) return unexpected(tok, 11); tok = expression(tok + 1); @@ -99,11 +107,13 @@ static const Token *keyword_var(const Token *tok) return NULL; if (tok->type != TOK_SEMICOLON) return unexpected(tok, 12); + if (scope_insert(scope, name->s, 0)) + return NULL; return tok + 1; } /* constant variable declaration */ -static const Token *keyword_const(const Token *tok) +static const Token *keyword_const(Scope *scope, const Token *tok) { tok += 1; if (tok->type != TOK_WORD) @@ -118,5 +128,7 @@ static const Token *keyword_const(const Token *tok) return NULL; if (tok->type != TOK_SEMICOLON) return unexpected(tok, 12); + if (scope_insert(scope, name->s, 0)) + return NULL; return tok + 1; } |