summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx@42l.fr>2023-01-22 23:01:16 +0100
committerkdx <kdx@42l.fr>2023-01-22 23:01:16 +0100
commit2a3dccf7ad817aede812b6474e9d14dabe750050 (patch)
tree4e3af60c1980aae2cf684bdbb691b75b1232f5d4
parenta2097e53d48b3779d92d8de1ae8b923ff8b79811 (diff)
downloadgolem-legacy.tar.gz
insert values in root scopelegacy
-rw-r--r--Node.h10
-rw-r--r--Scope.c2
-rw-r--r--Scope.h1
-rw-r--r--baby.golem4
-rw-r--r--parse.c32
5 files changed, 37 insertions, 12 deletions
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 <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;
}