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, tSpiderValue *Value);
+ int 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);
void Object_Dereference(tSpiderValue *Object)
{
if(!Object) return ;
+ if(Object == ERRPTR) return ;
Object->ReferenceCount --;
if( Object->ReferenceCount == 0 ) {
switch( (enum eSpiderScript_DataTypes) Object->Type )
tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source)
{
tSpiderValue *ret = ERRPTR;
+ int len = 0;
+
+ if( !Source ) return NULL;
+
// Check if anything needs to be done
if( Source->Type == Type ) {
Object_Reference(Source);
switch( (enum eSpiderScript_DataTypes)Type )
{
case SS_DATATYPE_UNDEF:
- case SS_DATATYPE_NULL:
case SS_DATATYPE_ARRAY:
case SS_DATATYPE_OPAQUE:
fprintf(stderr, "SpiderScript_CastValueTo - Invalid cast to %i\n", Type);
break;
}
break;
+
+ case SS_DATATYPE_STRING:
+ switch(Source->Type)
+ {
+ case SS_DATATYPE_INTEGER: len = snprintf(NULL, 0, "%li", Source->Integer); break;
+ case SS_DATATYPE_REAL: snprintf(NULL, 0, "%f", Source->Real); break;
+ default: break;
+ }
+ ret = malloc(sizeof(tSpiderValue) + len + 1);
+ ret->Type = SS_DATATYPE_STRING;
+ ret->ReferenceCount = 1;
+ ret->String.Length = len;
+ switch(Source->Type)
+ {
+ case SS_DATATYPE_INTEGER: sprintf(ret->String.Data, "%li", Source->Integer); break;
+ case SS_DATATYPE_REAL: sprintf(ret->String.Data, "%f", Source->Real); break;
+ default:
+ fprintf(stderr, "SpiderScript_CastValueTo - Invalid cast from %i\n", Source->Type);
+ free(ret);
+ ret = ERRPTR;
+ break;
+ }
+ break;
+
default:
fprintf(stderr, "BUG REPORT: Unimplemented cast target\n");
break;
switch( (enum eSpiderScript_DataTypes)Value->Type )
{
case SS_DATATYPE_UNDEF:
- case SS_DATATYPE_NULL:
return 0;
case SS_DATATYPE_INTEGER:
switch( (enum eSpiderScript_DataTypes)Value->Type )
{
case SS_DATATYPE_UNDEF: return strdup("undefined");
- case SS_DATATYPE_NULL: return strdup("null type");
case SS_DATATYPE_INTEGER:
ret = malloc( sizeof(Value->Integer)*2 + 3 );
}
ret = AST_ExecuteNode(Block, Node->Assign.Value);
if(ret != ERRPTR)
- Variable_SetValue( Block, Node->Assign.Dest->Variable.Name, ret );
+ {
+ if( Variable_SetValue( Block, Node->Assign.Dest->Variable.Name, ret ) ) {
+ Object_Dereference( ret );
+ fprintf(stderr, "on line %i\n", Node->Line);
+ return ERRPTR;
+ }
+ }
break;
// Function Call
return ERRPTR;
}
- // No conversion done for NULL
- // TODO: Determine if this will ever be needed
- if( op1->Type == SS_DATATYPE_NULL )
- {
- // NULLs always typecheck
- ret = SpiderScript_CreateInteger(op2->Type == SS_DATATYPE_NULL);
- break;
- }
-
// Convert types
if( op1->Type != op2->Type ) {
// If dynamically typed, convert op2 to op1's type
}
// If statically typed, this should never happen, but catch it anyway
else {
- fprintf(stderr, "PARSER ERROR: Statically typed implicit cast\n");
+ fprintf(stderr, "PARSER ERROR: Statically typed implicit cast (line %i)\n",
+ Node->Line);
ret = ERRPTR;
break;
}
// Do operation
switch(op1->Type)
{
- // - NULL
- case SS_DATATYPE_NULL: break;
// - String Compare (does a strcmp, well memcmp)
case SS_DATATYPE_STRING:
// Call memcmp to do most of the work
}
// If statically typed, this should never happen, but catch it anyway
else {
- fprintf(stderr, "PARSER ERROR: Statically typed implicit cast\n");
+ fprintf(stderr,
+ "PARSER ERROR: Statically typed implicit cast (from %i to %i)\n",
+ op2->Type, op1->Type
+ );
ret = ERRPTR;
break;
}
}
+ // NULL Check
+ if( op1 == NULL || op2 == NULL ) {
+ ret = NULL;
+ break;
+ }
+
// Do operation
switch(op1->Type)
{
- case SS_DATATYPE_NULL: break;
// String Concatenation
case SS_DATATYPE_STRING:
switch(Node->Type)
/**
* \brief Set the value of a variable
+ * \return Boolean Failure
*/
-void Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value)
+int Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value)
{
tAST_Variable *var;
tAST_BlockState *bs;
if( !Block->Script->Variant->bDyamicTyped
&& (Value && var->Type != Value->Type) ) {
fprintf(stderr, "ERROR: Type mismatch assigning to '%s'\n", Name);
- return ;
+ return -2;
}
Object_Reference(Value);
Object_Dereference(var->Object);
var->Object = Value;
- return ;
+ return 0;
}
}
}
var = Variable_Define(Block, Value->Type, Name);
Object_Reference(Value);
var->Object = Value;
+ return 0;
}
else
{
fprintf(stderr, "ERROR: Variable '%s' set while undefined\n", Name);
+ return -1;
}
}