Added if() statement (and internal support for for,while and do{}while())
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / exec_ast.c
index 2bbfd1c..b918608 100644 (file)
@@ -29,7 +29,20 @@ void Object_Dereference(tSpiderValue *Object)
 {
        if(!Object)     return ;
        Object->ReferenceCount --;
-       if( Object->ReferenceCount == 0 )       free(Object);
+       if( Object->ReferenceCount == 0 ) {
+               switch( (enum eSpiderScript_DataTypes) Object->Type )
+               {
+               case SS_DATATYPE_OBJECT:
+                       Object->Object->Type->Destructor( Object->Object );
+                       break;
+               case SS_DATATYPE_OPAQUE:
+                       Object->Opaque.Destroy( Object->Opaque.Data );
+                       break;
+               default:
+                       break;
+               }
+               free(Object);
+       }
 }
 
 void Object_Reference(tSpiderValue *Object)
@@ -103,6 +116,8 @@ tSpiderValue *Object_StringConcat(tSpiderValue *Str1, tSpiderValue *Str2)
 
 /**
  * \brief Cast one object to another
+ * \brief Type Destination type
+ * \brief Source       Input data
  */
 tSpiderValue *Object_CastTo(int Type, tSpiderValue *Source)
 {
@@ -113,11 +128,12 @@ tSpiderValue *Object_CastTo(int Type, tSpiderValue *Source)
                return Source;
        }
        
-       switch(Type)
+       switch( (enum eSpiderScript_DataTypes)Type )
        {
        case SS_DATATYPE_UNDEF:
        case SS_DATATYPE_NULL:
        case SS_DATATYPE_ARRAY:
+       case SS_DATATYPE_OPAQUE:
                fprintf(stderr, "Object_CastTo - Invalid cast to %i\n", Type);
                return ERRPTR;
        
@@ -150,7 +166,10 @@ tSpiderValue *Object_CastTo(int Type, tSpiderValue *Source)
  */
 int Object_IsTrue(tSpiderValue *Value)
 {
-       switch(Value->Type)
+       if( Value == ERRPTR )   return 0;
+       if( Value == NULL )     return 0;
+       
+       switch( (enum eSpiderScript_DataTypes)Value->Type )
        {
        case SS_DATATYPE_UNDEF:
        case SS_DATATYPE_NULL:
@@ -168,8 +187,14 @@ int Object_IsTrue(tSpiderValue *Value)
        case SS_DATATYPE_OBJECT:
                return Value->Object != NULL;
        
+       case SS_DATATYPE_OPAQUE:
+               return Value->Opaque.Data != NULL;
+       
        case SS_DATATYPE_ARRAY:
                return Value->Array.Length > 0;
+       default:
+               fprintf(stderr, "Spiderscript internal error: Unknown type %i in Object_IsTrue\n", Value->Type);
+               return 0;
        }
        return 0;
 }
@@ -180,7 +205,7 @@ int Object_IsTrue(tSpiderValue *Value)
 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
 {
        tAST_Node       *node;
-       tSpiderValue    *ret, *tmpobj;
+       tSpiderValue    *ret = NULL, *tmpobj;
        tSpiderValue    *op1, *op2;     // Binary operations
         int    cmp;    // Used in comparisons
        
@@ -194,23 +219,18 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                {
                        tAST_BlockState blockInfo;
                        blockInfo.FirstVar = NULL;
+                       blockInfo.RetVal = NULL;
                        blockInfo.Parent = Block;
                        blockInfo.Script = Block->Script;
                        ret = NULL;
-                       for(node = Node->Block.FirstChild; node; node = node->NextSibling )
+                       for(node = Node->Block.FirstChild; node && !blockInfo.RetVal; node = node->NextSibling )
                        {
-                               if(node->Type == NODETYPE_RETURN) {
-                                       ret = AST_ExecuteNode(&blockInfo, node);
+                               tmpobj = AST_ExecuteNode(&blockInfo, node);
+                               if(tmpobj == ERRPTR) {  // Error check
+                                       ret = ERRPTR;
                                        break ;
                                }
-                               else {
-                                       tmpobj = AST_ExecuteNode(&blockInfo, node);
-                                       if(tmpobj == ERRPTR) {  // Error check
-                                               ret = ERRPTR;
-                                               break ;
-                                       }
-                                       if(tmpobj)      Object_Dereference(tmpobj);     // Free unused value
-                               }
+                               if(tmpobj)      Object_Dereference(tmpobj);     // Free unused value
                        }
                        // Clean up variables
                        while(blockInfo.FirstVar)
@@ -219,6 +239,9 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                                Variable_Destroy( blockInfo.FirstVar );
                                blockInfo.FirstVar = nextVar;
                        }
+                       
+                       if( blockInfo.RetVal )
+                               Block->RetVal = blockInfo.RetVal;
                }
                
                break;
@@ -267,9 +290,46 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                }
                break;
        
-       // Return's special handling happens elsewhere
+       // Conditional
+       case NODETYPE_IF:
+               ret = AST_ExecuteNode(Block, Node->If.Condition);
+               if( Object_IsTrue(ret) ) {
+                       AST_ExecuteNode(Block, Node->If.True);
+               }
+               else {
+                       AST_ExecuteNode(Block, Node->If.False);
+               }
+               Object_Dereference(ret);
+               break;
+       
+       // Loop
+       case NODETYPE_LOOP:
+               ret = AST_ExecuteNode(Block, Node->For.Init);
+               if( Node->For.bCheckAfter ) {
+                       do {
+                               Object_Dereference(ret);
+                               ret = AST_ExecuteNode(Block, Node->For.Code);
+                               Object_Dereference(ret);
+                               ret = AST_ExecuteNode(Block, Node->For.Condition);
+                       } while( Object_IsTrue(ret) );
+               }
+               else {
+                       Object_Dereference(ret);
+                       ret = AST_ExecuteNode(Block, Node->For.Condition);
+                       while( Object_IsTrue(ret) ) {
+                               Object_Dereference(ret);
+                               ret = AST_ExecuteNode(Block, Node->For.Code);
+                               Object_Dereference(ret);
+                               ret = AST_ExecuteNode(Block, Node->For.Condition);
+                       }
+                       Object_Dereference(ret);
+               }
+               break;
+       
+       // Return
        case NODETYPE_RETURN:
                ret = AST_ExecuteNode(Block, Node->UniOp.Value);
+               Block->RetVal = ret;    // Return value set
                break;
        
        // Define a variable
@@ -403,12 +463,20 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                                        cmp = -1;
                        }
                        break;
+               default:
+                       fprintf(stderr, "SpiderScript internal error: TODO: Comparison of type %i\n", op1->Type);
+                       ret = ERRPTR;
+                       break;
                }
                
                // Free intermediate objects
                Object_Dereference(op1);
                Object_Dereference(op2);
                
+               // Error check
+               if( ret == ERRPTR )
+                       break;
+               
                // Create return
                switch(Node->Type)
                {
@@ -481,6 +549,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                                break;
                        }
                        break;
+               // Integer Operations
                case SS_DATATYPE_INTEGER:
                        switch(Node->Type)
                        {
@@ -498,11 +567,27 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                                ret = Object_CreateInteger( (op1->Integer << op2->Integer) | (op1->Integer >> (64-op2->Integer)) );
                                break;
                        default:
-                               fprintf(stderr, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i", Node->Type);
+                               fprintf(stderr, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i\n", Node->Type);
+                               ret = ERRPTR;
+                               break;
+                       }
+                       break;
+               
+               // Real Numbers
+               case SS_DATATYPE_REAL:
+                       switch(Node->Type)
+                       {
+                       default:
+                               fprintf(stderr, "SpiderScript internal error: Exec,BinOP,Real unknown op %i\n", Node->Type);
                                ret = ERRPTR;
                                break;
                        }
                        break;
+               
+               default:
+                       fprintf(stderr, "SpiderScript error: Invalid operation (%i) on type (%i)\n", Node->Type, op1->Type);
+                       ret = ERRPTR;
+                       break;
                }
                
                // Free intermediate objects

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