Compiler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. if(!noReturn && !f->returns) {
  137. cError("function '%s' needs a return value on line %d", f->name, line);
  138. }
  139. cAddInt(f->address);
  140. cAddInt(arguments);
  141. if(f->returns && noReturn) {
  142. cAddOperation(OP_POP);
  143. }
  144. }
  145. }
  146. static void cLiteral() {
  147. const char* literal = cReadString();
  148. if(cConsumeTokenIf(T_OPEN_BRACKET)) {
  149. cCallFunction(literal, false);
  150. } else {
  151. cGetVar(literal);
  152. }
  153. }
  154. static void cPrimary() {
  155. Token t = cReadTokenAndLine();
  156. switch(t) {
  157. case T_INT: cConstantInt(); break;
  158. case T_FLOAT: cConstantFloat(); break;
  159. case T_NULL: cAddOperation(OP_PUSH_NULL); break;
  160. case T_TRUE: cAddOperation(OP_PUSH_TRUE); break;
  161. case T_FALSE: cAddOperation(OP_PUSH_FALSE); break;
  162. case T_OPEN_BRACKET:
  163. cExpression();
  164. cConsumeToken(T_CLOSE_BRACKET);
  165. break;
  166. case T_LITERAL: cLiteral(); break;
  167. default: cUnexpectedToken(t); break;
  168. }
  169. }
  170. static void cPreUnary() {
  171. if(cConsumeTokenIf(T_SUB)) {
  172. cPrimary();
  173. cAddOperation(OP_INVERT_SIGN);
  174. return;
  175. }
  176. cPrimary();
  177. }
  178. static void cMul() {
  179. cPreUnary();
  180. while(true) {
  181. if(cConsumeTokenIf(T_MUL)) {
  182. cPreUnary();
  183. cAddOperation(OP_MUL);
  184. } else if(cConsumeTokenIf(T_DIV)) {
  185. cPreUnary();
  186. cAddOperation(OP_DIV);
  187. } else if(cConsumeTokenIf(T_MOD)) {
  188. cPreUnary();
  189. cAddOperation(OP_MOD);
  190. } else {
  191. break;
  192. }
  193. }
  194. }
  195. static void cAdd() {
  196. cMul();
  197. while(true) {
  198. if(cConsumeTokenIf(T_ADD)) {
  199. cMul();
  200. cAddOperation(OP_ADD);
  201. } else if(cConsumeTokenIf(T_SUB)) {
  202. cMul();
  203. cAddOperation(OP_SUB);
  204. } else {
  205. break;
  206. }
  207. }
  208. }
  209. static void cComparison() {
  210. cAdd();
  211. while(true) {
  212. if(cConsumeTokenIf(T_LESS)) {
  213. cAdd();
  214. cAddOperation(OP_LESS);
  215. } else if(cConsumeTokenIf(T_LESS_EQUAL)) {
  216. cAdd();
  217. cAddOperation(OP_GREATER);
  218. cAddOperation(OP_NOT);
  219. } else if(cConsumeTokenIf(T_GREATER)) {
  220. cAdd();
  221. cAddOperation(OP_GREATER);
  222. } else if(cConsumeTokenIf(T_GREATER_EQUAL)) {
  223. cAdd();
  224. cAddOperation(OP_LESS);
  225. cAddOperation(OP_NOT);
  226. } else if(cConsumeTokenIf(T_EQUAL)) {
  227. cAdd();
  228. cAddOperation(OP_EQUAL);
  229. } else if(cConsumeTokenIf(T_NOT_EQUAL)) {
  230. cAdd();
  231. cAddOperation(OP_EQUAL);
  232. cAddOperation(OP_NOT);
  233. } else {
  234. break;
  235. }
  236. }
  237. }
  238. static void cLogical() {
  239. cComparison();
  240. while(true) {
  241. if(cConsumeTokenIf(T_AND)) {
  242. cAddOperation(OP_DUPLICATE);
  243. cAddOperation(OP_IF_GOTO);
  244. int p = cReserveInt();
  245. cComparison();
  246. cAddOperation(OP_AND);
  247. cSetInt(p, code->length);
  248. } else if(cConsumeTokenIf(T_OR)) {
  249. cAddOperation(OP_DUPLICATE);
  250. cAddOperation(OP_NOT);
  251. cAddOperation(OP_IF_GOTO);
  252. int p = cReserveInt();
  253. cComparison();
  254. cAddOperation(OP_OR);
  255. cSetInt(p, code->length);
  256. } else {
  257. break;
  258. }
  259. }
  260. }
  261. static void cExpression() {
  262. cLogical();
  263. }
  264. static void cSetVar(const char* literal) {
  265. cExpression();
  266. cConsumeToken(T_SEMICOLON);
  267. cAddOperation(OP_SET);
  268. cAddInt(cAddVar(literal));
  269. }
  270. static void cLineLiteral() {
  271. const char* literal = cReadString();
  272. Token t = cReadTokenAndLine();
  273. switch(t) {
  274. case T_SET: cSetVar(literal); break;
  275. case T_OPEN_BRACKET:
  276. cCallFunction(literal, true);
  277. cConsumeToken(T_SEMICOLON);
  278. break;
  279. default: cUnexpectedToken(t);
  280. }
  281. }
  282. static int cFunctionArguments() {
  283. int arguments = 0;
  284. while(!cConsumeTokenIf(T_CLOSE_BRACKET)) {
  285. cConsumeToken(T_LITERAL);
  286. arguments++;
  287. cAddVar(cReadString());
  288. if(cConsumeTokenIf(T_COMMA) && tPeekToken() != T_LITERAL) {
  289. cUnexpectedToken(tPeekToken());
  290. }
  291. }
  292. return arguments;
  293. }
  294. static void cLine(Token t);
  295. static void cConsumeBody() {
  296. cConsumeToken(T_OPEN_CURVED_BRACKET);
  297. int oldLine = line;
  298. while(!cConsumeTokenIf(T_CLOSE_CURVED_BRACKET)) {
  299. Token t = cReadTokenAndLine();
  300. if(t == T_END) {
  301. cError("unexpected end of file: non closed curved bracket on line %d", oldLine);
  302. }
  303. cLine(t);
  304. }
  305. }
  306. static void cLinkReturns() {
  307. for(int i = 0; i < returnIndex; i++) {
  308. cSetInt(returns[i], vars[1].entries);
  309. }
  310. returnIndex = 0;
  311. }
  312. static void cFunctionBody(const char* name, int arguments) {
  313. int oldLine = line;
  314. cAddOperation(OP_GOTO);
  315. int gotoIndex = cReserveInt();
  316. int address = code->length;
  317. returnState = 0;
  318. int p = cAddPush(arguments);
  319. cConsumeBody();
  320. cAddPop(p, vars[1].entries);
  321. cLinkReturns();
  322. if(!fmAdd(&functions, name, arguments, address, returnState == 2)) {
  323. cError("function registered twice on line %d", oldLine);
  324. }
  325. cAddOperation(OP_RETURN);
  326. cSetInt(gotoIndex, code->length);
  327. }
  328. static void cFunction() {
  329. if(varIndex == 1) {
  330. cError("function inside function on line %d", line);
  331. }
  332. cConsumeToken(T_LITERAL);
  333. const char* name = cReadString();
  334. cConsumeToken(T_OPEN_BRACKET);
  335. varIndex = 1;
  336. vars[1].entries = 0;
  337. cFunctionBody(name, cFunctionArguments());
  338. varIndex = 0;
  339. }
  340. static void cAddReturn() {
  341. cAddOperation(OP_POP_VARS);
  342. returns[returnIndex++] = cReserveInt(vars);
  343. cAddOperation(OP_RETURN);
  344. }
  345. static void cReturn() {
  346. if(varIndex == 0) {
  347. cError("return without a function on line %d", line);
  348. } else if(returnIndex >= RETURN_BUFFER) {
  349. cError("too much returns in function around line %d", line);
  350. }
  351. if(cConsumeTokenIf(T_SEMICOLON)) {
  352. if(returnState == 2) {
  353. cError("mixed return type on line %d", line);
  354. }
  355. returnState = 1;
  356. cAddReturn();
  357. } else {
  358. if(returnState == 1) {
  359. cError("mixed return type on line %d", line);
  360. }
  361. returnState = 2;
  362. cExpression();
  363. cAddOperation(OP_SET_RETURN);
  364. cAddReturn();
  365. cConsumeToken(T_SEMICOLON);
  366. }
  367. }
  368. static void cPrint() {
  369. cExpression();
  370. cConsumeToken(T_SEMICOLON);
  371. cAddOperation(OP_PRINT);
  372. }
  373. static void cIf() {
  374. cConsumeToken(T_OPEN_BRACKET);
  375. cExpression();
  376. cConsumeToken(T_CLOSE_BRACKET);
  377. cAddOperation(OP_IF_GOTO);
  378. int ifP = cReserveInt();
  379. cConsumeBody();
  380. cSetInt(ifP, code->length);
  381. if(cConsumeTokenIf(T_ELSE)) {
  382. cAddOperation(OP_GOTO);
  383. int elseP = cReserveInt();
  384. cSetInt(ifP, code->length);
  385. if(cConsumeTokenIf(T_IF)) {
  386. cIf();
  387. } else {
  388. cConsumeBody();
  389. }
  390. cSetInt(elseP, code->length);
  391. }
  392. }
  393. static void cWhile() {
  394. int start = code->length;
  395. cConsumeToken(T_OPEN_BRACKET);
  396. cExpression();
  397. cConsumeToken(T_CLOSE_BRACKET);
  398. cAddOperation(OP_IF_GOTO);
  399. int ifP = cReserveInt();
  400. cConsumeBody();
  401. cAddOperation(OP_GOTO);
  402. cAddInt(start);
  403. cSetInt(ifP, code->length);
  404. }
  405. static void cLine(Token t) {
  406. cAddOperation(OP_LINE);
  407. cAddInt16(line);
  408. switch(t) {
  409. case T_PRINT: cPrint(); break;
  410. case T_LITERAL: cLineLiteral(); break;
  411. case T_FUNCTION: cFunction(); break;
  412. case T_RETURN: cReturn(); break;
  413. case T_IF: cIf(); break;
  414. case T_WHILE: cWhile(); break;
  415. default: cUnexpectedToken(t);
  416. }
  417. }
  418. static void cForEachLine() {
  419. Token t = cReadTokenAndLine();
  420. while(t != T_END) {
  421. cLine(t);
  422. t = cReadTokenAndLine();
  423. }
  424. }
  425. static void cLinkQueuedFunctions() {
  426. for(int i = 0; i < functions.queueEntries; i++) {
  427. Function* f = fmSearch(&functions, functions.queue[i].name, functions.queue[i].arguments);
  428. if(f == NULL) {
  429. cError("unknown function on line %d", functions.queue[i].line);
  430. } else if(!functions.queue[i].noReturn && !f->returns) {
  431. cError("function '%s' needs a return value on line %d", f->name, functions.queue[i].line);
  432. }
  433. cSetInt(functions.queue[i].reserved, f->address);
  434. if(functions.queue[i].noReturn && f->returns) {
  435. code->code[functions.queue[i].reserved + sizeof(int) * 2] = OP_POP;
  436. }
  437. }
  438. }
  439. static void cAllocAndCompile() {
  440. varIndex = 0;
  441. returnIndex = 0;
  442. simInit(vars);
  443. simInit(vars + 1);
  444. fmInit(&functions);
  445. if(!setjmp(errorJump)) {
  446. int p = cAddPush(0);
  447. cForEachLine();
  448. cAddPop(p, vars[varIndex].entries);
  449. cLinkQueuedFunctions();
  450. }
  451. fmDelete(&functions);
  452. simDelete(vars + 1);
  453. simDelete(vars);
  454. }
  455. ByteCode* cCompile() {
  456. error[0] = '\0';
  457. code = bcInit();
  458. cAllocAndCompile();
  459. if(error[0] != '\0') {
  460. bcDelete(code);
  461. return NULL;
  462. }
  463. return code;
  464. }
  465. const char* cGetError() {
  466. return error;
  467. }