symbol.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "hoc.h"
  12. #include "y.tab.h"
  13. static Symbol *symlist = 0; /* symbol table: linked list */
  14. Symbol*
  15. lookup(char* s) /* find s in symbol table */
  16. {
  17. Symbol *sp;
  18. for (sp = symlist; sp != (Symbol *) 0; sp = sp->next)
  19. if (strcmp(sp->name, s) == 0)
  20. return sp;
  21. return 0; /* 0 ==> not found */
  22. }
  23. Symbol*
  24. install(char* s, int t, double d) /* install s in symbol table */
  25. {
  26. Symbol *sp;
  27. sp = emalloc(sizeof(Symbol));
  28. sp->name = emalloc(strlen(s)+1); /* +1 for '\0' */
  29. strcpy(sp->name, s);
  30. sp->type = t;
  31. sp->u.val = d;
  32. sp->next = symlist; /* put at front of list */
  33. symlist = sp;
  34. return sp;
  35. }
  36. void*
  37. emalloc(unsigned n) /* check return from malloc */
  38. {
  39. char *p;
  40. p = malloc(n);
  41. if (p == 0)
  42. execerror("out of memory", (char *) 0);
  43. return p;
  44. }
  45. Formal*
  46. formallist(Symbol *formal, Formal *list) /* add formal to list */
  47. {
  48. Formal *f;
  49. f = emalloc(sizeof(Formal));
  50. f->sym = formal;
  51. f->save = 0;
  52. f->next = list;
  53. return f;
  54. }