summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-08-19 22:49:23 +0200
committertomsmeding <tom.smeding@gmail.com>2016-08-19 22:49:23 +0200
commitb477ac27210399b976e70195e92fb13315d39769 (patch)
treebab08e8a53887d356b78846001385f60ca379099
parent34dc898fde1562d4e31102c260755956d6c0eb4a (diff)
Start interpreter
-rw-r--r--interpreter.c82
-rw-r--r--interpreter.h10
2 files changed, 92 insertions, 0 deletions
diff --git a/interpreter.c b/interpreter.c
new file mode 100644
index 0000000..5a3d66a
--- /dev/null
+++ b/interpreter.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "interpreter.h"
+#include "util.h"
+
+
+int namehash(const char *name,int mod){
+ int h=0;
+ for(int i=0;name[i];i++)h+=name[i];
+ return h%mod;
+}
+
+
+#define VMAP_HASHSZ (61)
+
+typedef struct Vllist{
+ char *name;
+ AST *value;
+ struct Vllist *next;
+} Vllist;
+
+typedef struct Scope{
+ Vllist *vmap[VMAP_HASHSZ];
+ struct Scope *next;
+} Scope;
+
+
+typedef struct Symbolstore{
+ int sz,len;
+ char **syms;
+} Symbolstore;
+
+
+struct InterState{
+ Scope *scope;
+ Symbolstore ss;
+};
+
+
+Scope* scope_make(void){
+ Scope *scope=malloc(1,Scope);
+ memset(scope->vmap,0,VMAP_HASHSZ*sizeof(Vllist*));
+ scope->next=NULL;
+ return scope;
+}
+
+
+InterState* inter_make(void){
+ InterState *is=malloc(1,InterState);
+ is->scope=scope_make();
+ is->ss.sz=16;
+ is->ss.len=0;
+ is->ss.syms=malloc(is->ss.sz,char*);
+ return is;
+}
+
+static void scope_destroy(Scope *scope,bool recursive){
+ do {
+ for(int i=0;i<VMAP_HASHSZ;i++){
+ while(scope->vmap[i]){
+ Vllist *ll=scope->vmap[i];
+ free(ll->name);
+ ast_free(ll->value);
+ scope->vmap[i]=ll->next;
+ free(ll);
+ }
+ }
+ Scope *next=scope->next;
+ free(scope);
+ scope=next;
+ } while(recursive&&scope);
+}
+
+void inter_destroy(InterState *is){
+ assert(is);
+ scope_destroy(is->scope,true);
+ for(int i=0;i<is->ss.len;i++)free(is->ss.syms[i]);
+ free(is->ss.syms);
+}
diff --git a/interpreter.h b/interpreter.h
new file mode 100644
index 0000000..66edce0
--- /dev/null
+++ b/interpreter.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include "ast.h"
+
+
+typedef struct InterState InterState;
+
+InterState* inter_make(void);
+void inter_destroy(InterState *is);
+void inter_runcode(AST *ast); //updates symbol id's