SpiderScript - Speedups
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / exec_ast.c
index 22b2d20..d84b8f9 100644 (file)
@@ -1,4 +1,7 @@
 /*
+ * SpiderScript Library
+ *
+ * AST Execution
  */
 #include <stdlib.h>
 #include <stdio.h>
@@ -24,8 +27,8 @@ void  SpiderScript_FreeValue(tSpiderValue *Value);
 char   *SpiderScript_DumpValue(tSpiderValue *Value);
 
 tSpiderValue   *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
-tSpiderValue   *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpiderValue *Left, tSpiderValue *Right);
-tSpiderValue   *AST_ExecuteNode_UniOp(tAST_BlockState *Block, int Operation, tSpiderValue *Value);
+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);
 
 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);
@@ -396,6 +399,7 @@ char *SpiderScript_DumpValue(tSpiderValue *Value)
 /**
  * \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
@@ -404,7 +408,6 @@ tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
        tSpiderNamespace *Namespace, const char *Function,
        int NArguments, tSpiderValue **Arguments)
 {
-       char    *trueName = NULL;
         int    bFound = 0;     // Used to keep nesting levels down
        tSpiderValue    *ret = ERRPTR;
        tSpiderFunction *fcn;
@@ -508,7 +511,8 @@ tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
        // Not found?
        if(!bFound)
        {
-               fprintf(stderr, "Undefined reference to '%s'\n", trueName);
+               fprintf(stderr, "Undefined reference to function '%s' (ns='%s')\n",
+                       Function, Namespace->Name);
                return ERRPTR;
        }
        
@@ -518,12 +522,13 @@ tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
 /**
  * \brief Execute an object method function
  * \param Script       Script context to execute in
- * \param Function     Function name to execute
+ * \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,
+tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
+       tSpiderObject *Object, const char *MethodName,
        int NArguments, tSpiderValue **Arguments)
 {
        tSpiderFunction *fcn;
@@ -531,12 +536,15 @@ tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script, tSpiderObject *O
        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'",
@@ -544,19 +552,20 @@ tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script, tSpiderObject *O
                return ERRPTR;
        }
        
+       // 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*));
        
-       // TODO: Type Checking
+       // Check the type of the arguments
        for( i = 0; fcn->ArgTypes[i]; i ++ )
        {
                if( i >= NArguments ) {
-                       AST_RuntimeError(NULL, "Argument count mismatch (%i passed)",
-                               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] )
@@ -567,6 +576,7 @@ tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script, tSpiderObject *O
                }
        }
        
+       // Call handler
        return fcn->Handler(Script, NArguments+1, newargs);
 }
 
@@ -586,7 +596,7 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
        tSpiderObjectDef        *class;
        
        // First: Find the function in the script
-       // TODO: Implement scripted classes
+       // TODO: Implement script-defined classes
        #if 0
        {
                tAST_Function   *astClass;
@@ -644,6 +654,9 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
        {
                class = NULL;   // Just to allow the below code to be neat
                
+               //if( !Namespace )
+               //      Namespace = &Script->Variant->RootNamespace;
+               
                // Second: Scan current namespace
                if( !class && Namespace )
                {
@@ -683,10 +696,13 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
                {
                        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;
@@ -698,7 +714,7 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
        // Not found?
        if(!bFound)
        {
-               fprintf(stderr, "Undefined reference to '%s'\n", ClassName);
+               fprintf(stderr, "Undefined reference to class '%s'\n", ClassName);
                return ERRPTR;
        }
        
@@ -708,6 +724,8 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
 
 /**
  * \brief Execute an AST node and return its value
+ * \param Block        Execution context
+ * \param Node Node to execute
  */
 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
 {
@@ -720,7 +738,9 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        switch(Node->Type)
        {
        // No Operation
-       case NODETYPE_NOP:      ret = NULL;     break;
+       case NODETYPE_NOP:
+               ret = NULL;
+               break;
        
        // Code block
        case NODETYPE_BLOCK:
@@ -732,9 +752,13 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        blockInfo.RetVal = NULL;
                        blockInfo.BaseNamespace = Block->BaseNamespace;
                        blockInfo.CurNamespace = NULL;
+                       blockInfo.BreakTarget = NULL;
                        blockInfo.Ident = giNextBlockIdent ++;
                        ret = NULL;
-                       for(node = Node->Block.FirstChild; node && !blockInfo.RetVal; node = node->NextSibling )
+                       // Loop over all nodes, or until the return value is set
+                       for(node = Node->Block.FirstChild;
+                               node && !blockInfo.RetVal && !blockInfo.BreakTarget;
+                               node = node->NextSibling )
                        {
                                ret = AST_ExecuteNode(&blockInfo, node);
                                if(ret == ERRPTR)       break;  // Error check
@@ -747,16 +771,25 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                                Variable_Destroy( blockInfo.FirstVar );
                                blockInfo.FirstVar = nextVar;
                        }
-                       if(ret != ERRPTR)
-                               ret = NULL;
+                       // Clear ret if not an error
+                       if(ret != ERRPTR)       ret = NULL;
+                       
+                       // Set parent's return value if needed
                        if( blockInfo.RetVal )
                                Block->RetVal = blockInfo.RetVal;
+                       if( blockInfo.BreakTarget ) {
+                               Block->BreakTarget = blockInfo.BreakTarget;
+                               Block->BreakType = blockInfo.BreakType;
+                       }
+                       
+                       // TODO: Unset break if break type deontes a block break
                }
                
                break;
        
        // Assignment
        case NODETYPE_ASSIGN:
+               // TODO: Support assigning to object attributes
                if( Node->Assign.Dest->Type != NODETYPE_VARIABLE ) {
                        AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
                        return ERRPTR;
@@ -764,46 +797,76 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                ret = AST_ExecuteNode(Block, Node->Assign.Value);
                if(ret == ERRPTR)       return ERRPTR;
                
+               // Perform assignment operation
                if( Node->Assign.Operation != NODETYPE_NOP )
                {
-                       tSpiderValue    *varVal = Variable_GetValue(Block, Node->Assign.Dest);
-                       tSpiderValue    *value;
-                       value = AST_ExecuteNode_BinOp(Block, Node->Assign.Operation, varVal, ret);
+                       tSpiderValue    *varVal, *value;
+
+                       varVal = Variable_GetValue(Block, Node->Assign.Dest);
+                       if(varVal == ERRPTR)    return ERRPTR;
+                       #if 0
+                       #else
+                       if(varVal && varVal->ReferenceCount == 2) {
+                               Object_Dereference(varVal);
+//                             printf("pre: (%s) varVal->ReferenceCount = %i\n",
+//                                     Node->Assign.Dest->Variable.Name,
+//                                     varVal->ReferenceCount);
+                       }
+                       #endif
+                       value = AST_ExecuteNode_BinOp(Block, Node, Node->Assign.Operation, varVal, ret);
                        if(value == ERRPTR)     return ERRPTR;
+
                        if(ret) Object_Dereference(ret);
+                       #if 0
                        if(varVal)      Object_Dereference(varVal);
+                       #else
+                       if(varVal && varVal->ReferenceCount == 1) {
+                               Object_Reference(varVal);
+//                             printf("post: varVal->ReferenceCount = %i\n", varVal->ReferenceCount);
+                               break;  // If varVal was non-null, it has been updated by _BinOp
+                       }
+                       #endif
+                       // Else, it was NULL, so has to be assigned
                        ret = value;
                }
                
+               // Set the variable value
                if( Variable_SetValue( Block, Node->Assign.Dest, ret ) ) {
                        Object_Dereference( ret );
                        return ERRPTR;
                }
                break;
        
+       // Post increment/decrement
        case NODETYPE_POSTINC:
        case NODETYPE_POSTDEC:
                {
-                       tSpiderValue    *varVal, *value, *one;
+                       tSpiderValue    *varVal, *value;
+                       static tSpiderValue     one = {
+                               .Type = SS_DATATYPE_INTEGER,
+                               .ReferenceCount = 1,
+                               {.Integer = 1}
+                               };
+                       
+                       // TODO: Support assigning to object attributes
                        if( Node->UniOp.Value->Type != NODETYPE_VARIABLE ) {
                                AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
                                return ERRPTR;
                        }
                
-                       varVal = Variable_GetValue(Block, Node->Assign.Dest);
-                       one = SpiderScript_CreateInteger(1);
+                       // Get values (current variable contents and a static one)
+                       varVal = Variable_GetValue(Block, Node->UniOp.Value);
                        
                        if( Node->Type == NODETYPE_POSTDEC )
-                               value = AST_ExecuteNode_BinOp(Block, NODETYPE_SUBTRACT, varVal, one);
+                               value = AST_ExecuteNode_BinOp(Block, Node, NODETYPE_SUBTRACT, varVal, &one);
                        else
-                               value = AST_ExecuteNode_BinOp(Block, NODETYPE_ADD, varVal, one);
+                               value = AST_ExecuteNode_BinOp(Block, Node, NODETYPE_ADD, varVal, &one);
                        if( value == ERRPTR )
                                return ERRPTR;
-                       Object_Dereference(one);        // Free constant one
                        
                        ret = varVal;
                
-                       if( Variable_SetValue( Block, Node->Assign.Dest, value ) ) {
+                       if( Variable_SetValue( Block, Node->UniOp.Value, value ) ) {
                                Object_Dereference( ret );
                                return ERRPTR;
                        }
@@ -817,6 +880,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        case NODETYPE_CREATEOBJECT:
                // Logical block (used to allocate `params`)
                {
+                       tSpiderNamespace        *ns = Block->CurNamespace;
                        tSpiderValue    *params[Node->FunctionCall.NumArgs];
                        i = 0;
                        for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling)
@@ -830,14 +894,13 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                                i ++;
                        }
                        
-                       if( !Block->CurNamespace )
-                               Block->CurNamespace = Block->BaseNamespace;
+                       if( !ns )       ns = Block->BaseNamespace;
                        
                        // Call the function
                        if( Node->Type == NODETYPE_CREATEOBJECT )
                        {
                                ret = SpiderScript_CreateObject(Block->Script,
-                                       Block->CurNamespace,
+                                       ns,
                                        Node->FunctionCall.Name,
                                        Node->FunctionCall.NumArgs, params
                                        );
@@ -861,7 +924,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        else
                        {
                                ret = SpiderScript_ExecuteFunction(Block->Script,
-                                       Block->CurNamespace, Node->FunctionCall.Name,
+                                       ns, Node->FunctionCall.Name,
                                        Node->FunctionCall.NumArgs, params
                                        );
                        }
@@ -892,38 +955,59 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        
        // Loop
        case NODETYPE_LOOP:
+               // Initialise
                ret = AST_ExecuteNode(Block, Node->For.Init);
                if(ret == ERRPTR)       break;
-               if( Node->For.bCheckAfter )
+               
+               // Check initial condition
+               if( !Node->For.bCheckAfter )
                {
-                       do {
-                               Object_Dereference(ret);
-                               ret = AST_ExecuteNode(Block, Node->For.Code);
-                               if(ret == ERRPTR)       return ERRPTR;
-                               Object_Dereference(ret);
-                               ret = AST_ExecuteNode(Block, Node->For.Increment);
-                               if(ret == ERRPTR)       return ERRPTR;
+                       Object_Dereference(ret);
+               
+                       ret = AST_ExecuteNode(Block, Node->For.Condition);
+                       if(ret == ERRPTR)       return ERRPTR;
+                       if(!SpiderScript_IsValueTrue(ret)) {
                                Object_Dereference(ret);
-                               ret = AST_ExecuteNode(Block, Node->For.Condition);
-                               if(ret == ERRPTR)       return ERRPTR;
-                       } while( SpiderScript_IsValueTrue(ret) );
+                               ret = NULL;
+                               break;
+                       }
                }
-               else
+       
+               // Perform loop
+               for( ;; )
                {
                        Object_Dereference(ret);
-                       ret = AST_ExecuteNode(Block, Node->For.Condition);
+                       
+                       // Code
+                       ret = AST_ExecuteNode(Block, Node->For.Code);
                        if(ret == ERRPTR)       return ERRPTR;
-                       while( SpiderScript_IsValueTrue(ret) ) {
-                               Object_Dereference(ret);
-                               ret = AST_ExecuteNode(Block, Node->For.Code);
-                               if(ret == ERRPTR)       return ERRPTR;
-                               Object_Dereference(ret);
-                               ret = AST_ExecuteNode(Block, Node->For.Increment);
-                               if(ret == ERRPTR)       return ERRPTR;
-                               Object_Dereference(ret);
-                               ret = AST_ExecuteNode(Block, Node->For.Condition);
-                               if(ret == ERRPTR)       return ERRPTR;
+                       Object_Dereference(ret);
+                       
+                       if(Block->BreakTarget)
+                       {
+                               if( Block->BreakTarget[0] == '\0' || strcmp(Block->BreakTarget, Node->For.Tag) == 0 )
+                               {
+                                       // Ours
+                                       free((void*)Block->BreakTarget);        Block->BreakTarget = NULL;
+                                       if( Block->BreakType == NODETYPE_CONTINUE ) {
+                                               // Continue, just keep going
+                                       }
+                                       else
+                                               break;
+                               }
+                               else
+                                       break;  // Break out of this loop
                        }
+                       
+                       // Increment
+                       ret = AST_ExecuteNode(Block, Node->For.Increment);
+                       if(ret == ERRPTR)       return ERRPTR;
+                       Object_Dereference(ret);
+                       
+                       // Check condition
+                       ret = AST_ExecuteNode(Block, Node->For.Condition);
+                       if(ret == ERRPTR)       return ERRPTR;
+                       if(!SpiderScript_IsValueTrue(ret))      break;
                }
                Object_Dereference(ret);
                ret = NULL;
@@ -937,11 +1021,25 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                ret = NULL;     // the `return` statement does not return a value
                break;
        
+       case NODETYPE_BREAK:
+       case NODETYPE_CONTINUE:
+               Block->BreakTarget = strdup(Node->Variable.Name);
+               Block->BreakType = Node->Type;
+               break;
+       
        // Define a variable
        case NODETYPE_DEFVAR:
+               if( Node->DefVar.InitialValue ) {
+                       tmpobj = AST_ExecuteNode(Block, Node->DefVar.InitialValue);
+                       if(tmpobj == ERRPTR)    return ERRPTR;
+               }
+               else {
+                       tmpobj = NULL;
+               }
                ret = NULL;
-               if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name, NULL) == ERRPTR )
+               if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name, tmpobj) == ERRPTR )
                        ret = ERRPTR;
+               Object_Dereference(tmpobj);
                break;
        
        // Scope
@@ -1231,7 +1329,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        case NODETYPE_NEGATE:   // Negation (-)
                op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
                if(op1 == ERRPTR)       return ERRPTR;
-               ret = AST_ExecuteNode_UniOp(Block, Node->Type, op1);
+               ret = AST_ExecuteNode_UniOp(Block, Node, Node->Type, op1);
                Object_Dereference(op1);
                break;
        
@@ -1256,7 +1354,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        return ERRPTR;
                }
                
-               ret = AST_ExecuteNode_BinOp(Block, Node->Type, op1, op2);
+               ret = AST_ExecuteNode_BinOp(Block, Node, Node->Type, op1, op2);
                
                // Free intermediate objects
                Object_Dereference(op1);
@@ -1269,6 +1367,10 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        //      break;
        }
 _return:
+       // Reset namespace when no longer needed
+       if( Node->Type != NODETYPE_SCOPE )
+               Block->CurNamespace = NULL;
+
        #if TRACE_NODE_RETURNS
        if(ret && ret != ERRPTR) {
                AST_RuntimeError(Node, "Ret type of %p %i is %i", Node, Node->Type, ret->Type);
@@ -1281,7 +1383,7 @@ _return:
        return ret;
 }
 
-tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, int Operation, tSpiderValue *Value)
+tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, tAST_Node *Node, int Operation, tSpiderValue *Value)
 {
        tSpiderValue    *ret;
        #if 0
@@ -1308,12 +1410,17 @@ tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, int Operation, tSpid
        {
        // Integer Operations
        case SS_DATATYPE_INTEGER:
+               if( Value->ReferenceCount == 1 )
+                       Object_Reference(ret = Value);
+               else
+                       ret = SpiderScript_CreateInteger(0);
                switch(Operation)
                {
-               case NODETYPE_NEGATE:   ret = SpiderScript_CreateInteger( -Value->Integer );    break;
-               case NODETYPE_BWNOT:    ret = SpiderScript_CreateInteger( ~Value->Integer );    break;
+               case NODETYPE_NEGATE:   ret->Integer = -Value->Integer; break;
+               case NODETYPE_BWNOT:    ret->Integer = ~Value->Integer; break;
                default:
-                       AST_RuntimeError(NULL, "SpiderScript internal error: Exec,UniOP,Integer unknown op %i", Operation);
+                       AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Integer unknown op %i", Operation);
+                       Object_Dereference(ret);
                        ret = ERRPTR;
                        break;
                }
@@ -1324,7 +1431,7 @@ tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, int Operation, tSpid
                {
                case NODETYPE_NEGATE:   ret = SpiderScript_CreateInteger( -Value->Real );       break;
                default:
-                       AST_RuntimeError(NULL, "SpiderScript internal error: Exec,UniOP,Real unknown op %i", Operation);
+                       AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Real unknown op %i", Operation);
                        ret = ERRPTR;
                        break;
                }
@@ -1339,7 +1446,7 @@ tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, int Operation, tSpid
        return ret;
 }
 
-tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpiderValue *Left, tSpiderValue *Right)
+tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right)
 {
        tSpiderValue    *preCastValue = Right;
        tSpiderValue    *ret;
@@ -1388,7 +1495,7 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpid
                }
                // If statically typed, this should never happen, but catch it anyway
                else {
-                       AST_RuntimeError(NULL, "Implicit cast not allowed (from %i to %i)", Right->Type, Left->Type);
+                       AST_RuntimeError(Node, "Implicit cast not allowed (from %i to %i)", Right->Type, Left->Type);
                        return ERRPTR;
                }
        }
@@ -1409,31 +1516,41 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpid
                case NODETYPE_ADD:      // Concatenate
                        ret = Object_StringConcat(Left, Right);
                        break;
+               // TODO: Support python style 'i = %i' % i ?
+               // Might do it via a function call
+//             case NODETYPE_MODULUS:
+//                     break;
+
                default:
-                       AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,String unknown op %i", Operation);
+                       AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,String unknown op %i", Operation);
                        ret = ERRPTR;
                        break;
                }
                break;
        // Integer Operations
        case SS_DATATYPE_INTEGER:
+               if( Left->ReferenceCount == 1 )
+                       Object_Reference(ret = Left);
+               else
+                       ret = SpiderScript_CreateInteger(0);
                switch(Operation)
                {
-               case NODETYPE_ADD:      ret = SpiderScript_CreateInteger( Left->Integer + Right->Integer );     break;
-               case NODETYPE_SUBTRACT: ret = SpiderScript_CreateInteger( Left->Integer - Right->Integer );     break;
-               case NODETYPE_MULTIPLY: ret = SpiderScript_CreateInteger( Left->Integer * Right->Integer );     break;
-               case NODETYPE_DIVIDE:   ret = SpiderScript_CreateInteger( Left->Integer / Right->Integer );     break;
-               case NODETYPE_MODULO:   ret = SpiderScript_CreateInteger( Left->Integer % Right->Integer );     break;
-               case NODETYPE_BWAND:    ret = SpiderScript_CreateInteger( Left->Integer & Right->Integer );     break;
-               case NODETYPE_BWOR:     ret = SpiderScript_CreateInteger( Left->Integer | Right->Integer );     break;
-               case NODETYPE_BWXOR:    ret = SpiderScript_CreateInteger( Left->Integer ^ Right->Integer );     break;
-               case NODETYPE_BITSHIFTLEFT:     ret = SpiderScript_CreateInteger( Left->Integer << Right->Integer );    break;
-               case NODETYPE_BITSHIFTRIGHT:ret = SpiderScript_CreateInteger( Left->Integer >> Right->Integer );        break;
+               case NODETYPE_ADD:      ret->Integer = Left->Integer + Right->Integer;  break;
+               case NODETYPE_SUBTRACT: ret->Integer = Left->Integer - Right->Integer;  break;
+               case NODETYPE_MULTIPLY: ret->Integer = Left->Integer * Right->Integer;  break;
+               case NODETYPE_DIVIDE:   ret->Integer = Left->Integer / Right->Integer;  break;
+               case NODETYPE_MODULO:   ret->Integer = Left->Integer % Right->Integer;  break;
+               case NODETYPE_BWAND:    ret->Integer = Left->Integer & Right->Integer;  break;
+               case NODETYPE_BWOR:     ret->Integer = Left->Integer | Right->Integer;  break;
+               case NODETYPE_BWXOR:    ret->Integer = Left->Integer ^ Right->Integer;  break;
+               case NODETYPE_BITSHIFTLEFT: ret->Integer = Left->Integer << Right->Integer;     break;
+               case NODETYPE_BITSHIFTRIGHT:ret->Integer = Left->Integer >> Right->Integer;     break;
                case NODETYPE_BITROTATELEFT:
-                       ret = SpiderScript_CreateInteger( (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer)) );
+                       ret->Integer = (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer));
                        break;
                default:
-                       AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i", Operation);
+                       AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i", Operation);
+                       Object_Dereference(ret);
                        ret = ERRPTR;
                        break;
                }
@@ -1441,21 +1558,26 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpid
        
        // Real Numbers
        case SS_DATATYPE_REAL:
+               if( Left->ReferenceCount == 1 )
+                       Object_Reference(ret = Left);
+               else
+                       ret = SpiderScript_CreateReal(0);
                switch(Operation)
                {
-               case NODETYPE_ADD:      ret = SpiderScript_CreateReal( Left->Real + Right->Real );      break;
-               case NODETYPE_SUBTRACT: ret = SpiderScript_CreateReal( Left->Real - Right->Real );      break;
-               case NODETYPE_MULTIPLY: ret = SpiderScript_CreateReal( Left->Real * Right->Real );      break;
-               case NODETYPE_DIVIDE:   ret = SpiderScript_CreateReal( Left->Real / Right->Real );      break;
+               case NODETYPE_ADD:      ret->Real = Left->Real + Right->Real;   break;
+               case NODETYPE_SUBTRACT: ret->Real = Left->Real - Right->Real;   break;
+               case NODETYPE_MULTIPLY: ret->Real = Left->Real * Right->Real;   break;
+               case NODETYPE_DIVIDE:   ret->Real = Left->Real / Right->Real;   break;
                default:
-                       AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation);
+                       AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation);
+                       Object_Dereference(ret);
                        ret = ERRPTR;
                        break;
                }
                break;
        
        default:
-               AST_RuntimeError(NULL, "BUG - Invalid operation (%i) on type (%i)", Operation, Left->Type);
+               AST_RuntimeError(Node, "BUG - Invalid operation (%i) on type (%i)", Operation, Left->Type);
                ret = ERRPTR;
                break;
        }
@@ -1559,7 +1681,9 @@ tAST_Variable *Variable_Lookup(tAST_BlockState *Block, tAST_Node *VarNode, int C
  */
 int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value)
 {
-       tAST_Variable   *var = Variable_Lookup(Block, VarNode, Value->Type);
+       tAST_Variable   *var;
+       
+       var = Variable_Lookup(Block, VarNode, (Value ? Value->Type : SS_DATATYPE_UNDEF));
        
        if( !var )      return -1;
        

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