10 extern tSpiderFunction *gpExports_First;
13 void Object_Dereference(tSpiderValue *Object);
14 void Object_Reference(tSpiderValue *Object);
15 tSpiderValue *SpiderScript_CreateInteger(uint64_t Value);
16 tSpiderValue *SpiderScript_CreateReal(double Value);
17 tSpiderValue *SpiderScript_CreateString(int Length, const char *Data);
18 tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
19 int SpiderScript_IsValueTrue(tSpiderValue *Value);
20 void SpiderScript_FreeValue(tSpiderValue *Value);
21 char *SpiderScript_DumpValue(tSpiderValue *Value);
23 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
24 tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpiderValue *Left, tSpiderValue *Right);
26 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name);
27 int Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value);
28 tSpiderValue *Variable_GetValue(tAST_BlockState *Block, const char *Name);
29 void Variable_Destroy(tAST_Variable *Variable);
31 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
35 * \brief Dereference a created object
37 void Object_Dereference(tSpiderValue *Object)
40 if(Object == ERRPTR) return ;
41 Object->ReferenceCount --;
42 // printf("%p Dereferenced (%i)\n", Object, Object->ReferenceCount);
43 if( Object->ReferenceCount == 0 ) {
44 switch( (enum eSpiderScript_DataTypes) Object->Type )
46 case SS_DATATYPE_OBJECT:
47 Object->Object->Type->Destructor( Object->Object );
49 case SS_DATATYPE_OPAQUE:
50 Object->Opaque.Destroy( Object->Opaque.Data );
59 void Object_Reference(tSpiderValue *Object)
62 Object->ReferenceCount ++;
63 // printf("%p Referenced (%i)\n", Object, Object->ReferenceCount);
67 * \brief Allocate and initialise a SpiderScript object
69 tSpiderObject *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes)
71 int size = sizeof(tSpiderObject) + Class->NAttributes * sizeof(tSpiderValue*) + ExtraBytes;
72 tSpiderObject *ret = malloc(size);
75 ret->ReferenceCount = 1;
76 ret->OpaqueData = &ret->Attributes[ Class->NAttributes ];
77 memset( ret->Attributes, 0, Class->NAttributes * sizeof(tSpiderValue*) );
83 * \brief Create an integer object
85 tSpiderValue *SpiderScript_CreateInteger(uint64_t Value)
87 tSpiderValue *ret = malloc( sizeof(tSpiderValue) );
88 ret->Type = SS_DATATYPE_INTEGER;
89 ret->ReferenceCount = 1;
95 * \brief Create an real number object
97 tSpiderValue *SpiderScript_CreateReal(double Value)
99 tSpiderValue *ret = malloc( sizeof(tSpiderValue) );
100 ret->Type = SS_DATATYPE_REAL;
101 ret->ReferenceCount = 1;
107 * \brief Create an string object
109 tSpiderValue *SpiderScript_CreateString(int Length, const char *Data)
111 tSpiderValue *ret = malloc( sizeof(tSpiderValue) + Length + 1 );
112 ret->Type = SS_DATATYPE_STRING;
113 ret->ReferenceCount = 1;
114 ret->String.Length = Length;
116 memcpy(ret->String.Data, Data, Length);
118 memset(ret->String.Data, 0, Length);
119 ret->String.Data[Length] = '\0';
124 * \brief Concatenate two strings
126 tSpiderValue *Object_StringConcat(tSpiderValue *Str1, tSpiderValue *Str2)
130 if(Str1) newLen += Str1->String.Length;
131 if(Str2) newLen += Str2->String.Length;
132 ret = malloc( sizeof(tSpiderValue) + newLen + 1 );
133 ret->Type = SS_DATATYPE_STRING;
134 ret->ReferenceCount = 1;
135 ret->String.Length = newLen;
137 memcpy(ret->String.Data, Str1->String.Data, Str1->String.Length);
140 memcpy(ret->String.Data+Str1->String.Length, Str2->String.Data, Str2->String.Length);
142 memcpy(ret->String.Data, Str2->String.Data, Str2->String.Length);
144 ret->String.Data[ newLen ] = '\0';
149 * \brief Cast one object to another
150 * \brief Type Destination type
151 * \brief Source Input data
153 tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source)
155 tSpiderValue *ret = ERRPTR;
158 if( !Source ) return NULL;
160 // Check if anything needs to be done
161 if( Source->Type == Type ) {
162 Object_Reference(Source);
167 if( Source->Type == SS_DATATYPE_OBJECT )
169 const char *name = NULL;
172 case SS_DATATYPE_INTEGER: name = "cast Integer"; break;
173 case SS_DATATYPE_REAL: name = "cast Real"; break;
174 case SS_DATATYPE_STRING: name = "cast String"; break;
175 case SS_DATATYPE_ARRAY: name = "cast Array"; break;
177 AST_RuntimeError(NULL, "Invalid cast to %i from Object", Type);
182 ret = Object_ExecuteMethod(Left->Object, fcnname, Right);
185 // Fall through and try casting (which will usually fail)
190 switch( (enum eSpiderScript_DataTypes)Type )
192 case SS_DATATYPE_UNDEF:
193 case SS_DATATYPE_ARRAY:
194 case SS_DATATYPE_OPAQUE:
195 case SS_DATATYPE_OBJECT:
196 AST_RuntimeError(NULL, "Invalid cast to %i", Type);
199 case SS_DATATYPE_INTEGER:
200 ret = malloc(sizeof(tSpiderValue));
201 ret->Type = SS_DATATYPE_INTEGER;
202 ret->ReferenceCount = 1;
205 case SS_DATATYPE_INTEGER: break; // Handled above
206 case SS_DATATYPE_STRING: ret->Integer = atoi(Source->String.Data); break;
207 case SS_DATATYPE_REAL: ret->Integer = Source->Real; break;
209 AST_RuntimeError(NULL, "Invalid cast from %i to Integer", Source->Type);
216 case SS_DATATYPE_STRING:
219 case SS_DATATYPE_INTEGER: len = snprintf(NULL, 0, "%li", Source->Integer); break;
220 case SS_DATATYPE_REAL: snprintf(NULL, 0, "%f", Source->Real); break;
223 ret = malloc(sizeof(tSpiderValue) + len + 1);
224 ret->Type = SS_DATATYPE_STRING;
225 ret->ReferenceCount = 1;
226 ret->String.Length = len;
229 case SS_DATATYPE_INTEGER: sprintf(ret->String.Data, "%li", Source->Integer); break;
230 case SS_DATATYPE_REAL: sprintf(ret->String.Data, "%f", Source->Real); break;
232 AST_RuntimeError(NULL, "Invalid cast from %i to String", Source->Type);
240 AST_RuntimeError(NULL, "BUG - BUG REPORT: Unimplemented cast target");
248 * \brief Condenses a value down to a boolean
250 int SpiderScript_IsValueTrue(tSpiderValue *Value)
252 if( Value == ERRPTR ) return 0;
253 if( Value == NULL ) return 0;
255 switch( (enum eSpiderScript_DataTypes)Value->Type )
257 case SS_DATATYPE_UNDEF:
260 case SS_DATATYPE_INTEGER:
261 return !!Value->Integer;
263 case SS_DATATYPE_REAL:
264 return (-.5f < Value->Real && Value->Real < 0.5f);
266 case SS_DATATYPE_STRING:
267 return Value->String.Length > 0;
269 case SS_DATATYPE_OBJECT:
270 return Value->Object != NULL;
272 case SS_DATATYPE_OPAQUE:
273 return Value->Opaque.Data != NULL;
275 case SS_DATATYPE_ARRAY:
276 return Value->Array.Length > 0;
278 AST_RuntimeError(NULL, "Unknown type %i in SpiderScript_IsValueTrue", Value->Type);
285 * \brief Free a value
286 * \note Just calls Object_Dereference
288 void SpiderScript_FreeValue(tSpiderValue *Value)
290 Object_Dereference(Value);
294 * \brief Dump a value into a string
295 * \return Heap string
297 char *SpiderScript_DumpValue(tSpiderValue *Value)
300 if( Value == ERRPTR )
301 return strdup("ERRPTR");
303 return strdup("null");
305 switch( (enum eSpiderScript_DataTypes)Value->Type )
307 case SS_DATATYPE_UNDEF: return strdup("undefined");
309 case SS_DATATYPE_INTEGER:
310 ret = malloc( sizeof(Value->Integer)*2 + 3 );
311 sprintf(ret, "0x%lx", Value->Integer);
314 case SS_DATATYPE_REAL:
315 ret = malloc( sprintf(NULL, "%f", Value->Real) + 1 );
316 sprintf(ret, "%f", Value->Real);
319 case SS_DATATYPE_STRING:
320 ret = malloc( Value->String.Length + 3 );
322 strcpy(ret+1, Value->String.Data);
323 ret[Value->String.Length+1] = '"';
324 ret[Value->String.Length+2] = '\0';
327 case SS_DATATYPE_OBJECT:
328 ret = malloc( sprintf(NULL, "{%s *%p}", Value->Object->Type->Name, Value->Object) + 1 );
329 sprintf(ret, "{%s *%p}", Value->Object->Type->Name, Value->Object);
332 case SS_DATATYPE_OPAQUE:
333 ret = malloc( sprintf(NULL, "*%p", Value->Opaque.Data) + 1 );
334 sprintf(ret, "*%p", Value->Opaque.Data);
337 case SS_DATATYPE_ARRAY:
338 return strdup("Array");
341 AST_RuntimeError(NULL, "Unknown type %i in Object_Dump", Value->Type);
348 * \brief Execute a script function
349 * \param Script Script context to execute in
350 * \param Function Function name to execute
351 * \param NArguments Number of arguments to pass
352 * \param Arguments Arguments passed
354 tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
355 tSpiderNamespace *Namespace, const char *Function,
356 int NArguments, tSpiderValue **Arguments)
358 char *trueName = NULL;
359 int bFound = 0; // Used to keep nesting levels down
360 tSpiderValue *ret = ERRPTR;
361 tSpiderFunction *fcn;
363 // First: Find the function in the script
365 tAST_Function *astFcn;
366 for( astFcn = Script->Script->Functions; astFcn; astFcn = astFcn->Next )
368 if( strcmp(astFcn->Name, Function) == 0 )
378 // Build a block State
382 bs.BaseNamespace = &Script->Variant->RootNamespace;
383 bs.CurNamespace = NULL;
387 for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
390 Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name);
391 if( i >= NArguments ) break; // TODO: Return gracefully
392 Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]);
396 ret = AST_ExecuteNode(&bs, astFcn->Code);
397 Object_Dereference(ret); // Dereference output of last block statement
398 ret = bs.RetVal; // Set to return value of block
403 tAST_Variable *nextVar = bs.FirstVar->Next;
404 Variable_Destroy( bs.FirstVar );
405 bs.FirstVar = nextVar;
410 // Didn't find it in script?
413 fcn = NULL; // Just to allow the below code to be neat
415 // Second: Scan current namespace
416 if( !fcn && Namespace )
418 for( fcn = Namespace->Functions; fcn; fcn = fcn->Next )
420 if( strcmp( fcn->Name, Function ) == 0 )
425 // Third: Search the variant's global exports
428 for( fcn = Script->Variant->Functions; fcn; fcn = fcn->Next )
430 if( strcmp( fcn->Name, Function ) == 0 )
435 // Fourth: Search language exports
438 for( fcn = gpExports_First; fcn; fcn = fcn->Next )
440 if( strcmp( fcn->Name, Function ) == 0 )
448 // TODO: Type Checking
449 ret = fcn->Handler( Script, NArguments, Arguments );
457 fprintf(stderr, "Undefined reference to '%s'\n", trueName);
465 * \brief Execute an object method function
466 * \param Script Script context to execute in
467 * \param Function Function name to execute
468 * \param NArguments Number of arguments to pass
469 * \param Arguments Arguments passed
471 tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script, tSpiderObject *Object,
472 const char *MethodName,
473 int NArguments, tSpiderValue **Arguments)
475 tSpiderFunction *fcn;
477 tSpiderValue *newargs[NArguments+1];
480 for( fcn = Object->Type->Methods; fcn; fcn = fcn->Next )
482 if( strcmp(fcn->Name, MethodName) == 0 )
488 AST_RuntimeError(NULL, "Class '%s' does not have a method '%s'",
489 Object->Type->Name, MethodName);
493 this.Type = SS_DATATYPE_OBJECT;
494 this.ReferenceCount = 1;
495 this.Object = Object;
498 memcpy(&newargs[1], Arguments, NArguments*sizeof(tSpiderValue*));
500 // TODO: Type Checking
501 for( i = 0; fcn->ArgTypes[i]; i ++ )
503 if( i >= NArguments ) {
504 AST_RuntimeError(NULL, "Argument count mismatch (%i passed)",
508 if( Arguments[i] && Arguments[i]->Type != fcn->ArgTypes[i] )
510 AST_RuntimeError(NULL, "Argument type mismatch (%i, expected %i)",
511 Arguments[i]->Type, fcn->ArgTypes[i]);
516 return fcn->Handler(Script, NArguments+1, newargs);
520 * \brief Execute a script function
521 * \param Script Script context to execute in
522 * \param Function Function name to execute
523 * \param NArguments Number of arguments to pass
524 * \param Arguments Arguments passed
526 tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
527 tSpiderNamespace *Namespace, const char *ClassName,
528 int NArguments, tSpiderValue **Arguments)
530 int bFound = 0; // Used to keep nesting levels down
531 tSpiderValue *ret = ERRPTR;
532 tSpiderObjectDef *class;
534 // First: Find the function in the script
535 // TODO: Implement scripted classes
538 tAST_Function *astClass;
539 for( astClass = Script->Script->Classes; astClass; astClass = astClass->Next )
541 if( strcmp(astClass->Name, ClassName) == 0 )
551 // Build a block State
555 bs.BaseNamespace = &Script->Variant->RootNamespace;
556 bs.CurNamespace = NULL;
560 for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ )
563 Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name);
564 if( i >= NArguments ) break; // TODO: Return gracefully
565 Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]);
569 ret = AST_ExecuteNode(&bs, astFcn->Code);
570 Object_Dereference(ret); // Dereference output of last block statement
571 ret = bs.RetVal; // Set to return value of block
576 tAST_Variable *nextVar = bs.FirstVar->Next;
577 Variable_Destroy( bs.FirstVar );
578 bs.FirstVar = nextVar;
584 // Didn't find it in script?
587 class = NULL; // Just to allow the below code to be neat
589 // Second: Scan current namespace
590 if( !class && Namespace )
592 for( class = Namespace->Classes; class; class = class->Next )
594 if( strcmp( class->Name, ClassName ) == 0 )
600 // Third: Search the variant's global exports
603 for( class = Script->Variant->Classes; class; class = fcn->Next )
605 if( strcmp( class->Name, Function ) == 0 )
612 // Fourth: Search language exports
615 for( class = gpExports_First; class; class = fcn->Next )
617 if( strcmp( class->Name, ClassName ) == 0 )
627 // TODO: Type Checking
628 obj = class->Constructor( NArguments, Arguments );
629 if( obj == NULL || obj == ERRPTR )
632 ret = malloc( sizeof(tSpiderValue) );
633 ret->Type = SS_DATATYPE_OBJECT;
634 ret->ReferenceCount = 1;
643 fprintf(stderr, "Undefined reference to '%s'\n", ClassName);
652 * \brief Execute an AST node and return its value
654 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
657 tSpiderValue *ret = NULL, *tmpobj;
658 tSpiderValue *op1, *op2; // Binary operations
659 int cmp; // Used in comparisons
665 case NODETYPE_NOP: ret = NULL; break;
670 tAST_BlockState blockInfo;
671 memcpy(&blockInfo, Block, sizeof(tAST_BlockState));
672 blockInfo.FirstVar = NULL;
673 blockInfo.RetVal = NULL;
674 blockInfo.Parent = Block;
676 for(node = Node->Block.FirstChild; node && !blockInfo.RetVal; node = node->NextSibling )
678 tmpobj = AST_ExecuteNode(&blockInfo, node);
679 if(tmpobj == ERRPTR) { // Error check
683 if(tmpobj) Object_Dereference(tmpobj); // Free unused value
686 // Clean up variables
687 while(blockInfo.FirstVar)
689 tAST_Variable *nextVar = blockInfo.FirstVar->Next;
690 Variable_Destroy( blockInfo.FirstVar );
691 blockInfo.FirstVar = nextVar;
694 if( blockInfo.RetVal )
695 Block->RetVal = blockInfo.RetVal;
701 case NODETYPE_ASSIGN:
702 if( Node->Assign.Dest->Type != NODETYPE_VARIABLE ) {
703 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
706 ret = AST_ExecuteNode(Block, Node->Assign.Value);
710 if( Node->Assign.Operation != NODETYPE_NOP )
712 tSpiderValue *varVal = Variable_GetValue(Block, Node->Assign.Dest->Variable.Name);
714 value = AST_ExecuteNode_BinOp(Block, Node->Assign.Operation, varVal, ret);
715 if( value == ERRPTR )
717 if(ret) Object_Dereference(ret);
718 Object_Dereference(varVal);
722 if( Variable_SetValue( Block, Node->Assign.Dest->Variable.Name, ret ) ) {
723 Object_Dereference( ret );
729 case NODETYPE_METHODCALL:
730 case NODETYPE_FUNCTIONCALL:
731 case NODETYPE_CREATEOBJECT:
734 for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling) {
737 // Logical block (used to allocate `params`)
739 tSpiderValue *params[nParams];
741 for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling)
743 params[i] = AST_ExecuteNode(Block, node);
744 if( params[i] == ERRPTR ) {
745 while(i--) Object_Dereference(params[i]);
752 if( !Block->CurNamespace )
753 Block->CurNamespace = Block->BaseNamespace;
756 if( Node->Type == NODETYPE_CREATEOBJECT )
758 ret = SpiderScript_CreateObject(Block->Script,
760 Node->FunctionCall.Name,
764 else if( Node->Type == NODETYPE_METHODCALL )
766 tSpiderValue *obj = AST_ExecuteNode(Block, Node->FunctionCall.Object);
767 if( !obj || obj->Type != SS_DATATYPE_OBJECT ) {
768 AST_RuntimeError(Node->FunctionCall.Object,
769 "Type Mismatch - Required SS_DATATYPE_OBJECT for method call");
770 while(i--) Object_Dereference(params[i]);
774 ret = SpiderScript_ExecuteMethod(Block->Script,
775 obj->Object, Node->FunctionCall.Name,
778 Object_Dereference(obj);
782 ret = SpiderScript_ExecuteFunction(Block->Script,
783 Block->CurNamespace, Node->FunctionCall.Name,
789 // Dereference parameters
790 while(i--) Object_Dereference(params[i]);
799 ret = AST_ExecuteNode(Block, Node->If.Condition);
800 if( SpiderScript_IsValueTrue(ret) ) {
801 Object_Dereference(AST_ExecuteNode(Block, Node->If.True));
804 Object_Dereference(AST_ExecuteNode(Block, Node->If.False));
806 Object_Dereference(ret);
812 ret = AST_ExecuteNode(Block, Node->For.Init);
813 if( Node->For.bCheckAfter )
816 Object_Dereference(ret);
817 ret = AST_ExecuteNode(Block, Node->For.Code);
818 Object_Dereference(ret);
819 ret = AST_ExecuteNode(Block, Node->For.Increment);
820 Object_Dereference(ret);
821 ret = AST_ExecuteNode(Block, Node->For.Condition);
822 } while( SpiderScript_IsValueTrue(ret) );
826 Object_Dereference(ret);
827 ret = AST_ExecuteNode(Block, Node->For.Condition);
828 while( SpiderScript_IsValueTrue(ret) ) {
829 Object_Dereference(ret);
830 ret = AST_ExecuteNode(Block, Node->For.Code);
831 Object_Dereference(ret);
832 ret = AST_ExecuteNode(Block, Node->For.Increment);
833 Object_Dereference(ret);
834 ret = AST_ExecuteNode(Block, Node->For.Condition);
837 Object_Dereference(ret);
842 case NODETYPE_RETURN:
843 ret = AST_ExecuteNode(Block, Node->UniOp.Value);
844 Block->RetVal = ret; // Return value set
845 ret = NULL; // the `return` statement does not return a value
849 case NODETYPE_DEFVAR:
851 if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name) == ERRPTR )
858 tSpiderNamespace *ns;
860 // Set current namespace if unset
861 if( !Block->CurNamespace )
862 Block->CurNamespace = Block->BaseNamespace;
864 // Empty string means use the root namespace
865 if( Node->Scope.Name[0] == '\0' )
867 ns = &Block->Script->Variant->RootNamespace;
871 // Otherwise scan the current namespace for the element
872 for( ns = Block->CurNamespace->FirstChild; ns; ns = ns->Next )
874 if( strcmp(ns->Name, Node->Scope.Name) == 0 )
879 AST_RuntimeError(Node, "Unknown namespace '%s'", Node->Scope.Name);
883 Block->CurNamespace = ns;
885 ret = AST_ExecuteNode(Block, Node->Scope.Element);
890 case NODETYPE_VARIABLE:
891 ret = Variable_GetValue( Block, Node->Variable.Name );
894 // Element of an Object
895 case NODETYPE_ELEMENT:
896 tmpobj = AST_ExecuteNode( Block, Node->Scope.Element );
897 if( tmpobj->Type != SS_DATATYPE_OBJECT )
899 AST_RuntimeError(Node->Scope.Element, "Unable to dereference a non-object");
904 for( i = 0; i < tmpobj->Object->Type->NAttributes; i ++ )
906 if( strcmp(Node->Scope.Name, tmpobj->Object->Type->AttributeDefs[i].Name) == 0 )
908 ret = tmpobj->Object->Attributes[i];
909 Object_Reference(ret);
913 if( i == tmpobj->Object->Type->NAttributes )
915 AST_RuntimeError(Node->Scope.Element, "Unknown attribute '%s' of class '%s'",
916 Node->Scope.Name, tmpobj->Object->Type->Name);
921 // Cast a value to another
924 tmpobj = AST_ExecuteNode(Block, Node->Cast.Value);
925 ret = SpiderScript_CastValueTo( Node->Cast.DataType, tmpobj );
926 Object_Dereference(tmpobj);
930 // Index into an array
932 op1 = AST_ExecuteNode(Block, Node->BinOp.Left); // Array
933 op2 = AST_ExecuteNode(Block, Node->BinOp.Right); // Offset
935 if( op1->Type != SS_DATATYPE_ARRAY )
937 // TODO: Implement "operator []" on objects
938 AST_RuntimeError(Node, "Indexing non-array");
943 if( op2->Type != SS_DATATYPE_INTEGER && !Block->Script->Variant->bImplicitCasts ) {
944 AST_RuntimeError(Node, "Array index is not an integer");
949 if( op2->Type != SS_DATATYPE_INTEGER )
951 tmpobj = SpiderScript_CastValueTo(SS_DATATYPE_INTEGER, op2);
952 Object_Dereference(op2);
956 if( op2->Integer >= op1->Array.Length ) {
957 AST_RuntimeError(Node, "Array index out of bounds %i >= %i",
958 op2->Integer, op1->Array.Length);
963 ret = op1->Array.Items[ op2->Integer ];
964 Object_Reference(ret);
966 Object_Dereference(op1);
967 Object_Dereference(op2);
970 // TODO: Implement runtime constants
971 case NODETYPE_CONSTANT:
972 // TODO: Scan namespace for function
973 AST_RuntimeError(Node, "TODO - Runtime Constants");
978 case NODETYPE_STRING: ret = SpiderScript_CreateString( Node->String.Length, Node->String.Data ); break;
979 case NODETYPE_INTEGER: ret = SpiderScript_CreateInteger( Node->Integer ); break;
980 case NODETYPE_REAL: ret = SpiderScript_CreateReal( Node->Real ); break;
982 // --- Operations ---
983 // Boolean Operations
984 case NODETYPE_LOGICALAND: // Logical AND (&&)
985 case NODETYPE_LOGICALOR: // Logical OR (||)
986 case NODETYPE_LOGICALXOR: // Logical XOR (^^)
987 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
988 if(op1 == ERRPTR) return ERRPTR;
989 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
991 Object_Dereference(op1);
997 case NODETYPE_LOGICALAND:
998 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) && SpiderScript_IsValueTrue(op2) );
1000 case NODETYPE_LOGICALOR:
1001 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) || SpiderScript_IsValueTrue(op2) );
1003 case NODETYPE_LOGICALXOR:
1004 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) ^ SpiderScript_IsValueTrue(op2) );
1009 // Free intermediate objects
1010 Object_Dereference(op1);
1011 Object_Dereference(op2);
1015 case NODETYPE_EQUALS:
1016 case NODETYPE_LESSTHAN:
1017 case NODETYPE_GREATERTHAN:
1018 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
1019 if(op1 == ERRPTR) return ERRPTR;
1020 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
1022 Object_Dereference(op1);
1027 if( op1->Type != op2->Type ) {
1028 // If dynamically typed, convert op2 to op1's type
1029 if(Block->Script->Variant->bDyamicTyped)
1032 op2 = SpiderScript_CastValueTo(op1->Type, op2);
1033 Object_Dereference(tmpobj);
1035 Object_Dereference(op1);
1039 // If statically typed, this should never happen, but catch it anyway
1041 AST_RuntimeError(Node, "Statically typed implicit cast");
1049 // - String Compare (does a strcmp, well memcmp)
1050 case SS_DATATYPE_STRING:
1051 // Call memcmp to do most of the work
1053 op1->String.Data, op2->String.Data,
1054 (op1->String.Length < op2->String.Length) ? op1->String.Length : op2->String.Length
1056 // Handle reaching the end of the string
1058 if( op1->String.Length == op2->String.Length )
1060 else if( op1->String.Length < op2->String.Length )
1067 // - Integer Comparisons
1068 case SS_DATATYPE_INTEGER:
1069 if( op1->Integer == op2->Integer )
1071 else if( op1->Integer < op2->Integer )
1077 AST_RuntimeError(Node, "TODO - Comparison of type %i", op1->Type);
1082 // Free intermediate objects
1083 Object_Dereference(op1);
1084 Object_Dereference(op2);
1093 case NODETYPE_EQUALS: ret = SpiderScript_CreateInteger(cmp == 0); break;
1094 case NODETYPE_LESSTHAN: ret = SpiderScript_CreateInteger(cmp < 0); break;
1095 case NODETYPE_GREATERTHAN: ret = SpiderScript_CreateInteger(cmp > 0); break;
1097 AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Node->Type);
1103 // General Binary Operations
1105 case NODETYPE_SUBTRACT:
1106 case NODETYPE_MULTIPLY:
1107 case NODETYPE_DIVIDE:
1108 case NODETYPE_MODULO:
1109 case NODETYPE_BWAND:
1111 case NODETYPE_BWXOR:
1112 case NODETYPE_BITSHIFTLEFT:
1113 case NODETYPE_BITSHIFTRIGHT:
1114 case NODETYPE_BITROTATELEFT:
1116 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
1117 if(op1 == ERRPTR) return ERRPTR;
1118 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
1120 Object_Dereference(op1);
1124 ret = AST_ExecuteNode_BinOp(Block, Node->Type, op1, op2);
1126 // Free intermediate objects
1127 Object_Dereference(op1);
1128 Object_Dereference(op2);
1133 // AST_RuntimeError(Node, "BUG - SpiderScript AST_ExecuteNode Unimplemented %i\n", Node->Type);
1140 tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpiderValue *Left, tSpiderValue *Right)
1142 tSpiderValue *preCastValue = Right;
1146 if( Left && Right && Left->Type != Right->Type )
1150 // - Operator overload functions
1151 if( Left->Type == SS_DATATYPE_OBJECT )
1153 const char *fcnname;
1156 case NODETYPE_ADD: fcnname = "+"; break;
1157 case NODETYPE_SUBTRACT: fcnname = "-"; break;
1158 case NODETYPE_MULTIPLY: fcnname = "*"; break;
1159 case NODETYPE_DIVIDE: fcnname = "/"; break;
1160 case NODETYPE_MODULO: fcnname = "%"; break;
1161 case NODETYPE_BWAND: fcnname = "&"; break;
1162 case NODETYPE_BWOR: fcnname = "|"; break;
1163 case NODETYPE_BWXOR: fcnname = "^"; break;
1164 case NODETYPE_BITSHIFTLEFT: fcnname = "<<"; break;
1165 case NODETYPE_BITSHIFTRIGHT:fcnname = ">>"; break;
1166 case NODETYPE_BITROTATELEFT:fcnname = "<<<"; break;
1167 default: fcnname = NULL; break;
1172 ret = Object_ExecuteMethod(Left->Object, fcnname, Right);
1175 // Fall through and try casting (which will usually fail)
1180 // If implicit casts are allowed, convert Right to Left's type
1181 if(Block->Script->Variant->bImplicitCasts)
1183 Right = SpiderScript_CastValueTo(Left->Type, Right);
1187 // If statically typed, this should never happen, but catch it anyway
1189 AST_RuntimeError(NULL, "Implicit cast not allowed (from %i to %i)\n", Right->Type, Left->Type);
1195 if( Left == NULL || Right == NULL ) {
1196 if(Right && Right != preCastValue) free(Right);
1203 // String Concatenation
1204 case SS_DATATYPE_STRING:
1207 case NODETYPE_ADD: // Concatenate
1208 ret = Object_StringConcat(Left, Right);
1211 AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,String unknown op %i", Operation);
1216 // Integer Operations
1217 case SS_DATATYPE_INTEGER:
1220 case NODETYPE_ADD: ret = SpiderScript_CreateInteger( Left->Integer + Right->Integer ); break;
1221 case NODETYPE_SUBTRACT: ret = SpiderScript_CreateInteger( Left->Integer - Right->Integer ); break;
1222 case NODETYPE_MULTIPLY: ret = SpiderScript_CreateInteger( Left->Integer * Right->Integer ); break;
1223 case NODETYPE_DIVIDE: ret = SpiderScript_CreateInteger( Left->Integer / Right->Integer ); break;
1224 case NODETYPE_MODULO: ret = SpiderScript_CreateInteger( Left->Integer % Right->Integer ); break;
1225 case NODETYPE_BWAND: ret = SpiderScript_CreateInteger( Left->Integer & Right->Integer ); break;
1226 case NODETYPE_BWOR: ret = SpiderScript_CreateInteger( Left->Integer | Right->Integer ); break;
1227 case NODETYPE_BWXOR: ret = SpiderScript_CreateInteger( Left->Integer ^ Right->Integer ); break;
1228 case NODETYPE_BITSHIFTLEFT: ret = SpiderScript_CreateInteger( Left->Integer << Right->Integer ); break;
1229 case NODETYPE_BITSHIFTRIGHT:ret = SpiderScript_CreateInteger( Left->Integer >> Right->Integer ); break;
1230 case NODETYPE_BITROTATELEFT:
1231 ret = SpiderScript_CreateInteger( (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer)) );
1234 AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i\n", Operation);
1241 case SS_DATATYPE_REAL:
1245 AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation);
1252 AST_RuntimeError(NULL, "BUG - Invalid operation (%i) on type (%i)", Operation, Left->Type);
1257 if(Right && Right != preCastValue) free(Right);
1263 * \brief Define a variable
1264 * \param Block Current block state
1265 * \param Type Type of the variable
1266 * \param Name Name of the variable
1267 * \return Boolean Failure
1269 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name)
1271 tAST_Variable *var, *prev = NULL;
1273 for( var = Block->FirstVar; var; prev = var, var = var->Next )
1275 if( strcmp(var->Name, Name) == 0 ) {
1276 AST_RuntimeError(NULL, "Redefinition of variable '%s'", Name);
1281 var = malloc( sizeof(tAST_Variable) + strlen(Name) + 1 );
1285 strcpy(var->Name, Name);
1287 if(prev) prev->Next = var;
1288 else Block->FirstVar = var;
1290 //printf("Defined variable %s (%i)\n", Name, Type);
1296 * \brief Set the value of a variable
1297 * \return Boolean Failure
1299 int Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value)
1302 tAST_BlockState *bs;
1304 for( bs = Block; bs; bs = bs->Parent )
1306 for( var = bs->FirstVar; var; var = var->Next )
1308 if( strcmp(var->Name, Name) == 0 )
1310 if( !Block->Script->Variant->bDyamicTyped
1311 && (Value && var->Type != Value->Type) )
1313 AST_RuntimeError(NULL, "Type mismatch assigning to '%s'", Name);
1316 // printf("Assign %p to '%s'\n", Value, var->Name);
1317 Object_Reference(Value);
1318 Object_Dereference(var->Object);
1319 var->Object = Value;
1325 if( Block->Script->Variant->bDyamicTyped )
1328 var = Variable_Define(Block, Value->Type, Name);
1329 Object_Reference(Value);
1330 var->Object = Value;
1335 AST_RuntimeError(NULL, "Variable '%s' set while undefined", Name);
1341 * \brief Get the value of a variable
1343 tSpiderValue *Variable_GetValue(tAST_BlockState *Block, const char *Name)
1346 tAST_BlockState *bs;
1348 for( bs = Block; bs; bs = bs->Parent )
1350 for( var = bs->FirstVar; var; var = var->Next )
1352 if( strcmp(var->Name, Name) == 0 ) {
1353 Object_Reference(var->Object);
1360 AST_RuntimeError(NULL, "Variable '%s' used undefined", Name);
1366 * \brief Destorys a variable
1368 void Variable_Destroy(tAST_Variable *Variable)
1370 // printf("Variable_Destroy: (%p'%s')\n", Variable, Variable->Name);
1371 Object_Dereference(Variable->Object);
1375 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...)
1379 fprintf(stderr, "ERROR: ");
1380 va_start(args, Format);
1381 vfprintf(stderr, Format, args);
1383 fprintf(stderr, "\n");
1387 fprintf(stderr, " at %s:%i\n", Node->File, Node->Line);