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 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 |
#ifndef HEX_H #include "hex.h" #endif //////////////////////////////////////// // Virtual Machine // //////////////////////////////////////// static void encode_length(uint8_t **bytecode, size_t *size, size_t length) { while (length >= 0x80) { (*bytecode)[*size] = (length & 0x7F) | 0x80; length >>= 7; (*size)++; } (*bytecode)[*size] = length & 0x7F; (*size)++; } 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", 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 the length of the integer value size_t int_length = 0; if (value >= -0x80 && value < 0x80) { int_length = 1; } else if (value >= -0x8000 && value < 0x8000) { int_length = 2; } else if (value >= -0x800000 && value < 0x800000) { int_length = 3; } else { int_length = 4; } encode_length(bytecode, size, int_length); // Encode the integer value in the minimum number of bytes, in little endian if (value >= -0x80 && value < 0x80) { (*bytecode)[*size] = value & 0xFF; *size += 1; } else if (value >= -0x8000 && value < 0x8000) { (*bytecode)[*size] = value & 0xFF; (*bytecode)[*size + 1] = (value >> 8) & 0xFF; *size += 2; } else if (value >= -0x800000 && value < 0x800000) { (*bytecode)[*size] = value & 0xFF; (*bytecode)[*size + 1] = (value >> 8) & 0xFF; (*bytecode)[*size + 2] = (value >> 16) & 0xFF; *size += 3; } else { (*bytecode)[*size] = value & 0xFF; (*bytecode)[*size + 1] = (value >> 8) & 0xFF; (*bytecode)[*size + 2] = (value >> 16) & 0xFF; (*bytecode)[*size + 3] = (value >> 24) & 0xFF; *size += 4; } return 0; } int hex_bytecode_string(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, const char *value) { hex_debug(ctx, "PUSHST: \"%s\"", hex_process_string(ctx, value)); size_t len = strlen(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; 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)) { hex_debug(ctx, "NATSYM: %s", 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 } else { // Add to symbol table hex_symboltable_set(ctx, value); int index = hex_symboltable_get_index(ctx, value); hex_debug(ctx, "LOOKUP: #%d -> %s", index, value); // Check if we need to resize the buffer (size + 1 opcode + 2 max index) if (*size + 1 + 2 > *capacity) { *capacity = (*size + 1 + 2) * 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 // Add index to bytecode (little endian) (*bytecode)[*size] = index & 0xFF; (*bytecode)[*size + 1] = (index >> 8) & 0xFF; *size += 2; } 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: <end> (items: %d)", *n_items); return 0; } int hex_bytecode(hex_context_t *ctx, const char *input, uint8_t **output, size_t *output_size, hex_file_position_t *position) { 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; } hex_debug(ctx, "Generating bytecode"); 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) { hex_error(ctx, "(%d, %d) Unexpected end of quotation", position->line, position->column); return 1; } 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) { size_t length = 0; int32_t value = 0; // Use signed 32-bit integer to handle negative values int shift = 0; // Decode the variable-length integer for the length do { if (*size == 0) { hex_error(ctx, "Bytecode size too small to contain an integer length"); return 1; } length |= ((**bytecode & 0x7F) << shift); shift += 7; } while (*(*bytecode)++ & 0x80); *size -= shift / 7; if (*size < length) { hex_error(ctx, "Bytecode size too small to contain the integer value"); return 1; } // Decode the integer value based on the length in little-endian format value = 0; for (size_t i = 0; i < length; i++) { value |= (*bytecode)[i] << (8 * i); // Accumulate in little-endian order } // Handle sign extension for 32-bit value if (length == 4) { // If the value is negative, we need to sign-extend it. if (value & 0x80000000) { // If the sign bit is set (negative value) value |= 0xFF000000; // Sign-extend to 32 bits } } *bytecode += length; *size -= length; hex_debug(ctx, "PUSHIN[%zu]: %d", length, 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) { size_t length = 0; int shift = 0; // Decode the variable-length integer for the string length do { if (*size == 0) { hex_error(ctx, "Bytecode size too small to contain a string length"); return 1; } length |= ((**bytecode & 0x7F) << shift); shift += 7; (*bytecode)++; (*size)--; } while (**bytecode & 0x80); 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_item_t item = hex_string_item(ctx, value); *result = item; hex_debug(ctx, "PUSHST[%zu]: %s", length, 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, size_t position, hex_item_t *result) { // Get the index of the symbol (one byte) if (*size == 0) { hex_error(ctx, "Bytecode size too small to contain a symbol length"); return 1; } size_t index = **bytecode; (*bytecode)++; (*size)--; if (index >= ctx->symbol_table.count) { hex_error(ctx, "Symbol index out of bounds"); return 1; } char *value = hex_symboltable_get_value(ctx, index); size_t length = strlen(value); if (!value) { hex_error(ctx, "Memory allocation failed"); return 1; } *bytecode += 1; *size -= 1; hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t)); token->value = (char *)malloc(length + 1); strncpy(token->value, value, length + 1); token->position.line = 0; token->position.column = position; hex_item_t item; item.type = HEX_TYPE_USER_SYMBOL; item.token = token; hex_debug(ctx, "LOOKUP[%zu]: %s", length, value); *result = item; return 0; } int hex_interpret_bytecode_quotation(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t position, hex_item_t *result) { size_t n_items = 0; int shift = 0; // Decode the variable-length integer for the number of items do { if (*size == 0) { hex_error(ctx, "Bytecode size too small to contain a quotation length"); return 1; } n_items |= ((**bytecode & 0x7F) << shift); shift += 7; (*bytecode)++; (*size)--; } while (**bytecode & 0x80); hex_debug(ctx, "PUSHQT[%zu]: <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; } for (size_t i = 0; i < n_items; i++) { uint8_t opcode = **bytecode; (*bytecode)++; (*size)--; hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); switch (opcode) { case HEX_OP_PUSHIN: if (hex_interpret_bytecode_integer(ctx, bytecode, size, item) != 0) { hex_free_list(ctx, items, n_items); return 1; } break; case HEX_OP_PUSHST: if (hex_interpret_bytecode_string(ctx, bytecode, size, item) != 0) { hex_free_list(ctx, items, n_items); return 1; } break; case HEX_OP_LOOKUP: if (hex_interpret_bytecode_user_symbol(ctx, bytecode, size, position, item) != 0) { hex_free_list(ctx, items, n_items); return 1; } break; case HEX_OP_PUSHQT: if (hex_interpret_bytecode_quotation(ctx, bytecode, size, position, item) != 0) { hex_free_list(ctx, items, n_items); return 1; } break; default: if (hex_interpret_bytecode_native_symbol(ctx, opcode, *size, item) != 0) { hex_free_list(ctx, items, n_items); return 1; } break; } items[i] = item; } result->type = HEX_TYPE_QUOTATION; result->data.quotation_value = items; result->quotation_size = n_items; hex_debug(ctx, "PUSHQT[%zu]: <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; uint8_t header[8]; if (size < 8) { hex_error(ctx, "Bytecode size too small to contain a header"); return 1; } memcpy(header, bytecode, 8); int symbol_table_size = hex_validate_header(header); hex_debug(ctx, "hex executable file - version: %d - symbols: %d", header[4], symbol_table_size); if (symbol_table_size < 0) { hex_error(ctx, "Invalid bytecode header"); return 1; } bytecode += 8; size -= 8; // Extract the symbol table if (symbol_table_size > 0) { if (hex_decode_bytecode_symboltable(ctx, &bytecode, &size, symbol_table_size) != 0) { hex_error(ctx, "Failed to decode the symbol table"); return 1; } } // Debug: Print all symbols in the symbol table hex_debug(ctx, "Symbol Table:"); for (size_t i = 0; i < ctx->symbol_table.count; i++) { hex_debug(ctx, "Symbol %zu: %s", i, ctx->symbol_table.symbols[i]); } while (size > 0) { position = bytecode_size - size; uint8_t opcode = *bytecode; hex_debug(ctx, "Bytecode Position: %zu - opcode: %02X", position, opcode); bytecode++; size--; hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); switch (opcode) { case HEX_OP_PUSHIN: if (hex_interpret_bytecode_integer(ctx, &bytecode, &size, item) != 0) { HEX_FREE(ctx, *item); return 1; } break; case HEX_OP_PUSHST: if (hex_interpret_bytecode_string(ctx, &bytecode, &size, item) != 0) { HEX_FREE(ctx, *item); return 1; } break; case HEX_OP_LOOKUP: if (hex_interpret_bytecode_user_symbol(ctx, &bytecode, &size, position, item) != 0) { HEX_FREE(ctx, *item); return 1; } break; case HEX_OP_PUSHQT: if (hex_interpret_bytecode_quotation(ctx, &bytecode, &size, position, item) != 0) { HEX_FREE(ctx, *item); return 1; } break; default: if (hex_interpret_bytecode_native_symbol(ctx, opcode, position, item) != 0) { HEX_FREE(ctx, *item); return 1; } break; } if (hex_push(ctx, *item) != 0) { HEX_FREE(ctx, *item); return 1; } } return 0; } void hex_header(hex_context_t *ctx, uint8_t header[8]) { header[0] = 0x01; header[1] = 'h'; header[2] = 'e'; header[3] = 'x'; header[4] = 0x01; // version uint16_t symbol_table_size = (uint16_t)ctx->symbol_table.count; header[5] = symbol_table_size & 0xFF; header[6] = (symbol_table_size >> 8) & 0xFF; header[7] = 0x02; } int hex_validate_header(uint8_t header[8]) { if (header[0] != 0x01 || header[1] != 'h' || header[2] != 'e' || header[3] != 'x' || header[4] != 0x01 || header[7] != 0x02) { return -1; } uint16_t symbol_table_size = header[5] | (header[6] << 8); return symbol_table_size; } |