Compiler.c 8.3 KB

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