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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_vm_value_type(val)) {
  5. case L2_VAL_TYPE_NONE:
  6. l2_io_printf(out, "(none)");
  7. break;
  8. case L2_VAL_TYPE_ATOM:
  9. l2_io_printf(out, "(atom %u)", val->atom);
  10. break;
  11. case L2_VAL_TYPE_REAL:
  12. l2_io_printf(out, "%g", val->real);
  13. break;
  14. case L2_VAL_TYPE_BUFFER:
  15. if (val->buffer != NULL) {
  16. out->write(out, val->buffer->data, val->buffer->len);
  17. }
  18. break;
  19. case L2_VAL_TYPE_ARRAY:
  20. if (val->array == NULL) {
  21. out->write(out, "[]", 2);
  22. break;
  23. }
  24. out->write(out, "[", 1);
  25. for (size_t i = 0; i < val->array->len; ++i) {
  26. if (i != 0) {
  27. out->write(out, " ", 1);
  28. }
  29. print_val(vm, out, &vm->values[val->array->data[i]]);
  30. }
  31. out->write(out, "]", 1);
  32. break;
  33. case L2_VAL_TYPE_NAMESPACE:
  34. l2_io_printf(out, "(namespace)");
  35. break;
  36. case L2_VAL_TYPE_FUNCTION:
  37. case L2_VAL_TYPE_CFUNCTION:
  38. l2_io_printf(out, "(function)");
  39. break;
  40. case L2_VAL_TYPE_ERROR:
  41. l2_io_printf(out, "(error: %s)", val->error);
  42. break;
  43. }
  44. }
  45. l2_word l2_builtin_add(struct l2_vm *vm, struct l2_vm_array *args) {
  46. double sum = 0;
  47. for (size_t i = 0; i < args->len; ++i) {
  48. struct l2_vm_value *val = &vm->values[args->data[i]];
  49. if (l2_vm_value_type(val) != L2_VAL_TYPE_REAL) {
  50. // TODO: Error
  51. }
  52. sum += val->real;
  53. }
  54. l2_word id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  55. vm->values[id].real = sum;
  56. return id;
  57. }
  58. l2_word l2_builtin_sub(struct l2_vm *vm, struct l2_vm_array *args) {
  59. return 0;
  60. }
  61. l2_word l2_builtin_mul(struct l2_vm *vm, struct l2_vm_array *args) {
  62. return 0;
  63. }
  64. l2_word l2_builtin_div(struct l2_vm *vm, struct l2_vm_array *args) {
  65. return 0;
  66. }
  67. l2_word l2_builtin_print(struct l2_vm *vm, struct l2_vm_array *args) {
  68. for (size_t i = 0; i < args->len; ++i) {
  69. if (i != 0) {
  70. vm->std_output->write(vm->std_output, " ", 1);
  71. }
  72. struct l2_vm_value *val = &vm->values[args->data[i]];
  73. print_val(vm, vm->std_output, val);
  74. }
  75. vm->std_output->write(vm->std_output, "\n", 1);
  76. return 0;
  77. }
  78. l2_word l2_builtin_len(struct l2_vm *vm, struct l2_vm_array *args) {
  79. // TODO: error if wrong argc
  80. l2_word ret_id = l2_vm_alloc(vm, L2_VAL_TYPE_REAL, 0);
  81. struct l2_vm_value *ret = &vm->values[ret_id];
  82. ret->real = 0;
  83. struct l2_vm_value *val = &vm->values[args->data[0]];
  84. switch (l2_vm_value_type(val)) {
  85. case L2_VAL_TYPE_NONE:
  86. case L2_VAL_TYPE_ATOM:
  87. case L2_VAL_TYPE_REAL:
  88. case L2_VAL_TYPE_FUNCTION:
  89. case L2_VAL_TYPE_CFUNCTION:
  90. case L2_VAL_TYPE_ERROR:
  91. break;
  92. case L2_VAL_TYPE_BUFFER:
  93. if (val->buffer) {
  94. ret->real = val->buffer->len;
  95. }
  96. break;
  97. case L2_VAL_TYPE_ARRAY:
  98. if (val->array) {
  99. ret->real = val->array->len;
  100. }
  101. break;
  102. case L2_VAL_TYPE_NAMESPACE:
  103. if (val->ns) {
  104. ret->real = val->ns->len;
  105. }
  106. }
  107. return ret_id;
  108. }