Compiler.c 8.6 KB

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