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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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_CONTINUATION:
  49. l2_io_printf(out, "(continuation)");
  50. break;
  51. case L2_VAL_TYPE_RETURN:
  52. l2_io_printf(out, "(return)");
  53. break;
  54. case L2_VAL_TYPE_ERROR:
  55. l2_io_printf(out, "(error: %s)", val->error);
  56. break;
  57. }
  58. }
  59. #define X(name, identity, op) \
  60. l2_word name(struct l2_vm *vm, l2_word argc, l2_word *argv) { \
  61. if (argc == 0) { \
  62. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0); \
  63. vm->values[id].real = identity; \
  64. return id; \
  65. } \
  66. struct l2_vm_value *first = &vm->values[argv[0]]; \
  67. if (l2_value_get_type(first) != L2_VAL_TYPE_REAL) { \
  68. return l2_vm_type_error(vm, first); \
  69. } \
  70. if (argc == 1) { \
  71. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0); \
  72. vm->values[id].real = identity op first->real; \
  73. return id; \
  74. } \
  75. double sum = first->real; \
  76. for (l2_word i = 1; i < argc; ++i) { \
  77. struct l2_vm_value *val = &vm->values[argv[i]]; \
  78. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) { \
  79. return l2_vm_type_error(vm, val); \
  80. } \
  81. sum = sum op val->real; \
  82. } \
  83. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0); \
  84. vm->values[id].real = sum; \
  85. return id; \
  86. }
  87. X(l2_builtin_add, 0, +)
  88. X(l2_builtin_sub, 0, -)
  89. X(l2_builtin_mul, 1, *)
  90. X(l2_builtin_div, 1, /)
  91. #undef X
  92. l2_word l2_builtin_eq(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  93. if (argc < 2) {
  94. return vm->ktrue;
  95. }
  96. for (l2_word i = 1; i < argc; ++i) {
  97. if (argv[i - 1] == argv[i]) continue;
  98. struct l2_vm_value *a = &vm->values[argv[i - 1]];
  99. struct l2_vm_value *b = &vm->values[argv[i]];
  100. if (l2_value_get_type(a) != l2_value_get_type(b)) {
  101. return vm->kfalse;
  102. }
  103. enum l2_value_type typ = l2_value_get_type(a);
  104. if (typ == L2_VAL_TYPE_ATOM) {
  105. if (a->atom != b->atom) {
  106. return vm->kfalse;
  107. }
  108. } else if (typ == L2_VAL_TYPE_REAL) {
  109. if (a->real != b->real) {
  110. return vm->kfalse;
  111. }
  112. } else if (typ == L2_VAL_TYPE_BUFFER) {
  113. if (a->buffer == NULL && b->buffer == NULL) continue;
  114. if (a->buffer == NULL || b->buffer == NULL) {
  115. return vm->kfalse;
  116. }
  117. if (a->extra.buf_length != b->extra.buf_length) {
  118. return vm->kfalse;
  119. }
  120. if (memcmp(a->buffer, b->buffer, a->extra.buf_length) != 0) {
  121. return vm->kfalse;
  122. }
  123. } else {
  124. return vm->kfalse;
  125. }
  126. }
  127. return vm->ktrue;
  128. }
  129. l2_word l2_builtin_neq(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  130. l2_word ret_id = l2_builtin_eq(vm, argc, argv);
  131. if (ret_id == vm->ktrue) {
  132. return vm->kfalse;
  133. } else if (ret_id == vm->kfalse) {
  134. return vm->ktrue;
  135. } else {
  136. return ret_id;
  137. }
  138. }
  139. #define X(name, op) \
  140. l2_word name(struct l2_vm *vm, l2_word argc, l2_word *argv) { \
  141. if (argc < 2) { \
  142. return vm->ktrue; \
  143. } \
  144. struct l2_vm_value *lhs = &vm->values[argv[0]]; \
  145. if (l2_value_get_type(lhs) != L2_VAL_TYPE_REAL) { \
  146. return l2_vm_type_error(vm, lhs); \
  147. } \
  148. for (l2_word i = 1; i < argc; ++i) { \
  149. struct l2_vm_value *rhs = &vm->values[argv[i]]; \
  150. if (l2_value_get_type(rhs) != L2_VAL_TYPE_REAL) { \
  151. return l2_vm_type_error(vm, rhs); \
  152. } \
  153. if (!(lhs->real op rhs->real)) { \
  154. return vm->kfalse; \
  155. } \
  156. lhs = rhs; \
  157. } \
  158. return vm->ktrue; \
  159. }
  160. X(l2_builtin_lt, <)
  161. X(l2_builtin_lteq, <=)
  162. X(l2_builtin_gt, >)
  163. X(l2_builtin_gteq, >=)
  164. #undef X
  165. l2_word l2_builtin_land(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  166. for (l2_word i = 0; i < argc; ++i) {
  167. struct l2_vm_value *val = &vm->values[argv[i]];
  168. if (l2_value_get_type(val) == L2_VAL_TYPE_ERROR) {
  169. return argv[i];
  170. }
  171. if (!l2_vm_val_is_true(vm, val)) {
  172. return vm->kfalse;
  173. }
  174. }
  175. return vm->ktrue;
  176. }
  177. l2_word l2_builtin_lor(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  178. for (l2_word i = 0; i < argc; ++i) {
  179. struct l2_vm_value *val = &vm->values[argv[i]];
  180. if (l2_value_get_type(val) == L2_VAL_TYPE_ERROR) {
  181. return argv[i];
  182. }
  183. if (l2_vm_val_is_true(vm, val)) {
  184. return vm->ktrue;
  185. }
  186. }
  187. return vm->kfalse;
  188. }
  189. l2_word l2_builtin_first(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  190. for (l2_word i = 0; i < argc; ++i) {
  191. if (l2_value_get_type(&vm->values[argv[i]]) != L2_VAL_TYPE_NONE) {
  192. return argv[i];
  193. }
  194. }
  195. return vm->knone;
  196. }
  197. l2_word l2_builtin_print(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  198. for (size_t i = 0; i < argc; ++i) {
  199. if (i != 0) {
  200. vm->std_output->write(vm->std_output, " ", 1);
  201. }
  202. struct l2_vm_value *val = &vm->values[argv[i]];
  203. print_val(vm, vm->std_output, val);
  204. }
  205. vm->std_output->write(vm->std_output, "\n", 1);
  206. return 0;
  207. }
  208. l2_word l2_builtin_len(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  209. if (argc != 1) {
  210. return l2_vm_error(vm, "Expected 1 argument");
  211. }
  212. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  213. struct l2_vm_value *ret = &vm->values[ret_id];
  214. ret->real = 0;
  215. struct l2_vm_value *val = &vm->values[argv[0]];
  216. switch (l2_value_get_type(val)) {
  217. case L2_VAL_TYPE_NONE:
  218. case L2_VAL_TYPE_ATOM:
  219. case L2_VAL_TYPE_REAL:
  220. case L2_VAL_TYPE_FUNCTION:
  221. case L2_VAL_TYPE_CFUNCTION:
  222. case L2_VAL_TYPE_ERROR:
  223. case L2_VAL_TYPE_CONTINUATION:
  224. case L2_VAL_TYPE_RETURN:
  225. break;
  226. case L2_VAL_TYPE_BUFFER:
  227. ret->real = val->extra.buf_length;
  228. break;
  229. case L2_VAL_TYPE_ARRAY:
  230. ret->real = val->extra.arr_length;
  231. break;
  232. case L2_VAL_TYPE_NAMESPACE:
  233. if (val->ns) {
  234. ret->real = val->ns->len;
  235. }
  236. }
  237. return ret_id;
  238. }
  239. l2_word l2_builtin_if(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  240. if (argc != 2 && argc != 3) {
  241. return l2_vm_error(vm, "Expected 2 or 3 arguments");
  242. }
  243. if (l2_vm_val_is_true(vm, &vm->values[argv[0]])) {
  244. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  245. struct l2_vm_value *ret = &vm->values[ret_id];
  246. ret->extra.cont_call = argv[1];
  247. return ret_id;
  248. } else if (argc == 3) {
  249. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  250. struct l2_vm_value *ret = &vm->values[ret_id];
  251. ret->extra.cont_call = argv[2];
  252. return ret_id;
  253. } else {
  254. return 0;
  255. }
  256. }
  257. struct loop_context {
  258. struct l2_vm_contcontext base;
  259. l2_word func;
  260. };
  261. static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) {
  262. struct l2_vm_value *val = &vm->values[retval];
  263. if (l2_value_get_type(val) == L2_VAL_TYPE_ERROR) {
  264. return retval;
  265. } else if (
  266. l2_value_get_type(val) == L2_VAL_TYPE_ATOM &&
  267. val->atom == vm->values[vm->kstop].atom) {
  268. return vm->knone;
  269. } else {
  270. return cont;
  271. }
  272. }
  273. static void loop_marker(
  274. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id)) {
  275. struct loop_context *ctx = data;
  276. mark(vm, ctx->func);
  277. }
  278. l2_word l2_builtin_loop(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  279. if (argc != 1) {
  280. return l2_vm_error(vm, "Expected 1 argument");
  281. }
  282. struct loop_context *ctx = malloc(sizeof(*ctx));
  283. if (ctx == NULL) {
  284. return l2_vm_error(vm, "Allocation failure");
  285. }
  286. ctx->base.callback = loop_callback;
  287. ctx->base.marker = loop_marker;
  288. ctx->base.args = vm->knone;
  289. ctx->func = argv[0];
  290. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  291. struct l2_vm_value *cont = &vm->values[cont_id];
  292. cont->extra.cont_call = ctx->func;
  293. cont->cont = &ctx->base;
  294. return cont_id;
  295. }
  296. struct while_context {
  297. struct l2_vm_contcontext base;
  298. l2_word cond, body;
  299. };
  300. static l2_word while_callback(struct l2_vm *vm, l2_word retval, l2_word cont_id) {
  301. struct l2_vm_value *cont = &vm->values[cont_id];
  302. struct while_context *ctx = (struct while_context *)cont->cont;
  303. struct l2_vm_value *ret = &vm->values[retval];
  304. if (l2_value_get_type(ret) == L2_VAL_TYPE_ERROR) {
  305. return retval;
  306. }
  307. if (cont->extra.cont_call == ctx->cond) {
  308. if (l2_vm_val_is_true(vm, ret)) {
  309. cont->extra.cont_call = ctx->body;
  310. return cont_id;
  311. } else {
  312. return vm->knone;
  313. }
  314. } else {
  315. if (l2_value_get_type(ret) == L2_VAL_TYPE_ERROR) {
  316. return retval;
  317. } else {
  318. cont->extra.cont_call = ctx->cond;
  319. return cont_id;
  320. }
  321. }
  322. }
  323. static void while_marker(
  324. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id)) {
  325. struct while_context *ctx = data;
  326. mark(vm, ctx->cond);
  327. mark(vm, ctx->body);
  328. }
  329. l2_word l2_builtin_while(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  330. if (argc != 2) {
  331. return l2_vm_error(vm, "Expected 2 arguments");
  332. }
  333. struct while_context *ctx = malloc(sizeof(*ctx));
  334. if (ctx == NULL) {
  335. return l2_vm_error(vm, "Allocation failure");
  336. }
  337. ctx->base.callback = while_callback;
  338. ctx->base.marker = while_marker;
  339. ctx->base.args = vm->knone;
  340. ctx->cond = argv[0];
  341. ctx->body = argv[1];
  342. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  343. struct l2_vm_value *cont = &vm->values[cont_id];
  344. cont->extra.cont_call = ctx->cond;
  345. cont->cont = &ctx->base;
  346. return cont_id;
  347. }
  348. struct for_context {
  349. struct l2_vm_contcontext base;
  350. l2_word iter;
  351. l2_word func;
  352. };
  353. static l2_word for_callback(struct l2_vm *vm, l2_word retval, l2_word cont_id) {
  354. struct l2_vm_value *cont = &vm->values[cont_id];
  355. struct for_context *ctx = (struct for_context *)cont->cont;
  356. struct l2_vm_value *ret = &vm->values[retval];
  357. if (l2_value_get_type(ret) == L2_VAL_TYPE_ERROR) {
  358. return retval;
  359. }
  360. struct l2_vm_value *args = &vm->values[cont->cont->args];
  361. if (cont->extra.cont_call == ctx->iter) {
  362. if (
  363. l2_value_get_type(ret) == L2_VAL_TYPE_ATOM &&
  364. ret->atom == vm->values[vm->kstop].atom) {
  365. return vm->knone;
  366. } else {
  367. cont->extra.cont_call = ctx->func;
  368. args->extra.arr_length = 1;
  369. args->shortarray[0] = retval;
  370. return cont_id;
  371. }
  372. } else {
  373. cont->extra.cont_call = ctx->iter;
  374. args->extra.arr_length = 0;
  375. return cont_id;
  376. }
  377. }
  378. static void for_marker(
  379. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id)) {
  380. struct for_context *ctx = data;
  381. mark(vm, ctx->iter);
  382. mark(vm, ctx->func);
  383. }
  384. l2_word l2_builtin_for(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  385. if (argc != 2) {
  386. return l2_vm_error(vm, "Expected 2 arguments");
  387. }
  388. l2_word args_id = l2_vm_alloc(vm, L2_VAL_TYPE_ARRAY, L2_VAL_SBO);
  389. struct l2_vm_value *args = &vm->values[args_id];
  390. args->extra.arr_length = 0;
  391. struct for_context *ctx = malloc(sizeof(*ctx));
  392. ctx->base.callback = for_callback;
  393. ctx->base.marker = for_marker;
  394. ctx->base.args = args_id;
  395. ctx->iter = argv[0];
  396. ctx->func = argv[1];
  397. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  398. struct l2_vm_value *cont = &vm->values[cont_id];
  399. cont->extra.cont_call = ctx->iter;
  400. cont->cont = &ctx->base;
  401. return cont_id;
  402. }
  403. static l2_word guard_callback(struct l2_vm *vm, l2_word retval, l2_word cont_id) {
  404. struct l2_vm_value *ret = &vm->values[cont_id];
  405. free(ret->cont);
  406. ret->flags = L2_VAL_TYPE_RETURN;
  407. ret->ret = retval;
  408. return cont_id;
  409. }
  410. l2_word l2_builtin_guard(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  411. if (argc != 1 && argc != 2) {
  412. return l2_vm_error(vm, "Expected 1 or 2 arguments");
  413. }
  414. struct l2_vm_value *cond = &vm->values[argv[0]];
  415. if (l2_value_get_type(cond) == L2_VAL_TYPE_ERROR) {
  416. return argv[0];
  417. }
  418. if (argc == 1) {
  419. if (!l2_vm_val_is_true(vm, cond)) {
  420. return vm->knone;
  421. }
  422. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_RETURN, 0);
  423. vm->values[ret_id].ret = vm->knone;
  424. return ret_id;
  425. }
  426. struct l2_vm_value *body = &vm->values[argv[1]];
  427. if (l2_value_get_type(body) == L2_VAL_TYPE_ERROR) {
  428. return argv[1];
  429. }
  430. if (!l2_vm_val_is_true(vm, cond)) {
  431. return vm->knone;
  432. }
  433. struct l2_vm_contcontext *ctx = malloc(sizeof(*ctx));
  434. if (ctx == NULL) {
  435. return l2_vm_error(vm, "Allocation failure");
  436. }
  437. ctx->callback = guard_callback;
  438. ctx->marker = NULL;
  439. ctx->args = vm->knone;
  440. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  441. struct l2_vm_value *cont = &vm->values[cont_id];
  442. cont->extra.cont_call = argv[1];
  443. cont->cont = ctx;
  444. return cont_id;
  445. }