src/vm.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 |
#ifndef HEX_H #include "hex.h" #endif //////////////////////////////////////// // Virtual Machine // //////////////////////////////////////// static void encode_length(uint8_t **bytecode, size_t *size, size_t length) { (*bytecode)[*size] = (length >> 24) & 0xFF; (*bytecode)[*size + 1] = (length >> 16) & 0xFF; (*bytecode)[*size + 2] = (length >> 8) & 0xFF; (*bytecode)[*size + 3] = length & 0xFF; *size += 4; } uint8_t hex_symbol_to_opcode(const char *symbol) { // Native Symbols if (strcmp(symbol, ":") == 0) { return HEX_OP_STORE; } else if (strcmp(symbol, "#") == 0) { return HEX_OP_FREE; } else if (strcmp(symbol, "if") == 0) { return HEX_OP_IF; } else if (strcmp(symbol, "when") == 0) { return HEX_OP_WHEN; } else if (strcmp(symbol, "while") == 0) { return HEX_OP_WHILE; } else if (strcmp(symbol, "error") == 0) { return HEX_OP_ERROR; } else if (strcmp(symbol, "try") == 0) { return HEX_OP_TRY; } else if (strcmp(symbol, "dup") == 0) { return HEX_OP_DUP; } else if (strcmp(symbol, "stack") == 0) { return HEX_OP_STACK; } else if (strcmp(symbol, "clear") == 0) { return HEX_OP_CLEAR; } else if (strcmp(symbol, "pop") == 0) { return HEX_OP_POP; } else if (strcmp(symbol, "swap") == 0) { return HEX_OP_SWAP; } else if (strcmp(symbol, ".") == 0) { return HEX_OP_I; } else if (strcmp(symbol, "!") == 0) { return HEX_OP_EVAL; } else if (strcmp(symbol, "'") == 0) { return HEX_OP_QUOTE; } else if (strcmp(symbol, "+") == 0) { return HEX_OP_ADD; } else if (strcmp(symbol, "-") == 0) { return HEX_OP_SUB; } else if (strcmp(symbol, "*") == 0) { return HEX_OP_MUL; } else if (strcmp(symbol, "/") == 0) { return HEX_OP_DIV; } else if (strcmp(symbol, "%") == 0) { return HEX_OP_MOD; } else if (strcmp(symbol, "&") == 0) { return HEX_OP_BITAND; } else if (strcmp(symbol, "|") == 0) { return HEX_OP_BITOR; } else if (strcmp(symbol, "^") == 0) { return HEX_OP_BITXOR; } else if (strcmp(symbol, "~") == 0) { return HEX_OP_BITNOT; } else if (strcmp(symbol, "<<") == 0) { return HEX_OP_SHL; } else if (strcmp(symbol, ">>") == 0) { return HEX_OP_SHR; } else if (strcmp(symbol, "==") == 0) { return HEX_OP_EQUAL; } else if (strcmp(symbol, "!=") == 0) { return HEX_OP_NOTEQUAL; } else if (strcmp(symbol, ">") == 0) { return HEX_OP_GREATER; } else if (strcmp(symbol, "<") == 0) { return HEX_OP_LESS; } else if (strcmp(symbol, ">=") == 0) { return HEX_OP_GREATEREQUAL; } else if (strcmp(symbol, "<=") == 0) { return HEX_OP_LESSEQUAL; } else if (strcmp(symbol, "and") == 0) { return HEX_OP_AND; } else if (strcmp(symbol, "or") == 0) { return HEX_OP_OR; } else if (strcmp(symbol, "not") == 0) { return HEX_OP_NOT; } else if (strcmp(symbol, "xor") == 0) { return HEX_OP_XOR; } else if (strcmp(symbol, "int") == 0) { return HEX_OP_INT; } else if (strcmp(symbol, "str") == 0) { return HEX_OP_STR; } else if (strcmp(symbol, "dec") == 0) { return HEX_OP_DEC; } else if (strcmp(symbol, "hex") == 0) { return HEX_OP_HEX; } else if (strcmp(symbol, "ord") == 0) { return HEX_OP_ORD; } else if (strcmp(symbol, "chr") == 0) { return HEX_OP_CHR; } else if (strcmp(symbol, "type") == 0) { return HEX_OP_TYPE; } else if (strcmp(symbol, "cat") == 0) { return HEX_OP_CAT; } else if (strcmp(symbol, "len") == 0) { return HEX_OP_LEN; } else if (strcmp(symbol, "get") == 0) { return HEX_OP_GET; } else if (strcmp(symbol, "index") == 0) { return HEX_OP_INDEX; } else if (strcmp(symbol, "join") == 0) { return HEX_OP_JOIN; } else if (strcmp(symbol, "split") == 0) { return HEX_OP_SPLIT; } else if (strcmp(symbol, "replace") == 0) { return HEX_OP_REPLACE; } else if (strcmp(symbol, "each") == 0) { return HEX_OP_EACH; } else if (strcmp(symbol, "map") == 0) { return HEX_OP_MAP; } else if (strcmp(symbol, "filter") == 0) { return HEX_OP_FILTER; } else if (strcmp(symbol, "puts") == 0) { return HEX_OP_PUTS; } else if (strcmp(symbol, "warn") == 0) { return HEX_OP_WARN; } else if (strcmp(symbol, "print") == 0) { return HEX_OP_PRINT; } else if (strcmp(symbol, "gets") == 0) { return HEX_OP_GETS; } else if (strcmp(symbol, "read") == 0) { return HEX_OP_READ; } else if (strcmp(symbol, "write") == 0) { return HEX_OP_WRITE; } else if (strcmp(symbol, "append") == 0) { return HEX_OP_APPEND; } else if (strcmp(symbol, "args") == 0) { return HEX_OP_ARGS; } else if (strcmp(symbol, "exit") == 0) { return HEX_OP_EXIT; } else if (strcmp(symbol, "exec") == 0) { return HEX_OP_EXEC; } else if (strcmp(symbol, "run") == 0) { return HEX_OP_RUN; } return 0; } const char *hex_opcode_to_symbol(uint8_t opcode) { switch (opcode) { case HEX_OP_STORE: return ":"; case HEX_OP_FREE: return "#"; case HEX_OP_IF: return "if"; case HEX_OP_WHEN: return "when"; case HEX_OP_WHILE: return "while"; case HEX_OP_ERROR: return "error"; case HEX_OP_TRY: return "try"; case HEX_OP_DUP: return "dup"; case HEX_OP_STACK: return "stack"; case HEX_OP_CLEAR: return "clear"; case HEX_OP_POP: return "pop"; case HEX_OP_SWAP: return "swap"; case HEX_OP_I: return "."; case HEX_OP_EVAL: return "!"; case HEX_OP_QUOTE: return "'"; case HEX_OP_ADD: return "+"; case HEX_OP_SUB: return "-"; case HEX_OP_MUL: return "*"; case HEX_OP_DIV: return "/"; case HEX_OP_MOD: return "%"; case HEX_OP_BITAND: return "&"; case HEX_OP_BITOR: return "|"; case HEX_OP_BITXOR: return "^"; case HEX_OP_BITNOT: return "~"; case HEX_OP_SHL: return "<<"; case HEX_OP_SHR: return ">>"; case HEX_OP_EQUAL: return "=="; case HEX_OP_NOTEQUAL: return "!="; case HEX_OP_GREATER: return ">"; case HEX_OP_LESS: return "<"; case HEX_OP_GREATEREQUAL: return ">="; case HEX_OP_LESSEQUAL: return "<="; case HEX_OP_AND: return "and"; case HEX_OP_OR: return "or"; case HEX_OP_NOT: return "not"; case HEX_OP_XOR: return "xor"; case HEX_OP_INT: return "int"; case HEX_OP_STR: return "str"; case HEX_OP_DEC: return "dec"; case HEX_OP_HEX: return "hex"; case HEX_OP_ORD: return "ord"; case HEX_OP_CHR: return "chr"; case HEX_OP_TYPE: return "type"; case HEX_OP_CAT: return "cat"; case HEX_OP_LEN: return "len"; case HEX_OP_GET: return "get"; case HEX_OP_INDEX: return "index"; case HEX_OP_JOIN: return "join"; case HEX_OP_SPLIT: return "split"; case HEX_OP_REPLACE: return "replace"; case HEX_OP_EACH: return "each"; case HEX_OP_MAP: return "map"; case HEX_OP_FILTER: return "filter"; case HEX_OP_PUTS: return "puts"; case HEX_OP_WARN: return "warn"; case HEX_OP_PRINT: return "print"; case HEX_OP_GETS: return "gets"; case HEX_OP_READ: return "read"; case HEX_OP_WRITE: return "write"; case HEX_OP_APPEND: return "append"; case HEX_OP_ARGS: return "args"; case HEX_OP_EXIT: return "exit"; case HEX_OP_EXEC: return "exec"; case HEX_OP_RUN: return "run"; default: return NULL; } } int hex_bytecode_integer(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, int32_t value) { hex_debug(ctx, "PUSHIN[%d]: %d", sizeof(int32_t), value); // Check if we need to resize the buffer (size + int32_t size + opcode (1) + max encoded length (4)) if (*size + sizeof(int32_t) + 1 + 4 > *capacity) { *capacity = (*size + sizeof(int32_t) + 1 + 4 + *capacity) * 2; uint8_t *new_bytecode = (uint8_t *)realloc(*bytecode, *capacity); if (!new_bytecode) { hex_error(ctx, "Memory allocation failed"); return 1; } *bytecode = new_bytecode; } (*bytecode)[*size] = HEX_OP_PUSHIN; *size += 1; // opcode encode_length(bytecode, size, sizeof(int32_t)); // memcpy(&(*bytecode)[*size], &value, sizeof(int32_t)); memcpy(&(*bytecode)[*size], (uint8_t[]){(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF}, 4); *size += sizeof(int32_t); return 0; } int hex_bytecode_string(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, const char *value) { size_t len = strlen(value); hex_debug(ctx, "PUSHST[%d]: (total size start: %d) %s", len, *size, value); // Check if we need to resize the buffer (size + strlen + opcode (1) + max encoded length (4)) if (*size + len + 1 + 4 > *capacity) { *capacity = (*size + len + 1 + 4 + *capacity) * 2; uint8_t *new_bytecode = (uint8_t *)realloc(*bytecode, *capacity); if (!new_bytecode) { hex_error(ctx, "Memory allocation failed"); return 1; } *bytecode = new_bytecode; } (*bytecode)[*size] = HEX_OP_PUSHST; *size += 1; // opcode encode_length(bytecode, size, len); memcpy(&(*bytecode)[*size], value, len); *size += len; hex_debug(ctx, "PUSHST[%d]: (total size: %d) %s", len, *size, value); return 0; } int hex_bytecode_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, const char *value) { if (hex_valid_native_symbol(ctx, value)) { // Check if we need to resize the buffer (size + opcode (1)) if (*size + 1 > *capacity) { *capacity = (*size + 1 + *capacity) * 2; uint8_t *new_bytecode = (uint8_t *)realloc(*bytecode, *capacity); if (!new_bytecode) { hex_error(ctx, "Memory allocation failed"); return 1; } *bytecode = new_bytecode; } (*bytecode)[*size] = hex_symbol_to_opcode(value); *size += 1; // opcode hex_debug(ctx, "NATSYM[1]: (total size: %d) %s", *size, value); } else { hex_debug(ctx, "LOOKUP[%d]: %s", strlen(value), value); // Check if we need to resize the buffer (size + strlen + opcode (1) + max encoded length (4)) if (*size + strlen(value) + 1 + 4 > *capacity) { *capacity = (*size + strlen(value) + 1 + 4) * 2; uint8_t *new_bytecode = (uint8_t *)realloc(*bytecode, *capacity); if (!new_bytecode) { hex_error(ctx, "Memory allocation failed"); return 1; } *bytecode = new_bytecode; } (*bytecode)[*size] = HEX_OP_LOOKUP; *size += 1; // opcode encode_length(bytecode, size, strlen(value)); memcpy(&(*bytecode)[*size], value, strlen(value)); *size += strlen(value); } return 0; } int hex_bytecode_quotation(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, uint8_t **output, size_t *output_size, size_t *n_items) { // Check if we need to resize the buffer (size + opcode (1) + max encoded length (4) + quotation bytecode size) if (*size + 1 + 4 + *output_size > *capacity) { *capacity = (*size + 1 + 4 + *output_size + *capacity) * 2; uint8_t *new_bytecode = (uint8_t *)realloc(*bytecode, *capacity); if (!new_bytecode) { hex_error(ctx, "Memory allocation failed"); return 1; } *bytecode = new_bytecode; } (*bytecode)[*size] = HEX_OP_PUSHQT; *size += 1; // opcode encode_length(bytecode, size, *n_items); memcpy(&(*bytecode)[*size], *output, *output_size); *size += *output_size; hex_debug(ctx, "PUSHQT[%d]: (total size: %d) <end>", *n_items, *output_size); return 0; } int hex_bytecode(hex_context_t *ctx, const char *input, uint8_t **output, size_t *output_size, hex_file_position_t *position, int *open_quotations) { hex_token_t *token; size_t capacity = 128; size_t size = 0; uint8_t *bytecode = (uint8_t *)malloc(capacity); if (!bytecode) { hex_error(ctx, "Memory allocation failed"); return 1; } while ((token = hex_next_token(ctx, &input, position)) != NULL) { if (token->type == HEX_TOKEN_INTEGER) { int32_t value = hex_parse_integer(token->value); hex_bytecode_integer(ctx, &bytecode, &size, &capacity, value); } else if (token->type == HEX_TOKEN_STRING) { hex_bytecode_string(ctx, &bytecode, &size, &capacity, token->value); } else if (token->type == HEX_TOKEN_SYMBOL) { hex_bytecode_symbol(ctx, &bytecode, &size, &capacity, token->value); } else if (token->type == HEX_TOKEN_QUOTATION_START) { size_t n_items = 0; uint8_t *quotation_bytecode = NULL; size_t quotation_size = 0; hex_debug(ctx, "PUSHQT[-]: <start>"); if (hex_generate_quotation_bytecode(ctx, &input, "ation_bytecode, "ation_size, &n_items, position) != 0) { hex_error(ctx, "Failed to generate quotation bytecode (main)"); return 1; } hex_bytecode_quotation(ctx, &bytecode, &size, &capacity, "ation_bytecode, "ation_size, &n_items); } else if (token->type == HEX_TOKEN_QUOTATION_END) { open_quotations--; } else { // Ignore other tokens } } hex_debug(ctx, "Bytecode generated: %d bytes", size); *output = bytecode; *output_size = size; return 0; } int hex_generate_quotation_bytecode(hex_context_t *ctx, const char **input, uint8_t **output, size_t *output_size, size_t *n_items, hex_file_position_t *position) { hex_token_t *token; size_t capacity = 128; size_t size = 0; int balanced = 1; uint8_t *bytecode = (uint8_t *)malloc(capacity); if (!bytecode) { hex_error(ctx, "Memory allocation failed"); return 1; } *n_items = 0; while ((token = hex_next_token(ctx, input, position)) != NULL) { if (token->type == HEX_TOKEN_INTEGER) { int32_t value = hex_parse_integer(token->value); hex_bytecode_integer(ctx, &bytecode, &size, &capacity, value); } else if (token->type == HEX_TOKEN_STRING) { hex_bytecode_string(ctx, &bytecode, &size, &capacity, token->value); } else if (token->type == HEX_TOKEN_SYMBOL) { hex_bytecode_symbol(ctx, &bytecode, &size, &capacity, token->value); } else if (token->type == HEX_TOKEN_QUOTATION_START) { size_t n_items = 0; uint8_t *quotation_bytecode = NULL; size_t quotation_size = 0; hex_debug(ctx, "PUSHQT[-]: <start>"); if (hex_generate_quotation_bytecode(ctx, input, "ation_bytecode, "ation_size, &n_items, position) != 0) { hex_error(ctx, "Failed to generate quotation bytecode"); return 1; } hex_bytecode_quotation(ctx, &bytecode, &size, &capacity, "ation_bytecode, "ation_size, &n_items); } else if (token->type == HEX_TOKEN_QUOTATION_END) { balanced--; break; } else { // Ignore other tokens } (*n_items)++; } if (balanced != 0) { hex_error(ctx, "(%d,%d) Unterminated quotation", position->line, position->column); hex_free_token(token); return 1; } *output = bytecode; *output_size = size; return 0; } int hex_interpret_bytecode_integer(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result) { if (*size < 4) { hex_error(ctx, "Bytecode size too small to contain an integer"); return 1; } // Integers are always 4 bytes, big-endian, but at the moment we are always setting the size to 4 // So just shifting the bytes to the right should be enough (no need to actually compute the size) // size_t int_size = ((*bytecode)[0] << 24) | ((*bytecode)[1] << 16) | ((*bytecode)[2] << 8) | (*bytecode)[3]; *bytecode += 4; *size -= 4; uint32_t value = ((*bytecode)[0] << 24) | ((*bytecode)[1] << 16) | ((*bytecode)[2] << 8) | (*bytecode)[3]; *bytecode += 4; *size -= 4; hex_debug(ctx, "PUSHIN[%d]: %d", sizeof(int32_t), value); hex_item_t item = hex_integer_item(ctx, value); *result = item; return 0; } int hex_interpret_bytecode_string(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result) { if (*size < 4) { hex_error(ctx, "Bytecode size too small to contain a string length"); return 1; } size_t length = ((*bytecode)[0] << 24) | ((*bytecode)[1] << 16) | ((*bytecode)[2] << 8) | (*bytecode)[3]; *bytecode += 4; *size -= 4; if (*size < length) { hex_error(ctx, "Bytecode size too small to contain the string"); return 1; } char *value = (char *)malloc(length + 1); if (!value) { hex_error(ctx, "Memory allocation failed"); return 1; } memcpy(value, *bytecode, length); value[length] = '\0'; *bytecode += length; *size -= length; hex_debug(ctx, "PUSHST[%d]: %s", length, value); hex_item_t item = hex_string_item(ctx, value); *result = item; free(value); return 0; } int hex_interpret_bytecode_native_symbol(hex_context_t *ctx, uint8_t opcode, size_t position, hex_item_t *result) { const char *symbol = hex_opcode_to_symbol(opcode); if (!symbol) { hex_error(ctx, "Invalid opcode for symbol"); return 1; } hex_item_t item; item.type = HEX_TYPE_NATIVE_SYMBOL; hex_item_t value; hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t)); token->value = (char *)symbol; token->position.line = 0; token->position.column = position; if (hex_get_symbol(ctx, token->value, &value)) { item.token = token; item.type = HEX_TYPE_NATIVE_SYMBOL; item.data.fn_value = value.data.fn_value; } else { hex_error(ctx, "(%d,%d) Unable to reference native symbol: %s (bytecode)", token->position.line, token->position.column, token->value); hex_free_token(token); return 1; } hex_debug(ctx, "NATSYM[1]: %d (%s)", opcode, symbol); *result = item; return 0; } int hex_interpret_bytecode_user_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result) { if (*size < 4) { hex_error(ctx, "Bytecode size too small to contain a symbol length"); return 1; } size_t length = ((*bytecode)[0] << 24) | ((*bytecode)[1] << 16) | ((*bytecode)[2] << 8) | (*bytecode)[3]; *bytecode += 4; *size -= 4; if (*size < length) { hex_error(ctx, "Bytecode size too small to contain the symbol"); return 1; } char *value = (char *)malloc(length + 1); if (!value) { hex_error(ctx, "Memory allocation failed"); return 1; } memcpy(value, *bytecode, length); value[length] = '\0'; *bytecode += length; *size -= length; hex_item_t item; item.type = HEX_TYPE_USER_SYMBOL; item.data.str_value = value; hex_debug(ctx, "LOOKUP[%d]: %s", length, value); free(value); *result = item; return 0; } int hex_interpret_bytecode_quotation(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result) { if (*size < 4) { hex_error(ctx, "Bytecode size too small to contain a quotation length"); return 1; } size_t n_items = ((*bytecode)[0] << 24) | ((*bytecode)[1] << 16) | ((*bytecode)[2] << 8) | (*bytecode)[3]; *bytecode += 4; *size -= 4; hex_debug(ctx, "PUSHQT[%d]: <start>", n_items); hex_item_t *items = (hex_item_t *)malloc(n_items * sizeof(hex_item_t)); if (!items) { hex_error(ctx, "Memory allocation failed"); return 1; } size_t bytecode_size = *size; for (size_t i = 0; i < n_items; i++) { uint8_t opcode = **bytecode; (*bytecode)++; (*size)--; hex_item_t item; hex_debug(ctx, "[Quotation] Processing bytecode at absolute position: %zu, opcode: %u", bytecode_size - *size, opcode); switch (opcode) { case HEX_OP_PUSHIN: if (hex_interpret_bytecode_integer(ctx, bytecode, size, &item) != 0) { free(items); return 1; } break; case HEX_OP_PUSHST: if (hex_interpret_bytecode_string(ctx, bytecode, size, &item) != 0) { free(items); return 1; } break; case HEX_OP_LOOKUP: if (hex_interpret_bytecode_user_symbol(ctx, bytecode, size, &item) != 0) { free(items); return 1; } break; case HEX_OP_PUSHQT: if (hex_interpret_bytecode_quotation(ctx, bytecode, size, &item) != 0) { free(items); return 1; } break; default: if (hex_interpret_bytecode_native_symbol(ctx, opcode, *size, &item) != 0) { free(items); return 1; } break; } items[i] = item; } hex_item_t quotation; quotation.type = HEX_TYPE_QUOTATION; quotation.data.quotation_value = &items; quotation.quotation_size = n_items; *result = quotation; hex_debug(ctx, "PUSHQT[%d]: <end>", n_items); return 0; } int hex_interpret_bytecode(hex_context_t *ctx, uint8_t *bytecode, size_t size) { size_t bytecode_size = size; size_t position = bytecode_size; if (size < 6 || memcmp(bytecode, HEX_BYTECODE_HEADER, 6) != 0) { hex_error(ctx, "Invalid or missing bytecode header"); return 1; } bytecode += 6; size -= 6; while (size > 0) { position = bytecode_size - size; uint8_t opcode = *bytecode; hex_debug(ctx, "Processing bytecode at position: %zu, opcode: %u", position, opcode); bytecode++; size--; hex_item_t *item = malloc(sizeof(hex_item_t)); switch (opcode) { case HEX_OP_PUSHIN: if (hex_interpret_bytecode_integer(ctx, &bytecode, &size, item) != 0) { return 1; } break; case HEX_OP_PUSHST: if (hex_interpret_bytecode_string(ctx, &bytecode, &size, item) != 0) { return 1; } break; case HEX_OP_LOOKUP: if (hex_interpret_bytecode_user_symbol(ctx, &bytecode, &size, item) != 0) { return 1; } break; case HEX_OP_PUSHQT: if (hex_interpret_bytecode_quotation(ctx, &bytecode, &size, item) != 0) { return 1; } break; default: if (hex_interpret_bytecode_native_symbol(ctx, opcode, position, item) != 0) { return 1; } break; } if (hex_push(ctx, *item) != 0) { return 1; } } return 0; } |