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.8KB

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. l2_word *data = (l2_word *)((char *)arr + sizeof(struct l2_vm_array));
  20. printf("ARRAY, len %zu\n", arr->len);
  21. for (size_t i = 0; i < arr->len; ++i) {
  22. printf(" %zu: %u\n", i, data[i]);
  23. }
  24. }
  25. break;
  26. case L2_VAL_TYPE_BUFFER:
  27. {
  28. struct l2_vm_buffer *buf = (struct l2_vm_buffer *)val->data;
  29. char *data = (char *)buf + sizeof(struct l2_vm_buffer);
  30. printf("BUFFER, len %zu\n", buf->len);
  31. for (size_t i = 0; i < buf->len; ++i) {
  32. printf(" %zu: %c\n", i, data[i]);
  33. }
  34. }
  35. break;
  36. }
  37. }
  38. int main() {
  39. l2_word ops[] = {
  40. L2_OP_PUSH, 100,
  41. L2_OP_PUSH, 100,
  42. L2_OP_ADD,
  43. L2_OP_ALLOC_INTEGER_32,
  44. L2_OP_PUSH, 20 /* offset */,
  45. L2_OP_PUSH, 5 /* length */,
  46. L2_OP_ALLOC_BUFFER_CONST,
  47. L2_OP_POP,
  48. L2_OP_PUSH, 16,
  49. L2_OP_JUMP,
  50. L2_OP_HALT,
  51. L2_OP_PUSH, 53,
  52. L2_OP_ALLOC_INTEGER_32,
  53. L2_OP_HALT,
  54. 0, 0,
  55. };
  56. memcpy(&ops[20], "Hello", 5);
  57. struct l2_vm vm;
  58. l2_vm_init(&vm, ops, sizeof(ops) / sizeof(*ops));
  59. l2_vm_run(&vm);
  60. printf("Stack:\n");
  61. for (l2_word i = 0; i < vm.sptr; ++i) {
  62. printf(" %i: %i\n", i, vm.stack[i]);
  63. }
  64. printf("Heap:\n");
  65. for (l2_word i = 0; i < vm.valuessize; ++i) {
  66. if (l2_bitset_get(&vm.valueset, i)) {
  67. printf(" %u: ", i);
  68. print_var(&vm.values[i]);
  69. }
  70. }
  71. l2_vm_gc(&vm);
  72. printf("Heap:\n");
  73. for (l2_word i = 0; i < vm.valuessize; ++i) {
  74. if (l2_bitset_get(&vm.valueset, i)) {
  75. printf(" %u: ", i);
  76. print_var(&vm.values[i]);
  77. }
  78. }
  79. }