2
0

alang_eval.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "alang.h"
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. typedef union value {
  7. float f;
  8. int i;
  9. bool b;
  10. } value;
  11. typedef struct variable_storage {
  12. value val;
  13. type_id type;
  14. } variable_storage;
  15. typedef struct interpreter_state {
  16. variable_storage vars[1024];
  17. size_t pos;
  18. } interpreter_state;
  19. static value get_variable_value(interpreter_state *state, variable var) {
  20. return state->vars[var.index].val;
  21. }
  22. static void set_variable_value(interpreter_state *state, variable var, value v) {
  23. state->vars[var.index].val = v;
  24. state->vars[var.index].type = var.type.type;
  25. }
  26. static float eval() {
  27. interpreter_state state = {0};
  28. function *main_func = NULL;
  29. for (function_id i = 0; _get_function(i) != NULL; ++i) {
  30. function *f = _get_function(i);
  31. if (strcmp(_get_name(f->name), "main") == 0) {
  32. main_func = f;
  33. break;
  34. }
  35. }
  36. for (size_t i = 0; i < __allocated_globals_size; ++i) {
  37. global *g = __allocated_globals[i].g;
  38. uint64_t var_index = __allocated_globals[i].variable_id;
  39. if (g->value.kind == GLOBAL_VALUE_FLOAT) {
  40. state.vars[var_index].val.f = g->value.value.floats[0];
  41. state.vars[var_index].type = g->type;
  42. }
  43. else if (g->value.kind == GLOBAL_VALUE_INT) {
  44. state.vars[var_index].val.i = g->value.value.ints[0];
  45. state.vars[var_index].type = g->type;
  46. }
  47. else if (g->value.kind == GLOBAL_VALUE_BOOL) {
  48. state.vars[var_index].val.b = g->value.value.b;
  49. state.vars[var_index].type = g->type;
  50. }
  51. }
  52. state.pos = 0;
  53. opcodes *code = &main_func->code;
  54. while (state.pos < code->size) {
  55. opcode *op = (opcode *)&code->o[state.pos];
  56. state.pos += op->size;
  57. switch (op->type) {
  58. case OPCODE_VAR: {
  59. break;
  60. }
  61. case OPCODE_LOAD_FLOAT_CONSTANT: {
  62. value v;
  63. v.f = op->op_load_float_constant.number;
  64. set_variable_value(&state, op->op_load_float_constant.to, v);
  65. break;
  66. }
  67. case OPCODE_LOAD_INT_CONSTANT: {
  68. value v;
  69. v.i = op->op_load_int_constant.number;
  70. set_variable_value(&state, op->op_load_int_constant.to, v);
  71. break;
  72. }
  73. case OPCODE_LOAD_BOOL_CONSTANT: {
  74. value v;
  75. v.b = op->op_load_bool_constant.boolean;
  76. set_variable_value(&state, op->op_load_bool_constant.to, v);
  77. break;
  78. }
  79. case OPCODE_STORE_VARIABLE: {
  80. value v = get_variable_value(&state, op->op_store_var.from);
  81. set_variable_value(&state, op->op_store_var.to, v);
  82. break;
  83. }
  84. case OPCODE_ADD: {
  85. value left = get_variable_value(&state, op->op_binary.left);
  86. value right = get_variable_value(&state, op->op_binary.right);
  87. value result;
  88. type_id result_type = op->op_binary.result.type.type;
  89. if (result_type == _float_id) {
  90. float l = (op->op_binary.left.type.type == _float_id) ? left.f : (float)left.i;
  91. float r = (op->op_binary.right.type.type == _float_id) ? right.f : (float)right.i;
  92. result.f = l + r;
  93. }
  94. else {
  95. result.i = left.i + right.i;
  96. }
  97. set_variable_value(&state, op->op_binary.result, result);
  98. break;
  99. }
  100. case OPCODE_SUB: {
  101. value left = get_variable_value(&state, op->op_binary.left);
  102. value right = get_variable_value(&state, op->op_binary.right);
  103. value result;
  104. type_id result_type = op->op_binary.result.type.type;
  105. if (result_type == _float_id) {
  106. float l = (op->op_binary.left.type.type == _float_id) ? left.f : (float)left.i;
  107. float r = (op->op_binary.right.type.type == _float_id) ? right.f : (float)right.i;
  108. result.f = l - r;
  109. }
  110. else {
  111. result.i = left.i - right.i;
  112. }
  113. set_variable_value(&state, op->op_binary.result, result);
  114. break;
  115. }
  116. case OPCODE_MULTIPLY: {
  117. value left = get_variable_value(&state, op->op_binary.left);
  118. value right = get_variable_value(&state, op->op_binary.right);
  119. value result;
  120. type_id result_type = op->op_binary.result.type.type;
  121. if (result_type == _float_id) {
  122. float l = (op->op_binary.left.type.type == _float_id) ? left.f : (float)left.i;
  123. float r = (op->op_binary.right.type.type == _float_id) ? right.f : (float)right.i;
  124. result.f = l * r;
  125. }
  126. else {
  127. result.i = left.i * right.i;
  128. }
  129. set_variable_value(&state, op->op_binary.result, result);
  130. break;
  131. }
  132. case OPCODE_DIVIDE: {
  133. value left = get_variable_value(&state, op->op_binary.left);
  134. value right = get_variable_value(&state, op->op_binary.right);
  135. value result;
  136. type_id result_type = op->op_binary.result.type.type;
  137. if (result_type == _float_id) {
  138. float l = (op->op_binary.left.type.type == _float_id) ? left.f : (float)left.i;
  139. float r = (op->op_binary.right.type.type == _float_id) ? right.f : (float)right.i;
  140. result.f = l / r;
  141. }
  142. else {
  143. result.i = left.i / right.i;
  144. }
  145. set_variable_value(&state, op->op_binary.result, result);
  146. break;
  147. }
  148. case OPCODE_MOD: {
  149. value left = get_variable_value(&state, op->op_binary.left);
  150. value right = get_variable_value(&state, op->op_binary.right);
  151. value result;
  152. result.i = left.i % right.i;
  153. set_variable_value(&state, op->op_binary.result, result);
  154. break;
  155. }
  156. case OPCODE_EQUALS: {
  157. value left = get_variable_value(&state, op->op_binary.left);
  158. value right = get_variable_value(&state, op->op_binary.right);
  159. value result;
  160. type_id left_type = op->op_binary.left.type.type;
  161. type_id right_type = op->op_binary.right.type.type;
  162. if (left_type == _float_id || right_type == _float_id) {
  163. float l = (left_type == _float_id) ? left.f : (float)left.i;
  164. float r = (right_type == _float_id) ? right.f : (float)right.i;
  165. result.b = l == r;
  166. }
  167. else if (left_type == _bool_id && right_type == _bool_id) {
  168. result.b = left.b == right.b;
  169. }
  170. else {
  171. result.b = left.i == right.i;
  172. }
  173. set_variable_value(&state, op->op_binary.result, result);
  174. break;
  175. }
  176. case OPCODE_NOT_EQUALS: {
  177. value left = get_variable_value(&state, op->op_binary.left);
  178. value right = get_variable_value(&state, op->op_binary.right);
  179. value result;
  180. type_id left_type = op->op_binary.left.type.type;
  181. type_id right_type = op->op_binary.right.type.type;
  182. if (left_type == _float_id || right_type == _float_id) {
  183. float l = (left_type == _float_id) ? left.f : (float)left.i;
  184. float r = (right_type == _float_id) ? right.f : (float)right.i;
  185. result.b = l != r;
  186. }
  187. else if (left_type == _bool_id && right_type == _bool_id) {
  188. result.b = left.b != right.b;
  189. }
  190. else {
  191. result.b = left.i != right.i;
  192. }
  193. set_variable_value(&state, op->op_binary.result, result);
  194. break;
  195. }
  196. case OPCODE_GREATER: {
  197. value left = get_variable_value(&state, op->op_binary.left);
  198. value right = get_variable_value(&state, op->op_binary.right);
  199. value result;
  200. type_id left_type = op->op_binary.left.type.type;
  201. type_id right_type = op->op_binary.right.type.type;
  202. if (left_type == _float_id || right_type == _float_id) {
  203. float l = (left_type == _float_id) ? left.f : (float)left.i;
  204. float r = (right_type == _float_id) ? right.f : (float)right.i;
  205. result.b = l > r;
  206. }
  207. else {
  208. result.b = left.i > right.i;
  209. }
  210. set_variable_value(&state, op->op_binary.result, result);
  211. break;
  212. }
  213. case OPCODE_GREATER_EQUAL: {
  214. value left = get_variable_value(&state, op->op_binary.left);
  215. value right = get_variable_value(&state, op->op_binary.right);
  216. value result;
  217. type_id left_type = op->op_binary.left.type.type;
  218. type_id right_type = op->op_binary.right.type.type;
  219. if (left_type == _float_id || right_type == _float_id) {
  220. float l = (left_type == _float_id) ? left.f : (float)left.i;
  221. float r = (right_type == _float_id) ? right.f : (float)right.i;
  222. result.b = l >= r;
  223. }
  224. else {
  225. result.b = left.i >= right.i;
  226. }
  227. set_variable_value(&state, op->op_binary.result, result);
  228. break;
  229. }
  230. case OPCODE_LESS: {
  231. value left = get_variable_value(&state, op->op_binary.left);
  232. value right = get_variable_value(&state, op->op_binary.right);
  233. value result;
  234. type_id left_type = op->op_binary.left.type.type;
  235. type_id right_type = op->op_binary.right.type.type;
  236. if (left_type == _float_id || right_type == _float_id) {
  237. float l = (left_type == _float_id) ? left.f : (float)left.i;
  238. float r = (right_type == _float_id) ? right.f : (float)right.i;
  239. result.b = l < r;
  240. }
  241. else {
  242. result.b = left.i < right.i;
  243. }
  244. set_variable_value(&state, op->op_binary.result, result);
  245. break;
  246. }
  247. case OPCODE_LESS_EQUAL: {
  248. value left = get_variable_value(&state, op->op_binary.left);
  249. value right = get_variable_value(&state, op->op_binary.right);
  250. value result;
  251. type_id left_type = op->op_binary.left.type.type;
  252. type_id right_type = op->op_binary.right.type.type;
  253. if (left_type == _float_id || right_type == _float_id) {
  254. float l = (left_type == _float_id) ? left.f : (float)left.i;
  255. float r = (right_type == _float_id) ? right.f : (float)right.i;
  256. result.b = l <= r;
  257. }
  258. else {
  259. result.b = left.i <= right.i;
  260. }
  261. set_variable_value(&state, op->op_binary.result, result);
  262. break;
  263. }
  264. case OPCODE_AND: {
  265. value left = get_variable_value(&state, op->op_binary.left);
  266. value right = get_variable_value(&state, op->op_binary.right);
  267. value result;
  268. result.b = left.b && right.b;
  269. set_variable_value(&state, op->op_binary.result, result);
  270. break;
  271. }
  272. case OPCODE_OR: {
  273. value left = get_variable_value(&state, op->op_binary.left);
  274. value right = get_variable_value(&state, op->op_binary.right);
  275. value result;
  276. result.b = left.b || right.b;
  277. set_variable_value(&state, op->op_binary.result, result);
  278. break;
  279. }
  280. case OPCODE_BITWISE_XOR: {
  281. value left = get_variable_value(&state, op->op_binary.left);
  282. value right = get_variable_value(&state, op->op_binary.right);
  283. value result;
  284. result.i = left.i ^ right.i;
  285. set_variable_value(&state, op->op_binary.result, result);
  286. break;
  287. }
  288. case OPCODE_BITWISE_AND: {
  289. value left = get_variable_value(&state, op->op_binary.left);
  290. value right = get_variable_value(&state, op->op_binary.right);
  291. value result;
  292. result.i = left.i & right.i;
  293. set_variable_value(&state, op->op_binary.result, result);
  294. break;
  295. }
  296. case OPCODE_BITWISE_OR: {
  297. value left = get_variable_value(&state, op->op_binary.left);
  298. value right = get_variable_value(&state, op->op_binary.right);
  299. value result;
  300. result.i = left.i | right.i;
  301. set_variable_value(&state, op->op_binary.result, result);
  302. break;
  303. }
  304. case OPCODE_LEFT_SHIFT: {
  305. value left = get_variable_value(&state, op->op_binary.left);
  306. value right = get_variable_value(&state, op->op_binary.right);
  307. value result;
  308. result.i = left.i << right.i;
  309. set_variable_value(&state, op->op_binary.result, result);
  310. break;
  311. }
  312. case OPCODE_RIGHT_SHIFT: {
  313. value left = get_variable_value(&state, op->op_binary.left);
  314. value right = get_variable_value(&state, op->op_binary.right);
  315. value result;
  316. result.i = left.i >> right.i;
  317. set_variable_value(&state, op->op_binary.result, result);
  318. break;
  319. }
  320. case OPCODE_NOT: {
  321. value from = get_variable_value(&state, op->op_not.from);
  322. value result;
  323. result.b = !from.b;
  324. set_variable_value(&state, op->op_not.to, result);
  325. break;
  326. }
  327. case OPCODE_NEGATE: {
  328. value from = get_variable_value(&state, op->op_negate.from);
  329. value result;
  330. if (op->op_negate.from.type.type == _float_id) {
  331. result.f = -from.f;
  332. }
  333. else {
  334. result.i = -from.i;
  335. }
  336. set_variable_value(&state, op->op_negate.to, result);
  337. break;
  338. }
  339. case OPCODE_IF: {
  340. value condition = get_variable_value(&state, op->op_if.condition);
  341. if (!condition.b) {
  342. state.pos = op->op_if.end_id;
  343. }
  344. break;
  345. }
  346. case OPCODE_WHILE_START: {
  347. break;
  348. }
  349. case OPCODE_WHILE_CONDITION: {
  350. value condition = get_variable_value(&state, op->op_while.condition);
  351. if (!condition.b) {
  352. state.pos = op->op_while.end_id;
  353. }
  354. break;
  355. }
  356. case OPCODE_WHILE_END: {
  357. state.pos = op->op_while_end.start_id;
  358. break;
  359. }
  360. case OPCODE_BLOCK_START:
  361. case OPCODE_BLOCK_END: {
  362. break;
  363. }
  364. case OPCODE_RETURN: {
  365. if (op->op_return.var.index != 0) {
  366. value return_val = get_variable_value(&state, op->op_return.var);
  367. return return_val.f;
  368. }
  369. return 0.0;
  370. }
  371. case OPCODE_CALL: {
  372. if (strcmp(_get_name(op->op_call.func), "print") == 0) {
  373. value param = get_variable_value(&state, op->op_call.parameters[0]);
  374. value result;
  375. printf("%f\n", param.f);
  376. set_variable_value(&state, op->op_call.var, result);
  377. }
  378. break;
  379. }
  380. default: {
  381. break;
  382. }
  383. }
  384. }
  385. return 0.0;
  386. }
  387. float alang_eval(char *data) {
  388. _names_init();
  389. _types_init();
  390. _functions_init();
  391. _globals_init();
  392. char buffer[2048];
  393. strcpy(buffer, "fun main(): float { return ");
  394. strcat(buffer, data);
  395. strcat(buffer, "; }");
  396. char *filename = "main.kong";
  397. tokens tokens = _tokenize(filename, buffer);
  398. _parse(filename, &tokens);
  399. _resolve_types();
  400. // allocate_globals();
  401. for (function_id i = 0; _get_function(i) != NULL; ++i) {
  402. _compile_function_block(&_get_function(i)->code, _get_function(i)->block);
  403. }
  404. return eval(buffer);
  405. }