#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 ++;
/**
* \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;
/**
* \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;
/**
* \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;
/**
* \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;
/**
* \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)
/**
* \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:
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;
}
/**
* \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)
{
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)
Variable_Destroy( blockInfo.FirstVar );
blockInfo.FirstVar = nextVar;
}
+
+ if( blockInfo.RetVal )
+ Block->RetVal = blockInfo.RetVal;
}
break;
}
// 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);
}
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
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)
{
break;
}
break;
+ // Integer Operations
case SS_DATATYPE_INTEGER:
switch(Node->Type)
{
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
/**
* \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;
/**
* \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;