Usermode/ld-acess - Fixing Elf64 support (and incorrect Uint* sizes)
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / exec_ast.c
index 0c1a402..7a94dc4 100644 (file)
@@ -7,19 +7,19 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
+#include "common.h"
 #include "ast.h"
 
 #define TRACE_VAR_LOOKUPS      0
 #define TRACE_NODE_RETURNS     0
 
 // === IMPORTS ===
-extern tSpiderFunction *gpExports_First;
 
 // === PROTOTYPES ===
 // - Node Execution
 tSpiderValue   *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
-tSpiderValue   *AST_ExecuteNode_BinOp(tAST_BlockState *Block, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right);
-tSpiderValue   *AST_ExecuteNode_UniOp(tAST_BlockState *Block, tAST_Node *Node, int Operation, tSpiderValue *Value);
+tSpiderValue   *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right);
+tSpiderValue   *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value);
 // - Variables
 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value);
  int   Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value);
@@ -33,332 +33,48 @@ void       AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
  int   giNextBlockIdent = 1;
 
 // === CODE ===
-/**
- * \brief Execute a script function
- * \param Script       Script context to execute in
- * \param Namespace    Namespace to search for the function
- * \param Function     Function name to execute
- * \param NArguments   Number of arguments to pass
- * \param Arguments    Arguments passed
- */
-tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
-       tSpiderNamespace *Namespace, const char *Function,
-       int NArguments, tSpiderValue **Arguments)
-{
-        int    bFound = 0;     // Used to keep nesting levels down
-       tSpiderValue    *ret = ERRPTR;
-       tSpiderFunction *fcn;
-       
-       // First: Find the function in the script
-       {
-               tAST_Function   *astFcn;
-               for( astFcn = Script->Script->Functions; astFcn; astFcn = astFcn->Next )
-               {
-                       if( strcmp(astFcn->Name, Function) == 0 )
-                               break;
-               }
-               // Execute!
-               if(astFcn)
-               {
-                       tAST_BlockState bs;
-                       tAST_Node       *arg;
-                        int    i = 0;
-                       
-                       // Build a block State
-                       bs.FirstVar = NULL;
-                       bs.RetVal = NULL;
-                       bs.Parent = NULL;
-                       bs.BaseNamespace = &Script->Variant->RootNamespace;
-                       bs.CurNamespace = NULL;
-                       bs.Script = Script;
-                       bs.Ident = giNextBlockIdent ++;
-                       
-                       // Parse arguments
-                       for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
-                       {
-                               if( i >= NArguments )   break;  // TODO: Return gracefully
-                               // TODO: Type checks
-                               Variable_Define(&bs,
-                                       arg->DefVar.DataType, arg->DefVar.Name,
-                                       Arguments[i]);
-                       }
-                       
-                       // Execute function
-                       ret = AST_ExecuteNode(&bs, astFcn->Code);
-                       if(ret != ERRPTR)
-                       {
-                               SpiderScript_DereferenceValue(ret);     // Dereference output of last block statement
-                               ret = bs.RetVal;        // Set to return value of block
-                       }
-                       bFound = 1;
-                       
-                       while(bs.FirstVar)
-                       {
-                               tAST_Variable   *nextVar = bs.FirstVar->Next;
-                               Variable_Destroy( bs.FirstVar );
-                               bs.FirstVar = nextVar;
-                       }
-               }
-       }
-       
-       // Didn't find it in script?
-       if(!bFound)
-       {
-               fcn = NULL;     // Just to allow the below code to be neat
-               
-               // Second: Scan current namespace
-               if( !fcn && Namespace )
-               {
-                       for( fcn = Namespace->Functions; fcn; fcn = fcn->Next )
-                       {
-                               if( strcmp( fcn->Name, Function ) == 0 )
-                                       break;
-                       }
-               }
-               
-               // Third: Search the variant's global exports
-               if( !fcn )
-               {
-                       for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
-                       {
-                               if( strcmp( fcn->Name, Function ) == 0 )
-                                       break;
-                       }
-               }
-               
-               // Fourth: Search language exports
-               if( !fcn )
-               {
-                       for( fcn = gpExports_First; fcn; fcn = fcn->Next )
-                       {
-                               if( strcmp( fcn->Name, Function ) == 0 )
-                                       break;
-                       }
-               }
-               
-               // Execute!
-               if(fcn)
-               {
-                       // TODO: Type Checking
-                       ret = fcn->Handler( Script, NArguments, Arguments );
-                       bFound = 1;
-               }
-       }
-       
-       // Not found?
-       if(!bFound)
-       {
-               fprintf(stderr, "Undefined reference to function '%s' (ns='%s')\n",
-                       Function, Namespace->Name);
-               return ERRPTR;
-       }
-       
-       return ret;
-}
-
-/**
- * \brief Execute an object method function
- * \param Script       Script context to execute in
- * \param Object       Object in which to find the method
- * \param MethodName   Name of method to call
- * \param NArguments   Number of arguments to pass
- * \param Arguments    Arguments passed
- */
-tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
-       tSpiderObject *Object, const char *MethodName,
-       int NArguments, tSpiderValue **Arguments)
+tSpiderValue *AST_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Arguments)
 {
-       tSpiderFunction *fcn;
-       tSpiderValue    this;
-       tSpiderValue    *newargs[NArguments+1];
-        int    i;
-       
-       // TODO: Support program defined objects
-       
-       // Search for the function
-       for( fcn = Object->Type->Methods; fcn; fcn = fcn->Next )
-       {
-               if( strcmp(fcn->Name, MethodName) == 0 )
-                       break;
-       }
-       // Error
-       if( !fcn )
-       {
-               AST_RuntimeError(NULL, "Class '%s' does not have a method '%s'",
-                       Object->Type->Name, MethodName);
-               return ERRPTR;
-       }
+       tAST_BlockState bs;
+       tSpiderValue    *ret;
+        int    i = 0;
        
-       // Create the "this" argument
-       this.Type = SS_DATATYPE_OBJECT;
-       this.ReferenceCount = 1;
-       this.Object = Object;
-       newargs[0] = &this;
-       memcpy(&newargs[1], Arguments, NArguments*sizeof(tSpiderValue*));
+       // Build a block State
+       bs.FirstVar = NULL;
+       bs.RetVal = NULL;
+       bs.Parent = NULL;
+       bs.BaseNamespace = &Script->Variant->RootNamespace;
+       bs.CurNamespace = NULL;
+       bs.Script = Script;
+       bs.Ident = giNextBlockIdent ++;
        
-       // Check the type of the arguments
-       for( i = 0; fcn->ArgTypes[i]; i ++ )
+       // Parse arguments
+       for( i = 0; i < Fcn->ArgumentCount; i ++ )
        {
-               if( i >= NArguments ) {
-                       for( ; fcn->ArgTypes[i]; i ++ ) ;
-                       AST_RuntimeError(NULL, "Argument count mismatch (%i passed, %i expected)",
-                               NArguments, i);
-                       return ERRPTR;
-               }
-               if( Arguments[i] && Arguments[i]->Type != fcn->ArgTypes[i] )
-               {
-                       AST_RuntimeError(NULL, "Argument type mismatch (%i, expected %i)",
-                               Arguments[i]->Type, fcn->ArgTypes[i]);
-                       return ERRPTR;
-               }
+               if( i >= NArguments )   break;  // TODO: Return gracefully
+               // TODO: Type checks
+               Variable_Define(&bs,
+                       Fcn->Arguments[i].Type, Fcn->Arguments[i].Name,
+                       Arguments[i]);
        }
-       
-       // Call handler
-       return fcn->Handler(Script, NArguments+1, newargs);
-}
-
-/**
- * \brief Execute a script function
- * \param Script       Script context to execute in
- * \param Function     Function name to execute
- * \param NArguments   Number of arguments to pass
- * \param Arguments    Arguments passed
- */
-tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
-       tSpiderNamespace *Namespace, const char *ClassName,
-       int NArguments, tSpiderValue **Arguments)
-{
-        int    bFound = 0;     // Used to keep nesting levels down
-       tSpiderValue    *ret = ERRPTR;
-       tSpiderObjectDef        *class;
-       
-       // First: Find the function in the script
-       // TODO: Implement script-defined classes
-       #if 0
-       {
-               tAST_Function   *astClass;
-               for( astClass = Script->Script->Classes; astClass; astClass = astClass->Next )
-               {
-                       if( strcmp(astClass->Name, ClassName) == 0 )
-                               break;
-               }
-               // Execute!
-               if(astClass)
-               {
-                       tAST_BlockState bs;
-                       tAST_Node       *arg;
-                        int    i = 0;
                        
-                       // Build a block State
-                       bs.FirstVar = NULL;
-                       bs.RetVal = NULL;
-                       bs.Parent = NULL;
-                       bs.BaseNamespace = &Script->Variant->RootNamespace;
-                       bs.CurNamespace = NULL;
-                       bs.Script = Script;
-                       bs.Ident = giNextBlockIdent ++;
-                       
-                       for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
-                       {
-                               if( i >= NArguments )   break;  // TODO: Return gracefully
-                               // TODO: Type checks
-                               Variable_Define(&bs,
-                                       arg->DefVar.DataType, arg->DefVar.Name,
-                                       Arguments[i]);
-                       }
-                       
-                       // Execute function
-                       ret = AST_ExecuteNode(&bs, astFcn->Code);
-                       if( ret != ERRPTR )
-                       {
-                               SpiderScript_DereferenceValue(ret);     // Dereference output of last block statement
-                               ret = bs.RetVal;        // Set to return value of block
-                       }
-                       bFound = 1;
-                       
-                       while(bs.FirstVar)
-                       {
-                               tAST_Variable   *nextVar = bs.FirstVar->Next;
-                               Variable_Destroy( bs.FirstVar );
-                               bs.FirstVar = nextVar;
-                       }
-               }
-       }
-       #endif
-       
-       // Didn't find it in script?
-       if(!bFound)
+       // Execute function
+       ret = AST_ExecuteNode(&bs, Fcn->ASTFcn);
+       if(ret != ERRPTR)
        {
-               class = NULL;   // Just to allow the below code to be neat
-               
-               //if( !Namespace )
-               //      Namespace = &Script->Variant->RootNamespace;
-               
-               // Second: Scan current namespace
-               if( !class && Namespace )
-               {
-                       for( class = Namespace->Classes; class; class = class->Next )
-                       {
-                               if( strcmp( class->Name, ClassName ) == 0 )
-                                       break;
-                       }
-               }
-               
-               #if 0
-               // Third: Search the variant's global exports
-               if( !class )
-               {
-                       for( class = Script->Variant->Classes; class; class = fcn->Next )
-                       {
-                               if( strcmp( class->Name, Function ) == 0 )
-                                       break;
-                       }
-               }
-               #endif
-               
-               #if 0
-               // Fourth: Search language exports
-               if( !class )
-               {
-                       for( class = gpExports_First; class; class = fcn->Next )
-                       {
-                               if( strcmp( class->Name, ClassName ) == 0 )
-                                       break;
-                       }
-               }
-               #endif
-               
-               // Execute!
-               if(class)
-               {
-                       tSpiderObject   *obj;
-                       // TODO: Type Checking
-                       
-                       // Call constructor
-                       obj = class->Constructor( NArguments, Arguments );
-                       if( obj == NULL || obj == ERRPTR )
-                               return (void *)obj;
-                       
-                       // Creatue return object
-                       ret = malloc( sizeof(tSpiderValue) );
-                       ret->Type = SS_DATATYPE_OBJECT;
-                       ret->ReferenceCount = 1;
-                       ret->Object = obj;
-                       bFound = 1;
-               }
+               SpiderScript_DereferenceValue(ret);     // Dereference output of last block statement
+               ret = bs.RetVal;        // Set to return value of block
        }
-       
-       // Not found?
-       if(!bFound)
+                       
+       while(bs.FirstVar)
        {
-               fprintf(stderr, "Undefined reference to class '%s'\n", ClassName);
-               return ERRPTR;
+               tAST_Variable   *nextVar = bs.FirstVar->Next;
+               Variable_Destroy( bs.FirstVar );
+               bs.FirstVar = nextVar;
        }
-       
        return ret;
 }
 
-
 /**
  * \brief Execute an AST node and return its value
  * \param Block        Execution context
@@ -369,7 +85,6 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        tAST_Node       *node;
        tSpiderValue    *ret = NULL, *tmpobj;
        tSpiderValue    *op1, *op2;     // Binary operations
-        int    cmp;    // Used in comparisons
         int    i;
        
        switch(Node->Type)
@@ -450,7 +165,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
 //                                     varVal->ReferenceCount);
                        }
                        #endif
-                       value = AST_ExecuteNode_BinOp(Block, Node, Node->Assign.Operation, varVal, ret);
+                       value = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Assign.Operation, varVal, ret);
                        if(value == ERRPTR)     return ERRPTR;
 
                        if(ret) SpiderScript_DereferenceValue(ret);
@@ -495,9 +210,9 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        varVal = Variable_GetValue(Block, Node->UniOp.Value);
                        
                        if( Node->Type == NODETYPE_POSTDEC )
-                               value = AST_ExecuteNode_BinOp(Block, Node, NODETYPE_SUBTRACT, varVal, &one);
+                               value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_SUBTRACT, varVal, &one);
                        else
-                               value = AST_ExecuteNode_BinOp(Block, Node, NODETYPE_ADD, varVal, &one);
+                               value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_ADD, varVal, &one);
                        if( value == ERRPTR )
                                return ERRPTR;
                        
@@ -855,119 +570,12 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                SpiderScript_DereferenceValue(op2);
                break;
        
-       // Comparisons
-       case NODETYPE_EQUALS:
-       case NODETYPE_LESSTHAN:
-       case NODETYPE_GREATERTHAN:
-       case NODETYPE_LESSTHANEQUAL:
-       case NODETYPE_GREATERTHANEQUAL:
-               op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
-               if(op1 == ERRPTR)       return ERRPTR;
-               op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
-               if(op2 == ERRPTR) {
-                       SpiderScript_DereferenceValue(op1);
-                       ret = ERRPTR;
-                       break;
-               }
-               
-               if( !op1 || !op2 ) {
-                       AST_RuntimeError(Node, "NULL Comparison (%p and %p)", op1, op2);
-                       if(op1) SpiderScript_DereferenceValue(op1);
-                       if(op2) SpiderScript_DereferenceValue(op2);
-                       ret = SpiderScript_CreateInteger( !op1 && !op2 );
-                       break;
-               }
-               
-               // Convert types
-               if( op1->Type != op2->Type ) {
-                       // If dynamically typed, convert op2 to op1's type
-                       if(Block->Script->Variant->bImplicitCasts)
-                       {
-                               tmpobj = op2;
-                               op2 = SpiderScript_CastValueTo(op1->Type, op2);
-                               SpiderScript_DereferenceValue(tmpobj);
-                               if(op2 == ERRPTR) {
-                                       SpiderScript_DereferenceValue(op1);
-                                       return ERRPTR;
-                               }
-                       }
-                       // If statically typed, this should never happen, but catch it anyway
-                       else {
-                               AST_RuntimeError(Node, "Statically typed implicit cast %i <op> %i",
-                                       op1->Type, op2->Type);
-                               ret = ERRPTR;
-                               break;
-                       }
-               }
-               // Do operation
-               switch(op1->Type)
-               {
-               // - String Compare (does a strcmp, well memcmp)
-               case SS_DATATYPE_STRING:
-                       // Call memcmp to do most of the work
-                       cmp = memcmp(
-                               op1->String.Data, op2->String.Data,
-                               (op1->String.Length < op2->String.Length) ? op1->String.Length : op2->String.Length
-                               );
-                       // Handle reaching the end of the string
-                       if( cmp == 0 ) {
-                               if( op1->String.Length == op2->String.Length )
-                                       cmp = 0;
-                               else if( op1->String.Length < op2->String.Length )
-                                       cmp = 1;
-                               else
-                                       cmp = -1;
-                       }
-                       break;
-               
-               // - Integer Comparisons
-               case SS_DATATYPE_INTEGER:
-                       if( op1->Integer == op2->Integer )
-                               cmp = 0;
-                       else if( op1->Integer < op2->Integer )
-                               cmp = -1;
-                       else
-                               cmp = 1;
-                       break;
-               // - Real Number Comparisons
-               case SS_DATATYPE_REAL:
-                       cmp = (op1->Real - op2->Real) / op2->Real * 10000;      // < 0.1% difference is equality
-                       break;
-               default:
-                       AST_RuntimeError(Node, "TODO - Comparison of type %i", op1->Type);
-                       ret = ERRPTR;
-                       break;
-               }
-               
-               // Free intermediate objects
-               SpiderScript_DereferenceValue(op1);
-               SpiderScript_DereferenceValue(op2);
-               
-               // Error check
-               if( ret == ERRPTR )
-                       break;
-               
-               // Create return
-               switch(Node->Type)
-               {
-               case NODETYPE_EQUALS:   ret = SpiderScript_CreateInteger(cmp == 0);     break;
-               case NODETYPE_LESSTHAN: ret = SpiderScript_CreateInteger(cmp < 0);      break;
-               case NODETYPE_GREATERTHAN:      ret = SpiderScript_CreateInteger(cmp > 0);      break;
-               case NODETYPE_LESSTHANEQUAL:    ret = SpiderScript_CreateInteger(cmp <= 0);     break;
-               case NODETYPE_GREATERTHANEQUAL: ret = SpiderScript_CreateInteger(cmp >= 0);     break;
-               default:
-                       AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Node->Type);
-                       ret = ERRPTR;
-                       break;
-               }
-               break;
-       
        // General Unary Operations
        case NODETYPE_BWNOT:    // Bitwise NOT (~)
        case NODETYPE_NEGATE:   // Negation (-)
                op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
                if(op1 == ERRPTR)       return ERRPTR;
-               ret = AST_ExecuteNode_UniOp(Block, Node, Node->Type, op1);
+               ret = AST_ExecuteNode_UniOp(Block->Script, Node, Node->Type, op1);
                SpiderScript_DereferenceValue(op1);
                break;
        
@@ -983,6 +591,11 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        case NODETYPE_BITSHIFTLEFT:
        case NODETYPE_BITSHIFTRIGHT:
        case NODETYPE_BITROTATELEFT:
+       case NODETYPE_EQUALS:
+       case NODETYPE_LESSTHAN:
+       case NODETYPE_GREATERTHAN:
+       case NODETYPE_LESSTHANEQUAL:
+       case NODETYPE_GREATERTHANEQUAL:
                // Get operands
                op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
                if(op1 == ERRPTR)       return ERRPTR;
@@ -992,7 +605,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        return ERRPTR;
                }
                
-               ret = AST_ExecuteNode_BinOp(Block, Node, Node->Type, op1, op2);
+               ret = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Type, op1, op2);
                
                // Free intermediate objects
                SpiderScript_DereferenceValue(op1);
@@ -1021,7 +634,7 @@ _return:
        return ret;
 }
 
-tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, tAST_Node *Node, int Operation, tSpiderValue *Value)
+tSpiderValue *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value)
 {
        tSpiderValue    *ret;
        #if 0
@@ -1083,7 +696,7 @@ tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, tAST_Node *Node, int
        return ret;
 }
 
-tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right)
+tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right)
 {
        tSpiderValue    *preCastValue = Right;
        tSpiderValue    *ret;
@@ -1124,7 +737,7 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, tAST_Node *Node, int
                #endif
                
                // If implicit casts are allowed, convert Right to Left's type
-               if(Block->Script->Variant->bImplicitCasts)
+               if(Script->Variant->bImplicitCasts)
                {
                        Right = SpiderScript_CastValueTo(Left->Type, Right);
                        if(Right == ERRPTR)
@@ -1142,6 +755,88 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, tAST_Node *Node, int
                if(Right && Right != preCastValue)      free(Right);
                return NULL;
        }
+
+       // Catch comparisons
+       switch(Operation)
+       {
+       case NODETYPE_EQUALS:
+       case NODETYPE_LESSTHAN:
+       case NODETYPE_GREATERTHAN:
+       case NODETYPE_LESSTHANEQUAL:
+       case NODETYPE_GREATERTHANEQUAL: {
+                int    cmp;
+               ret = NULL;
+               // Do operation
+               switch(Left->Type)
+               {
+               // - String Compare (does a strcmp, well memcmp)
+               case SS_DATATYPE_STRING:
+                       // Call memcmp to do most of the work
+                       cmp = memcmp(
+                               Left->String.Data, Right->String.Data,
+                               (Left->String.Length < Right->String.Length) ? Left->String.Length : Right->String.Length
+                               );
+                       // Handle reaching the end of the string
+                       if( cmp == 0 ) {
+                               if( Left->String.Length == Right->String.Length )
+                                       cmp = 0;
+                               else if( Left->String.Length < Right->String.Length )
+                                       cmp = 1;
+                               else
+                                       cmp = -1;
+                       }
+                       break;
+               
+               // - Integer Comparisons
+               case SS_DATATYPE_INTEGER:
+                       if( Left->Integer == Right->Integer )
+                               cmp = 0;
+                       else if( Left->Integer < Right->Integer )
+                               cmp = -1;
+                       else
+                               cmp = 1;
+                       break;
+               // - Real Number Comparisons
+               case SS_DATATYPE_REAL:
+                       cmp = (Left->Real - Right->Real) / Right->Real * 10000; // < 0.1% difference is equality
+                       break;
+               default:
+                       AST_RuntimeError(Node, "TODO - Comparison of type %i", Left->Type);
+                       ret = ERRPTR;
+                       break;
+               }
+               
+               // Error check
+               if( ret != ERRPTR )
+               {
+                       if(Left->ReferenceCount == 1 && Left->Type != SS_DATATYPE_STRING)
+                               SpiderScript_ReferenceValue(ret = Left);
+                       else
+                               ret = SpiderScript_CreateInteger(0);
+                       
+                       // Create return
+                       switch(Operation)
+                       {
+                       case NODETYPE_EQUALS:   ret->Integer = (cmp == 0);      break;
+                       case NODETYPE_LESSTHAN: ret->Integer = (cmp < 0);       break;
+                       case NODETYPE_GREATERTHAN:      ret->Integer = (cmp > 0);       break;
+                       case NODETYPE_LESSTHANEQUAL:    ret->Integer = (cmp <= 0);      break;
+                       case NODETYPE_GREATERTHANEQUAL: ret->Integer = (cmp >= 0);      break;
+                       default:
+                               AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Operation);
+                               SpiderScript_DereferenceValue(ret);
+                               ret = ERRPTR;
+                               break;
+                       }
+               }
+               if(Right && Right != preCastValue)      free(Right);
+               return ret;
+               }
+
+       // Fall through and sort by type instead
+       default:
+               break;
+       }
        
        // Do operation
        switch(Left->Type)
@@ -1365,29 +1060,3 @@ void Variable_Destroy(tAST_Variable *Variable)
        free(Variable);
 }
 
-void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...)
-{
-       va_list args;
-       
-       if(Node) {
-               fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
-       }
-       fprintf(stderr, "%s: ", Type);
-       va_start(args, Format);
-       vfprintf(stderr, Format, args);
-       va_end(args);
-       fprintf(stderr, "\n");
-}
-void AST_RuntimeError(tAST_Node *Node, const char *Format, ...)
-{
-       va_list args;
-       
-       if(Node) {
-               fprintf(stderr, "%s:%i: ", Node->File, Node->Line);
-       }
-       fprintf(stderr, "error: ");
-       va_start(args, Format);
-       vfprintf(stderr, Format, args);
-       va_end(args);
-       fprintf(stderr, "\n");
-}

UCC git Repository :: git.ucc.asn.au