X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibspiderscript.so_src%2Fexec_ast.c;h=b9186087677214625810654d8fb962f1843de0a5;hb=ec6f426069129f0a3d207f743ecddceddb57db8e;hp=cf7416a417612420f4c79c9df86a2ccdf30bc133;hpb=1529dadb6c2170bf9899fbde46d06a3d9a392b52;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libspiderscript.so_src/exec_ast.c b/Usermode/Libraries/libspiderscript.so_src/exec_ast.c index cf7416a4..b9186087 100644 --- a/Usermode/Libraries/libspiderscript.so_src/exec_ast.c +++ b/Usermode/Libraries/libspiderscript.so_src/exec_ast.c @@ -6,33 +6,46 @@ #include "ast.h" // === PROTOTYPES === -void Object_Dereference(tSpiderObject *Object); -void Object_Reference(tSpiderObject *Object); -tSpiderObject *Object_CreateInteger(uint64_t Value); -tSpiderObject *Object_CreateReal(double Value); -tSpiderObject *Object_CreateString(int Length, const char *Data); -tSpiderObject *Object_CastTo(int Type, tSpiderObject *Source); - int Object_IsTrue(tSpiderObject *Value); +void Object_Dereference(tSpiderValue *Object); +void Object_Reference(tSpiderValue *Object); +tSpiderValue *Object_CreateInteger(uint64_t Value); +tSpiderValue *Object_CreateReal(double Value); +tSpiderValue *Object_CreateString(int Length, const char *Data); +tSpiderValue *Object_CastTo(int Type, tSpiderValue *Source); + int Object_IsTrue(tSpiderValue *Value); -tSpiderObject *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node); +tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node); tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name); -void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderObject *Value); -tSpiderObject *Variable_GetValue(tAST_BlockState *Block, const char *Name); +void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value); +tSpiderValue *Variable_GetValue(tAST_BlockState *Block, const char *Name); void Variable_Destroy(tAST_Variable *Variable); // === CODE === /** * \brief Dereference a created object */ -void Object_Dereference(tSpiderObject *Object) +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(tSpiderObject *Object) +void Object_Reference(tSpiderValue *Object) { if(!Object) return ; Object->ReferenceCount ++; @@ -41,9 +54,9 @@ void Object_Reference(tSpiderObject *Object) /** * \brief Create an integer object */ -tSpiderObject *Object_CreateInteger(uint64_t Value) +tSpiderValue *Object_CreateInteger(uint64_t Value) { - tSpiderObject *ret = malloc( sizeof(tSpiderObject) ); + tSpiderValue *ret = malloc( sizeof(tSpiderValue) ); ret->Type = SS_DATATYPE_INTEGER; ret->ReferenceCount = 1; ret->Integer = Value; @@ -53,9 +66,9 @@ tSpiderObject *Object_CreateInteger(uint64_t Value) /** * \brief Create an real number object */ -tSpiderObject *Object_CreateReal(double Value) +tSpiderValue *Object_CreateReal(double Value) { - tSpiderObject *ret = malloc( sizeof(tSpiderObject) ); + tSpiderValue *ret = malloc( sizeof(tSpiderValue) ); ret->Type = SS_DATATYPE_REAL; ret->ReferenceCount = 1; ret->Real = Value; @@ -65,9 +78,9 @@ tSpiderObject *Object_CreateReal(double Value) /** * \brief Create an string object */ -tSpiderObject *Object_CreateString(int Length, const char *Data) +tSpiderValue *Object_CreateString(int Length, const char *Data) { - tSpiderObject *ret = malloc( sizeof(tSpiderObject) + Length + 1 ); + tSpiderValue *ret = malloc( sizeof(tSpiderValue) + Length + 1 ); ret->Type = SS_DATATYPE_STRING; ret->ReferenceCount = 1; ret->String.Length = Length; @@ -79,13 +92,13 @@ tSpiderObject *Object_CreateString(int Length, const char *Data) /** * \brief Concatenate two strings */ -tSpiderObject *Object_StringConcat(tSpiderObject *Str1, tSpiderObject *Str2) +tSpiderValue *Object_StringConcat(tSpiderValue *Str1, tSpiderValue *Str2) { int newLen = 0; - tSpiderObject *ret; + tSpiderValue *ret; if(Str1) newLen += Str1->String.Length; if(Str2) newLen += Str2->String.Length; - ret = malloc( sizeof(tSpiderObject) + newLen + 1 ); + ret = malloc( sizeof(tSpiderValue) + newLen + 1 ); ret->Type = SS_DATATYPE_STRING; ret->ReferenceCount = 1; ret->String.Length = newLen; @@ -103,26 +116,29 @@ tSpiderObject *Object_StringConcat(tSpiderObject *Str1, tSpiderObject *Str2) /** * \brief Cast one object to another + * \brief Type Destination type + * \brief Source Input data */ -tSpiderObject *Object_CastTo(int Type, tSpiderObject *Source) +tSpiderValue *Object_CastTo(int Type, tSpiderValue *Source) { - tSpiderObject *ret = ERRPTR; + tSpiderValue *ret = ERRPTR; // Check if anything needs to be done if( Source->Type == Type ) { Object_Reference(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; case SS_DATATYPE_INTEGER: - ret = malloc(sizeof(tSpiderObject)); + ret = malloc(sizeof(tSpiderValue)); ret->Type = SS_DATATYPE_INTEGER; ret->ReferenceCount = 1; switch(Source->Type) @@ -148,9 +164,12 @@ tSpiderObject *Object_CastTo(int Type, tSpiderObject *Source) /** * \brief Condenses a value down to a boolean */ -int Object_IsTrue(tSpiderObject *Value) +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(tSpiderObject *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; } @@ -177,11 +202,11 @@ int Object_IsTrue(tSpiderObject *Value) /** * \brief Execute an AST node and return its value */ -tSpiderObject *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) +tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) { tAST_Node *node; - tSpiderObject *ret, *tmpobj; - tSpiderObject *op1, *op2; // Binary operations + tSpiderValue *ret = NULL, *tmpobj; + tSpiderValue *op1, *op2; // Binary operations int cmp; // Used in comparisons switch(Node->Type) @@ -194,23 +219,18 @@ tSpiderObject *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 @@ tSpiderObject *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) Variable_Destroy( blockInfo.FirstVar ); blockInfo.FirstVar = nextVar; } + + if( blockInfo.RetVal ) + Block->RetVal = blockInfo.RetVal; } break; @@ -243,7 +266,7 @@ tSpiderObject *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) } // Logical block (used to allocate `params`) { - tSpiderObject *params[nParams]; + tSpiderValue *params[nParams]; int i=0; for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling) { params[i] = AST_ExecuteNode(Block, node); @@ -267,9 +290,46 @@ tSpiderObject *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 @@ tSpiderObject *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 @@ tSpiderObject *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) break; } break; + // Integer Operations case SS_DATATYPE_INTEGER: switch(Node->Type) { @@ -498,11 +567,27 @@ tSpiderObject *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 @@ -555,7 +640,7 @@ tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Nam /** * \brief Set the value of a variable */ -void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderObject *Value) +void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value) { tAST_Variable *var; tAST_BlockState *bs; @@ -594,7 +679,7 @@ void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderObject * /** * \brief Get the value of a variable */ -tSpiderObject *Variable_GetValue(tAST_BlockState *Block, const char *Name) +tSpiderValue *Variable_GetValue(tAST_BlockState *Block, const char *Name) { tAST_Variable *var; tAST_BlockState *bs;