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.

main.c 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "vm/vm.h"
  2. #include "bitset.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. void print_var(struct l2_vm_value *val) {
  6. switch (val->flags & 0x0f) {
  7. case L2_VAL_TYPE_NONE:
  8. printf("NONE\n");
  9. break;
  10. case L2_VAL_TYPE_INTEGER:
  11. printf("INTEGER %zi\n", val->integer);
  12. break;
  13. case L2_VAL_TYPE_REAL:
  14. printf("REAL %f\n", val->real);
  15. break;
  16. case L2_VAL_TYPE_ARRAY:
  17. {
  18. struct l2_vm_array *arr = (struct l2_vm_array *)val->data;
  19. printf("ARRAY, len %zu\n", arr->len);
  20. for (size_t i = 0; i < arr->len; ++i) {
  21. printf(" %zu: %u\n", i, arr->data[i]);
  22. }
  23. }
  24. break;
  25. case L2_VAL_TYPE_BUFFER:
  26. {
  27. struct l2_vm_buffer *buf = (struct l2_vm_buffer *)val->data;
  28. printf("BUFFER, len %zu\n", buf->len);
  29. for (size_t i = 0; i < buf->len; ++i) {
  30. printf(" %zu: %c\n", i, buf->data[i]);
  31. }
  32. }
  33. break;
  34. }
  35. }
  36. int main() {
  37. l2_word ops[] = {
  38. L2_OP_PUSH, 100,
  39. L2_OP_PUSH, 100,
  40. L2_OP_ADD,
  41. L2_OP_ALLOC_INTEGER_32,
  42. L2_OP_PUSH, 20 /* offset */,
  43. L2_OP_PUSH, 5 /* length */,
  44. L2_OP_ALLOC_BUFFER_CONST,
  45. L2_OP_POP,
  46. L2_OP_PUSH, 16,
  47. L2_OP_JUMP,
  48. L2_OP_HALT,
  49. L2_OP_PUSH, 53,
  50. L2_OP_ALLOC_INTEGER_32,
  51. L2_OP_HALT,
  52. 0, 0,
  53. };
  54. memcpy(&ops[20], "Hello", 5);
  55. struct l2_vm vm;
  56. l2_vm_init(&vm, ops, sizeof(ops) / sizeof(*ops));
  57. l2_vm_run(&vm);
  58. printf("Stack:\n");
  59. for (l2_word i = 0; i < vm.sptr; ++i) {
  60. printf(" %i: %i\n", i, vm.stack[i]);
  61. }
  62. printf("Heap:\n");
  63. for (l2_word i = 0; i < vm.valuessize; ++i) {
  64. if (l2_bitset_get(&vm.valueset, i)) {
  65. printf(" %u: ", i);
  66. print_var(&vm.values[i]);
  67. }
  68. }
  69. l2_vm_gc(&vm);
  70. printf("Heap:\n");
  71. for (l2_word i = 0; i < vm.valuessize; ++i) {
  72. if (l2_bitset_get(&vm.valueset, i)) {
  73. printf(" %u: ", i);
  74. print_var(&vm.values[i]);
  75. }
  76. }
  77. l2_vm_free(&vm);
  78. }