symbol.c (980B)
1 #include <u.h> 2 #include <libc.h> 3 #include "hoc.h" 4 #include "y.tab.h" 5 6 static Symbol *symlist = 0; /* symbol table: linked list */ 7 8 Symbol* 9 lookup(char* s) /* find s in symbol table */ 10 { 11 Symbol *sp; 12 13 for (sp = symlist; sp != (Symbol *) 0; sp = sp->next) 14 if (strcmp(sp->name, s) == 0) 15 return sp; 16 return 0; /* 0 ==> not found */ 17 } 18 19 Symbol* 20 install(char* s, int t, double d) /* install s in symbol table */ 21 { 22 Symbol *sp; 23 24 sp = emalloc(sizeof(Symbol)); 25 sp->name = emalloc(strlen(s)+1); /* +1 for '\0' */ 26 strcpy(sp->name, s); 27 sp->type = t; 28 sp->u.val = d; 29 sp->next = symlist; /* put at front of list */ 30 symlist = sp; 31 return sp; 32 } 33 34 void* 35 emalloc(unsigned n) /* check return from malloc */ 36 { 37 char *p; 38 39 p = malloc(n); 40 if (p == 0) 41 execerror("out of memory", (char *) 0); 42 return p; 43 } 44 45 Formal* 46 formallist(Symbol *formal, Formal *list) /* add formal to list */ 47 { 48 Formal *f; 49 50 f = emalloc(sizeof(Formal)); 51 f->sym = formal; 52 f->save = 0; 53 f->next = list; 54 return f; 55 }