File.c 8.5 KB

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