+ size_t baseOfs = Offset;
+
+ if(!Node) {
+ //fprintf(stderr, "Possible Bug - NULL passed to AST_WriteNode\n");
+ WRITE_32(Buffer, Offset, 0);
+ WRITE_16(Buffer, Offset, NODETYPE_NOP);
+ WRITE_16(Buffer, Offset, 0); // Line (0)
+ return 0;
+ }
+
+ WRITE_32(Buffer, Offset, 0); // Next
+ WRITE_16(Buffer, Offset, Node->Type);
+ // TODO: Scan the buffer for the location of the filename (with NULL byte)
+ // else, write the string at the end of the node
+ WRITE_16(Buffer, Offset, Node->Line); // Line
+ //WRITE_32(Buffer, Offset, 0); // File
+
+ switch(Node->Type)
+ {
+ // Block of code
+ case NODETYPE_BLOCK:
+ WRITE_NODELIST(Buffer, Offset, Node->Block.FirstChild);
+ break;
+
+ // Function Call
+ case NODETYPE_METHODCALL:
+ Offset += AST_WriteNode(Buffer, Offset, Node->FunctionCall.Object);
+ case NODETYPE_FUNCTIONCALL:
+ case NODETYPE_CREATEOBJECT:
+ // TODO: Search for the same function name and add a pointer
+ WRITE_STR(Buffer, Offset, Node->FunctionCall.Name);
+ WRITE_NODELIST(Buffer, Offset, Node->FunctionCall.FirstArg);
+ break;
+
+ // If node
+ case NODETYPE_IF:
+ Offset += AST_WriteNode(Buffer, Offset, Node->If.Condition);
+ Offset += AST_WriteNode(Buffer, Offset, Node->If.True);
+ Offset += AST_WriteNode(Buffer, Offset, Node->If.False);
+ break;
+
+ // Looping Construct (For loop node)
+ case NODETYPE_LOOP:
+ WRITE_8(Buffer, Offset, Node->For.bCheckAfter);
+ WRITE_STR(Buffer, Offset, Node->For.Tag);
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Init);
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Condition);
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Increment);
+ Offset += AST_WriteNode(Buffer, Offset, Node->For.Code);
+ break;
+
+ // Asignment
+ case NODETYPE_ASSIGN:
+ WRITE_8(Buffer, Offset, Node->Assign.Operation);
+ Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Dest);
+ Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Value);
+ break;
+
+ // Casting
+ case NODETYPE_CAST:
+ WRITE_8(Buffer, Offset, Node->Cast.DataType);
+ Offset += AST_WriteNode(Buffer, Offset, Node->Cast.Value);
+ break;
+
+ // Define a variable
+ case NODETYPE_DEFVAR:
+ WRITE_8(Buffer, Offset, Node->DefVar.DataType);
+ // TODO: Duplicate compress the strings
+ WRITE_STR(Buffer, Offset, Node->DefVar.Name);
+
+ WRITE_NODELIST(Buffer, Offset, Node->DefVar.LevelSizes);
+ Offset += AST_WriteNode(Buffer, Offset, Node->DefVar.InitialValue);
+ break;
+
+ // Scope Reference
+ case NODETYPE_SCOPE:
+ case NODETYPE_ELEMENT:
+ WRITE_STR(Buffer, Offset, Node->Scope.Name);
+ Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
+ break;
+
+ // Unary Operations
+ case NODETYPE_RETURN:
+ case NODETYPE_BWNOT:
+ case NODETYPE_LOGICALNOT:
+ case NODETYPE_NEGATE:
+ case NODETYPE_POSTINC:
+ case NODETYPE_POSTDEC:
+ Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
+ break;
+
+ // Binary Operations
+ case NODETYPE_INDEX:
+ case NODETYPE_ADD:
+ case NODETYPE_SUBTRACT:
+ case NODETYPE_MULTIPLY:
+ case NODETYPE_DIVIDE:
+ case NODETYPE_MODULO:
+ case NODETYPE_BITSHIFTLEFT:
+ case NODETYPE_BITSHIFTRIGHT:
+ case NODETYPE_BITROTATELEFT:
+ case NODETYPE_BWAND: case NODETYPE_LOGICALAND:
+ case NODETYPE_BWOR: case NODETYPE_LOGICALOR:
+ case NODETYPE_BWXOR: case NODETYPE_LOGICALXOR:
+ case NODETYPE_EQUALS:
+ case NODETYPE_LESSTHAN: case NODETYPE_LESSTHANEQUAL:
+ case NODETYPE_GREATERTHAN: case NODETYPE_GREATERTHANEQUAL:
+ Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Left);
+ Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Right);
+ break;
+
+ // Node types with no children
+ case NODETYPE_NOP:
+ break;
+ case NODETYPE_VARIABLE:
+ case NODETYPE_CONSTANT:
+ case NODETYPE_BREAK:
+ case NODETYPE_CONTINUE:
+ // TODO: De-Duplicate the strings
+ WRITE_STR(Buffer, Offset, Node->Variable.Name);
+ break;
+ case NODETYPE_STRING:
+ WRITE_32(Buffer, Offset, Node->Constant.String.Length);
+ WRITE_N(Buffer, Offset, Node->Constant.String.Length, Node->Constant.String.Data);
+ break;
+ case NODETYPE_INTEGER:
+ WRITE_64(Buffer, Offset, Node->Constant.Integer);
+ break;
+ case NODETYPE_REAL:
+ WRITE_REAL(Buffer, Offset, Node->Constant.Real);
+ break;
+
+ //default:
+ // fprintf(stderr, "AST_WriteNode: Unknown node type %i\n", Node->Type);
+ // break;
+ }
+
+ return Offset - baseOfs;