File.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. bool newLines;
  13. char* path;
  14. FILE* file;
  15. } OpenFile;
  16. typedef struct {
  17. int fileIndex;
  18. int index;
  19. } FilePointer;
  20. typedef struct {
  21. FilePointer name;
  22. FilePointer code;
  23. } Define;
  24. typedef struct {
  25. int fileIndex;
  26. int defineIndex;
  27. OpenFile files[MAX_INDEX];
  28. Define defines[MAX_INDEX];
  29. FilePointer readIndex;
  30. FileError error;
  31. } OpenFiles;
  32. static OpenFiles files;
  33. static int lastChar = 0;
  34. static int stackIndex = 0;
  35. static FilePointer stack[MAX_INDEX];
  36. static void fAdd(OpenFile* of, const void* data, int size) {
  37. while(of->index + size > of->size) {
  38. of->size *= 2;
  39. of->content = realloc(of->content, of->size);
  40. }
  41. memcpy(of->content + of->index, data, size);
  42. of->index += size;
  43. }
  44. static void fAddChar(OpenFile* of, int ic) {
  45. char c = ic;
  46. fAdd(of, &c, 1);
  47. }
  48. static int fReserverInt(OpenFile* of) {
  49. int address = of->index;
  50. int empty = 0;
  51. fAdd(of, &empty, sizeof(int));
  52. return address;
  53. }
  54. static void fSetInt(OpenFile* of, int address, int i) {
  55. memcpy(of->content + address, &i, sizeof(int));
  56. }
  57. static int fReadInt(OpenFile* of, int address) {
  58. int i = 0;
  59. memcpy(&i, of->content + address, sizeof(int));
  60. of->index += sizeof(int);
  61. return i;
  62. }
  63. static int fGet(OpenFile* of) {
  64. int c = fgetc(of->file);
  65. if(!of->newLines && c == '\n') {
  66. return ' ';
  67. }
  68. return c;
  69. }
  70. static bool fReadCommandString(OpenFile* of, char* buffer, int size) {
  71. int index = 0;
  72. int c = fGet(of);
  73. while(c == ' ') {
  74. c = fGet(of);
  75. }
  76. int text = 0;
  77. while(true) {
  78. if(c == EOF) {
  79. files.error("unexpected end of file");
  80. } else if(index >= size - 1) {
  81. files.error("unknown too long command");
  82. } else if(c == '"' && text < 2) {
  83. text++;
  84. buffer[index++] = c;
  85. } else if((isLetter(c) || text == 1) && text < 2) {
  86. buffer[index++] = c;
  87. } else if(c == ' ' || c == '\n') {
  88. buffer[index] = '\0';
  89. return c == '\n';
  90. } else {
  91. files.error("unexpected character '%c'", (char)c);
  92. }
  93. c = fGet(of);
  94. }
  95. }
  96. static const char* fGetDefineName(Define* d) {
  97. return files.files[d->name.fileIndex].content + d->name.index;
  98. }
  99. static int fStartDefine(OpenFile* of, bool* newLine) {
  100. fAddChar(of, '#');
  101. int end = fReserverInt(of);
  102. int nameFileIndex = fReserverInt(of);
  103. int nameIndex = fReserverInt(of);
  104. int codeFileIndex = fReserverInt(of);
  105. int codeIndex = fReserverInt(of);
  106. char command[64];
  107. *newLine = fReadCommandString(of, command, 64);
  108. fSetInt(of, nameFileIndex, of->arrayIndex);
  109. fSetInt(of, nameIndex, of->index);
  110. fAdd(of, command, strlen(command) + 1);
  111. fSetInt(of, codeFileIndex, of->arrayIndex);
  112. fSetInt(of, codeIndex, of->index);
  113. return end;
  114. }
  115. static void fFinishDefine(OpenFile* of, int end, int newLines) {
  116. fAddChar(of, '\0');
  117. fSetInt(of, end, of->index);
  118. for(int i = 0; i < newLines; i++) {
  119. fAddChar(of, '\n');
  120. }
  121. }
  122. static void fDefine(OpenFile* of) {
  123. bool newLine = false;
  124. int end = fStartDefine(of, &newLine);
  125. int newLines = newLine;
  126. while(true) {
  127. int c = fGet(of);
  128. if(c == '#') {
  129. char end[64];
  130. newLines += fReadCommandString(of, end, 64);
  131. if(strcmp(end, "end") == 0) {
  132. break;
  133. }
  134. files.error("invalid command '%s' inside define region", end);
  135. } else if(c == EOF) {
  136. files.error("unclosed #define");
  137. }
  138. if(c == '\n') {
  139. newLines++;
  140. c = ' ';
  141. }
  142. fAddChar(of, c);
  143. }
  144. fFinishDefine(of, end, newLines);
  145. }
  146. static void fReadFile(const char* path, bool newLines);
  147. static void fEnter(FilePointer* fp);
  148. static int fConcat(char* buffer, int size, const char* s) {
  149. int l = strlen(s) + 1;
  150. if(l > size) {
  151. files.error("too long include path");
  152. }
  153. memcpy(buffer, s, l);
  154. return l;
  155. }
  156. static void fInclude(OpenFile* of) {
  157. char path[64];
  158. if(fReadCommandString(of, path, 64)) {
  159. fAddChar(of, '\n');
  160. }
  161. int l = strlen(path);
  162. if(l < 2 || path[0] != '"' || path[l - 1] != '"') {
  163. files.error("invalid include path '%s'", path);
  164. }
  165. path[l - 1] = '\0';
  166. const char* cutPath = path + 1;
  167. char fullName[256];
  168. int index = fConcat(fullName, 256, of->path);
  169. while(index > 0 && fullName[index - 1] != '/') {
  170. index--;
  171. }
  172. fConcat(fullName + index, 256 - index, cutPath);
  173. FilePointer fp = {files.fileIndex, 0};
  174. fEnter(&fp);
  175. fReadFile(fullName, false);
  176. }
  177. static void fReadCommand(OpenFile* of) {
  178. char command[64];
  179. fReadCommandString(of, command, 64);
  180. if(strcmp(command, "define") == 0) {
  181. fDefine(of);
  182. } else if(strcmp(command, "include") == 0) {
  183. fInclude(of);
  184. } else {
  185. files.error("invalid command '%s'", command);
  186. }
  187. }
  188. static void fReadFile(const char* path, bool newLines) {
  189. if(files.fileIndex >= MAX_INDEX) {
  190. files.error("cannot read file '%s': too many open files", path);
  191. }
  192. OpenFile* of = files.files + files.fileIndex;
  193. of->arrayIndex = files.fileIndex++;
  194. of->index = 0;
  195. of->size = 16;
  196. of->newLines = newLines;
  197. of->content = malloc(of->size);
  198. of->file = fopen(path, "r");
  199. of->path = strdup(path);
  200. if(of->file == NULL) {
  201. files.error("cannot read file '%s'", path);
  202. }
  203. while(true) {
  204. int c = fGet(of);
  205. if(c == '#') {
  206. fReadCommand(of);
  207. continue;
  208. }
  209. fAddChar(of, c);
  210. if(c == EOF) {
  211. break;
  212. }
  213. }
  214. fAddChar(of, '\0');
  215. }
  216. void fOpen(const char* path, FileError fe) {
  217. files.fileIndex = 0;
  218. files.defineIndex = 0;
  219. files.readIndex.fileIndex = 0;
  220. files.readIndex.index = 0;
  221. files.error = fe;
  222. lastChar = 0;
  223. stackIndex = 0;
  224. fReadFile(path, true);
  225. }
  226. void fClose() {
  227. for(int i = 0; i < files.fileIndex; i++) {
  228. if(files.files[i].file != NULL) {
  229. fclose(files.files[i].file);
  230. }
  231. free(files.files[i].path);
  232. free(files.files[i].content);
  233. }
  234. }
  235. static OpenFile* currentFile() {
  236. return files.files + files.readIndex.fileIndex;
  237. }
  238. static int fReadChar() {
  239. return currentFile()->content[files.readIndex.index];
  240. }
  241. static bool fCompare(const char* a, const char* b, unsigned int bLength) {
  242. return strncmp(a, b, bLength) == 0 && strlen(a) == bLength;
  243. }
  244. static void fEnter(FilePointer* fp) {
  245. if(stackIndex >= MAX_INDEX) {
  246. files.error("define stack overflow");
  247. }
  248. stack[stackIndex++] = files.readIndex;
  249. files.readIndex = *fp;
  250. }
  251. static bool fCheckForReplacement() {
  252. OpenFile* of = currentFile();
  253. int start = files.readIndex.index;
  254. int index = start;
  255. while(index < of->index && isLetter(of->content[index])) {
  256. index++;
  257. }
  258. for(int i = 0; i < files.defineIndex; i++) {
  259. Define* d = files.defines + i;
  260. const char* name = fGetDefineName(d);
  261. if(fCompare(name, of->content + start, index - start)) {
  262. files.readIndex.index = index;
  263. fEnter(&d->code);
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. static void fReadDefine() {
  270. int base = files.readIndex.index + 1;
  271. int end = fReadInt(currentFile(), base);
  272. int nameFileIndex = fReadInt(currentFile(), base + sizeof(int));
  273. int nameIndex = fReadInt(currentFile(), base + sizeof(int) * 2);
  274. int codeFileIndex = fReadInt(currentFile(), base + sizeof(int) * 3);
  275. int codeIndex = fReadInt(currentFile(), base + sizeof(int) * 4);
  276. const char* name = files.files[nameFileIndex].content + nameIndex;
  277. if(files.defineIndex >= MAX_INDEX) {
  278. files.error("too many defines");
  279. }
  280. Define* d = files.defines + files.defineIndex++;
  281. for(int i = 0; i < files.defineIndex - 1; i++) {
  282. if(strcmp(name, fGetDefineName(files.defines + i)) == 0) {
  283. d = files.defines + i;
  284. files.defineIndex--;
  285. break;
  286. }
  287. }
  288. d->name.fileIndex = nameFileIndex;
  289. d->name.index = nameIndex;
  290. d->code.fileIndex = codeFileIndex;
  291. d->code.index = codeIndex;
  292. files.readIndex.index = end;
  293. }
  294. static void fPrepareChar() {
  295. while(true) {
  296. int c = fReadChar();
  297. if(c == '\0') {
  298. if(stackIndex <= 0) {
  299. files.error("define stack underflow");
  300. }
  301. files.readIndex = stack[--stackIndex];
  302. } else if(c == EOF && stackIndex > 0) {
  303. files.readIndex = stack[--stackIndex];
  304. } else if(c == '#') {
  305. fReadDefine();
  306. } else if(isLetter(c) && !isLetter(lastChar)) {
  307. if(fCheckForReplacement()) {
  308. continue;
  309. }
  310. break;
  311. } else {
  312. break;
  313. }
  314. }
  315. }
  316. static int fReadI() {
  317. fPrepareChar();
  318. lastChar = fReadChar();
  319. return currentFile()->content[files.readIndex.index++];
  320. }
  321. int fRead() {
  322. int i = fReadI();
  323. // putchar(i);
  324. return i;
  325. }
  326. int fPeek() {
  327. fPrepareChar();
  328. return fReadChar();
  329. }
  330. bool fReadIf(int c) {
  331. if(fPeek() == c) {
  332. fRead();
  333. return true;
  334. }
  335. return false;
  336. }