File.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "tokenizer/File.h"
  5. #include "utils/Utils.h"
  6. #define MAX_INDEX 50
  7. typedef struct {
  8. char* content;
  9. int size;
  10. int index;
  11. int arrayIndex;
  12. FILE* file;
  13. } OpenFile;
  14. typedef struct {
  15. int fileIndex;
  16. int index;
  17. } FilePointer;
  18. typedef struct {
  19. FilePointer name;
  20. FilePointer code;
  21. } Define;
  22. typedef struct {
  23. int fileIndex;
  24. int defineIndex;
  25. OpenFile files[MAX_INDEX];
  26. Define defines[MAX_INDEX];
  27. FilePointer readIndex;
  28. FileError error;
  29. } OpenFiles;
  30. static OpenFiles files;
  31. static int lastChar = 0;
  32. static int stackIndex = 0;
  33. static FilePointer stack[MAX_INDEX];
  34. static void fAdd(OpenFile* of, const void* data, int size) {
  35. while(of->index + size > of->size) {
  36. of->size *= 2;
  37. of->content = realloc(of->content, of->size);
  38. }
  39. memcpy(of->content + of->index, data, size);
  40. of->index += size;
  41. }
  42. static void fAddChar(OpenFile* of, int ic) {
  43. char c = ic;
  44. fAdd(of, &c, 1);
  45. }
  46. static int fReserverInt(OpenFile* of) {
  47. int address = of->index;
  48. int empty = 0x40404040;
  49. fAdd(of, &empty, sizeof(int));
  50. return address;
  51. }
  52. static void fSetInt(OpenFile* of, int address, int i) {
  53. memcpy(of->content + address, &i, sizeof(int));
  54. }
  55. static int fReadInt(OpenFile* of, int address) {
  56. int i = 0;
  57. memcpy(&i, of->content + address, sizeof(int));
  58. of->index += sizeof(int);
  59. return i;
  60. }
  61. static int fGet(OpenFile* of) {
  62. return fgetc(of->file);
  63. }
  64. static void fReadCommandString(OpenFile* of, char* buffer, int size) {
  65. int index = 0;
  66. int c = fGet(of);
  67. while(c == ' ') {
  68. c = fGet(of);
  69. }
  70. while(true) {
  71. if(c == EOF) {
  72. files.error("unexpected end of file");
  73. } else if(index >= size - 1) {
  74. files.error("unknown too long command");
  75. } else if(isLetter(c)) {
  76. buffer[index++] = c;
  77. } else if(c == ' ') {
  78. break;
  79. } else {
  80. files.error("unexpected character '%c'", (char)c);
  81. }
  82. c = fGet(of);
  83. }
  84. buffer[index] = '\0';
  85. }
  86. static void fDefine(OpenFile* of) {
  87. if(files.defineIndex >= MAX_INDEX) {
  88. files.error("too many defines");
  89. }
  90. Define* d = files.defines + files.defineIndex++;
  91. fAddChar(of, '#');
  92. int end = fReserverInt(of);
  93. char command[64];
  94. fReadCommandString(of, command, 64);
  95. d->name.fileIndex = of->arrayIndex;
  96. d->name.index = of->index;
  97. fAdd(of, command, strlen(command) + 1);
  98. d->code.fileIndex = of->arrayIndex;
  99. d->code.index = of->index;
  100. while(true) {
  101. int c = fGet(of);
  102. if(c == EOF || c == '\n') {
  103. break;
  104. }
  105. fAddChar(of, c);
  106. }
  107. fAddChar(of, '\0');
  108. fSetInt(of, end, of->index);
  109. fAddChar(of, '\n');
  110. }
  111. static void fReadCommand(OpenFile* of) {
  112. char command[64];
  113. fReadCommandString(of, command, 64);
  114. if(strcmp(command, "define") == 0) {
  115. fDefine(of);
  116. } else {
  117. files.error("invalid command '%s'", command);
  118. }
  119. }
  120. static void fReadFile(const char* path) {
  121. if(files.fileIndex >= MAX_INDEX) {
  122. files.error("cannot read file '%s': too many open files", path);
  123. }
  124. OpenFile* of = files.files + files.fileIndex;
  125. of->arrayIndex = files.fileIndex++;
  126. of->index = 0;
  127. of->size = 16;
  128. of->content = malloc(of->size);
  129. of->file = fopen(path, "r");
  130. if(of->file == NULL) {
  131. files.error("cannot read file '%s'", path);
  132. }
  133. while(true) {
  134. int c = fGet(of);
  135. if(c == '#') {
  136. fReadCommand(of);
  137. continue;
  138. }
  139. fAddChar(of, c);
  140. if(c == EOF) {
  141. break;
  142. }
  143. }
  144. fAddChar(of, '\0');
  145. }
  146. void fOpen(const char* path, FileError fe) {
  147. files.fileIndex = 0;
  148. files.defineIndex = 0;
  149. files.readIndex.fileIndex = 0;
  150. files.readIndex.index = 0;
  151. files.error = fe;
  152. lastChar = 0;
  153. stackIndex = 0;
  154. fReadFile(path);
  155. }
  156. void fClose() {
  157. for(int i = 0; i < files.fileIndex; i++) {
  158. fclose(files.files[i].file);
  159. free(files.files[i].content);
  160. }
  161. }
  162. static OpenFile* currentFile() {
  163. return files.files + files.readIndex.fileIndex;
  164. }
  165. static int fReadChar() {
  166. return currentFile()->content[files.readIndex.index];
  167. }
  168. static const char* fGetDefineName(Define* d) {
  169. return files.files[d->name.fileIndex].content + d->name.index;
  170. }
  171. static bool fCompare(const char* a, const char* b, unsigned int bLength) {
  172. return strncmp(a, b, bLength) == 0 && strlen(a) == bLength;
  173. }
  174. static void fEnter(Define* d) {
  175. if(stackIndex >= MAX_INDEX) {
  176. files.error("define stack overflow");
  177. }
  178. stack[stackIndex++] = files.readIndex;
  179. files.readIndex = d->code;
  180. }
  181. static void fPrepareChar() {
  182. int c = fReadChar();
  183. if(c == '\0') {
  184. if(stackIndex <= 0) {
  185. files.error("define stack underflow");
  186. }
  187. files.readIndex = stack[--stackIndex];
  188. }
  189. if(c == '#') {
  190. int end = fReadInt(currentFile(), files.readIndex.index + 1);
  191. files.readIndex.index = end;
  192. }
  193. if(isLetter(c) && !isLetter(lastChar)) {
  194. OpenFile* of = currentFile();
  195. int start = files.readIndex.index;
  196. int index = start;
  197. while(index < of->index && isLetter(of->content[index])) {
  198. index++;
  199. }
  200. for(int i = 0; i < files.defineIndex; i++) {
  201. Define* d = files.defines + i;
  202. const char* name = fGetDefineName(d);
  203. if(fCompare(name, of->content + start, index - start)) {
  204. files.readIndex.index = index;
  205. fEnter(d);
  206. }
  207. }
  208. }
  209. }
  210. int fRead() {
  211. fPrepareChar();
  212. lastChar = fReadChar();
  213. return currentFile()->content[files.readIndex.index++];
  214. }
  215. int fPeek() {
  216. fPrepareChar();
  217. return fReadChar();
  218. }
  219. bool fReadIf(int c) {
  220. if(fPeek() == c) {
  221. fRead();
  222. return true;
  223. }
  224. return false;
  225. }