📄 mal_module.c
字号:
#line 121 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"#include "mal_config.h"#include "mal_module.h"#include "mal_function.h" /* for printFunction() */#include "mal_namespace.h"#include "mal_client.h"#include "mal_interpreter.h"Module mal_scope; /* the root of the tree */Module scopeJump[256][256]; /* to speedup access to correct scope */void newSubScope(Module scope){ int len = (MAXSCOPE)*sizeof(Module); scope->subscope = (Symbol *) GDKzalloc(len);}#line 143 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"void clrModuleJump(str nme, Module cur){ if( scopeJump[(int)(*nme)][(int)(*(nme+1))]== cur) scopeJump[(int)(*nme)][(int)(*(nme+1))]= cur->sibling;}void setModuleJump(str nme, Module cur){ cur->sibling= scopeJump[(int)(*nme)][(int)(*(nme+1))]; scopeJump[(int)(*nme)][(int)(*(nme+1))]= cur;}Module newModule(Module scope, str nme){ Module cur; nme= getName(nme,strlen(nme)); #line 106 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx" if( nme == NULL){ GDKfatal("newModule:unexpected name (=null)\n"); } cur = (Module) GDKzalloc(sizeof(ModuleRecord)); if( cur == NULL){ GDKfatal("newModule: cannot initialize scope\n"); } cur->name = nme; cur->outer = NULL; cur->sibling = NULL; cur->inheritance = TRUE; cur->subscope = NULL; cur->isAtomModule = FALSE;#line 155 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx" newSubScope(cur); if( scope != NULL){ cur->outer = scope->outer; scope->outer= cur; setModuleJump(nme,cur); } return cur;}Module cpyModule(Module scope){ Module nxt= NULL; Module cur; str nme; if( scope->outer) nxt= cpyModule(scope->outer); nme= GDKstrdup(scope->name); #line 106 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx" if( nme == NULL){ GDKfatal("newModule:unexpected name (=null)\n"); } cur = (Module) GDKzalloc(sizeof(ModuleRecord)); if( cur == NULL){ GDKfatal("newModule: cannot initialize scope\n"); } cur->name = nme; cur->outer = NULL; cur->sibling = NULL; cur->inheritance = TRUE; cur->subscope = NULL; cur->isAtomModule = FALSE;#line 170 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx" newSubScope(cur); cur->outer = nxt; return cur;}#line 183 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"Module fixModule(Module scope, str nme){ Module s= scope; if( scopeJump[(int)(*nme)][(int)(*(nme+1))]) s= scopeJump[(int)(*nme)][(int)(*(nme+1))]; while(s != NULL){ if( nme == s->name ) return s; s= s->outer; } return newModule(scope, nme);}#line 202 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"void freeSubScope(Module scope){ int i; if(scope->subscope==0) return; for(i=0;i<MAXSCOPE;i++) if( scope->subscope[i]){ freeSymbolList(scope->subscope[i]); scope->subscope[i]= NULL; } GDKfree(scope->subscope); scope->subscope = 0;}void freeModule(Module m){ Symbol s; if (m==NULL) return; if ((s=findSymbolInModule(m, "epilogue")) != NULL) { InstrPtr pci = getInstrPtr(s->def,0); if (pci && pci->token == COMMANDsymbol && pci->argc == 1) { int ret = 0; (*pci->fcn)(&ret); (void)ret; } } freeSubScope(m); clrModuleJump(m->name, m); GDKfree(m);}void freeModuleList(Module s){ Module t=s; while(s){ t= s->outer; s->outer= NULL; freeModule(s); s=t; }}#line 245 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"void insertSymbol(Module scope, Symbol prg){ InstrPtr sig; int t; Module c; sig = getSignature(prg); if(getModuleId(sig) && getModuleId(sig)!= scope->name){ /* move the definition to the proper place */ c= findModule(scope,getModuleId(sig)); if(c == NULL){ GDKwarning("insertSymbol:undefined module\n"); } else scope = c; } t = getSubScope(getFunctionId(sig)); if( scope->subscope == NULL) newSubScope(scope); if(scope->subscope[t] == prg){ /* already known, last inserted */ } else { prg->peer= scope->subscope[t]; scope->subscope[t] = prg; if( prg->peer && idcmp(prg->name,prg->peer->name) == 0) prg->skip = prg->peer->skip; else prg->skip = prg->peer; } assert(prg != prg->peer);}#line 281 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"void deleteSymbol(Module scope, Symbol prg){ InstrPtr sig; int t; sig = getSignature(prg); if( getModuleId(sig) && getModuleId(sig)!= scope->name ){ /* move the definition to the proper place */ Module c= findModule(scope,getModuleId(sig)); if(c == NULL){ GDKwarning("deleteSymbol:undefined module\n"); } else scope = c; } t = getSubScope(getFunctionId(sig)); if( scope->subscope[t] == prg){ scope->subscope[t] = scope->subscope[t]->peer; freeSymbol(prg); } else { Symbol nxt = scope->subscope[t]; while( nxt->peer != NULL){ if( nxt->peer == prg){ nxt->peer = prg->peer; nxt->skip = prg->peer; freeSymbol(prg); return; } nxt = nxt->peer; } }}#line 334 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"void setInheritanceMode(Module m,int flag){ (void) flag; m->inheritance = 0;}Module setInheritance(Module h, Module f, Module s){ Module fp, sp; int i=0, j=0; sp= h; while(sp->outer && sp->outer->outer !=s) { sp= s; i++; } fp= h; while(fp->outer !=f) { fp= f; j++; } if( j<i) return h; if(h==s){ h= s->outer; s->outer= f->outer; f->outer=s; } else { sp->outer=s->outer; s->outer = f->outer; f->outer =s; } return h;}#line 372 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"Module findModule(Module scope, str name){ Module def=scope; if( name==NULL) return scope; scope= scopeJump[(int)(*name)][(int)(*(name+1))]; while(scope != NULL){ if( name == scope->name ) return scope; scope= scope->sibling; } /* default is always matched with current */ if( def->name==NULL) return NULL; return def;}#line 397 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"Symbol findSymbolInModule(Module v, str fcn){ Symbol s; if( v == NULL || fcn == NULL) return NULL; s= v->subscope[(int)(*fcn)]; while(s!=NULL){ if( idcmp(s->name,fcn)==0 ) return s; s= s->skip; } return NULL;}Symbol findMALSymbol(str mod, str fcn){ Module v; v= findModule(mal_scope, getName(mod,strlen(mod)) ); return findSymbolInModule(v,fcn);}int displayModule(stream *f, Module v, str fcn, int listing){ Symbol s; int k=0; if( v == NULL || fcn == NULL) return 0; s= v->subscope[(int)(*fcn)]; while(s!=NULL){ if( idcmp(s->name,fcn)==0 ) { printFunction(f,s->def,listing); k++; } s= s->peer; } return k;}#line 429 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"void printModuleScope(stream *fd, Module scope, int tab, int outer){ int j; Module s=scope; Symbol t; stream_printf(fd,"%smodule %s", (scope->isAtomModule?"atom ":""),s->name); stream_printf(fd,"\n"); if( s->subscope) for(j=0;j<MAXSCOPE;j++) if(s->subscope[j]){ stream_printf(fd,"[%c]",j); for(t= s->subscope[j];t!=NULL;t=t->peer) { stream_printf(fd," %s",t->name); if( getSignature(t)==NULL || (getSignature(t)->fcn==0 && getSignature(t)->token == COMMANDsymbol && getSignature(t)->blk==0) ) stream_printf(fd,"(?)"); } stream_printf(fd,"\n"); } stream_printf(fd,"\n"); if(outer && scope->outer) printModuleScope(fd,scope->outer,tab+1, outer);}void showModules(stream *f, Module s){ for(; s; s= s->outer) { stream_printf(f,"%s",s->name); if( s== mal_scope) stream_printf(f,"(*)"); if( s->subscope==0) stream_printf(f,"?"); if(s->outer) stream_printf(f,","); } stream_printf(f,"\n");}void debugModule(stream *f, Module start, str nme){ Module m; if( nme==0 || *nme ==0) printModuleScope(f, start,0,TRUE); else{ char *s; for(s=nme;*s && (isalnum((int) *s) ||*s=='_');s++); *s = 0; m= findModule(start,nme); if( m== NULL) stream_printf(f,"Module '%s' not found\n",nme); else printModuleScope(f,m,0,FALSE); }}#line 484 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_module.mx"void dumpManualHeader(stream *f){ stream_printf(f,"<?xml version=\"1.0\"?>\n"); stream_printf(f,"<manual>\n");}void dumpManualFooter(stream *f){ stream_printf(f,"</manual>\n");}int cmpModName(Module *f, Module *l){ return strcmp((*f)->name, (*l)->name);}int cmpFcnName(InstrPtr f, InstrPtr l){ if(getFunctionId(f) && getFunctionId(l)) return strcmp(getFunctionId(f), getFunctionId(l)); return 0;}void dumpManual(stream *f, Module s, int recursive){ int j; Symbol t; str ps, lnk, hlp=0, op=0, endtag=0; InstrPtr sig; Module list[256]; int k, top=0; if(s==NULL || f==NULL){ return; } list[top++]=s; while(s->outer && recursive){ list[top++]= s->outer;s=s->outer;} if(top>1) qsort(list, top, sizeof(Module), (int(*)(const void *, const void *))cmpModName); for(k=0;k<top;k++){ s= list[k]; stream_printf(f,"<%smodule name=\"%s\">\n", (s->isAtomModule?"atom":""),xmlChr(s->name)); if(s->help) stream_printf(f,"%s\n",s->help); if( s->subscope) for(j=0;j<MAXSCOPE;j++) if(s->subscope[j]){ for(t= s->subscope[j];t!=NULL;t=t->peer) { sig= getSignature(t); if(op==0 || strcmp(op,t->name)){ if(endtag) stream_printf(f," </%s>\n",endtag); stream_printf(f," <%s",fcnClass(sig)); op = t->name; stream_printf(f," name=\"%s\">\n",xmlChr(op)); if(t->def->help) stream_printf(f," <comment>%s</comment>\n", xmlChr(t->def->help)); op= t->name; endtag= fcnClass(sig); } ps= instruction2str(t->def,sig,0); lnk= strrchr(ps,'='); if(lnk && *(lnk+1)!='(') *lnk=0; stream_printf(f," <instantiation>\n"); stream_printf(f," <signature>%s</signature>\n", xmlChr(strchr(ps,'('))); if(lnk) stream_printf(f," <implementation>%s</implementation>\n",xmlChr(lnk+1)); GDKfree(ps); if(t->def->help){ if(hlp && strcmp(hlp,t->def->help)) stream_printf(f," <comment>%s</comment>\n", xmlChr(t->def->help)); } stream_printf(f," </instantiation>\n"); } } if(endtag) stream_printf(f," </%s>\n",endtag); stream_printf(f,"</%smodule>\n", (s->isAtomModule?"atom":"")); endtag=0; }}void dumpManualSection(stream *f, Module s){ int j; Symbol t; InstrPtr sig; str ps;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -