Compiler.c 6.8 KB

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