Guest User

Untitled

a guest
Aug 9th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 80.74 KB | None | 0 0
  1. #include "toy_compiler.h"
  2.  
  3. #include "toy_memory.h"
  4.  
  5. #include "toy_literal.h"
  6. #include "toy_literal_array.h"
  7. #include "toy_literal_dictionary.h"
  8.  
  9. #include "toy_console_colors.h"
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define OP_DEBUG
  15.  
  16. #ifdef OP_DEBUG
  17. #define OP_PRINT(fmt, ...)  fprintf(stderr, "" fmt, ##__VA_ARGS__)
  18. #else
  19.     #define OP_PRINT(fmt, ...)
  20. #endif
  21.  
  22. #define EP(x) [x] = #x  // enum print
  23. static const char *OP_STR[] = {
  24.         EP(TOY_OP_EOF), //
  25.         EP(TOY_OP_PASS), //
  26.         EP(TOY_OP_ASSERT), //
  27.         EP(TOY_OP_PRINT), //
  28.         EP(TOY_OP_LITERAL), //
  29.         EP(TOY_OP_LITERAL_LONG), //
  30.         EP(TOY_OP_LITERAL_RAW), //
  31.         EP(TOY_OP_NEGATE), //
  32.         EP(TOY_OP_ADDITION), //
  33.         EP(TOY_OP_SUBTRACTION), //
  34.         EP(TOY_OP_MULTIPLICATION), //
  35.         EP(TOY_OP_DIVISION), //
  36.         EP(TOY_OP_MODULO), //
  37.         EP(TOY_OP_GROUPING_BEGIN), //
  38.         EP(TOY_OP_GROUPING_END), //
  39.         EP(TOY_OP_SCOPE_BEGIN), //
  40.         EP(TOY_OP_SCOPE_END), //
  41.         EP(TOY_OP_TYPE_DECL_removed), //
  42.         EP(TOY_OP_TYPE_DECL_LONG_removed), //
  43.         EP(TOY_OP_VAR_DECL), //
  44.         EP(TOY_OP_VAR_DECL_LONG), //
  45.         EP(TOY_OP_FN_DECL), //
  46.         EP(TOY_OP_FN_DECL_LONG), //
  47.         EP(TOY_OP_VAR_ASSIGN), //
  48.         EP(TOY_OP_VAR_ADDITION_ASSIGN), //
  49.         EP(TOY_OP_VAR_SUBTRACTION_ASSIGN), //
  50.         EP(TOY_OP_VAR_MULTIPLICATION_ASSIGN), //
  51.         EP(TOY_OP_VAR_DIVISION_ASSIGN), //
  52.         EP(TOY_OP_VAR_MODULO_ASSIGN), //
  53.         EP(TOY_OP_TYPE_CAST), //
  54.         EP(TOY_OP_TYPE_OF), //
  55.         EP(TOY_OP_IMPORT), //
  56.         EP(TOY_OP_EXPORT_removed), //
  57.         EP(TOY_OP_INDEX), //
  58.         EP(TOY_OP_INDEX_ASSIGN), //
  59.         EP(TOY_OP_INDEX_ASSIGN_INTERMEDIATE), //
  60.         EP(TOY_OP_DOT), //
  61.         EP(TOY_OP_COMPARE_EQUAL), //
  62.         EP(TOY_OP_COMPARE_NOT_EQUAL), //
  63.         EP(TOY_OP_COMPARE_LESS), //
  64.         EP(TOY_OP_COMPARE_LESS_EQUAL), //
  65.         EP(TOY_OP_COMPARE_GREATER), //
  66.         EP(TOY_OP_COMPARE_GREATER_EQUAL), //
  67.         EP(TOY_OP_INVERT), //
  68.         EP(TOY_OP_AND), //
  69.         EP(TOY_OP_OR), //
  70.         EP(TOY_OP_JUMP), //
  71.         EP(TOY_OP_IF_FALSE_JUMP), //
  72.         EP(TOY_OP_FN_CALL), //
  73.         EP(TOY_OP_FN_RETURN), //
  74.         EP(TOY_OP_POP_STACK), //
  75.         EP(TOY_OP_TERNARY), //
  76.         EP(TOY_OP_FN_END), //
  77.         EP(TOY_OP_SECTION_END), //
  78.         EP(TOY_OP_PREFIX), //
  79.         EP(TOY_OP_POSTFIX), //
  80. };
  81.  
  82. void Toy_initCompiler(Toy_Compiler *compiler) {
  83.     Toy_initLiteralArray(&compiler->literalCache);
  84.     compiler->bytecode = NULL;
  85.     compiler->capacity = 0;
  86.     compiler->count = 0;
  87.     compiler->panic = false;
  88. }
  89.  
  90. //separated out, so it can be recursive
  91. static int writeLiteralTypeToCache(Toy_LiteralArray *literalCache, Toy_Literal literal) {
  92.     bool shouldFree = false;
  93.  
  94.     //if it's a compound type, recurse and store the results
  95.     if (TOY_AS_TYPE(literal).typeOf == TOY_LITERAL_ARRAY || TOY_AS_TYPE(literal).typeOf == TOY_LITERAL_DICTIONARY) {
  96.         //I don't like storing types in an array, but it's the easiest and most straight forward method
  97.         Toy_LiteralArray *store = TOY_ALLOCATE(Toy_LiteralArray, 1);
  98.         Toy_initLiteralArray(store);
  99.  
  100.         //store the base literal in the store
  101.         Toy_pushLiteralArray(store, literal);
  102.  
  103.         for (int i = 0; i < TOY_AS_TYPE(literal).count; i++) {
  104.             //write the values to the cache, and the indexes to the store
  105.             int subIndex = writeLiteralTypeToCache(literalCache, ((Toy_Literal*) (TOY_AS_TYPE(literal).subtypes))[i]);
  106.  
  107.             Toy_Literal lit = TOY_TO_INTEGER_LITERAL(subIndex);
  108.             Toy_pushLiteralArray(store, lit);
  109.             Toy_freeLiteral(lit);
  110.         }
  111.  
  112.         //push the store to the cache, tweaking the type
  113.         shouldFree = true;
  114.         literal = TOY_TO_ARRAY_LITERAL(store);
  115.         literal.type = TOY_LITERAL_TYPE_INTERMEDIATE; //NOTE: tweaking the type usually isn't a good idea
  116.     }
  117.  
  118.     //optimization: check if exactly this literal array exists
  119.     int index = Toy_private_findLiteralIndex(literalCache, literal);
  120.     if (index < 0) {
  121.         index = Toy_pushLiteralArray(literalCache, literal);
  122.     }
  123.  
  124.     if (shouldFree) {
  125.         Toy_freeLiteral(literal);
  126.     }
  127.     return index;
  128. }
  129.  
  130. static int writeNodeCompoundToCache(Toy_Compiler *compiler, Toy_ASTNode *node) {
  131.     int index = -1;
  132.  
  133.     //for both, stored as an array
  134.     Toy_LiteralArray *store = TOY_ALLOCATE(Toy_LiteralArray, 1);
  135.     Toy_initLiteralArray(store);
  136.  
  137.     //emit an array or a dictionary definition
  138.     if (node->compound.literalType == TOY_LITERAL_DICTIONARY) {
  139.         //ensure each literal key and value are in the cache, individually
  140.         for (int i = 0; i < node->compound.count; i++) {
  141.             //keys
  142.             switch (node->compound.nodes[i].pair.left->type) {
  143.                 case TOY_AST_NODE_LITERAL: {
  144.                     //keys are literals
  145.                     int key = Toy_private_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].pair.left->atomic.literal);
  146.                     if (key < 0) {
  147.                         key = Toy_pushLiteralArray(&compiler->literalCache, node->compound.nodes[i].pair.left->atomic.literal);
  148.                     }
  149.  
  150.                     Toy_Literal literal = TOY_TO_INTEGER_LITERAL(key);
  151.                     Toy_pushLiteralArray(store, literal);
  152.                     Toy_freeLiteral(literal);
  153.                 }
  154.                 break;
  155.  
  156.                 case TOY_AST_NODE_COMPOUND: {
  157.                     int key = writeNodeCompoundToCache(compiler, node->compound.nodes[i].pair.left);
  158.  
  159.                     Toy_Literal literal = TOY_TO_INTEGER_LITERAL(key);
  160.                     Toy_pushLiteralArray(store, literal);
  161.                     Toy_freeLiteral(literal);
  162.                 }
  163.                 break;
  164.  
  165.                 default:
  166.                     fprintf(stderr,
  167.                     TOY_CC_ERROR "[internal] Unrecognized key node type in writeNodeCompoundToCache()\n" TOY_CC_RESET);
  168.                     return -1;
  169.             }
  170.  
  171.             //values
  172.             switch (node->compound.nodes[i].pair.right->type) {
  173.                 case TOY_AST_NODE_LITERAL: {
  174.                     //values are literals
  175.                     int val = Toy_private_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].pair.right->atomic.literal);
  176.                     if (val < 0) {
  177.                         val = Toy_pushLiteralArray(&compiler->literalCache, node->compound.nodes[i].pair.right->atomic.literal);
  178.                     }
  179.  
  180.                     Toy_Literal literal = TOY_TO_INTEGER_LITERAL(val);
  181.                     Toy_pushLiteralArray(store, literal);
  182.                     Toy_freeLiteral(literal);
  183.                 }
  184.                 break;
  185.  
  186.                 case TOY_AST_NODE_COMPOUND: {
  187.                     int val = writeNodeCompoundToCache(compiler, node->compound.nodes[i].pair.right);
  188.  
  189.                     Toy_Literal literal = TOY_TO_INTEGER_LITERAL(val);
  190.                     Toy_pushLiteralArray(store, literal);
  191.                     Toy_freeLiteral(literal);
  192.                 }
  193.                 break;
  194.  
  195.                 default:
  196.                     fprintf(stderr,
  197.                     TOY_CC_ERROR "[internal] Unrecognized value node type in writeNodeCompoundToCache()\n" TOY_CC_RESET);
  198.                     return -1;
  199.             }
  200.         }
  201.  
  202.         //push the store to the cache, with instructions about how pack it
  203.         Toy_Literal literal = TOY_TO_DICTIONARY_LITERAL((Toy_LiteralDictionary* )store); //cast from array to dict, because it's intermediate
  204.         literal.type = TOY_LITERAL_DICTIONARY_INTERMEDIATE; //god damn it - nested in a dictionary
  205.         index = Toy_pushLiteralArray(&compiler->literalCache, literal);
  206.         Toy_freeLiteral(literal);
  207.     }
  208.  
  209.     else
  210.         if (node->compound.literalType == TOY_LITERAL_ARRAY) {
  211.             //ensure each literal value is in the cache, individually
  212.             for (int i = 0; i < node->compound.count; i++) {
  213.                 switch (node->compound.nodes[i].type) {
  214.                     case TOY_AST_NODE_LITERAL: {
  215.                         //values
  216.                         int val = Toy_private_findLiteralIndex(&compiler->literalCache, node->compound.nodes[i].atomic.literal);
  217.                         if (val < 0) {
  218.                             val = Toy_pushLiteralArray(&compiler->literalCache, node->compound.nodes[i].atomic.literal);
  219.                         }
  220.  
  221.                         Toy_Literal literal = TOY_TO_INTEGER_LITERAL(val);
  222.                         Toy_pushLiteralArray(store, literal);
  223.                         Toy_freeLiteral(literal);
  224.                     }
  225.                     break;
  226.  
  227.                     case TOY_AST_NODE_COMPOUND: {
  228.                         int val = writeNodeCompoundToCache(compiler, &node->compound.nodes[i]);
  229.  
  230.                         Toy_Literal literal = TOY_TO_INTEGER_LITERAL(val);
  231.                         index = Toy_pushLiteralArray(store, literal);
  232.                         Toy_freeLiteral(literal);
  233.                     }
  234.                     break;
  235.  
  236.                     default:
  237.                         fprintf(stderr,
  238.                         TOY_CC_ERROR "[internal] Unrecognized node type in writeNodeCompoundToCache()\n" TOY_CC_RESET);
  239.                         return -1;
  240.                 }
  241.             }
  242.  
  243.             //push the store to the cache, with instructions about how pack it
  244.             Toy_Literal literal = TOY_TO_ARRAY_LITERAL(store);
  245.             literal.type = TOY_LITERAL_ARRAY_INTERMEDIATE; //god damn it - nested in an array
  246.             index = Toy_pushLiteralArray(&compiler->literalCache, literal);
  247.             Toy_freeLiteral(literal);
  248.         } else {
  249.             fprintf(stderr,
  250.             TOY_CC_ERROR "[internal] Unrecognized compound type in writeNodeCompoundToCache()\n" TOY_CC_RESET);
  251.             return -1;
  252.         }
  253.  
  254.     return index;
  255. }
  256.  
  257. static int writeNodeCollectionToCache(Toy_Compiler *compiler, Toy_ASTNode *node) {
  258.     Toy_LiteralArray *store = TOY_ALLOCATE(Toy_LiteralArray, 1);
  259.     Toy_initLiteralArray(store);
  260.  
  261.     //ensure each literal value is in the cache, individually
  262.     for (int i = 0; i < node->fnCollection.count; i++) {
  263.         switch (node->fnCollection.nodes[i].type) {
  264.             case TOY_AST_NODE_VAR_DECL: {
  265.                 //write each piece of the declaration to the cache
  266.                 int identifierIndex = Toy_pushLiteralArray(&compiler->literalCache, node->fnCollection.nodes[i].varDecl.identifier); //store without duplication optimisation
  267.                 int typeIndex = writeLiteralTypeToCache(&compiler->literalCache, node->fnCollection.nodes[i].varDecl.typeLiteral);
  268.  
  269.                 Toy_Literal identifierLiteral = TOY_TO_INTEGER_LITERAL(identifierIndex);
  270.                 Toy_pushLiteralArray(store, identifierLiteral);
  271.                 Toy_freeLiteral(identifierLiteral);
  272.  
  273.                 Toy_Literal typeLiteral = TOY_TO_INTEGER_LITERAL(typeIndex);
  274.                 Toy_pushLiteralArray(store, typeLiteral);
  275.                 Toy_freeLiteral(typeLiteral);
  276.             }
  277.             break;
  278.  
  279.             case TOY_AST_NODE_LITERAL: {
  280.                 //write each piece of the declaration to the cache
  281.                 int typeIndex = writeLiteralTypeToCache(&compiler->literalCache, node->fnCollection.nodes[i].atomic.literal);
  282.  
  283.                 Toy_Literal typeLiteral = TOY_TO_INTEGER_LITERAL(typeIndex);
  284.                 Toy_pushLiteralArray(store, typeLiteral);
  285.                 Toy_freeLiteral(typeLiteral);
  286.             }
  287.             break;
  288.  
  289.             default:
  290.                 fprintf(stderr,
  291.                 TOY_CC_ERROR "[internal] Unrecognized node type in writeNodeCollectionToCache()\n" TOY_CC_RESET);
  292.                 return -1;
  293.         }
  294.     }
  295.  
  296.     //store the store
  297.     Toy_Literal literal = TOY_TO_ARRAY_LITERAL(store);
  298.     int storeIndex = Toy_pushLiteralArray(&compiler->literalCache, literal);
  299.     Toy_freeLiteral(literal);
  300.  
  301.     return storeIndex;
  302. }
  303.  
  304. static int writeLiteralToCompiler(Toy_Compiler *compiler, Toy_Literal literal) {
  305.     //get the index
  306.     int index = Toy_private_findLiteralIndex(&compiler->literalCache, literal);
  307.  
  308.     if (index < 0) {
  309.         if (TOY_IS_TYPE(literal)) {
  310.             //check for the type literal as value
  311.             index = writeLiteralTypeToCache(&compiler->literalCache, literal);
  312.         } else {
  313.             index = Toy_pushLiteralArray(&compiler->literalCache, literal);
  314.         }
  315.     }
  316.  
  317.     //push the literal to the bytecode
  318.     if (index >= 256) {
  319.         //push a "long" index
  320.         OP_PRINT("[%05d] ", compiler->count);
  321.         compiler->bytecode[compiler->count++] = TOY_OP_LITERAL_LONG; //1 byte
  322.         memcpy(compiler->bytecode + compiler->count, &index, sizeof(unsigned short)); //2 bytes
  323.         OP_PRINT("LITERAL_LONG %d\n", index);
  324.  
  325.         compiler->count += sizeof(unsigned short);
  326.     } else {
  327.         //push the index
  328.         OP_PRINT("[%05d] ", compiler->count);
  329.         compiler->bytecode[compiler->count++] = TOY_OP_LITERAL; //1 byte
  330.         compiler->bytecode[compiler->count++] = (unsigned char) index; //1 byte
  331.         OP_PRINT("LITERAL %d\n", index);
  332.     }
  333.  
  334.     return index;
  335. }
  336.  
  337. //BUGFIX: check to see if this node lies within this tree
  338. bool checkNodeInTree(Toy_ASTNode *tree, Toy_ASTNode *node) {
  339.     if (tree == node) {
  340.         return true;
  341.     }
  342.  
  343.     if (tree == NULL) {
  344.         return false;
  345.     }
  346.  
  347.     switch (tree->type) {
  348.         case TOY_AST_NODE_UNARY:
  349.             return checkNodeInTree(tree->unary.child, node);
  350.  
  351.         case TOY_AST_NODE_BINARY:
  352.             return checkNodeInTree(tree->binary.left, node) || checkNodeInTree(tree->binary.right, node);
  353.  
  354.         case TOY_AST_NODE_TERNARY:
  355.             return checkNodeInTree(tree->ternary.condition, node) || checkNodeInTree(tree->ternary.thenPath, node)
  356.                     || checkNodeInTree(tree->ternary.elsePath, node);
  357.  
  358.         case TOY_AST_NODE_GROUPING:
  359.             return checkNodeInTree(tree->grouping.child, node);
  360.  
  361.         case TOY_AST_NODE_BLOCK:
  362.             return checkNodeInTree(tree->block.nodes, node);
  363.  
  364.         case TOY_AST_NODE_COMPOUND:
  365.             return checkNodeInTree(tree->compound.nodes, node);
  366.  
  367.         case TOY_AST_NODE_PAIR:
  368.             return checkNodeInTree(tree->pair.left, node) || checkNodeInTree(tree->pair.right, node);
  369.  
  370.         case TOY_AST_NODE_INDEX:
  371.             return checkNodeInTree(tree->index.first, node) || checkNodeInTree(tree->index.second, node) || checkNodeInTree(tree->index.third, node);
  372.  
  373.         case TOY_AST_NODE_VAR_DECL:
  374.             return checkNodeInTree(tree->varDecl.expression, node);
  375.  
  376.         case TOY_AST_NODE_FN_COLLECTION:
  377.             return checkNodeInTree(tree->fnCollection.nodes, node);
  378.  
  379.         case TOY_AST_NODE_FN_DECL:
  380.             return checkNodeInTree(tree->fnDecl.arguments, node) || checkNodeInTree(tree->fnDecl.returns, node) || checkNodeInTree(tree->fnDecl.block, node);
  381.  
  382.         case TOY_AST_NODE_FN_CALL:
  383.             return checkNodeInTree(tree->fnCall.arguments, node);
  384.  
  385.         case TOY_AST_NODE_FN_RETURN:
  386.             return checkNodeInTree(tree->returns.returns, node);
  387.  
  388.         case TOY_AST_NODE_IF:
  389.             return checkNodeInTree(tree->pathIf.condition, node) || checkNodeInTree(tree->pathIf.thenPath, node) || checkNodeInTree(tree->pathIf.elsePath, node);
  390.  
  391.         case TOY_AST_NODE_WHILE:
  392.             return checkNodeInTree(tree->pathWhile.condition, node) || checkNodeInTree(tree->pathWhile.thenPath, node);
  393.  
  394.         case TOY_AST_NODE_FOR:
  395.             return checkNodeInTree(tree->pathFor.preClause, node) || checkNodeInTree(tree->pathFor.condition, node)
  396.                     || checkNodeInTree(tree->pathFor.postClause, node) || checkNodeInTree(tree->pathFor.thenPath, node);
  397.  
  398.         case TOY_AST_NODE_AND:
  399.             return checkNodeInTree(tree->pathAnd.left, node) || checkNodeInTree(tree->pathAnd.right, node);
  400.  
  401.         case TOY_AST_NODE_OR:
  402.             return checkNodeInTree(tree->pathOr.left, node) || checkNodeInTree(tree->pathOr.right, node);
  403.  
  404.         case TOY_AST_NODE_ERROR:
  405.         case TOY_AST_NODE_LITERAL:
  406.         case TOY_AST_NODE_BREAK:
  407.         case TOY_AST_NODE_CONTINUE:
  408.         case TOY_AST_NODE_PREFIX_INCREMENT:
  409.         case TOY_AST_NODE_PREFIX_DECREMENT:
  410.         case TOY_AST_NODE_POSTFIX_INCREMENT:
  411.         case TOY_AST_NODE_POSTFIX_DECREMENT:
  412.         case TOY_AST_NODE_IMPORT:
  413.         case TOY_AST_NODE_PASS:
  414.             return false;
  415.     }
  416.  
  417.     return false;
  418. }
  419.  
  420. //NOTE: jumpOfsets are included, because function arg and return indexes are embedded in the code body i.e. need to include their sizes in the jump
  421. //NOTE: rootNode should NOT include groupings and blocks
  422. static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler *compiler, Toy_ASTNode *node, void *breakAddressesPtr, void *continueAddressesPtr, int jumpOffsets,
  423.         Toy_ASTNode *rootNode) {
  424.     //grow if the bytecode space is too small
  425.     if (compiler->count + 32 > compiler->capacity) {
  426.         int oldCapacity = compiler->capacity;
  427.  
  428.         compiler->capacity = TOY_GROW_CAPACITY_FAST(oldCapacity);
  429.         compiler->bytecode = TOY_GROW_ARRAY(unsigned char, compiler->bytecode, oldCapacity, compiler->capacity);
  430.     }
  431.  
  432.     //determine node type
  433.     switch (node->type) {
  434.         case TOY_AST_NODE_ERROR: {
  435.             fprintf(stderr,
  436.             TOY_CC_ERROR "[internal] TOY_AST_NODEERROR encountered in Toy_writeCompilerWithJumps()\n" TOY_CC_RESET);
  437.             OP_PRINT("[%05d] ", compiler->count);
  438.             compiler->bytecode[compiler->count++] = TOY_OP_EOF; //1 byte
  439.             OP_PRINT("EOF %s\n", "");
  440.         }
  441.         break;
  442.  
  443.         case TOY_AST_NODE_LITERAL: {
  444.             writeLiteralToCompiler(compiler, node->atomic.literal);
  445.         }
  446.         break;
  447.  
  448.         case TOY_AST_NODE_UNARY: {
  449.             //pass to the child node, then embed the unary command (print, negate, etc.)
  450.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->unary.child, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  451.  
  452.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  453.                 OP_PRINT("[%05d] ", compiler->count);
  454.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  455.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  456.             }
  457.  
  458.             OP_PRINT("[%05d] ", compiler->count);
  459.             compiler->bytecode[compiler->count++] = (unsigned char) node->unary.opcode; //1 byte
  460.             OP_PRINT("[%d: %s]\n", (unsigned char ) node->unary.opcode, OP_STR[(unsigned char ) node->unary.opcode]);
  461.         }
  462.         break;
  463.  
  464.             //all infixes come here
  465.         case TOY_AST_NODE_BINARY: {
  466.             //pass to the child nodes, then embed the binary command (math, etc.)
  467.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->binary.left, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  468.  
  469.             //special case for when indexing and assigning
  470.             if (override != TOY_OP_EOF && node->binary.opcode >= TOY_OP_VAR_ASSIGN && node->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN) {
  471.                 Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  472.  
  473.                 //Special case if there's an index on both sides of the sign, just set it as indexing
  474.                 if (node->binary.left->type == TOY_AST_NODE_BINARY && node->binary.right->type == TOY_AST_NODE_BINARY
  475.                         && node->binary.left->binary.opcode == TOY_OP_INDEX && node->binary.right->binary.opcode == TOY_OP_INDEX) {
  476.                     OP_PRINT("[%05d] ", compiler->count);
  477.                     compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_INDEX;
  478.                     OP_PRINT("INDEX %s\n", "");
  479.                 }
  480.  
  481.                 OP_PRINT("[%05d] ", compiler->count);
  482.                 compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_INDEX_ASSIGN; //1 byte WARNING: enum trickery
  483.                 compiler->bytecode[compiler->count++] = (unsigned char) node->binary.opcode; //1 byte
  484.                 OP_PRINT("INDEX_ASSIGN %d: %s\n", (unsigned char ) node->binary.opcode, OP_STR[(unsigned char ) node->binary.opcode]);
  485.                 return TOY_OP_EOF;
  486.             }
  487.  
  488.             //compensate for... yikes
  489.             if (override != TOY_OP_EOF) {
  490.                 OP_PRINT("[%05d] ", compiler->count);
  491.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  492.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  493.             }
  494.  
  495.             //return this if...
  496.             Toy_Opcode ret = Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  497.  
  498.             //range-based check for assignment type; make sure the index is on the left of the assignment symbol
  499.             if (node->binary.opcode == TOY_OP_INDEX && rootNode->type == TOY_AST_NODE_BINARY
  500.                     && (rootNode->binary.opcode >= TOY_OP_VAR_ASSIGN && rootNode->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN)
  501.                     && !checkNodeInTree(rootNode->binary.right, node)) {
  502.                 return TOY_OP_INDEX_ASSIGN_INTERMEDIATE;
  503.             }
  504.  
  505.             //loopy logic - if opcode == index or dot
  506.             if (node->binary.opcode == TOY_OP_INDEX || node->binary.opcode == TOY_OP_DOT) {
  507.                 return node->binary.opcode;
  508.             }
  509.  
  510.             //untangle in these cases - (WTF, are you serious?)
  511.             if (ret != TOY_OP_EOF) {
  512.                 switch (node->binary.opcode) {
  513.                     case TOY_OP_NEGATE:
  514.                     case TOY_OP_ADDITION:
  515.                     case TOY_OP_SUBTRACTION:
  516.                     case TOY_OP_MULTIPLICATION:
  517.                     case TOY_OP_DIVISION:
  518.                     case TOY_OP_MODULO:
  519.                     case TOY_OP_VAR_ASSIGN:
  520.                     case TOY_OP_VAR_ADDITION_ASSIGN:
  521.                     case TOY_OP_VAR_SUBTRACTION_ASSIGN:
  522.                     case TOY_OP_VAR_MULTIPLICATION_ASSIGN:
  523.                     case TOY_OP_VAR_DIVISION_ASSIGN:
  524.                     case TOY_OP_VAR_MODULO_ASSIGN:
  525.                     case TOY_OP_COMPARE_EQUAL:
  526.                     case TOY_OP_COMPARE_NOT_EQUAL:
  527.                     case TOY_OP_COMPARE_LESS:
  528.                     case TOY_OP_COMPARE_LESS_EQUAL:
  529.                     case TOY_OP_COMPARE_GREATER:
  530.                     case TOY_OP_COMPARE_GREATER_EQUAL:
  531.                     case TOY_OP_INVERT:
  532.                         //place the rhs result before the outer instruction
  533.                         OP_PRINT("[%05d] ", compiler->count);
  534.                         compiler->bytecode[compiler->count++] = (unsigned char) ret; //1 byte
  535.                         OP_PRINT("[%d: %s]\n", (unsigned char ) ret, OP_STR[(unsigned char ) ret]);
  536.                         ret = TOY_OP_EOF;
  537.  
  538.                     default:
  539.                     break;
  540.                 }
  541.             }
  542.  
  543.             OP_PRINT("[%05d] ", compiler->count);
  544.             compiler->bytecode[compiler->count++] = (unsigned char) node->binary.opcode; //1 byte
  545.             OP_PRINT("[%d: %s]\n", (unsigned char ) node->binary.opcode, OP_STR[(unsigned char ) node->binary.opcode]);
  546.  
  547.             return ret;
  548.         }
  549.         break;
  550.  
  551.         case TOY_AST_NODE_TERNARY: {
  552.             // TODO: a ?: b;
  553.  
  554.             //process the condition
  555.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->ternary.condition, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  556.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  557.                 OP_PRINT("[%05d] ", compiler->count);
  558.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  559.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  560.             }
  561.  
  562.             //cache the point to insert the jump distance at
  563.             OP_PRINT("[%05d] ", compiler->count);
  564.             compiler->bytecode[compiler->count++] = TOY_OP_IF_FALSE_JUMP; //1 byte
  565.             OP_PRINT("IF_FALSE_JUMP %s\n", "");
  566.             int jumpToElse = compiler->count;
  567.             compiler->count += sizeof(unsigned short); //2 bytes
  568.  
  569.             //write the then path
  570.             override = Toy_writeCompilerWithJumps(compiler, node->pathIf.thenPath, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  571.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  572.                 OP_PRINT("[%05d] ", compiler->count);
  573.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  574.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  575.             }
  576.  
  577.             int jumpToEnd = 0;
  578.  
  579.             //insert jump to end
  580.             OP_PRINT("[%05d] ", compiler->count);
  581.             compiler->bytecode[compiler->count++] = TOY_OP_JUMP; //1 byte
  582.             OP_PRINT("JUMP %s\n", "");
  583.             jumpToEnd = compiler->count;
  584.             compiler->count += sizeof(unsigned short); //2 bytes
  585.  
  586.             //update the jumpToElse to point here
  587.             unsigned short tmpVal = compiler->count + jumpOffsets;
  588.             memcpy(compiler->bytecode + jumpToElse, &tmpVal, sizeof(tmpVal)); //2 bytes
  589.             OP_PRINT("[%05d] ", compiler->count);
  590.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  591.  
  592.             //write the else path
  593.             Toy_Opcode override2 = Toy_writeCompilerWithJumps(compiler, node->pathIf.elsePath, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  594.             if (override2 != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  595.                 OP_PRINT("[%05d] ", compiler->count);
  596.                 compiler->bytecode[compiler->count++] = (unsigned char) override2; //1 byte
  597.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override2, OP_STR[(unsigned char ) override2]);
  598.             }
  599.  
  600.             //update the jumpToEnd to point here
  601.             tmpVal = compiler->count + jumpOffsets;
  602.             memcpy(compiler->bytecode + jumpToEnd, &tmpVal, sizeof(tmpVal)); //2 bytes
  603.             OP_PRINT("[%05d] ", compiler->count);
  604.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  605.         }
  606.         break;
  607.  
  608.         case TOY_AST_NODE_GROUPING: {
  609.             OP_PRINT("[%05d] ", compiler->count);
  610.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_GROUPING_BEGIN; //1 byte
  611.             OP_PRINT("GROUP_BEGIN %s\n", "");
  612.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->grouping.child, breakAddressesPtr, continueAddressesPtr, jumpOffsets,
  613.                     node->grouping.child);
  614.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  615.                 OP_PRINT("[%05d] ", compiler->count);
  616.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  617.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  618.             }
  619.             OP_PRINT("[%05d] ", compiler->count);
  620.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_GROUPING_END; //1 byte
  621.             OP_PRINT("GROUPING_END %s\n", "");
  622.         }
  623.         break;
  624.  
  625.         case TOY_AST_NODE_BLOCK: {
  626.             OP_PRINT("[%05d] ", compiler->count);
  627.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_SCOPE_BEGIN; //1 byte
  628.             OP_PRINT("SCOPE_BEGIN %s\n", "");
  629.  
  630.             for (int i = 0; i < node->block.count; i++) {
  631.                 Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, &(node->block.nodes[i]), breakAddressesPtr, continueAddressesPtr, jumpOffsets,
  632.                         &(node->block.nodes[i]));
  633.                 if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  634.                     OP_PRINT("[%05d] ", compiler->count);
  635.                     compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  636.                     OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  637.                 }
  638.             }
  639.  
  640.             OP_PRINT("[%05d] ", compiler->count);
  641.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_SCOPE_END; //1 byte
  642.             OP_PRINT("SCOPE_END %s\n", "");
  643.         }
  644.         break;
  645.  
  646.         case TOY_AST_NODE_COMPOUND: {
  647.             int index = writeNodeCompoundToCache(compiler, node);
  648.  
  649.             if (index < 0) {
  650.                 compiler->panic = true;
  651.                 return TOY_OP_EOF;
  652.             }
  653.  
  654.             //push the node opcode to the bytecode
  655.             if (index >= 256) {
  656.                 //push a "long" index
  657.                 OP_PRINT("[%05d] ", compiler->count);
  658.                 compiler->bytecode[compiler->count++] = TOY_OP_LITERAL_LONG; //1 byte
  659.                 memcpy(compiler->bytecode + compiler->count, &index, sizeof(unsigned short));
  660.                 OP_PRINT("LITERAL_LONG %d\n", index);
  661.  
  662.                 compiler->count += sizeof(unsigned short);
  663.             } else {
  664.                 //push the index
  665.                 OP_PRINT("[%05d] ", compiler->count);
  666.                 compiler->bytecode[compiler->count++] = TOY_OP_LITERAL; //1 byte
  667.                 compiler->bytecode[compiler->count++] = (unsigned char) index; //1 byte
  668.                 OP_PRINT("LITERAL %d\n", index);
  669.             }
  670.         }
  671.         break;
  672.  
  673.         case TOY_AST_NODE_PAIR:
  674.             fprintf(stderr,
  675.             TOY_CC_ERROR "[internal] TOY_AST_NODEPAIR encountered in Toy_writeCompilerWithJumps()\n" TOY_CC_RESET);
  676.             OP_PRINT("[%05d] ", compiler->count);
  677.             compiler->bytecode[compiler->count++] = TOY_OP_EOF; //1 byte
  678.             OP_PRINT("EOF %s\n", "");
  679.         break;
  680.  
  681.         case TOY_AST_NODE_VAR_DECL: {
  682.             //first, embed the expression (leaves it on the stack)
  683.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->varDecl.expression, breakAddressesPtr, continueAddressesPtr, jumpOffsets,
  684.                     rootNode);
  685.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  686.                 OP_PRINT("[%05d] ", compiler->count);
  687.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  688.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  689.             }
  690.  
  691.             //write each piece of the declaration to the bytecode
  692.             int identifierIndex = Toy_private_findLiteralIndex(&compiler->literalCache, node->varDecl.identifier);
  693.             if (identifierIndex < 0) {
  694.                 identifierIndex = Toy_pushLiteralArray(&compiler->literalCache, node->varDecl.identifier);
  695.             }
  696.  
  697.             int typeIndex = writeLiteralTypeToCache(&compiler->literalCache, node->varDecl.typeLiteral);
  698.  
  699.             //embed the info into the bytecode
  700.             if (identifierIndex >= 256 || typeIndex >= 256) {
  701.                 //push a "long" declaration
  702.                 OP_PRINT("[%05d] ", compiler->count);
  703.                 compiler->bytecode[compiler->count++] = TOY_OP_VAR_DECL_LONG; //1 byte
  704.                 OP_PRINT("VAR_DECL_LONG %s\n", "");
  705.  
  706.                 *((unsigned short*) (compiler->bytecode + compiler->count)) = (unsigned short) identifierIndex; //2 bytes
  707.                 compiler->count += sizeof(unsigned short);
  708.                 OP_PRINT("[%d: %s]\n", (unsigned char ) identifierIndex, OP_STR[(unsigned char ) identifierIndex]);
  709.  
  710.                 *((unsigned short*) (compiler->bytecode + compiler->count)) = (unsigned short) typeIndex; //2 bytes
  711.                 compiler->count += sizeof(unsigned short);
  712.                 OP_PRINT("[%d: %s]\n", (unsigned char ) typeIndex, OP_STR[(unsigned char ) typeIndex]);
  713.             } else {
  714.                 //push a declaration
  715.                 OP_PRINT("[%05d] ", compiler->count);
  716.                 compiler->bytecode[compiler->count++] = TOY_OP_VAR_DECL; //1 byte
  717.                 compiler->bytecode[compiler->count++] = (unsigned char) identifierIndex; //1 byte
  718.                 compiler->bytecode[compiler->count++] = (unsigned char) typeIndex; //1 byte
  719.                 OP_PRINT("VAR_DECL %d %d\n", (unsigned char ) identifierIndex, (unsigned char ) typeIndex);
  720.             }
  721.         }
  722.         break;
  723.  
  724.         case TOY_AST_NODE_FN_DECL: {
  725.             //run a compiler over the function
  726.             Toy_Compiler *fnCompiler = TOY_ALLOCATE(Toy_Compiler, 1);
  727.             Toy_initCompiler(fnCompiler);
  728.             Toy_writeCompiler(fnCompiler, node->fnDecl.arguments); //can be empty, but not NULL
  729.             Toy_writeCompiler(fnCompiler, node->fnDecl.returns); //can be empty, but not NULL
  730.             Toy_Opcode override = Toy_writeCompilerWithJumps(fnCompiler, node->fnDecl.block, NULL, NULL, -4, rootNode); //can be empty, but not NULL
  731.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  732.                 OP_PRINT("[%05d] ", compiler->count);
  733.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  734.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  735.             }
  736.  
  737.             //adopt the panic state if anything happened
  738.             if (fnCompiler->panic) {
  739.                 compiler->panic = true;
  740.             }
  741.  
  742.             //create the function in the literal cache (by storing the compiler object)
  743.             Toy_Literal fnLiteral = ((Toy_Literal ) { .as = { .generic = fnCompiler }, .type = TOY_LITERAL_FUNCTION_INTERMEDIATE } );
  744.  
  745.             //push the name
  746.             int identifierIndex = Toy_private_findLiteralIndex(&compiler->literalCache, node->fnDecl.identifier);
  747.             if (identifierIndex < 0) {
  748.                 identifierIndex = Toy_pushLiteralArray(&compiler->literalCache, node->fnDecl.identifier);
  749.             }
  750.  
  751.             //push to function (functions are never equal)
  752.             int fnIndex = Toy_pushLiteralArray(&compiler->literalCache, fnLiteral);
  753.  
  754.             //embed the info into the bytecode
  755.             if (identifierIndex >= 256 || fnIndex >= 256) {
  756.                 //push a "long" declaration
  757.                 OP_PRINT("[%05d] ", compiler->count);
  758.                 compiler->bytecode[compiler->count++] = TOY_OP_FN_DECL_LONG; //1 byte
  759.  
  760.                 *((unsigned short*) (compiler->bytecode + compiler->count)) = (unsigned short) identifierIndex; //2 bytes
  761.                 compiler->count += sizeof(unsigned short);
  762.  
  763.                 *((unsigned short*) (compiler->bytecode + compiler->count)) = (unsigned short) fnIndex; //2 bytes
  764.                 compiler->count += sizeof(unsigned short);
  765.                 OP_PRINT("FN_DECL_LONG %d %d\n", (unsigned short ) identifierIndex, (unsigned short ) fnIndex);
  766.             } else {
  767.                 //push a declaration
  768.                 OP_PRINT("[%05d] ", compiler->count);
  769.                 compiler->bytecode[compiler->count++] = TOY_OP_FN_DECL; //1 byte
  770.                 compiler->bytecode[compiler->count++] = (unsigned char) identifierIndex; //1 byte
  771.                 compiler->bytecode[compiler->count++] = (unsigned char) fnIndex; //1 byte
  772.                 OP_PRINT("FN_DECL %d %d\n", (unsigned short ) identifierIndex, (unsigned short ) fnIndex);
  773.             }
  774.         }
  775.         break;
  776.  
  777.         case TOY_AST_NODE_FN_COLLECTION: {
  778.             //embed these in the bytecode...
  779.             unsigned short index = (unsigned short) writeNodeCollectionToCache(compiler, node);
  780.  
  781.             if (index == (unsigned short) -1) {
  782.                 compiler->panic = true;
  783.                 return TOY_OP_EOF;
  784.             }
  785.  
  786.             memcpy(compiler->bytecode + compiler->count, &index, sizeof(index));
  787.             OP_PRINT("[%d: %s]\n", (unsigned char ) index, OP_STR[(unsigned char ) index]);
  788.             compiler->count += sizeof(unsigned short);
  789.         }
  790.         break;
  791.  
  792.         case TOY_AST_NODE_FN_CALL: {
  793.             //NOTE: assume the function definition/name is above us
  794.  
  795.             for (int i = 0; i < node->fnCall.arguments->fnCollection.count; i++) { //reverse order, to count from the beginning in the interpreter
  796.                 //sub-calls
  797.                 if (node->fnCall.arguments->fnCollection.nodes[i].type != TOY_AST_NODE_LITERAL) {
  798.                     Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, &node->fnCall.arguments->fnCollection.nodes[i], breakAddressesPtr,
  799.                             continueAddressesPtr, jumpOffsets, rootNode);
  800.                     if (override != TOY_OP_EOF) {            //compensate for indexing & dot notation being screwy
  801.                         OP_PRINT("[%05d] ", compiler->count);
  802.                         compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  803.                         OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  804.                     }
  805.                     continue;
  806.                 }
  807.  
  808.                 //write each argument to the bytecode
  809.                 int argumentsIndex = Toy_private_findLiteralIndex(&compiler->literalCache, node->fnCall.arguments->fnCollection.nodes[i].atomic.literal);
  810.                 if (argumentsIndex < 0) {
  811.                     argumentsIndex = Toy_pushLiteralArray(&compiler->literalCache, node->fnCall.arguments->fnCollection.nodes[i].atomic.literal);
  812.                 }
  813.  
  814.                 //push the node opcode to the bytecode
  815.                 if (argumentsIndex >= 256) {
  816.                     //push a "long" index
  817.                     OP_PRINT("[%05d] ", compiler->count);
  818.                     compiler->bytecode[compiler->count++] = TOY_OP_LITERAL_LONG; //1 byte
  819.  
  820.                     *((unsigned short*) (compiler->bytecode + compiler->count)) = (unsigned short) argumentsIndex; //2 bytes
  821.                     compiler->count += sizeof(unsigned short);
  822.                     OP_PRINT("LITERAL_LONG %d\n", (unsigned short ) argumentsIndex);
  823.                 } else {
  824.                     //push the index
  825.                     OP_PRINT("[%05d] ", compiler->count);
  826.                     compiler->bytecode[compiler->count++] = TOY_OP_LITERAL; //1 byte
  827.                     compiler->bytecode[compiler->count++] = (unsigned char) argumentsIndex; //1 byte
  828.                     OP_PRINT("LITERAL %d\n", (unsigned short ) argumentsIndex);
  829.                 }
  830.             }
  831.  
  832.             //push the argument COUNT to the top of the stack
  833.             Toy_Literal argumentsCountLiteral = TOY_TO_INTEGER_LITERAL(node->fnCall.argumentCount); //argumentCount is set elsewhere to support dot operator
  834.             int argumentsCountIndex = Toy_private_findLiteralIndex(&compiler->literalCache, argumentsCountLiteral);
  835.             if (argumentsCountIndex < 0) {
  836.                 argumentsCountIndex = Toy_pushLiteralArray(&compiler->literalCache, argumentsCountLiteral);
  837.             }
  838.             Toy_freeLiteral(argumentsCountLiteral);
  839.  
  840.             if (argumentsCountIndex >= 256) {
  841.                 //push a "long" index
  842.                 OP_PRINT("[%05d] ", compiler->count);
  843.                 compiler->bytecode[compiler->count++] = TOY_OP_LITERAL_LONG; //1 byte
  844.  
  845.                 *((unsigned short*) (compiler->bytecode + compiler->count)) = (unsigned short) argumentsCountIndex; //2 bytes
  846.                 compiler->count += sizeof(unsigned short);
  847.                 OP_PRINT("LITERAL_LONG %d\n", (unsigned short ) argumentsCountIndex);
  848.             } else {
  849.                 //push the index
  850.                 OP_PRINT("[%05d] ", compiler->count);
  851.                 compiler->bytecode[compiler->count++] = TOY_OP_LITERAL; //1 byte
  852.                 compiler->bytecode[compiler->count++] = (unsigned char) argumentsCountIndex; //1 byte
  853.                 OP_PRINT("LITERAL %d\n", (unsigned short ) argumentsCountIndex);
  854.             }
  855.  
  856.             //call the function
  857.             //DO NOT call the collection, this is done in binary
  858.         }
  859.         break;
  860.  
  861.         case TOY_AST_NODE_IF: {
  862.             //process the condition
  863.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->pathIf.condition, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  864.             if (override != TOY_OP_EOF) {        //compensate for indexing & dot notation being screwy
  865.                 OP_PRINT("[%05d] ", compiler->count);
  866.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  867.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  868.             }
  869.  
  870.             //cache the point to insert the jump distance at
  871.             OP_PRINT("[%05d] ", compiler->count);
  872.             compiler->bytecode[compiler->count++] = TOY_OP_IF_FALSE_JUMP; //1 byte
  873.             OP_PRINT("IF_FALSE_JUMP %s\n", "");
  874.             int jumpToElse = compiler->count;
  875.             compiler->count += sizeof(unsigned short); //2 bytes
  876.  
  877.             //write the then path
  878.             override = Toy_writeCompilerWithJumps(compiler, node->pathIf.thenPath, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  879.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  880.                 OP_PRINT("[%05d] ", compiler->count);
  881.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  882.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  883.             }
  884.  
  885.             int jumpToEnd = 0;
  886.  
  887.             if (node->pathIf.elsePath) {
  888.                 //insert jump to end
  889.                 OP_PRINT("[%05d] ", compiler->count);
  890.                 compiler->bytecode[compiler->count++] = TOY_OP_JUMP; //1 byte
  891.                 OP_PRINT("JUMP %s\n", "");
  892.                 jumpToEnd = compiler->count;
  893.                 compiler->count += sizeof(unsigned short); //2 bytes
  894.             }
  895.  
  896.             //update the jumpToElse to point here
  897.             unsigned short tmpVal = compiler->count + jumpOffsets;
  898.             memcpy(compiler->bytecode + jumpToElse, &tmpVal, sizeof(tmpVal)); //2 bytes
  899.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  900.  
  901.             if (node->pathIf.elsePath) {
  902.                 //if there's an else path, write it and
  903.                 Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->pathIf.elsePath, breakAddressesPtr, continueAddressesPtr, jumpOffsets,
  904.                         rootNode);
  905.                 if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  906.                     OP_PRINT("[%05d] ", compiler->count);
  907.                     compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  908.                     OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  909.                 }
  910.  
  911.                 //update the jumpToEnd to point here
  912.                 tmpVal = compiler->count + jumpOffsets;
  913.                 memcpy(compiler->bytecode + jumpToEnd, &tmpVal, sizeof(tmpVal)); //2 bytes
  914.                 OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  915.             }
  916.         }
  917.         break;
  918.  
  919.         case TOY_AST_NODE_WHILE: {
  920.             //for breaks and continues
  921.             Toy_LiteralArray breakAddresses;
  922.             Toy_LiteralArray continueAddresses;
  923.  
  924.             Toy_initLiteralArray(&breakAddresses);
  925.             Toy_initLiteralArray(&continueAddresses);
  926.  
  927.             //cache the jump point
  928.             unsigned short jumpToStart = compiler->count;
  929.  
  930.             //process the condition
  931.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->pathWhile.condition, &breakAddresses, &continueAddresses, jumpOffsets, rootNode);
  932.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  933.                 OP_PRINT("[%05d] ", compiler->count);
  934.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  935.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  936.             }
  937.  
  938.             //if false, jump to end
  939.             OP_PRINT("[%05d] ", compiler->count);
  940.             compiler->bytecode[compiler->count++] = TOY_OP_IF_FALSE_JUMP; //1 byte
  941.             OP_PRINT("IF_FALSE_JUMP %s\n", "");
  942.             unsigned short jumpToEnd = compiler->count;
  943.             compiler->count += sizeof(unsigned short); //2 bytes
  944.  
  945.             //write the body
  946.             override = Toy_writeCompilerWithJumps(compiler, node->pathWhile.thenPath, &breakAddresses, &continueAddresses, jumpOffsets, rootNode);
  947.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  948.                 OP_PRINT("[%05d] ", compiler->count);
  949.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  950.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  951.             }
  952.  
  953.             //jump to condition
  954.             OP_PRINT("[%05d] ", compiler->count);
  955.             compiler->bytecode[compiler->count++] = TOY_OP_JUMP; //1 byte
  956.             OP_PRINT("JUMP %s\n", "");
  957.             unsigned short tmpVal = jumpToStart + jumpOffsets;
  958.             memcpy(compiler->bytecode + compiler->count, &tmpVal, sizeof(tmpVal));
  959.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  960.             compiler->count += sizeof(unsigned short); //2 bytes
  961.  
  962.             //jump from condition
  963.             tmpVal = compiler->count + jumpOffsets;
  964.             memcpy(compiler->bytecode + jumpToEnd, &tmpVal, sizeof(tmpVal));
  965.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  966.  
  967.             //set the breaks and continues
  968.             for (int i = 0; i < breakAddresses.count; i++) {
  969.                 int point = TOY_AS_INTEGER(breakAddresses.literals[i]);
  970.                 tmpVal = compiler->count + jumpOffsets;
  971.                 memcpy(compiler->bytecode + point, &tmpVal, sizeof(tmpVal));
  972.                 OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  973.             }
  974.  
  975.             for (int i = 0; i < continueAddresses.count; i++) {
  976.                 int point = TOY_AS_INTEGER(continueAddresses.literals[i]);
  977.                 tmpVal = jumpToStart + jumpOffsets;
  978.                 memcpy(compiler->bytecode + point, &tmpVal, sizeof(tmpVal));
  979.                 OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  980.             }
  981.  
  982.             //clear the stack after use
  983.             OP_PRINT("[%05d] ", compiler->count);
  984.             compiler->bytecode[compiler->count++] = TOY_OP_POP_STACK; //1 byte
  985.             OP_PRINT("POP_STACK %s\n", "");
  986.  
  987.             //cleanup
  988.             Toy_freeLiteralArray(&breakAddresses);
  989.             Toy_freeLiteralArray(&continueAddresses);
  990.         }
  991.         break;
  992.  
  993.         case TOY_AST_NODE_FOR: {
  994.             //for breaks and continues
  995.             Toy_LiteralArray breakAddresses;
  996.             Toy_LiteralArray continueAddresses;
  997.  
  998.             Toy_initLiteralArray(&breakAddresses);
  999.             Toy_initLiteralArray(&continueAddresses);
  1000.  
  1001.             OP_PRINT("[%05d] ", compiler->count);
  1002.             compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_BEGIN; //1 byte
  1003.             OP_PRINT("SCOPE_BEGIN %s\n", "");
  1004.  
  1005.             //initial setup
  1006.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->pathFor.preClause, &breakAddresses, &continueAddresses, jumpOffsets, rootNode);
  1007.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1008.                 OP_PRINT("[%05d] ", compiler->count);
  1009.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1010.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1011.             }
  1012.  
  1013.             //conditional
  1014.             unsigned short jumpToStart = compiler->count;
  1015.             override = Toy_writeCompilerWithJumps(compiler, node->pathFor.condition, &breakAddresses, &continueAddresses, jumpOffsets, rootNode);
  1016.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1017.                 OP_PRINT("[%05d] ", compiler->count);
  1018.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1019.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1020.             }
  1021.  
  1022.             //if false jump to end
  1023.             OP_PRINT("[%05d] ", compiler->count);
  1024.             compiler->bytecode[compiler->count++] = TOY_OP_IF_FALSE_JUMP; //1 byte
  1025.             OP_PRINT("IF_FALSE_JUMP %s\n", "");
  1026.             unsigned short jumpToEnd = compiler->count;
  1027.             compiler->count += sizeof(unsigned short); //2 bytes
  1028.  
  1029.             //write the body
  1030.             OP_PRINT("[%05d] ", compiler->count);
  1031.             compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_BEGIN; //1 byte
  1032.             OP_PRINT("SCOPE_BEGIN %s\n", "");
  1033.             override = Toy_writeCompilerWithJumps(compiler, node->pathFor.thenPath, &breakAddresses, &continueAddresses, jumpOffsets, rootNode);
  1034.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1035.                 OP_PRINT("[%05d] ", compiler->count);
  1036.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1037.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1038.             }
  1039.             OP_PRINT("[%05d] ", compiler->count);
  1040.             compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_END; //1 byte
  1041.             OP_PRINT("SCOPE_END %s\n", "");
  1042.  
  1043.             //for-breaks actually jump to the bottom
  1044.             int jumpToIncrement = compiler->count;
  1045.  
  1046.             //evaluate third clause, restart
  1047.             override = Toy_writeCompilerWithJumps(compiler, node->pathFor.postClause, &breakAddresses, &continueAddresses, jumpOffsets, rootNode);
  1048.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1049.                 OP_PRINT("[%05d] ", compiler->count);
  1050.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1051.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1052.             }
  1053.  
  1054.             OP_PRINT("[%05d] ", compiler->count);
  1055.             compiler->bytecode[compiler->count++] = TOY_OP_JUMP; //1 byte
  1056.             OP_PRINT("JUMP %s\n", "");
  1057.             unsigned short tmpVal = jumpToStart + jumpOffsets;
  1058.             memcpy(compiler->bytecode + compiler->count, &tmpVal, sizeof(tmpVal));
  1059.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  1060.             compiler->count += sizeof(unsigned short); //2 bytes
  1061.  
  1062.             tmpVal = compiler->count + jumpOffsets;
  1063.             memcpy(compiler->bytecode + jumpToEnd, &tmpVal, sizeof(tmpVal));
  1064.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);;
  1065.  
  1066.             OP_PRINT("[%05d] ", compiler->count);
  1067.             compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_END; //1 byte
  1068.             OP_PRINT("SCOPE_END %s\n", "");
  1069.  
  1070.             //set the breaks and continues
  1071.             for (int i = 0; i < breakAddresses.count; i++) {
  1072.                 int point = TOY_AS_INTEGER(breakAddresses.literals[i]);
  1073.                 tmpVal = compiler->count + jumpOffsets;
  1074.                 memcpy(compiler->bytecode + point, &tmpVal, sizeof(tmpVal));
  1075.                 OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  1076.             }
  1077.  
  1078.             for (int i = 0; i < continueAddresses.count; i++) {
  1079.                 int point = TOY_AS_INTEGER(continueAddresses.literals[i]);
  1080.                 tmpVal = jumpToIncrement + jumpOffsets;
  1081.                 memcpy(compiler->bytecode + point, &tmpVal, sizeof(tmpVal));
  1082.                 OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  1083.             }
  1084.  
  1085.             //clear the stack after use
  1086.             OP_PRINT("[%05d] ", compiler->count);
  1087.             compiler->bytecode[compiler->count++] = TOY_OP_POP_STACK; //1 byte
  1088.             OP_PRINT("POP_STACK %s\n", "");
  1089.  
  1090.             //cleanup
  1091.             Toy_freeLiteralArray(&breakAddresses);
  1092.             Toy_freeLiteralArray(&continueAddresses);
  1093.         }
  1094.         break;
  1095.  
  1096.         case TOY_AST_NODE_BREAK: {
  1097.             if (!breakAddressesPtr) {
  1098.                 fprintf(stderr,
  1099.                 TOY_CC_ERROR "[internal] Can't place a break statement here\n" TOY_CC_RESET);
  1100.                 break;
  1101.             }
  1102.  
  1103.             //insert into bytecode
  1104.             OP_PRINT("[%05d] ", compiler->count);
  1105.             compiler->bytecode[compiler->count++] = TOY_OP_JUMP; //1 byte
  1106.             OP_PRINT("JUMP %s\n", "");
  1107.  
  1108.             //push to the breakAddresses array
  1109.             Toy_Literal literal = TOY_TO_INTEGER_LITERAL(compiler->count);
  1110.             Toy_pushLiteralArray((Toy_LiteralArray*) breakAddressesPtr, literal);
  1111.             Toy_freeLiteral(literal);
  1112.  
  1113.             compiler->count += sizeof(unsigned short); //2 bytes
  1114.         }
  1115.         break;
  1116.  
  1117.         case TOY_AST_NODE_CONTINUE: {
  1118.             if (!continueAddressesPtr) {
  1119.                 fprintf(stderr,
  1120.                 TOY_CC_ERROR "[internal] Can't place a continue statement here\n" TOY_CC_RESET);
  1121.                 break;
  1122.             }
  1123.  
  1124.             //insert into bytecode
  1125.             OP_PRINT("[%05d] ", compiler->count);
  1126.             compiler->bytecode[compiler->count++] = TOY_OP_JUMP; //1 byte
  1127.             OP_PRINT("JUMP %s\n", "");
  1128.  
  1129.             //push to the continueAddresses array
  1130.             Toy_Literal literal = TOY_TO_INTEGER_LITERAL(compiler->count);
  1131.             Toy_pushLiteralArray((Toy_LiteralArray*) continueAddressesPtr, literal);
  1132.             Toy_freeLiteral(literal);
  1133.  
  1134.             compiler->count += sizeof(unsigned short); //2 bytes
  1135.         }
  1136.         break;
  1137.  
  1138.         case TOY_AST_NODE_AND: {
  1139.             //process the lhs
  1140.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->pathAnd.left, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  1141.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1142.                 OP_PRINT("[%05d] ", compiler->count);
  1143.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1144.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1145.             }
  1146.  
  1147.             //insert the AND opcode to signal a possible jump
  1148.             OP_PRINT("[%05d] ", compiler->count);
  1149.             compiler->bytecode[compiler->count++] = TOY_OP_AND; //1 byte
  1150.             OP_PRINT("AND %s\n", "");
  1151.             int jumpToEnd = compiler->count;
  1152.             compiler->count += sizeof(unsigned short); //2 bytes
  1153.  
  1154.             //process the rhs
  1155.             override = Toy_writeCompilerWithJumps(compiler, node->pathAnd.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  1156.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1157.                 OP_PRINT("[%05d] ", compiler->count);
  1158.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1159.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1160.             }
  1161.  
  1162.             //set the spot to jump to, to proceed
  1163.             unsigned short tmpVal = compiler->count + jumpOffsets;
  1164.             memcpy(compiler->bytecode + jumpToEnd, &tmpVal, sizeof(tmpVal));
  1165.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  1166.         }
  1167.         break;
  1168.  
  1169.         case TOY_AST_NODE_OR: {
  1170.             //process the lhs
  1171.             Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->pathOr.left, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  1172.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1173.                 OP_PRINT("[%05d] ", compiler->count);
  1174.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1175.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1176.             }
  1177.  
  1178.             //insert the AND opcode to signal a possible jump
  1179.             OP_PRINT("[%05d] ", compiler->count);
  1180.             compiler->bytecode[compiler->count++] = TOY_OP_OR; //1 byte
  1181.             OP_PRINT("OR %s\n", "");
  1182.             int jumpToEnd = compiler->count;
  1183.             compiler->count += sizeof(unsigned short); //2 bytes
  1184.  
  1185.             //process the rhs
  1186.             override = Toy_writeCompilerWithJumps(compiler, node->pathOr.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  1187.             if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1188.                 OP_PRINT("[%05d] ", compiler->count);
  1189.                 compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1190.                 OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1191.             }
  1192.  
  1193.             //set the spot to jump to, to proceed
  1194.             unsigned short tmpVal = compiler->count + jumpOffsets;
  1195.             memcpy(compiler->bytecode + jumpToEnd, &tmpVal, sizeof(tmpVal));
  1196.             OP_PRINT("[%d: %s]\n", (unsigned char ) tmpVal, OP_STR[(unsigned char ) tmpVal]);
  1197.         }
  1198.         break;
  1199.  
  1200.         case TOY_AST_NODE_FN_RETURN: {
  1201.             //read each returned literal onto the stack, and return the number of values to return
  1202.             for (int i = 0; i < node->returns.returns->fnCollection.count; i++) {
  1203.                 Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, &node->returns.returns->fnCollection.nodes[i], breakAddressesPtr,
  1204.                         continueAddressesPtr, jumpOffsets, rootNode);
  1205.                 if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1206.                     OP_PRINT("[%05d] ", compiler->count);
  1207.                     compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1208.                     OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1209.                 }
  1210.             }
  1211.  
  1212.             //push the return, with the number of literals
  1213.             OP_PRINT("[%05d] ", compiler->count);
  1214.             compiler->bytecode[compiler->count++] = TOY_OP_FN_RETURN; //1 byte
  1215.             OP_PRINT("FN_RETURN %s\n", "");
  1216.  
  1217.             memcpy(compiler->bytecode + compiler->count, &node->returns.returns->fnCollection.count, sizeof(unsigned short));
  1218.             OP_PRINT("[%d: %s]\n", node->returns.returns->fnCollection.count, OP_STR[node->returns.returns->fnCollection.count]);
  1219.             compiler->count += sizeof(unsigned short);
  1220.         }
  1221.         break;
  1222.  
  1223.         case TOY_AST_NODE_PREFIX_INCREMENT: {
  1224.             //push the literal to the stack (twice: add + assign)
  1225.             writeLiteralToCompiler(compiler, node->prefixIncrement.identifier);
  1226.             writeLiteralToCompiler(compiler, node->prefixIncrement.identifier);
  1227.  
  1228.             //push the increment / decrement
  1229.             Toy_Literal increment = TOY_TO_INTEGER_LITERAL(1);
  1230.             writeLiteralToCompiler(compiler, increment);
  1231.  
  1232.             //push the add opcode
  1233.             OP_PRINT("[%05d] ", compiler->count);
  1234.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_ADDITION; //1 byte
  1235.             OP_PRINT("ADDITION %s\n", "");
  1236.  
  1237.             //push the assign
  1238.             OP_PRINT("[%05d] ", compiler->count);
  1239.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_VAR_ASSIGN; //1 byte
  1240.             OP_PRINT("VAR_ASSIGN %s\n", "");
  1241.  
  1242.             //leave the result on the stack
  1243.             writeLiteralToCompiler(compiler, node->prefixIncrement.identifier);
  1244.             OP_PRINT("[%05d] ", compiler->count);
  1245.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_LITERAL_RAW; //1 byte
  1246.             OP_PRINT("LITERAL_RAW %s\n", "");
  1247.         }
  1248.         break;
  1249.  
  1250.         case TOY_AST_NODE_PREFIX_DECREMENT: {
  1251.             //push the literal to the stack (twice: add + assign)
  1252.             writeLiteralToCompiler(compiler, node->prefixDecrement.identifier);
  1253.             writeLiteralToCompiler(compiler, node->prefixDecrement.identifier);
  1254.  
  1255.             //push the increment / decrement
  1256.             Toy_Literal increment = TOY_TO_INTEGER_LITERAL(1);
  1257.             writeLiteralToCompiler(compiler, increment);
  1258.  
  1259.             //push the subtract opcode
  1260.             OP_PRINT("[%05d] ", compiler->count);
  1261.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_SUBTRACTION; //1 byte
  1262.             OP_PRINT("SUBSTRACTION %s\n", "");
  1263.  
  1264.             //push the assign
  1265.             OP_PRINT("[%05d] ", compiler->count);
  1266.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_VAR_ASSIGN; //1 byte
  1267.             OP_PRINT("VAR_ASSIGN %s\n", "");
  1268.  
  1269.             //leave the result on the stack
  1270.             writeLiteralToCompiler(compiler, node->prefixDecrement.identifier);
  1271.             OP_PRINT("[%05d] ", compiler->count);
  1272.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_LITERAL_RAW; //1 byte
  1273.             OP_PRINT("LITERAL_RAW %s\n", "");
  1274.         }
  1275.         break;
  1276.  
  1277.         case TOY_AST_NODE_POSTFIX_INCREMENT: {
  1278.             //push the identifier's VALUE to the stack
  1279.             writeLiteralToCompiler(compiler, node->postfixIncrement.identifier);
  1280.             OP_PRINT("[%05d] ", compiler->count);
  1281.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_LITERAL_RAW; //1 byte
  1282.             OP_PRINT("LITERAL_RAW %s\n", "");
  1283.  
  1284.             //push the identifier (twice: add + assign)
  1285.             writeLiteralToCompiler(compiler, node->postfixIncrement.identifier);
  1286.             writeLiteralToCompiler(compiler, node->postfixIncrement.identifier);
  1287.  
  1288.             //push the increment / decrement
  1289.             Toy_Literal increment = TOY_TO_INTEGER_LITERAL(1);
  1290.             writeLiteralToCompiler(compiler, increment);
  1291.  
  1292.             //push the add opcode
  1293.             OP_PRINT("[%05d] ", compiler->count);
  1294.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_ADDITION; //1 byte
  1295.             OP_PRINT("ADDITION %s\n", "");
  1296.  
  1297.             //push the assign
  1298.             OP_PRINT("[%05d] ", compiler->count);
  1299.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_VAR_ASSIGN; //1 byte
  1300.             OP_PRINT("VAR_ASSIGN %s\n", "");
  1301.         }
  1302.         break;
  1303.  
  1304.         case TOY_AST_NODE_POSTFIX_DECREMENT: {
  1305.             //push the identifier's VALUE to the stack
  1306.             writeLiteralToCompiler(compiler, node->postfixDecrement.identifier);
  1307.             OP_PRINT("[%05d] ", compiler->count);
  1308.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_LITERAL_RAW; //1 byte
  1309.             OP_PRINT("LITERAL_RAW %s\n", "");
  1310.  
  1311.             //push the identifier (twice: add + assign)
  1312.             writeLiteralToCompiler(compiler, node->postfixDecrement.identifier);
  1313.             writeLiteralToCompiler(compiler, node->postfixDecrement.identifier);
  1314.  
  1315.             //push the increment / decrement
  1316.             Toy_Literal increment = TOY_TO_INTEGER_LITERAL(1);
  1317.             writeLiteralToCompiler(compiler, increment);
  1318.  
  1319.             //push the subtract opcode
  1320.             OP_PRINT("[%05d] ", compiler->count);
  1321.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_SUBTRACTION; //1 byte
  1322.             OP_PRINT("SUBSTRACTION %s\n", "");
  1323.  
  1324.             //push the assign
  1325.             OP_PRINT("[%05d] ", compiler->count);
  1326.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_VAR_ASSIGN; //1 byte
  1327.             OP_PRINT("VAR_ASSIGN %s\n", "");
  1328.         }
  1329.         break;
  1330.  
  1331.         case TOY_AST_NODE_IMPORT: {
  1332.             //push the identifier, and the alias
  1333.             writeLiteralToCompiler(compiler, node->import.identifier);
  1334.             writeLiteralToCompiler(compiler, node->import.alias);
  1335.  
  1336.             //push the import opcode
  1337.             OP_PRINT("[%05d] ", compiler->count);
  1338.             compiler->bytecode[compiler->count++] = (unsigned char) TOY_OP_IMPORT; //1 byte
  1339.             OP_PRINT("IMPORT %s\n", "");
  1340.         }
  1341.         break;
  1342.  
  1343.         case TOY_AST_NODE_INDEX: {
  1344.             //pass to the child nodes, then embed the opcode
  1345.  
  1346.             //first
  1347.             if (!node->index.first) {
  1348.                 writeLiteralToCompiler(compiler, TOY_TO_NULL_LITERAL);
  1349.             } else {
  1350.                 Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->index.first, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  1351.                 if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1352.                     OP_PRINT("[%05d] ", compiler->count);
  1353.                     compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1354.                     OP_PRINT("[%d: %s]\n", override, OP_STR[override]);
  1355.                 }
  1356.             }
  1357.  
  1358.             //second
  1359.             if (!node->index.second) {
  1360.                 writeLiteralToCompiler(compiler, TOY_TO_NULL_LITERAL);
  1361.             } else {
  1362.                 Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->index.second, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  1363.                 if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1364.                     OP_PRINT("[%05d] ", compiler->count);
  1365.                     compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1366.                     OP_PRINT("[%d: %s]\n", override, OP_STR[override]);
  1367.                 }
  1368.             }
  1369.  
  1370.             //third
  1371.             if (!node->index.third) {
  1372.                 writeLiteralToCompiler(compiler, TOY_TO_NULL_LITERAL);
  1373.             } else {
  1374.                 Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, node->index.third, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
  1375.                 if (override != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1376.                     OP_PRINT("[%05d] ", compiler->count);
  1377.                     compiler->bytecode[compiler->count++] = (unsigned char) override; //1 byte
  1378.                     OP_PRINT("[%d: %s]\n", (unsigned char ) override, OP_STR[(unsigned char ) override]);
  1379.                 }
  1380.             }
  1381.  
  1382.             // OP_PRINT("[%05d] ", compiler->count);compiler->bytecode[compiler->count++] = (unsigned char)OP_INDEX; //1 byte
  1383.  
  1384.             return TOY_OP_INDEX_ASSIGN; //override binary's instruction IF it is assign
  1385.         }
  1386.         break;
  1387.  
  1388.         case TOY_AST_NODE_PASS: {
  1389.             return TOY_OP_PASS;
  1390.         }
  1391.         break;
  1392.     }
  1393.  
  1394.     return TOY_OP_EOF;
  1395. }
  1396.  
  1397. void Toy_writeCompiler(Toy_Compiler *compiler, Toy_ASTNode *node) {
  1398.     Toy_Opcode op = Toy_writeCompilerWithJumps(compiler, node, NULL, NULL, 0, node); //pass in "node" as the root node
  1399.  
  1400.     if (op != TOY_OP_EOF) { //compensate for indexing & dot notation being screwy
  1401.         OP_PRINT("[%05d] ", compiler->count);
  1402.         compiler->bytecode[compiler->count++] = (unsigned char) op; //1 byte
  1403.         OP_PRINT("[%d: %s]\n", op, OP_STR[op]);
  1404.     }
  1405.  
  1406.     //TODO: could free up AST Nodes
  1407. }
  1408.  
  1409. void Toy_freeCompiler(Toy_Compiler *compiler) {
  1410.     Toy_freeLiteralArray(&compiler->literalCache);
  1411.     TOY_FREE_ARRAY(unsigned char, compiler->bytecode, compiler->capacity);
  1412.     compiler->bytecode = NULL;
  1413.     compiler->capacity = 0;
  1414.     compiler->count = 0;
  1415.     compiler->panic = false;
  1416. }
  1417.  
  1418. static void emitByte(unsigned char **collationPtr, int *capacityPtr, int *countPtr, unsigned char byte) {
  1419.     //grow the array
  1420.     if (*countPtr + 1 > *capacityPtr) {
  1421.         int oldCapacity = *capacityPtr;
  1422.         *capacityPtr = TOY_GROW_CAPACITY(*capacityPtr);
  1423.         *collationPtr = TOY_GROW_ARRAY(unsigned char, *collationPtr, oldCapacity, *capacityPtr);
  1424.     }
  1425.  
  1426.     //append to the collation
  1427.     (*collationPtr)[(*countPtr)++] = byte;
  1428. }
  1429.  
  1430. static void Toy_emitShort(unsigned char **collationPtr, int *capacityPtr, int *countPtr, unsigned short bytes) {
  1431.     char *ptr = (char*) &bytes;
  1432.  
  1433.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1434.     ptr++;
  1435.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1436. }
  1437.  
  1438. static void emitInt(unsigned char **collationPtr, int *capacityPtr, int *countPtr, int bytes) {
  1439.     char *ptr = (char*) &bytes;
  1440.  
  1441.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1442.     ptr++;
  1443.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1444.     ptr++;
  1445.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1446.     ptr++;
  1447.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1448. }
  1449.  
  1450. static void emitFloat(unsigned char **collationPtr, int *capacityPtr, int *countPtr, float bytes) {
  1451.     char *ptr = (char*) &bytes;
  1452.  
  1453.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1454.     ptr++;
  1455.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1456.     ptr++;
  1457.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1458.     ptr++;
  1459.     emitByte(collationPtr, capacityPtr, countPtr, *ptr);
  1460. }
  1461.  
  1462. //return the result
  1463. static unsigned char* collateCompilerHeaderOpt(Toy_Compiler *compiler, size_t *size, bool embedHeader) {
  1464.     if (compiler->panic) {
  1465.         fprintf(stderr,
  1466.         TOY_CC_ERROR "[internal] Can't collate a panicked compiler\n" TOY_CC_RESET);
  1467.         return NULL;
  1468.     }
  1469.  
  1470.     int capacity = TOY_GROW_CAPACITY(0);
  1471.     int count = 0;
  1472.     unsigned char *collation = TOY_ALLOCATE(unsigned char, capacity);
  1473.  
  1474.     //for the function-section at the end of the main-collation
  1475.     int fnIndex = 0; //counts up for each fn
  1476.     int fnCapacity = TOY_GROW_CAPACITY(0);
  1477.     int fnCount = 0;
  1478.     unsigned char *fnCollation = TOY_ALLOCATE(unsigned char, fnCapacity);
  1479.  
  1480.     if (embedHeader) {
  1481.         //embed the header with version information
  1482.         emitByte(&collation, &capacity, &count, TOY_VERSION_MAJOR);
  1483.         emitByte(&collation, &capacity, &count, TOY_VERSION_MINOR);
  1484.         emitByte(&collation, &capacity, &count, TOY_VERSION_PATCH);
  1485.  
  1486.         //embed the build info
  1487.         if ((int) strlen(TOY_VERSION_BUILD) + count + 1 > capacity) {
  1488.             int oldCapacity = capacity;
  1489.             capacity = strlen(TOY_VERSION_BUILD) + count + 1; //full header size
  1490.             collation = TOY_GROW_ARRAY(unsigned char, collation, oldCapacity, capacity);
  1491.         }
  1492.  
  1493.         memcpy(&collation[count], TOY_VERSION_BUILD, strlen(TOY_VERSION_BUILD));
  1494.         count += strlen(TOY_VERSION_BUILD);
  1495.         collation[count++] = '\0'; //terminate the build string
  1496.  
  1497.         emitByte(&collation, &capacity, &count, TOY_OP_SECTION_END); //terminate header
  1498.     }
  1499.  
  1500.     //embed the data section (first short is the number of literals)
  1501.     Toy_emitShort(&collation, &capacity, &count, compiler->literalCache.count);
  1502.  
  1503.     //emit each literal by type
  1504.     for (int i = 0; i < compiler->literalCache.count; i++) {
  1505.         //literal Opcode
  1506.         // emitShort(&collation, &capacity, &count, OP_LITERAL); //This isn't needed
  1507.  
  1508.         //literal type, followed by literal value
  1509.         switch (compiler->literalCache.literals[i].type) {
  1510.             case TOY_LITERAL_NULL:
  1511.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_NULL);
  1512.                 //null has no following value
  1513.             break;
  1514.  
  1515.             case TOY_LITERAL_BOOLEAN:
  1516.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_BOOLEAN);
  1517.                 emitByte(&collation, &capacity, &count, TOY_AS_BOOLEAN(compiler->literalCache.literals[i]));
  1518.             break;
  1519.  
  1520.             case TOY_LITERAL_INTEGER:
  1521.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_INTEGER);
  1522.                 emitInt(&collation, &capacity, &count, TOY_AS_INTEGER(compiler->literalCache.literals[i]));
  1523.             break;
  1524.  
  1525.             case TOY_LITERAL_FLOAT:
  1526.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_FLOAT);
  1527.                 emitFloat(&collation, &capacity, &count, TOY_AS_FLOAT(compiler->literalCache.literals[i]));
  1528.             break;
  1529.  
  1530.             case TOY_LITERAL_STRING: {
  1531.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_STRING);
  1532.  
  1533.                 Toy_Literal str = compiler->literalCache.literals[i];
  1534.  
  1535.                 for (int c = 0; c < (int) Toy_lengthRefString(TOY_AS_STRING(str)); c++) {
  1536.                     emitByte(&collation, &capacity, &count, Toy_toCString(TOY_AS_STRING(str))[c]);
  1537.                 }
  1538.  
  1539.                 emitByte(&collation, &capacity, &count, '\0'); //terminate the string
  1540.             }
  1541.             break;
  1542.  
  1543.             case TOY_LITERAL_ARRAY: {
  1544.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_ARRAY);
  1545.  
  1546.                 Toy_LiteralArray *ptr = TOY_AS_ARRAY(compiler->literalCache.literals[i]);
  1547.  
  1548.                 //length of the array, as a short
  1549.                 Toy_emitShort(&collation, &capacity, &count, ptr->count);
  1550.  
  1551.                 //each element of the array
  1552.                 for (int i = 0; i < ptr->count; i++) {
  1553.                     Toy_emitShort(&collation, &capacity, &count, (unsigned short) TOY_AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
  1554.                 }
  1555.             }
  1556.             break;
  1557.  
  1558.             case TOY_LITERAL_ARRAY_INTERMEDIATE: {
  1559.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_ARRAY_INTERMEDIATE);
  1560.  
  1561.                 Toy_LiteralArray *ptr = TOY_AS_ARRAY(compiler->literalCache.literals[i]);
  1562.  
  1563.                 //length of the array, as a short
  1564.                 Toy_emitShort(&collation, &capacity, &count, ptr->count);
  1565.  
  1566.                 //each element of the array
  1567.                 for (int i = 0; i < ptr->count; i++) {
  1568.                     Toy_emitShort(&collation, &capacity, &count, (unsigned short) TOY_AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
  1569.                 }
  1570.             }
  1571.             break;
  1572.  
  1573.             case TOY_LITERAL_DICTIONARY: {
  1574.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_DICTIONARY);
  1575.  
  1576.                 Toy_LiteralArray *ptr = TOY_AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
  1577.  
  1578.                 //length of the array, as a short
  1579.                 Toy_emitShort(&collation, &capacity, &count, ptr->count); //count is the array size, NOT the dictionary size
  1580.  
  1581.                 //each element of the array
  1582.                 for (int i = 0; i < ptr->count; i++) {
  1583.                     Toy_emitShort(&collation, &capacity, &count, (unsigned short) TOY_AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
  1584.                 }
  1585.             }
  1586.             break;
  1587.  
  1588.             case TOY_LITERAL_DICTIONARY_INTERMEDIATE: {
  1589.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_DICTIONARY_INTERMEDIATE);
  1590.  
  1591.                 Toy_LiteralArray *ptr = TOY_AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
  1592.  
  1593.                 //length of the array, as a short
  1594.                 Toy_emitShort(&collation, &capacity, &count, ptr->count); //count is the array size, NOT the dictionary size
  1595.  
  1596.                 //each element of the array
  1597.                 for (int i = 0; i < ptr->count; i++) {
  1598.                     Toy_emitShort(&collation, &capacity, &count, (unsigned short) TOY_AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the values
  1599.                 }
  1600.             }
  1601.             break;
  1602.  
  1603.             case TOY_LITERAL_FUNCTION_INTERMEDIATE: {
  1604.                 //extract the compiler
  1605.                 Toy_Literal fn = compiler->literalCache.literals[i];
  1606.                 void *fnCompiler = fn.as.generic; //store the compiler here for now
  1607.  
  1608.                 //collate the function into bytecode (without header)
  1609.                 size_t size = 0;
  1610.                 unsigned char *bytes = collateCompilerHeaderOpt((Toy_Compiler*) fnCompiler, &size, false);
  1611.  
  1612.                 //emit how long this section is, +1 for ending mark
  1613.                 Toy_emitShort(&fnCollation, &fnCapacity, &fnCount, (unsigned short) size + 1);
  1614.  
  1615.                 //write the fn to the fn collation
  1616.                 for (size_t i = 0; i < size; i++) {
  1617.                     emitByte(&fnCollation, &fnCapacity, &fnCount, bytes[i]);
  1618.                 }
  1619.  
  1620.                 emitByte(&fnCollation, &fnCapacity, &fnCount, TOY_OP_FN_END); //for marking the correct end-point of the function
  1621.  
  1622.                 //embed the reference to the function implementation into the current collation (to be extracted later)
  1623.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_FUNCTION);
  1624.                 Toy_emitShort(&collation, &capacity, &count, (unsigned short) (fnIndex++));
  1625.  
  1626.                 Toy_freeCompiler((Toy_Compiler*) fnCompiler);
  1627.                 TOY_FREE(compiler, fnCompiler);
  1628.                 TOY_FREE_ARRAY(unsigned char, bytes, size);
  1629.             }
  1630.             break;
  1631.  
  1632.             case TOY_LITERAL_IDENTIFIER: {
  1633.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_IDENTIFIER);
  1634.  
  1635.                 Toy_Literal identifier = compiler->literalCache.literals[i];
  1636.  
  1637.                 for (int c = 0; c < (int) Toy_lengthRefString(TOY_AS_IDENTIFIER(identifier)); c++) {
  1638.                     emitByte(&collation, &capacity, &count, Toy_toCString(TOY_AS_IDENTIFIER(identifier))[c]);
  1639.                 }
  1640.  
  1641.                 emitByte(&collation, &capacity, &count, '\0'); //terminate the string
  1642.             }
  1643.             break;
  1644.  
  1645.             case TOY_LITERAL_TYPE: {
  1646.                 //push a raw type
  1647.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_TYPE);
  1648.  
  1649.                 Toy_Literal typeLiteral = compiler->literalCache.literals[i];
  1650.  
  1651.                 //what type this literal represents
  1652.                 emitByte(&collation, &capacity, &count,
  1653.                 TOY_AS_TYPE(typeLiteral).typeOf);
  1654.                 emitByte(&collation, &capacity, &count,
  1655.                 TOY_AS_TYPE(typeLiteral).constant); //if it's constant
  1656.             }
  1657.             break;
  1658.  
  1659.             case TOY_LITERAL_TYPE_INTERMEDIATE: {
  1660.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_TYPE_INTERMEDIATE);
  1661.  
  1662.                 Toy_LiteralArray *ptr = TOY_AS_ARRAY(compiler->literalCache.literals[i]); //used an array for storage above
  1663.  
  1664.                 //the base literal
  1665.                 Toy_Literal typeLiteral = Toy_copyLiteral(ptr->literals[0]);
  1666.  
  1667.                 //what type this literal represents
  1668.                 emitByte(&collation, &capacity, &count,
  1669.                 TOY_AS_TYPE(typeLiteral).typeOf);
  1670.                 emitByte(&collation, &capacity, &count,
  1671.                 TOY_AS_TYPE(typeLiteral).constant); //if it's constant
  1672.  
  1673.                 //each element of the array, If they exist, representing sub-types already in the cache
  1674.                 if (TOY_AS_TYPE(typeLiteral).typeOf == TOY_LITERAL_ARRAY || TOY_AS_TYPE(typeLiteral).typeOf == TOY_LITERAL_DICTIONARY) {
  1675.                     //the type will represent how many to expect in the array
  1676.                     for (int i = 1; i < ptr->count; i++) {
  1677.                         Toy_emitShort(&collation, &capacity, &count, (unsigned short) TOY_AS_INTEGER(ptr->literals[i])); //shorts representing the indexes of the types
  1678.                     }
  1679.                 }
  1680.  
  1681.                 Toy_freeLiteral(typeLiteral);
  1682.             }
  1683.             break;
  1684.  
  1685.             case TOY_LITERAL_INDEX_BLANK:
  1686.                 emitByte(&collation, &capacity, &count, TOY_LITERAL_INDEX_BLANK);
  1687.                 //blank has no following value
  1688.             break;
  1689.  
  1690.             default:
  1691.                 fprintf(stderr,
  1692.                 TOY_CC_ERROR "[internal] Unknown literal type encountered within literal cache: %d\n" TOY_CC_RESET, compiler->literalCache.literals[i].type);
  1693.                 return NULL;
  1694.         }
  1695.     }
  1696.  
  1697.     emitByte(&collation, &capacity, &count, TOY_OP_SECTION_END); //terminate data
  1698.  
  1699.     //embed the function section (beginning with function count, size)
  1700.     Toy_emitShort(&collation, &capacity, &count, fnIndex);
  1701.     Toy_emitShort(&collation, &capacity, &count, fnCount);
  1702.  
  1703.     for (int i = 0; i < fnCount; i++) {
  1704.         emitByte(&collation, &capacity, &count, fnCollation[i]);
  1705.     }
  1706.  
  1707.     emitByte(&collation, &capacity, &count, TOY_OP_SECTION_END); //terminate function section
  1708.  
  1709.     TOY_FREE_ARRAY(unsigned char, fnCollation, fnCapacity); //clear the function stuff
  1710.  
  1711.     //code section
  1712.     for (int i = 0; i < compiler->count; i++) {
  1713.         emitByte(&collation, &capacity, &count, compiler->bytecode[i]);
  1714.     }
  1715.  
  1716.     emitByte(&collation, &capacity, &count, TOY_OP_SECTION_END); //terminate code
  1717.  
  1718.     emitByte(&collation, &capacity, &count, TOY_OP_EOF); //terminate bytecode
  1719.  
  1720.     //finalize
  1721.     collation = TOY_SHRINK_ARRAY(unsigned char, collation, capacity, count);
  1722.  
  1723.     *size = count;
  1724.  
  1725.     return collation;
  1726. }
  1727.  
  1728. //the whole point of the compiler is to alter bytecode, so leave it as non-const
  1729. unsigned char* Toy_collateCompiler(Toy_Compiler *compiler, size_t *size) {
  1730.     return collateCompilerHeaderOpt(compiler, size, true);
  1731. }
Tags: Toy
Advertisement
Add Comment
Please, Sign In to add comment