symtable.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "symtable.h"
  5. int symtable_current = -1;
  6. static const char *native_functions[] = {
  7. "dd_translatef",
  8. "dd_math_rand",
  9. "dd_math_ease_linear",
  10. "dd_math_ease_linear2d",
  11. "dd_math_ease_bezier",
  12. "dd_math_ease_bezier2d",
  13. "dd_math_ease_catmullrom",
  14. "dd_math_ease_catmullrom2d",
  15. "dd_math_pow",
  16. "dd_math_abs",
  17. "dd_math_sqrt",
  18. "dd_math_sin",
  19. "dd_math_cos",
  20. "dd_math_tan",
  21. "dd_math_asin",
  22. "dd_math_acos",
  23. "dd_math_atan",
  24. "dd_math_min",
  25. "dd_math_max",
  26. "dd_math_minf",
  27. "dd_math_maxf",
  28. "dd_math_tan",
  29. "dd_math_rad2dec",
  30. "dd_math_dec2rad",
  31. "dd_math_dot2",
  32. "dd_math_dot3",
  33. };
  34. static unsigned int native_functions_count = sizeof(native_functions) /sizeof(char *);
  35. // number of entries on each symtable
  36. #define SYMMAX 600
  37. // how many scopes to descent
  38. #define SYMTABLE_SCOPE_MAX 100
  39. // symtable
  40. struct symtable {
  41. struct entry entry[SYMMAX];
  42. int lastentry;
  43. } symtable_array[SYMTABLE_SCOPE_MAX], *symtable;
  44. /* init
  45. * allocate memory
  46. * no symbols inside
  47. */
  48. void symtable_init() {
  49. symtable_current = -1;
  50. symtable_push();
  51. /*
  52. // init language's native functions
  53. for (unsigned int i = 0; i < native_functions_count; i++) {
  54. symtable_insert(native_functions[i], DD_SYMTABLE_NATIVE);
  55. }
  56. */
  57. }
  58. void symtable_push() {
  59. if (symtable_current == SYMTABLE_SCOPE_MAX-1) {
  60. printf("symtable_push: cannot push another table\n");
  61. exit(-1);
  62. }
  63. symtable_current++;
  64. symtable = &symtable_array[symtable_current];
  65. symtable->lastentry = -1;
  66. }
  67. void symtable_pop() {
  68. if (symtable_current == -1) {
  69. printf("symtable_pop: no table to pop\n");
  70. exit(-1);
  71. }
  72. symtable_current--;
  73. symtable = &symtable_array[symtable_current];
  74. }
  75. // find symbol, and return its index
  76. symtable_index symtable_lookup(const char s[]) {
  77. int p;
  78. for (p = 0; p <= symtable->lastentry; p++) {
  79. if (strcmp(symtable->entry[p].lexptr, s) == 0) {
  80. return p;
  81. }
  82. }
  83. return -1;
  84. }
  85. // find symbol, and return its entry
  86. struct entry *symtable_lookupEntry(const char s[]) {
  87. for (int i = symtable_current; i >= 0; i--) {
  88. struct symtable *st = &symtable_array[i];
  89. int p;
  90. for (p = 0; p <= st->lastentry; p++) {
  91. if (strcmp(st->entry[p].lexptr, s) == 0) {
  92. return &st->entry[p];
  93. }
  94. }
  95. }
  96. return 0;
  97. }
  98. // insert new symbol
  99. symtable_index symtable_insert(const char s[], int tok) {
  100. // entry to be returned
  101. struct entry *symentry;
  102. // entry already in sym table - return it
  103. int index = symtable_lookup(s);
  104. if (index >= 0) {
  105. return index;
  106. }
  107. // make sure sym table has space
  108. if (symtable->lastentry +1 >= SYMMAX) {
  109. printf("symbol table full\n");
  110. return 0;
  111. }
  112. // add new entry to symbol table
  113. // entry's index
  114. symtable->lastentry++;
  115. // create new entry
  116. symentry = &symtable->entry[symtable->lastentry];
  117. symentry->token = tok;
  118. symentry->isRef = 0;
  119. symentry->scope = 0;
  120. symentry->varType = DD_VARIABLE_TYPE_UNKNOWN;
  121. strncpy(symentry->lexptr, s, ENTRY_LEXPTR_SIZE -1);
  122. symentry->lexptr[ENTRY_LEXPTR_SIZE-1] = '\0';
  123. symentry->value = 0;
  124. return symtable->lastentry;
  125. }
  126. // clean current system table and all it's parents
  127. void symtable_clean() {
  128. /*
  129. struct symtable *csymtable;
  130. while (symtable != 0) {
  131. csymtable = symtable->parent;
  132. free(symtable);
  133. symtable = csymtable;
  134. }
  135. */
  136. }
  137. // print symbol table
  138. void symtable_print() {
  139. for (int j = 0; j <= symtable_current; j++) {
  140. printf("symtable %d:\n", j);
  141. for (int i = 0; i <= symtable_array[j].lastentry; i++) {
  142. if (symtable_array[j].entry[i].token == SYMTABLE_VARIABLE) {
  143. printf("\t%s | variable of type: %s\n", symtable_array[j].entry[i].lexptr,
  144. dd_variable_type_getString(symtable_array[j].entry[i].value)
  145. );
  146. }
  147. else {
  148. printf("\t%s | token: %d\n", symtable_array[j].entry[i].lexptr, symtable_array[j].entry[i].token);
  149. }
  150. }
  151. printf("end symtable %d:\n", j);
  152. }
  153. }
  154. // get entry from index
  155. struct entry *symtable_entryat(symtable_index index) {
  156. return &symtable->entry[index];
  157. }