symboltable.c 729 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include "sans.h"
  7. #define MAX_SYMBOLS 4096
  8. u num_symbols = 0;
  9. char* symboltable[MAX_SYMBOLS];
  10. char* lookup(u ref) {
  11. if (ref < num_symbols) {
  12. return symboltable[ref];
  13. }
  14. else {
  15. log_err("symboltable/lookup: %lu out of bounds %lu\n", ref, num_symbols);
  16. }
  17. }
  18. u intern(char *name) {
  19. u i;
  20. for (i = 0; i < num_symbols; i++) {
  21. if (!strcmp(symboltable[i], name)) {
  22. return i;
  23. }
  24. }
  25. symboltable[num_symbols] = malloc(strlen(name)+1);
  26. if (!symboltable[num_symbols]) {
  27. log_err("symboltable/intern: malloc failure\n");
  28. }
  29. strcpy(symboltable[num_symbols], name);
  30. return num_symbols++;
  31. }