SpiderScript - Moved header to directory, ready to remove from tree
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / exec_ast.c
index 3d3c76d..a9970ee 100644 (file)
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "ast.h"
 
+#define USE_AST_EXEC   1
 #define TRACE_VAR_LOOKUPS      0
 #define TRACE_NODE_RETURNS     0
 
@@ -20,6 +21,7 @@
 tSpiderValue   *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
 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);
+tSpiderValue   *AST_ExecuteNode_Index(tSpiderScript *Script, tAST_Node *Node, tSpiderValue *Array, int Index, tSpiderValue *SaveValue);
 // - 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,6 +35,7 @@ void  AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
  int   giNextBlockIdent = 1;
 
 // === CODE ===
+#if USE_AST_EXEC
 tSpiderValue *AST_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Arguments)
 {
        tAST_BlockState bs;
@@ -85,7 +88,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)
@@ -233,9 +235,11 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        case NODETYPE_CREATEOBJECT:
                // Logical block (used to allocate `params`)
                {
-                       tSpiderNamespace        *ns = Block->CurNamespace;
+                       const char      *namespaces[] = {NULL}; // TODO: Default namespaces?
                        tSpiderValue    *params[Node->FunctionCall.NumArgs];
                        i = 0;
+                       
+                       // Get arguments
                        for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling)
                        {
                                params[i] = AST_ExecuteNode(Block, node);
@@ -246,15 +250,15 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                                }
                                i ++;
                        }
-                       
-                       if( !ns )       ns = Block->BaseNamespace;
-                       
+
+                       // TODO: Check for cached function reference                    
+
                        // Call the function
                        if( Node->Type == NODETYPE_CREATEOBJECT )
                        {
                                ret = SpiderScript_CreateObject(Block->Script,
-                                       ns,
                                        Node->FunctionCall.Name,
+                                       namespaces,
                                        Node->FunctionCall.NumArgs, params
                                        );
                        }
@@ -277,8 +281,10 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        else
                        {
                                ret = SpiderScript_ExecuteFunction(Block->Script,
-                                       ns, Node->FunctionCall.Name,
-                                       Node->FunctionCall.NumArgs, params
+                                       Node->FunctionCall.Name,
+                                       namespaces,
+                                       Node->FunctionCall.NumArgs, params,
+                                       NULL
                                        );
                        }
 
@@ -425,7 +431,9 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        break;
                }
                Block->CurNamespace = ns;
-               
+       
+               // TODO: Check type of child node (Scope, Constant or Function) 
+       
                ret = AST_ExecuteNode(Block, Node->Scope.Element);
                }
                break;
@@ -439,28 +447,8 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        case NODETYPE_ELEMENT:
                tmpobj = AST_ExecuteNode( Block, Node->Scope.Element );
                if(tmpobj == ERRPTR)    return ERRPTR;
-               if( !tmpobj || tmpobj->Type != SS_DATATYPE_OBJECT )
-               {
-                       AST_RuntimeError(Node->Scope.Element, "Unable to dereference a non-object");
-                       ret = ERRPTR;
-                       break ;
-               }
-               
-               for( i = 0; i < tmpobj->Object->Type->NAttributes; i ++ )
-               {
-                       if( strcmp(Node->Scope.Name, tmpobj->Object->Type->AttributeDefs[i].Name) == 0 )
-                       {
-                               ret = tmpobj->Object->Attributes[i];
-                               SpiderScript_ReferenceValue(ret);
-                               break;
-                       }
-               }
-               if( i == tmpobj->Object->Type->NAttributes )
-               {
-                       AST_RuntimeError(Node->Scope.Element, "Unknown attribute '%s' of class '%s'",
-                               Node->Scope.Name, tmpobj->Object->Type->Name);
-                       ret = ERRPTR;
-               }
+
+               ret = AST_ExecuteNode_Element(Block->Script, Node, tmpobj, Node->Scope.Name, ERRPTR);
                break;
 
        // Cast a value to another
@@ -483,37 +471,30 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                        return ERRPTR;
                }
                
-               if( !op1 || op1->Type != SS_DATATYPE_ARRAY )
+               if( !op2 || op2->Type != SS_DATATYPE_INTEGER )
                {
-                       // TODO: Implement "operator []" on objects
-                       AST_RuntimeError(Node, "Indexing non-array");
-                       ret = ERRPTR;
-                       break;
-               }
-               
-               if( (!op2 || op2->Type != SS_DATATYPE_INTEGER) && !Block->Script->Variant->bImplicitCasts ) {
-                       AST_RuntimeError(Node, "Array index is not an integer");
-                       ret = ERRPTR;
-                       break;
+                       if( !Block->Script->Variant->bImplicitCasts ) {
+                               AST_RuntimeError(Node, "Array index is not an integer");
+                               ret = ERRPTR;
+                               break;
+                       }
+                       else {
+                               tmpobj = SpiderScript_CastValueTo(SS_DATATYPE_INTEGER, op2);
+                               SpiderScript_DereferenceValue(op2);
+                               op2 = tmpobj;
+                       }
                }
-               
-               if( !op2 || op2->Type != SS_DATATYPE_INTEGER )
+       
+               if( !op1 )
                {
-                       tmpobj = SpiderScript_CastValueTo(SS_DATATYPE_INTEGER, op2);
                        SpiderScript_DereferenceValue(op2);
-                       op2 = tmpobj;
-               }
-               
-               if( op2->Integer >= op1->Array.Length ) {
-                       AST_RuntimeError(Node, "Array index out of bounds %i >= %i",
-                               op2->Integer, op1->Array.Length);
+                       AST_RuntimeError(Node, "Indexing NULL value");
                        ret = ERRPTR;
                        break;
                }
-               
-               ret = op1->Array.Items[ op2->Integer ];
-               SpiderScript_ReferenceValue(ret);
-               
+
+               ret = AST_ExecuteNode_Index(Block->Script, Node, op1, op2->Integer, ERRPTR);
+
                SpiderScript_DereferenceValue(op1);
                SpiderScript_DereferenceValue(op2);
                break;
@@ -532,6 +513,9 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
                ret = &Node->Constant;
                SpiderScript_ReferenceValue(ret);
                break;
+       case NODETYPE_NULL:
+               ret = NULL;
+               break;
        
        // --- Operations ---
        // Boolean Operations
@@ -571,113 +555,6 @@ 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 (-)
@@ -699,6 +576,12 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
        case NODETYPE_BITSHIFTLEFT:
        case NODETYPE_BITSHIFTRIGHT:
        case NODETYPE_BITROTATELEFT:
+       case NODETYPE_EQUALS:
+       case NODETYPE_NOTEQUALS:
+       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;
@@ -736,6 +619,7 @@ _return:
 
        return ret;
 }
+#endif
 
 tSpiderValue *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value)
 {
@@ -858,6 +742,90 @@ tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int
                if(Right && Right != preCastValue)      free(Right);
                return NULL;
        }
+
+       // Catch comparisons
+       switch(Operation)
+       {
+       case NODETYPE_EQUALS:
+       case NODETYPE_NOTEQUALS:
+       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_NOTEQUALS:        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)
@@ -944,6 +912,100 @@ tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int
        return ret;
 }
 
+tSpiderValue *AST_ExecuteNode_Index(tSpiderScript *Script, tAST_Node *Node,
+       tSpiderValue *Array, int Index, tSpiderValue *SaveValue)
+{
+       // Quick sanity check
+       if( !Array )
+       {
+               AST_RuntimeError(Node, "Indexing NULL, not a good idea");
+               return ERRPTR;
+       }
+
+       // Array?
+       if( SS_GETARRAYDEPTH(Array->Type) )
+       {
+               if( Index < 0 || Index >= Array->Array.Length ) {
+                       AST_RuntimeError(Node, "Array index out of bounds %i not in (0, %i]",
+                               Index, Array->Array.Length);
+                       return ERRPTR;
+               }
+               
+               if( SaveValue != ERRPTR )
+               {
+                       if( SaveValue && SaveValue->Type != SS_DOWNARRAY(Array->Type) ) {
+                               // TODO: Implicit casting
+                               AST_RuntimeError(Node, "Type mismatch assiging to array element");
+                               return ERRPTR;
+                       }
+                       SpiderScript_DereferenceValue( Array->Array.Items[Index] );
+                       Array->Array.Items[Index] = SaveValue;
+                       SpiderScript_ReferenceValue( Array->Array.Items[Index] );
+                       return NULL;
+               }
+               else
+               {
+                       SpiderScript_ReferenceValue( Array->Array.Items[Index] );
+                       return Array->Array.Items[Index];
+               }
+       }
+       else
+       {
+               AST_RuntimeError(Node, "TODO - Implement indexing on non-arrays (type = %x)",
+                       Array->Type);
+               return ERRPTR;
+       }
+}
+
+/**
+ * \brief Get/Set the value of an element/attribute of a class
+ * \param Script       Executing script
+ * \param Node Current execution node (only used for AST_RuntimeError)
+ * \param Object       Object value
+ * \param ElementName  Name of the attribute to be accessed
+ * \param SaveValue    Value to set the element to (if ERRPTR, element value is returned)
+ */
+tSpiderValue *AST_ExecuteNode_Element(tSpiderScript *Script, tAST_Node *Node,
+       tSpiderValue *Object, const char *ElementName, tSpiderValue *SaveValue)
+{
+        int    i;
+       tSpiderValue    *ret;   
+
+       if( !Object ) {
+               AST_RuntimeError(Node, "Tried to access an element of NULL");
+               return ERRPTR;
+       }
+       
+       switch( Object->Type )
+       {
+       case SS_DATATYPE_OBJECT: {
+               tSpiderObjectDef        *class = Object->Object->Type;
+               for( i = 0; i < class->NAttributes; i ++ )
+               {
+                       if( strcmp(ElementName, class->AttributeDefs[i].Name) == 0 )
+                       {
+                               if( SaveValue != ERRPTR ) {
+                                       Object->Object->Attributes[i] = SaveValue;
+                                       SpiderScript_ReferenceValue(SaveValue);
+                                       return NULL;
+                               }
+                               else {
+                                       ret = Object->Object->Attributes[i];
+                                       SpiderScript_ReferenceValue(ret);
+                                       return ret;
+                               }
+                       }
+               }
+               AST_RuntimeError(Node, "Unknown attribute '%s' of class '%s'",
+                       ElementName, class->Name);
+               return ERRPTR; }
+       default:
+               AST_RuntimeError(Node, "Unable to get element of type %i", Object->Type);
+               return ERRPTR;
+       }
+}
+
+#if USE_AST_EXEC
 /**
  * \brief Define a variable
  * \param Block        Current block state
@@ -1080,4 +1142,5 @@ void Variable_Destroy(tAST_Variable *Variable)
        SpiderScript_DereferenceValue(Variable->Object);
        free(Variable);
 }
+#endif
 

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