From 2a3dccf7ad817aede812b6474e9d14dabe750050 Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 22 Jan 2023 23:01:16 +0100 Subject: insert values in root scope --- Node.h | 10 ++++++++++ Scope.c | 2 ++ Scope.h | 1 + baby.golem | 4 ++-- parse.c | 32 ++++++++++++++++++++++---------- 5 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 Node.h diff --git a/Node.h b/Node.h new file mode 100644 index 0000000..846e20b --- /dev/null +++ b/Node.h @@ -0,0 +1,10 @@ +#pragma once +#include "Token.h" + +/* AST element */ +typedef struct Node Node; +struct Node { + const Token *tok; + Node *lhs; + Node *rhs; +}; diff --git a/Scope.c b/Scope.c index 3df00b9..530fa9e 100644 --- a/Scope.c +++ b/Scope.c @@ -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; } diff --git a/Scope.h b/Scope.h index e5adacd..f4fc498 100644 --- a/Scope.h +++ b/Scope.h @@ -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); diff --git a/baby.golem b/baby.golem index 6da1cb9..52a07f9 100644 --- a/baby.golem +++ b/baby.golem @@ -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; } diff --git a/parse.c b/parse.c index d2e1bcd..e059eb5 100644 --- a/parse.c +++ b/parse.c @@ -1,34 +1,39 @@ #include "parse.h" #include "Token.h" +#include "Scope.h" #include +#include 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; } -- cgit v1.2.3