From 79ab1a4223d53bbdbffc55ae7f9740d953c57945 Mon Sep 17 00:00:00 2001 From: glenda Date: Sat, 22 Oct 2022 19:03:56 +0000 Subject: Prepare for a namespace implementation at some point --- symbol.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'symbol.c') diff --git a/symbol.c b/symbol.c index 61629ee..4fc8142 100644 --- a/symbol.c +++ b/symbol.c @@ -5,7 +5,7 @@ #include "apl9.h" -Symtab *globalsymtab; +Array *rootns; Symtab *newsymtab(Symtab *); Symbol *dupsymbol(Symbol *); @@ -17,6 +17,7 @@ Datum *getalphao(void); Datum *getomegao(void); void setalpha(Datum *); void setsyntaxerr(Datum *); +Namespace *newnamespace(Rune *, Symtab *); Symbol * getsym(Rune *name, int fresh) @@ -86,9 +87,6 @@ freesymtab(Symtab *tab) if(tab == nil) return; - if(tab == globalsymtab) /* Never free the global symtab */ - return; - int i; for(i = 0; i < tab->nsyms; i++){ Symbol *s = tab->syms[i]; @@ -102,7 +100,10 @@ freesymtab(Symtab *tab) void initsymtab(void) { - globalsymtab = newsymtab(nil); + ThreadData *td = getthreaddata(); + Namespace *ns = newnamespace(L"#", newsymtab(nil)); + td->ns = rootns = fnSame(mkscalarns(ns)); + Symbol *s; s = getsym(L"⍺", 1); s->getfn = getalpha; @@ -205,8 +206,8 @@ globalIO(void) ThreadData *td = getthreaddata(); if(td->currentdfn) return td->currentdfn->symtab->io; - else if(globalsymtab) - return globalsymtab->io; + else if(td->ns) + return td->ns->nsdata[0]->syms->io; else return 1; } @@ -218,7 +219,7 @@ globalIOset(vlong io) if(td->currentdfn) td->currentdfn->symtab->io = io; else - globalsymtab->io = io; + td->ns->nsdata[0]->syms->io = io; } int @@ -227,8 +228,8 @@ globalDIV(void) ThreadData *td = getthreaddata(); if(td->currentdfn) return td->currentdfn->symtab->div; - else if(globalsymtab) - return globalsymtab->div; + else if(td->ns) + return td->ns->nsdata[0]->syms->div; else return 0; } @@ -240,7 +241,7 @@ globalDIVset(int div) if(td->currentdfn) td->currentdfn->symtab->div = div; else - globalsymtab->div = div; + td->ns->nsdata[0]->syms->div = div; } /* getters and setters for ⍺⍵⍶⍹ */ @@ -322,10 +323,11 @@ setsyntaxerr(Datum *) Symtab * dupscope(Symtab *old) { - Symtab *new = newsymtab(globalsymtab); + ThreadData *td = getthreaddata(); + Symtab *new = newsymtab(td->ns->nsdata[0]->syms); /* copy ALL symbols which are in scope, into the new symtab */ - for(Symtab *o = old; o != globalsymtab; o = o->chain){ + for(Symtab *o = old; o != td->ns->nsdata[0]->syms; o = o->chain){ /* Add all new symbols */ for(int i = 0; i < o->nsyms; i++){ Symbol *sym = o->syms[i]; @@ -358,9 +360,19 @@ dupscope(Symtab *old) Symtab * getcurrentsymtab(void) { + ThreadData *td = getthreaddata(); DfnFrame *dfn = getcurrentdfn(); if(dfn == nil) - return globalsymtab; + return td->ns->nsdata[0]->syms; else return dfn->symtab; +} + +Namespace * +newnamespace(Rune *displayform, Symtab *syms) +{ + Namespace *ns = emalloc(sizeof(Namespace)); + ns->displayform = runestrdup(displayform); + ns->syms = syms; + return ns; } \ No newline at end of file -- cgit v1.2.3