Compiler.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include <setjmp.h>
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include "Compiler.h"
  5. #include "FunctionMap.h"
  6. #include "Operation.h"
  7. #include "StringIntMap.h"
  8. #include "Tokenizer.h"
  9. #define ERROR_LENGTH 256
  10. #define RETURN_BUFFER 16
  11. static jmp_buf errorJump;
  12. static char error[ERROR_LENGTH] = {'\0'};
  13. static ByteCode* code;
  14. static int16 line = 1;
  15. static int varIndex = 0;
  16. static StringIntMap vars[2];
  17. static FunctionMap functions;
  18. static int returns[RETURN_BUFFER];
  19. static int returnIndex = 0;
  20. static int returnState = 0;
  21. static void cError(const char* format, ...) {
  22. va_list args;
  23. va_start(args, format);
  24. vsnprintf(error, ERROR_LENGTH, format, args);
  25. va_end(args);
  26. longjmp(errorJump, 0);
  27. }
  28. static int cAddVar(const char* var) {
  29. int index = vars[varIndex].entries;
  30. simAdd(vars + varIndex, var, &index);
  31. return index;
  32. }
  33. static void cUnexpectedToken(Token t) {
  34. cError("unexpected token on line %d: %s", line, tGetTokenName(t));
  35. }
  36. static void cAddOperation(Operation token) {
  37. unsigned char c = token;
  38. bcAddBytes(code, &c, 1);
  39. }
  40. static int cReserveInt() {
  41. return bcReserveBytes(code, sizeof(int));
  42. }
  43. static void cSetInt(int p, int i) {
  44. bcSetBytes(code, p, &i, sizeof(int));
  45. }
  46. static void cAddInt(int i) {
  47. bcAddBytes(code, &i, sizeof(int));
  48. }
  49. static void cAddInt16(int16 i) {
  50. bcAddBytes(code, &i, sizeof(int16));
  51. }
  52. static void cAddFloat(float f) {
  53. bcAddBytes(code, &f, sizeof(float));
  54. }
  55. static int cAddPush(int offset) {
  56. cAddOperation(OP_PUSH_VARS);
  57. int p = cReserveInt();
  58. cAddInt(offset);
  59. return p;
  60. }
  61. static void cAddPop(int p, int vars) {
  62. cAddOperation(OP_POP_VARS);
  63. cAddInt(vars);
  64. cSetInt(p, vars);
  65. }
  66. static Token cReadTokenAndLine() {
  67. Token t = tReadToken();
  68. if(tReadInt16(&line)) {
  69. return t;
  70. }
  71. return T_END;
  72. }
  73. static void cConsumeToken(Token wanted) {
  74. Token t = cReadTokenAndLine();
  75. if(wanted != t) {
  76. cError("unexpected token on line %d: expected '%s' got '%s'", line, tGetTokenName(wanted), tGetTokenName(t));
  77. }
  78. }
  79. static bool cConsumeTokenIf(Token t) {
  80. if(tPeekToken() == t) {
  81. cReadTokenAndLine();
  82. return true;
  83. }
  84. return false;
  85. }
  86. static void cConstantInt() {
  87. int value;
  88. if(!tReadInt(&value)) {
  89. cError("int token without an int on line %d", line);
  90. }
  91. cAddOperation(OP_PUSH_INT);
  92. cAddInt(value);
  93. }
  94. static void cConstantFloat() {
  95. float value;
  96. if(!tReadFloat(&value)) {
  97. cError("float token without a float on line %d", line);
  98. }
  99. cAddOperation(OP_PUSH_FLOAT);
  100. cAddFloat(value);
  101. }
  102. static const char* cReadString() {
  103. const char* literal = tReadString();
  104. if(literal == NULL) {
  105. cError("literal without string on line %d", line);
  106. }
  107. return literal;
  108. }
  109. static void cGetVar(const char* var) {
  110. cAddOperation(OP_GET);
  111. cAddInt(cAddVar(var));
  112. }
  113. static void cExpression();
  114. static int cCallFunctionArguments() {
  115. int arguments = 0;
  116. while(!cConsumeTokenIf(T_CLOSE_BRACKET)) {
  117. arguments++;
  118. cExpression();
  119. if(cConsumeTokenIf(T_COMMA) && tPeekToken() == T_CLOSE_BRACKET) {
  120. cUnexpectedToken(tPeekToken());
  121. }
  122. }
  123. return arguments;
  124. }
  125. static void cCallFunction(const char* literal, bool noReturn) {
  126. cAddOperation(OP_PUSH_INT);
  127. cAddInt(0);
  128. int arguments = cCallFunctionArguments();
  129. Function* f = fmSearch(&functions, literal, arguments);
  130. cAddOperation(OP_GOSUB);
  131. if(f == NULL) {
  132. fmEnqueue(&functions, literal, arguments, line, cReserveInt(), noReturn);
  133. cAddInt(arguments);
  134. cAddOperation(OP_NOTHING);
  135. } else {
  136. cAddInt(f->address);
  137. cAddInt(arguments);
  138. if(f->returns && noReturn) {
  139. cAddOperation(OP_POP);
  140. }
  141. }
  142. }
  143. static void cLiteral() {
  144. const char* literal = cReadString();
  145. if(cConsumeTokenIf(T_OPEN_BRACKET)) {
  146. cCallFunction(literal, false);
  147. } else {
  148. cGetVar(literal);
  149. }
  150. }
  151. static void cPrimary() {
  152. Token t = cReadTokenAndLine();
  153. switch(t) {
  154. case T_INT: cConstantInt(); break;
  155. case T_FLOAT: cConstantFloat(); break;
  156. case T_NULL: cAddOperation(OP_PUSH_NULL); break;
  157. case T_TRUE: cAddOperation(OP_PUSH_TRUE); break;
  158. case T_FALSE: cAddOperation(OP_PUSH_FALSE); break;
  159. case T_OPEN_BRACKET:
  160. cExpression();
  161. cConsumeToken(T_CLOSE_BRACKET);
  162. break;
  163. case T_LITERAL: cLiteral(); break;
  164. default: cUnexpectedToken(t); break;
  165. }
  166. }
  167. static void cMul() {
  168. cPrimary();
  169. while(cConsumeTokenIf(T_MUL)) {
  170. cPrimary();
  171. cAddOperation(OP_MUL);
  172. }
  173. }
  174. static void cAdd() {
  175. cMul();
  176. while(cConsumeTokenIf(T_ADD)) {
  177. cMul();
  178. cAddOperation(OP_ADD);
  179. }
  180. }
  181. static void cExpression() {
  182. cAdd();
  183. }
  184. static void cSetVar(const char* literal) {
  185. cExpression();
  186. cConsumeToken(T_SEMICOLON);
  187. cAddOperation(OP_SET);
  188. cAddInt(cAddVar(literal));
  189. }
  190. static void cLineLiteral() {
  191. const char* literal = cReadString();
  192. Token t = cReadTokenAndLine();
  193. switch(t) {
  194. case T_SET: cSetVar(literal); break;
  195. case T_OPEN_BRACKET:
  196. cCallFunction(literal, true);
  197. cConsumeToken(T_SEMICOLON);
  198. break;
  199. default: cUnexpectedToken(t);
  200. }
  201. }
  202. static int cFunctionArguments() {
  203. int arguments = 0;
  204. while(!cConsumeTokenIf(T_CLOSE_BRACKET)) {
  205. cConsumeToken(T_LITERAL);
  206. arguments++;
  207. cAddVar(cReadString());
  208. if(cConsumeTokenIf(T_COMMA) && tPeekToken() != T_LITERAL) {
  209. cUnexpectedToken(tPeekToken());
  210. }
  211. }
  212. return arguments;
  213. }
  214. static void cLine(Token t);
  215. static void cFunctionInnerBody(int arguments) {
  216. int p = cAddPush(arguments);
  217. int oldLine = line;
  218. while(!cConsumeTokenIf(T_CLOSE_CURVED_BRACKET)) {
  219. Token t = cReadTokenAndLine();
  220. if(t == T_END) {
  221. cError("unexpected end of file: function not closed on line %d", oldLine);
  222. }
  223. cLine(t);
  224. }
  225. cAddPop(p, vars[1].entries);
  226. for(int i = 0; i < returnIndex; i++) {
  227. cSetInt(returns[i], vars[1].entries);
  228. }
  229. returnIndex = 0;
  230. }
  231. static void cFunctionBody(const char* name, int arguments) {
  232. cConsumeToken(T_OPEN_CURVED_BRACKET);
  233. cAddOperation(OP_GOTO);
  234. int gotoIndex = cReserveInt();
  235. int address = code->length;
  236. returnState = 0;
  237. cFunctionInnerBody(arguments);
  238. if(!fmAdd(&functions, name, arguments, address, returnState == 2)) {
  239. cError("function registered twice on line %d", line);
  240. }
  241. cAddOperation(OP_RETURN);
  242. cSetInt(gotoIndex, code->length);
  243. }
  244. static void cFunction() {
  245. if(varIndex == 1) {
  246. cError("function inside function on line %d", line);
  247. }
  248. cConsumeToken(T_LITERAL);
  249. const char* name = cReadString();
  250. cConsumeToken(T_OPEN_BRACKET);
  251. varIndex = 1;
  252. vars[1].entries = 0;
  253. cFunctionBody(name, cFunctionArguments());
  254. varIndex = 0;
  255. }
  256. static void cAddReturn() {
  257. cAddOperation(OP_POP_VARS);
  258. returns[returnIndex++] = cReserveInt(vars);
  259. cAddOperation(OP_RETURN);
  260. }
  261. static void cReturn() {
  262. if(varIndex == 0) {
  263. cError("return without a function on line %d", line);
  264. } else if(returnIndex >= RETURN_BUFFER) {
  265. cError("too much returns in function around line %d", line);
  266. }
  267. if(cConsumeTokenIf(T_SEMICOLON)) {
  268. if(returnState == 2) {
  269. cError("mixed return type on line %d", line);
  270. }
  271. returnState = 1;
  272. cAddReturn();
  273. } else {
  274. if(returnState == 1) {
  275. cError("mixed return type on line %d", line);
  276. }
  277. returnState = 2;
  278. cExpression();
  279. cAddOperation(OP_SET_RETURN);
  280. cAddReturn();
  281. cConsumeToken(T_SEMICOLON);
  282. }
  283. }
  284. static void cPrint() {
  285. cExpression();
  286. cConsumeToken(T_SEMICOLON);
  287. cAddOperation(OP_PRINT);
  288. }
  289. static void cLine(Token t) {
  290. cAddOperation(OP_LINE);
  291. cAddInt16(line);
  292. switch(t) {
  293. case T_PRINT: cPrint(); break;
  294. case T_LITERAL: cLineLiteral(); break;
  295. case T_FUNCTION: cFunction(); break;
  296. case T_RETURN: cReturn(); break;
  297. default: cUnexpectedToken(t);
  298. }
  299. }
  300. static void cForEachLine() {
  301. Token t = cReadTokenAndLine();
  302. while(t != T_END) {
  303. cLine(t);
  304. t = cReadTokenAndLine();
  305. }
  306. }
  307. static void cLinkQueuedFunctions() {
  308. for(int i = 0; i < functions.queueEntries; i++) {
  309. Function* f = fmSearch(&functions, functions.queue[i].name, functions.queue[i].arguments);
  310. if(f == NULL) {
  311. cError("unknown function on line %d", functions.queue[i].line);
  312. }
  313. cSetInt(functions.queue[i].reserved, f->address);
  314. if(functions.queue[i].noReturn && f->returns) {
  315. code->code[functions.queue[i].reserved + sizeof(int) * 2] = OP_POP;
  316. }
  317. }
  318. }
  319. static void cAllocAndCompile() {
  320. varIndex = 0;
  321. returnIndex = 0;
  322. simInit(vars);
  323. simInit(vars + 1);
  324. fmInit(&functions);
  325. if(!setjmp(errorJump)) {
  326. int p = cAddPush(0);
  327. cForEachLine();
  328. cAddPop(p, vars[varIndex].entries);
  329. cLinkQueuedFunctions();
  330. }
  331. fmDelete(&functions);
  332. simDelete(vars + 1);
  333. simDelete(vars);
  334. }
  335. ByteCode* cCompile() {
  336. error[0] = '\0';
  337. code = bcInit();
  338. cAllocAndCompile();
  339. if(error[0] != '\0') {
  340. bcDelete(code);
  341. return NULL;
  342. }
  343. return code;
  344. }
  345. const char* cGetError() {
  346. return error;
  347. }