Compiler.c 6.4 KB

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