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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #define X(name, identity, op) \
  57. l2_word name(struct l2_vm *vm, l2_word argc, l2_word *argv) { \
  58. if (argc == 0) { \
  59. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0); \
  60. vm->values[id].real = identity; \
  61. return id; \
  62. } \
  63. struct l2_vm_value *first = &vm->values[argv[0]]; \
  64. if (l2_value_get_type(first) != L2_VAL_TYPE_REAL) { \
  65. return l2_vm_type_error(vm, first); \
  66. } \
  67. if (argc == 1) { \
  68. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0); \
  69. vm->values[id].real = identity op first->real; \
  70. return id; \
  71. } \
  72. double sum = first->real; \
  73. for (l2_word i = 1; i < argc; ++i) { \
  74. struct l2_vm_value *val = &vm->values[argv[i]]; \
  75. if (l2_value_get_type(val) != L2_VAL_TYPE_REAL) { \
  76. return l2_vm_type_error(vm, val); \
  77. } \
  78. sum = sum op val->real; \
  79. } \
  80. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0); \
  81. vm->values[id].real = sum; \
  82. return id; \
  83. }
  84. X(l2_builtin_add, 0, +)
  85. X(l2_builtin_sub, 0, -)
  86. X(l2_builtin_mul, 1, *)
  87. X(l2_builtin_div, 1, /)
  88. #undef X
  89. l2_word l2_builtin_eq(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  90. if (argc < 2) {
  91. return vm->ktrue;
  92. }
  93. for (l2_word i = 1; i < argc; ++i) {
  94. if (argv[i - 1] == argv[i]) continue;
  95. struct l2_vm_value *a = &vm->values[argv[i - 1]];
  96. struct l2_vm_value *b = &vm->values[argv[i]];
  97. if (a->flags != b->flags) {
  98. return vm->kfalse;
  99. }
  100. enum l2_value_type typ = l2_value_get_type(a);
  101. if (typ == L2_VAL_TYPE_ATOM) {
  102. if (a->atom != b->atom) {
  103. return vm->kfalse;
  104. }
  105. } else if (typ == L2_VAL_TYPE_REAL) {
  106. if (a->real != b->real) {
  107. return vm->kfalse;
  108. }
  109. } else if (typ == L2_VAL_TYPE_BUFFER) {
  110. if (a->buffer == NULL && b->buffer == NULL) continue;
  111. if (a->buffer == NULL || b->buffer == NULL) {
  112. return vm->kfalse;
  113. }
  114. if (a->extra.buf_length != b->extra.buf_length) {
  115. return vm->kfalse;
  116. }
  117. if (memcmp(a->buffer, b->buffer, a->extra.buf_length) != 0) {
  118. return vm->kfalse;
  119. }
  120. } else {
  121. return vm->kfalse;
  122. }
  123. }
  124. return vm->ktrue;
  125. }
  126. l2_word l2_builtin_neq(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  127. l2_word ret_id = l2_builtin_eq(vm, argc, argv);
  128. if (ret_id == vm->ktrue) {
  129. return vm->kfalse;
  130. } else if (ret_id == vm->kfalse) {
  131. return vm->ktrue;
  132. } else {
  133. return ret_id;
  134. }
  135. }
  136. #define X(name, op) \
  137. l2_word name(struct l2_vm *vm, l2_word argc, l2_word *argv) { \
  138. if (argc < 2) { \
  139. return vm->ktrue; \
  140. } \
  141. struct l2_vm_value *lhs = &vm->values[argv[0]]; \
  142. if (l2_value_get_type(lhs) != L2_VAL_TYPE_REAL) { \
  143. return l2_vm_type_error(vm, lhs); \
  144. } \
  145. for (l2_word i = 1; i < argc; ++i) { \
  146. struct l2_vm_value *rhs = &vm->values[argv[i]]; \
  147. if (l2_value_get_type(rhs) != L2_VAL_TYPE_REAL) { \
  148. return l2_vm_type_error(vm, rhs); \
  149. } \
  150. if (!(lhs->real op rhs->real)) { \
  151. return vm->kfalse; \
  152. } \
  153. lhs = rhs; \
  154. } \
  155. return vm->ktrue; \
  156. }
  157. X(l2_builtin_lt, <)
  158. X(l2_builtin_lteq, <=)
  159. X(l2_builtin_gt, >)
  160. X(l2_builtin_gteq, >=)
  161. #undef X
  162. l2_word l2_builtin_print(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  163. for (size_t i = 0; i < argc; ++i) {
  164. if (i != 0) {
  165. vm->std_output->write(vm->std_output, " ", 1);
  166. }
  167. struct l2_vm_value *val = &vm->values[argv[i]];
  168. print_val(vm, vm->std_output, val);
  169. }
  170. vm->std_output->write(vm->std_output, "\n", 1);
  171. return 0;
  172. }
  173. l2_word l2_builtin_len(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  174. if (argc != 1) {
  175. return l2_vm_error(vm, "Expected 1 argument");
  176. }
  177. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  178. struct l2_vm_value *ret = &vm->values[ret_id];
  179. ret->real = 0;
  180. struct l2_vm_value *val = &vm->values[argv[0]];
  181. switch (l2_value_get_type(val)) {
  182. case L2_VAL_TYPE_NONE:
  183. case L2_VAL_TYPE_ATOM:
  184. case L2_VAL_TYPE_REAL:
  185. case L2_VAL_TYPE_FUNCTION:
  186. case L2_VAL_TYPE_CFUNCTION:
  187. case L2_VAL_TYPE_ERROR:
  188. case L2_VAL_TYPE_CONTINUATION:
  189. break;
  190. case L2_VAL_TYPE_BUFFER:
  191. ret->real = val->extra.buf_length;
  192. break;
  193. case L2_VAL_TYPE_ARRAY:
  194. ret->real = val->extra.arr_length;
  195. break;
  196. case L2_VAL_TYPE_NAMESPACE:
  197. if (val->ns) {
  198. ret->real = val->ns->len;
  199. }
  200. }
  201. return ret_id;
  202. }
  203. l2_word l2_builtin_if(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  204. if (argc != 2 && argc != 3) {
  205. return l2_vm_error(vm, "Expected 2 or 3 arguments");
  206. }
  207. if (l2_vm_val_is_true(vm, argv[0])) {
  208. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  209. struct l2_vm_value *ret = &vm->values[ret_id];
  210. ret->extra.cont_call = argv[1];
  211. return ret_id;
  212. } else if (argc == 3) {
  213. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  214. struct l2_vm_value *ret = &vm->values[ret_id];
  215. ret->extra.cont_call = argv[2];
  216. return ret_id;
  217. } else {
  218. return 0;
  219. }
  220. }
  221. struct loop_context {
  222. struct l2_vm_contcontext base;
  223. l2_word func;
  224. };
  225. static l2_word loop_callback(struct l2_vm *vm, l2_word retval, l2_word cont) {
  226. if (l2_vm_val_is_true(vm, retval)) {
  227. return cont;
  228. }
  229. return retval;
  230. }
  231. static void loop_marker(
  232. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id)) {
  233. struct loop_context *ctx = data;
  234. mark(vm, ctx->func);
  235. }
  236. l2_word l2_builtin_loop(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  237. if (argc != 1) {
  238. return l2_vm_error(vm, "Expected 1 argument");
  239. }
  240. struct loop_context *ctx = malloc(sizeof(*ctx));
  241. if (ctx == NULL) {
  242. return l2_vm_error(vm, "Allocation failure");
  243. }
  244. ctx->base.callback = loop_callback;
  245. ctx->base.marker = loop_marker;
  246. ctx->func = argv[0];
  247. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  248. struct l2_vm_value *cont = &vm->values[cont_id];
  249. cont->extra.cont_call = ctx->func;
  250. cont->cont = &ctx->base;
  251. return cont_id;
  252. }
  253. struct while_context {
  254. struct l2_vm_contcontext base;
  255. l2_word cond, body;
  256. };
  257. static l2_word while_callback(struct l2_vm *vm, l2_word retval, l2_word cont_id) {
  258. struct l2_vm_value *cont = &vm->values[cont_id];
  259. struct while_context *ctx = (struct while_context *)cont->cont;
  260. if (cont->extra.cont_call == ctx->cond) {
  261. if (l2_vm_val_is_true(vm, retval)) {
  262. cont->extra.cont_call = ctx->body;
  263. return cont_id;
  264. } else {
  265. return retval;
  266. }
  267. } else {
  268. struct l2_vm_value *ret = &vm->values[retval];
  269. if (l2_value_get_type(ret) == L2_VAL_TYPE_ERROR) {
  270. return retval;
  271. } else {
  272. cont->extra.cont_call = ctx->cond;
  273. return cont_id;
  274. }
  275. }
  276. }
  277. static void while_marker(
  278. struct l2_vm *vm, void *data, void (*mark)(struct l2_vm *vm, l2_word id)) {
  279. struct while_context *ctx = data;
  280. mark(vm, ctx->cond);
  281. mark(vm, ctx->body);
  282. }
  283. l2_word l2_builtin_while(struct l2_vm *vm, l2_word argc, l2_word *argv) {
  284. if (argc != 2) {
  285. return l2_vm_error(vm, "Expected 2 arguments");
  286. }
  287. struct while_context *ctx = malloc(sizeof(*ctx));
  288. if (ctx == NULL) {
  289. return l2_vm_error(vm, "Allocation failure");
  290. }
  291. ctx->base.callback = while_callback;
  292. ctx->base.marker = while_marker;
  293. ctx->cond = argv[0];
  294. ctx->body = argv[1];
  295. l2_word cont_id = l2_vm_alloc(vm, L2_VAL_TYPE_CONTINUATION, 0);
  296. struct l2_vm_value *cont = &vm->values[cont_id];
  297. cont->extra.cont_call = ctx->cond;
  298. cont->cont = &ctx->base;
  299. return cont_id;
  300. }