File.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 == ' ' || c == '\n') {
  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 const char* fGetDefineName(Define* d) {
  87. return files.files[d->name.fileIndex].content + d->name.index;
  88. }
  89. static int fStartDefine(OpenFile* of) {
  90. if(files.defineIndex >= MAX_INDEX) {
  91. files.error("too many defines");
  92. }
  93. Define* d = files.defines + files.defineIndex++;
  94. fAddChar(of, '#');
  95. int end = fReserverInt(of);
  96. char command[64];
  97. fReadCommandString(of, command, 64);
  98. d->name.fileIndex = of->arrayIndex;
  99. d->name.index = of->index;
  100. fAdd(of, command, strlen(command) + 1);
  101. for(int i = 0; i < files.defineIndex - 1; i++) {
  102. if(strcmp(command, fGetDefineName(files.defines + i)) == 0) {
  103. files.error("'%s' is already defined", command);
  104. }
  105. }
  106. d->code.fileIndex = of->arrayIndex;
  107. d->code.index = of->index;
  108. return end;
  109. }
  110. static void fFinishDefine(OpenFile* of, int end, int newLines) {
  111. fAddChar(of, '\0');
  112. fSetInt(of, end, of->index);
  113. for(int i = 0; i < newLines; i++) {
  114. fAddChar(of, '\n');
  115. }
  116. }
  117. static void fDefine(OpenFile* of) {
  118. int end = fStartDefine(of);
  119. while(true) {
  120. int c = fGet(of);
  121. if(c == '#') {
  122. files.error("# inside define");
  123. } else if(c == EOF || c == '\n') {
  124. break;
  125. }
  126. fAddChar(of, c);
  127. }
  128. fFinishDefine(of, end, 1);
  129. }
  130. static void fStart(OpenFile* of) {
  131. int end = fStartDefine(of);
  132. int newLines = 2;
  133. while(true) {
  134. int c = fGet(of);
  135. if(c == '#') {
  136. char end[64];
  137. fReadCommandString(of, end, 64);
  138. if(strcmp(end, "end") == 0) {
  139. break;
  140. }
  141. files.error("invalid command '%s' inside define region", end);
  142. } else if(c == EOF) {
  143. files.error("unclosed #start");
  144. }
  145. if(c == '\n') {
  146. newLines++;
  147. c = ' ';
  148. }
  149. fAddChar(of, c);
  150. }
  151. fFinishDefine(of, end, newLines);
  152. }
  153. static void fReadCommand(OpenFile* of) {
  154. char command[64];
  155. fReadCommandString(of, command, 64);
  156. if(strcmp(command, "define") == 0) {
  157. fDefine(of);
  158. } else if(strcmp(command, "start") == 0) {
  159. fStart(of);
  160. } else {
  161. files.error("invalid command '%s'", command);
  162. }
  163. }
  164. static void fReadFile(const char* path) {
  165. if(files.fileIndex >= MAX_INDEX) {
  166. files.error("cannot read file '%s': too many open files", path);
  167. }
  168. OpenFile* of = files.files + files.fileIndex;
  169. of->arrayIndex = files.fileIndex++;
  170. of->index = 0;
  171. of->size = 16;
  172. of->content = malloc(of->size);
  173. of->file = fopen(path, "r");
  174. if(of->file == NULL) {
  175. files.error("cannot read file '%s'", path);
  176. }
  177. while(true) {
  178. int c = fGet(of);
  179. if(c == '#') {
  180. fReadCommand(of);
  181. continue;
  182. }
  183. fAddChar(of, c);
  184. if(c == EOF) {
  185. break;
  186. }
  187. }
  188. fAddChar(of, '\0');
  189. }
  190. void fOpen(const char* path, FileError fe) {
  191. files.fileIndex = 0;
  192. files.defineIndex = 0;
  193. files.readIndex.fileIndex = 0;
  194. files.readIndex.index = 0;
  195. files.error = fe;
  196. lastChar = 0;
  197. stackIndex = 0;
  198. fReadFile(path);
  199. }
  200. void fClose() {
  201. for(int i = 0; i < files.fileIndex; i++) {
  202. if(files.files[i].file != NULL) {
  203. fclose(files.files[i].file);
  204. }
  205. free(files.files[i].content);
  206. }
  207. }
  208. static OpenFile* currentFile() {
  209. return files.files + files.readIndex.fileIndex;
  210. }
  211. static int fReadChar() {
  212. return currentFile()->content[files.readIndex.index];
  213. }
  214. static bool fCompare(const char* a, const char* b, unsigned int bLength) {
  215. return strncmp(a, b, bLength) == 0 && strlen(a) == bLength;
  216. }
  217. static void fEnter(Define* d) {
  218. if(stackIndex >= MAX_INDEX) {
  219. files.error("define stack overflow");
  220. }
  221. stack[stackIndex++] = files.readIndex;
  222. files.readIndex = d->code;
  223. }
  224. static bool fCheckForReplacement() {
  225. OpenFile* of = currentFile();
  226. int start = files.readIndex.index;
  227. int index = start;
  228. while(index < of->index && isLetter(of->content[index])) {
  229. index++;
  230. }
  231. for(int i = 0; i < files.defineIndex; i++) {
  232. Define* d = files.defines + i;
  233. const char* name = fGetDefineName(d);
  234. if(fCompare(name, of->content + start, index - start)) {
  235. files.readIndex.index = index;
  236. fEnter(d);
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. static void fPrepareChar() {
  243. while(true) {
  244. int c = fReadChar();
  245. if(c == '\0') {
  246. if(stackIndex <= 0) {
  247. files.error("define stack underflow");
  248. }
  249. files.readIndex = stack[--stackIndex];
  250. } else if(c == '#') {
  251. files.readIndex.index =
  252. fReadInt(currentFile(), files.readIndex.index + 1);
  253. } else if(isLetter(c) && !isLetter(lastChar)) {
  254. if(fCheckForReplacement()) {
  255. continue;
  256. }
  257. break;
  258. } else {
  259. break;
  260. }
  261. }
  262. }
  263. static int fReadI() {
  264. fPrepareChar();
  265. lastChar = fReadChar();
  266. return currentFile()->content[files.readIndex.index++];
  267. }
  268. int fRead() {
  269. int i = fReadI();
  270. putchar(i);
  271. return i;
  272. }
  273. int fPeek() {
  274. fPrepareChar();
  275. return fReadChar();
  276. }
  277. bool fReadIf(int c) {
  278. if(fPeek() == c) {
  279. fRead();
  280. return true;
  281. }
  282. return false;
  283. }