You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

builtins.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include "vm/builtins.h"
  2. #include <stdio.h>
  3. static void print_val(struct l2_vm *vm, struct l2_io_writer *out, struct l2_vm_value *val) {
  4. switch (l2_value_get_type(val)) {
  5. case L2_VAL_TYPE_NONE:
  6. l2_io_printf(out, "(none)");
  7. break;
  8. case L2_VAL_TYPE_ATOM:
  9. if (val->atom == vm->values[vm->ktrue].atom) {
  10. l2_io_printf(out, "(true)");
  11. } else if (val->atom == vm->values[vm->kfalse].atom) {
  12. l2_io_printf(out, "(false)");
  13. } else {
  14. l2_io_printf(out, "(atom %u)", val->atom);
  15. }
  16. break;
  17. case L2_VAL_TYPE_REAL:
  18. l2_io_printf(out, "%g", val->real);
  19. break;
  20. case L2_VAL_TYPE_BUFFER:
  21. if (val->buffer != NULL) {
  22. out->write(out, val->buffer, val->extra.buf_length);
  23. }
  24. break;
  25. case L2_VAL_TYPE_ARRAY:
  26. out->write(out, "[", 1);
  27. l2_word *data;
  28. if (val->flags & L2_VAL_SBO) {
  29. data = val->shortarray;
  30. } else {
  31. data = val->array->data;
  32. }
  33. for (size_t i = 0; i < val->extra.arr_length; ++i) {
  34. if (i != 0) {
  35. out->write(out, " ", 1);
  36. }
  37. print_val(vm, out, &vm->values[data[i]]);
  38. }
  39. out->write(out, "]", 1);
  40. break;
  41. case L2_VAL_TYPE_NAMESPACE:
  42. l2_io_printf(out, "(namespace)");
  43. break;
  44. case L2_VAL_TYPE_FUNCTION:
  45. case L2_VAL_TYPE_CFUNCTION:
  46. l2_io_printf(out, "(function)");
  47. break;
  48. case L2_VAL_TYPE_ERROR:
  49. l2_io_printf(out, "(error: %s)", val->error);
  50. break;
  51. case L2_VAL_TYPE_CONTINUATION:
  52. l2_io_printf(out, "(continuation)");
  53. break;
  54. }
  55. }
  56. l2_word l2_builtin_add(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  57. if (argc < 1) {
  58. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  59. vm->values[id].real = 0;
  60. return id;
  61. }
  62. struct l2_vm_value *val = &vm->values[argv[0]];
  63. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  64. return l2_vm_type_error(vm, val);
  65. }
  66. double sum = val->real;
  67. for (l2_word i = 1; i < argc; ++i) {
  68. val = &vm->values[argv[i]];
  69. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  70. return l2_vm_type_error(vm, val);
  71. }
  72. sum += val->real;
  73. }
  74. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  75. vm->values[id].real = sum;
  76. return id;
  77. }
  78. l2_word l2_builtin_sub(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  79. if (argc < 1) {
  80. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  81. vm->values[id].real = 0;
  82. return id;
  83. }
  84. struct l2_vm_value *val = &vm->values[argv[0]];
  85. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  86. return l2_vm_type_error(vm, val);
  87. }
  88. double sum = val->real;
  89. for (l2_word i = 1; i < argc; ++i) {
  90. val = &vm->values[argv[i]];
  91. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  92. return l2_vm_type_error(vm, val);
  93. }
  94. sum -= val->real;
  95. }
  96. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  97. vm->values[id].real = sum;
  98. return id;
  99. }
  100. l2_word l2_builtin_mul(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  101. if (argc < 1) {
  102. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  103. vm->values[id].real = 1;
  104. return id;
  105. }
  106. struct l2_vm_value *val = &vm->values[argv[0]];
  107. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  108. return l2_vm_type_error(vm, val);
  109. }
  110. double sum = val->real;
  111. for (l2_word i = 1; i < argc; ++i) {
  112. val = &vm->values[argv[i]];
  113. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  114. return l2_vm_type_error(vm, val);
  115. }
  116. sum *= val->real;
  117. }
  118. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  119. vm->values[id].real = sum;
  120. return id;
  121. }
  122. l2_word l2_builtin_div(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  123. if (argc < 1) {
  124. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  125. vm->values[id].real = 1;
  126. return id;
  127. }
  128. struct l2_vm_value *val = &vm->values[argv[0]];
  129. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  130. return l2_vm_type_error(vm, val);
  131. }
  132. double sum = val->real;
  133. for (l2_word i = 1; i < argc; ++i) {
  134. val = &vm->values[argv[i]];
  135. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) {
  136. return l2_vm_type_error(vm, val);
  137. }
  138. sum /= val->real;
  139. }
  140. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  141. vm->values[id].real = sum;
  142. return id;
  143. }
  144. l2_word l2_builtin_eq(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  145. if (argc < 2) {
  146. return vm->ktrue;
  147. }
  148. for (l2_word i = 1; i < argc; ++i) {
  149. if (argv[i - 1] == argv[i]) continue;
  150. struct l2_vm_value *a = &vm->values[argv[i - 1]];
  151. struct l2_vm_value *b = &vm->values[argv[i]];
  152. if (a->flags != b->flags) {
  153. return vm->kfalse;
  154. }
  155. enum l2_value_type typ = l2_value_get_type(a);
  156. if (typ == L2_VAL_TYPE_ATOM) {
  157. if (a->atom != b->atom) {
  158. return vm->kfalse;
  159. }
  160. } else if (typ == L2_VAL_TYPE_REAL) {
  161. if (a->real != b->real) {
  162. return vm->kfalse;
  163. }
  164. } else if (typ == L2_VAL_TYPE_BUFFER) {
  165. if (a->buffer == NULL && b->buffer == NULL) continue;
  166. if (a->buffer == NULL || b->buffer == NULL) {
  167. return vm->kfalse;
  168. }
  169. if (a->extra.buf_length != b->extra.buf_length) {
  170. return vm->kfalse;
  171. }
  172. if (memcmp(a->buffer, b->buffer, a->extra.buf_length) != 0) {
  173. return vm->kfalse;
  174. }
  175. } else {
  176. return vm->kfalse;
  177. }
  178. }
  179. return vm->ktrue;
  180. }
  181. l2_word l2_builtin_neq(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  182. l2_word ret_id = l2_builtin_eq(vm, argc, argv);
  183. if (ret_id == vm->ktrue) {
  184. return vm->kfalse;
  185. } else if (ret_id == vm->kfalse) {
  186. return vm->ktrue;
  187. } else {
  188. return ret_id;
  189. }
  190. }
  191. #define X(name, op) \
  192. l2_word name(struct l2_vm *vm, l2_word argc, l2_word *argv) { \
  193. if (argc < 2) { \
  194. return vm->ktrue; \
  195. } \
  196. struct l2_vm_value *lhs = &vm->values[argv[0]]; \
  197. if (l2_value_get_type(lhs) != L2_VAL_TYPE_REAL) { \
  198. return l2_vm_type_error(vm, lhs); \
  199. } \
  200. for (l2_word i = 1; i < argc; ++i) { \
  201. struct l2_vm_value *rhs = &vm->values[argv[i]]; \
  202. if (l2_value_get_type(rhs) != L2_VAL_TYPE_REAL) { \
  203. return l2_vm_type_error(vm, rhs); \
  204. } \
  205. if (!(lhs->real op rhs->real)) { \
  206. return vm->kfalse; \
  207. } \
  208. lhs = rhs; \
  209. } \
  210. return vm->ktrue; \
  211. }
  212. X(l2_builtin_lt, <)
  213. X(l2_builtin_lteq, <=)
  214. X(l2_builtin_gt, >)
  215. X(l2_builtin_gteq, >=)
  216. #undef X
  217. l2_word l2_builtin_print(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  218. for (size_t i = 0; i < argc; ++i) {
  219. if (i != 0) {
  220. vm->std_output->write(vm->std_output, " ", 1);
  221. }
  222. struct l2_vm_value *val = &vm->values[argv[i]];
  223. print_val(vm, vm->std_output, val);
  224. }
  225. vm->std_output->write(vm->std_output, "\n", 1);
  226. return 0;
  227. }
  228. l2_word l2_builtin_len(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  229. if (argc != 1) {
  230. return l2_vm_error(vm, "Expected 1 argument");
  231. }
  232. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  233. struct l2_vm_value *ret = &vm->values[ret_id];
  234. ret->real = 0;
  235. struct l2_vm_value *val = &vm->values[argv[0]];
  236. switch (l2_value_get_type(val)) {
  237. case L2_VAL_TYPE_NONE:
  238. case L2_VAL_TYPE_ATOM:
  239. case L2_VAL_TYPE_REAL:
  240. case L2_VAL_TYPE_FUNCTION:
  241. case L2_VAL_TYPE_CFUNCTION:
  242. case L2_VAL_TYPE_ERROR:
  243. case L2_VAL_TYPE_CONTINUATION:
  244. break;
  245. case L2_VAL_TYPE_BUFFER:
  246. ret->real = val->extra.buf_length;
  247. break;
  248. case L2_VAL_TYPE_ARRAY:
  249. ret->real = val->extra.arr_length;
  250. break;
  251. case L2_VAL_TYPE_NAMESPACE:
  252. if (val->ns) {
  253. ret->real = val->ns->len;
  254. }
  255. }
  256. return ret_id;
  257. }
  258. l2_word l2_builtin_if(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  259. if (argc != 2 && argc != 3) {
  260. return l2_vm_error(vm, "Expected 2 or 3 arguments");
  261. }
  262. if (l2_vm_val_is_true(vm, argv[0])) {
  263. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  264. struct l2_vm_value *ret = &vm->values[ret_id];
  265. ret->extra.cont_call = argv[1];
  266. return ret_id;
  267. } else if (argc == 3) {
  268. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  269. struct l2_vm_value *ret = &vm->values[ret_id];
  270. ret->extra.cont_call = argv[2];
  271. return ret_id;
  272. } else {
  273. return 0;
  274. }
  275. }
  276. struct loop_context {
  277. struct l2_vm_contcontext base;
  278. l2_word func;
  279. };
  280. static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) {
  281. if (l2_vm_val_is_true(vm, retval)) {
  282. return cont;
  283. }
  284. return retval;
  285. }
  286. static void loop_marker(
  287. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id)) {
  288. struct loop_context *ctx = data;
  289. mark(vm, ctx->func);
  290. }
  291. l2_word l2_builtin_loop(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  292. if (argc != 1) {
  293. return l2_vm_error(vm, "Expected 1 argument");
  294. }
  295. struct loop_context *ctx = malloc(sizeof(*ctx));
  296. if (ctx == NULL) {
  297. return l2_vm_error(vm, "Allocation failure");
  298. }
  299. ctx->base.callback = loop_callback;
  300. ctx->base.marker = loop_marker;
  301. ctx->func = argv[0];
  302. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  303. struct l2_vm_value *cont = &vm->values[cont_id];
  304. cont->extra.cont_call = ctx->func;
  305. cont->cont = &ctx->base;
  306. return cont_id;
  307. }
  308. struct while_context {
  309. struct l2_vm_contcontext base;
  310. l2_word cond, body;
  311. };
  312. static l2_word while_callback(struct l2_vm *vm, l2_word retval, l2_word cont_id) {
  313. struct l2_vm_value *cont = &vm->values[cont_id];
  314. struct while_context *ctx = (struct while_context *)cont->cont;
  315. if (cont->extra.cont_call == ctx->cond) {
  316. if (l2_vm_val_is_true(vm, retval)) {
  317. cont->extra.cont_call = ctx->body;
  318. return cont_id;
  319. } else {
  320. return retval;
  321. }
  322. } else {
  323. struct l2_vm_value *ret = &vm->values[retval];
  324. if (l2_value_get_type(ret) == L2_VAL_TYPE_ERROR) {
  325. return retval;
  326. } else {
  327. cont->extra.cont_call = ctx->cond;
  328. return cont_id;
  329. }
  330. }
  331. }
  332. static void while_marker(
  333. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id)) {
  334. struct while_context *ctx = data;
  335. mark(vm, ctx->cond);
  336. mark(vm, ctx->body);
  337. }
  338. l2_word l2_builtin_while(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  339. if (argc != 2) {
  340. return l2_vm_error(vm, "Expected 2 arguments");
  341. }
  342. struct while_context *ctx = malloc(sizeof(*ctx));
  343. if (ctx == NULL) {
  344. return l2_vm_error(vm, "Allocation failure");
  345. }
  346. ctx->base.callback = while_callback;
  347. ctx->base.marker = while_marker;
  348. ctx->cond = argv[0];
  349. ctx->body = argv[1];
  350. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  351. struct l2_vm_value *cont = &vm->values[cont_id];
  352. cont->extra.cont_call = ctx->cond;
  353. cont->cont = &ctx->base;
  354. return cont_id;
  355. }