13 #define USE_AST_EXEC 1
14 #define TRACE_VAR_LOOKUPS 0
15 #define TRACE_NODE_RETURNS 0
21 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
22 tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right);
23 tSpiderValue *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value);
25 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value);
26 int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value);
27 tSpiderValue *Variable_GetValue(tAST_BlockState *Block, tAST_Node *VarNode);
28 void Variable_Destroy(tAST_Variable *Variable);
30 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...);
31 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
34 int giNextBlockIdent = 1;
38 tSpiderValue *AST_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Arguments)
44 // Build a block State
48 bs.BaseNamespace = &Script->Variant->RootNamespace;
49 bs.CurNamespace = NULL;
51 bs.Ident = giNextBlockIdent ++;
54 for( i = 0; i < Fcn->ArgumentCount; i ++ )
56 if( i >= NArguments ) break; // TODO: Return gracefully
59 Fcn->Arguments[i].Type, Fcn->Arguments[i].Name,
64 ret = AST_ExecuteNode(&bs, Fcn->ASTFcn);
67 SpiderScript_DereferenceValue(ret); // Dereference output of last block statement
68 ret = bs.RetVal; // Set to return value of block
73 tAST_Variable *nextVar = bs.FirstVar->Next;
74 Variable_Destroy( bs.FirstVar );
75 bs.FirstVar = nextVar;
81 * \brief Execute an AST node and return its value
82 * \param Block Execution context
83 * \param Node Node to execute
85 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
88 tSpiderValue *ret = NULL, *tmpobj;
89 tSpiderValue *op1, *op2; // Binary operations
102 tAST_BlockState blockInfo;
103 blockInfo.Parent = Block;
104 blockInfo.Script = Block->Script;
105 blockInfo.FirstVar = NULL;
106 blockInfo.RetVal = NULL;
107 blockInfo.BaseNamespace = Block->BaseNamespace;
108 blockInfo.CurNamespace = NULL;
109 blockInfo.BreakTarget = NULL;
110 blockInfo.Ident = giNextBlockIdent ++;
112 // Loop over all nodes, or until the return value is set
113 for(node = Node->Block.FirstChild;
114 node && !blockInfo.RetVal && !blockInfo.BreakTarget;
115 node = node->NextSibling )
117 ret = AST_ExecuteNode(&blockInfo, node);
118 if(ret == ERRPTR) break; // Error check
119 if(ret != NULL) SpiderScript_DereferenceValue(ret); // Free unused value
121 // Clean up variables
122 while(blockInfo.FirstVar)
124 tAST_Variable *nextVar = blockInfo.FirstVar->Next;
125 Variable_Destroy( blockInfo.FirstVar );
126 blockInfo.FirstVar = nextVar;
128 // Clear ret if not an error
129 if(ret != ERRPTR) ret = NULL;
131 // Set parent's return value if needed
132 if( blockInfo.RetVal )
133 Block->RetVal = blockInfo.RetVal;
134 if( blockInfo.BreakTarget ) {
135 Block->BreakTarget = blockInfo.BreakTarget;
136 Block->BreakType = blockInfo.BreakType;
139 // TODO: Unset break if break type deontes a block break
145 case NODETYPE_ASSIGN:
146 // TODO: Support assigning to object attributes
147 if( Node->Assign.Dest->Type != NODETYPE_VARIABLE ) {
148 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
151 ret = AST_ExecuteNode(Block, Node->Assign.Value);
152 if(ret == ERRPTR) return ERRPTR;
154 // Perform assignment operation
155 if( Node->Assign.Operation != NODETYPE_NOP )
157 tSpiderValue *varVal, *value;
159 varVal = Variable_GetValue(Block, Node->Assign.Dest);
160 if(varVal == ERRPTR) return ERRPTR;
163 if(varVal && varVal->ReferenceCount == 2) {
164 SpiderScript_DereferenceValue(varVal);
165 // printf("pre: (%s) varVal->ReferenceCount = %i\n",
166 // Node->Assign.Dest->Variable.Name,
167 // varVal->ReferenceCount);
170 value = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Assign.Operation, varVal, ret);
171 if(value == ERRPTR) return ERRPTR;
173 if(ret) SpiderScript_DereferenceValue(ret);
175 if(varVal) SpiderScript_DereferenceValue(varVal);
177 if(varVal && varVal->ReferenceCount == 1) {
178 SpiderScript_ReferenceValue(varVal);
179 // printf("post: varVal->ReferenceCount = %i\n", varVal->ReferenceCount);
180 break; // If varVal was non-null, it has been updated by _BinOp
183 // Else, it was NULL, so has to be assigned
187 // Set the variable value
188 if( Variable_SetValue( Block, Node->Assign.Dest, ret ) ) {
189 SpiderScript_DereferenceValue( ret );
194 // Post increment/decrement
195 case NODETYPE_POSTINC:
196 case NODETYPE_POSTDEC:
198 tSpiderValue *varVal, *value;
199 static tSpiderValue one = {
200 .Type = SS_DATATYPE_INTEGER,
205 // TODO: Support assigning to object attributes
206 if( Node->UniOp.Value->Type != NODETYPE_VARIABLE ) {
207 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
211 // Get values (current variable contents and a static one)
212 varVal = Variable_GetValue(Block, Node->UniOp.Value);
214 if( Node->Type == NODETYPE_POSTDEC )
215 value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_SUBTRACT, varVal, &one);
217 value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_ADD, varVal, &one);
218 if( value == ERRPTR )
223 if( Variable_SetValue( Block, Node->UniOp.Value, value ) ) {
224 SpiderScript_DereferenceValue( ret );
227 SpiderScript_DereferenceValue( value );
232 case NODETYPE_METHODCALL:
233 case NODETYPE_FUNCTIONCALL:
234 case NODETYPE_CREATEOBJECT:
235 // Logical block (used to allocate `params`)
237 const char *namespaces[] = {NULL}; // TODO: Default namespaces?
238 tSpiderValue *params[Node->FunctionCall.NumArgs];
242 for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling)
244 params[i] = AST_ExecuteNode(Block, node);
245 if( params[i] == ERRPTR ) {
246 while(i--) SpiderScript_DereferenceValue(params[i]);
253 // TODO: Check for cached function reference
256 if( Node->Type == NODETYPE_CREATEOBJECT )
258 ret = SpiderScript_CreateObject(Block->Script,
259 Node->FunctionCall.Name,
261 Node->FunctionCall.NumArgs, params
264 else if( Node->Type == NODETYPE_METHODCALL )
266 tSpiderValue *obj = AST_ExecuteNode(Block, Node->FunctionCall.Object);
267 if( !obj || obj == ERRPTR || obj->Type != SS_DATATYPE_OBJECT ) {
268 AST_RuntimeError(Node->FunctionCall.Object,
269 "Type Mismatch - Required SS_DATATYPE_OBJECT for method call");
270 while(i--) SpiderScript_DereferenceValue(params[i]);
274 ret = SpiderScript_ExecuteMethod(Block->Script,
275 obj->Object, Node->FunctionCall.Name,
276 Node->FunctionCall.NumArgs, params
278 SpiderScript_DereferenceValue(obj);
282 ret = SpiderScript_ExecuteFunction(Block->Script,
283 Node->FunctionCall.Name,
285 Node->FunctionCall.NumArgs, params,
291 // Dereference parameters
292 while(i--) SpiderScript_DereferenceValue(params[i]);
300 ret = AST_ExecuteNode(Block, Node->If.Condition);
301 if( ret == ERRPTR ) break;
302 if( SpiderScript_IsValueTrue(ret) ) {
303 tmpobj = AST_ExecuteNode(Block, Node->If.True);
306 tmpobj = AST_ExecuteNode(Block, Node->If.False);
308 SpiderScript_DereferenceValue(ret);
309 if( tmpobj == ERRPTR ) return ERRPTR;
310 SpiderScript_DereferenceValue(tmpobj);
317 ret = AST_ExecuteNode(Block, Node->For.Init);
318 if(ret == ERRPTR) break;
320 // Check initial condition
321 if( !Node->For.bCheckAfter )
323 SpiderScript_DereferenceValue(ret);
325 ret = AST_ExecuteNode(Block, Node->For.Condition);
326 if(ret == ERRPTR) return ERRPTR;
327 if(!SpiderScript_IsValueTrue(ret)) {
328 SpiderScript_DereferenceValue(ret);
337 SpiderScript_DereferenceValue(ret);
340 ret = AST_ExecuteNode(Block, Node->For.Code);
341 if(ret == ERRPTR) return ERRPTR;
342 SpiderScript_DereferenceValue(ret);
344 if(Block->BreakTarget)
346 if( Block->BreakTarget[0] == '\0' || strcmp(Block->BreakTarget, Node->For.Tag) == 0 )
349 free((void*)Block->BreakTarget); Block->BreakTarget = NULL;
350 if( Block->BreakType == NODETYPE_CONTINUE ) {
351 // Continue, just keep going
357 break; // Break out of this loop
361 ret = AST_ExecuteNode(Block, Node->For.Increment);
362 if(ret == ERRPTR) return ERRPTR;
363 SpiderScript_DereferenceValue(ret);
366 ret = AST_ExecuteNode(Block, Node->For.Condition);
367 if(ret == ERRPTR) return ERRPTR;
368 if(!SpiderScript_IsValueTrue(ret)) break;
370 SpiderScript_DereferenceValue(ret);
375 case NODETYPE_RETURN:
376 ret = AST_ExecuteNode(Block, Node->UniOp.Value);
377 if(ret == ERRPTR) break;
378 Block->RetVal = ret; // Return value set
379 ret = NULL; // the `return` statement does not return a value
383 case NODETYPE_CONTINUE:
384 Block->BreakTarget = strdup(Node->Variable.Name);
385 Block->BreakType = Node->Type;
389 case NODETYPE_DEFVAR:
390 if( Node->DefVar.InitialValue ) {
391 tmpobj = AST_ExecuteNode(Block, Node->DefVar.InitialValue);
392 if(tmpobj == ERRPTR) return ERRPTR;
397 // TODO: Handle arrays
399 if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name, tmpobj) == ERRPTR )
401 SpiderScript_DereferenceValue(tmpobj);
407 tSpiderNamespace *ns;
409 // Set current namespace if unset
410 if( !Block->CurNamespace )
411 Block->CurNamespace = Block->BaseNamespace;
413 // Empty string means use the root namespace
414 if( Node->Scope.Name[0] == '\0' )
416 ns = &Block->Script->Variant->RootNamespace;
420 // Otherwise scan the current namespace for the element
421 for( ns = Block->CurNamespace->FirstChild; ns; ns = ns->Next )
423 if( strcmp(ns->Name, Node->Scope.Name) == 0 )
428 AST_RuntimeError(Node, "Unknown namespace '%s'", Node->Scope.Name);
432 Block->CurNamespace = ns;
434 // TODO: Check type of child node (Scope, Constant or Function)
436 ret = AST_ExecuteNode(Block, Node->Scope.Element);
441 case NODETYPE_VARIABLE:
442 ret = Variable_GetValue( Block, Node );
445 // Element of an Object
446 case NODETYPE_ELEMENT:
447 tmpobj = AST_ExecuteNode( Block, Node->Scope.Element );
448 if(tmpobj == ERRPTR) return ERRPTR;
449 if( !tmpobj || tmpobj->Type != SS_DATATYPE_OBJECT )
451 AST_RuntimeError(Node->Scope.Element, "Unable to dereference a non-object");
456 for( i = 0; i < tmpobj->Object->Type->NAttributes; i ++ )
458 if( strcmp(Node->Scope.Name, tmpobj->Object->Type->AttributeDefs[i].Name) == 0 )
460 ret = tmpobj->Object->Attributes[i];
461 SpiderScript_ReferenceValue(ret);
465 if( i == tmpobj->Object->Type->NAttributes )
467 AST_RuntimeError(Node->Scope.Element, "Unknown attribute '%s' of class '%s'",
468 Node->Scope.Name, tmpobj->Object->Type->Name);
473 // Cast a value to another
476 tmpobj = AST_ExecuteNode(Block, Node->Cast.Value);
477 if(tmpobj == ERRPTR) return ERRPTR;
478 ret = SpiderScript_CastValueTo( Node->Cast.DataType, tmpobj );
479 SpiderScript_DereferenceValue(tmpobj);
483 // Index into an array
485 op1 = AST_ExecuteNode(Block, Node->BinOp.Left); // Array
486 if(op1 == ERRPTR) return ERRPTR;
487 op2 = AST_ExecuteNode(Block, Node->BinOp.Right); // Offset
489 SpiderScript_DereferenceValue(op1);
493 if( !op2 || op2->Type != SS_DATATYPE_INTEGER )
495 if( !Block->Script->Variant->bImplicitCasts ) {
496 AST_RuntimeError(Node, "Array index is not an integer");
501 tmpobj = SpiderScript_CastValueTo(SS_DATATYPE_INTEGER, op2);
502 SpiderScript_DereferenceValue(op2);
509 SpiderScript_DereferenceValue(op2);
510 AST_RuntimeError(Node, "Indexing NULL value");
517 case SS_DATATYPE_ARRAY:
518 if( op2->Integer >= op1->Array.Length ) {
519 AST_RuntimeError(Node, "Array index out of bounds %i >= %i",
520 op2->Integer, op1->Array.Length);
525 ret = op1->Array.Items[ op2->Integer ];
526 SpiderScript_ReferenceValue(ret);
530 // TODO: Implement "operator []" on objects
531 AST_RuntimeError(Node, "Indexing non-array");
536 SpiderScript_DereferenceValue(op1);
537 SpiderScript_DereferenceValue(op2);
540 // TODO: Implement runtime constants
541 case NODETYPE_CONSTANT:
542 // TODO: Scan namespace for constant name
543 AST_RuntimeError(Node, "TODO - Runtime Constants");
548 case NODETYPE_STRING:
549 case NODETYPE_INTEGER:
551 ret = &Node->Constant;
552 SpiderScript_ReferenceValue(ret);
558 // --- Operations ---
559 // Boolean Operations
560 case NODETYPE_LOGICALNOT: // Logical NOT (!)
561 op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
562 if(op1 == ERRPTR) return ERRPTR;
563 ret = SpiderScript_CreateInteger( !SpiderScript_IsValueTrue(op1) );
564 SpiderScript_DereferenceValue(op1);
566 case NODETYPE_LOGICALAND: // Logical AND (&&)
567 case NODETYPE_LOGICALOR: // Logical OR (||)
568 case NODETYPE_LOGICALXOR: // Logical XOR (^^)
569 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
570 if(op1 == ERRPTR) return ERRPTR;
571 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
573 SpiderScript_DereferenceValue(op1);
579 case NODETYPE_LOGICALAND:
580 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) && SpiderScript_IsValueTrue(op2) );
582 case NODETYPE_LOGICALOR:
583 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) || SpiderScript_IsValueTrue(op2) );
585 case NODETYPE_LOGICALXOR:
586 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) ^ SpiderScript_IsValueTrue(op2) );
591 // Free intermediate objects
592 SpiderScript_DereferenceValue(op1);
593 SpiderScript_DereferenceValue(op2);
596 // General Unary Operations
597 case NODETYPE_BWNOT: // Bitwise NOT (~)
598 case NODETYPE_NEGATE: // Negation (-)
599 op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
600 if(op1 == ERRPTR) return ERRPTR;
601 ret = AST_ExecuteNode_UniOp(Block->Script, Node, Node->Type, op1);
602 SpiderScript_DereferenceValue(op1);
605 // General Binary Operations
607 case NODETYPE_SUBTRACT:
608 case NODETYPE_MULTIPLY:
609 case NODETYPE_DIVIDE:
610 case NODETYPE_MODULO:
614 case NODETYPE_BITSHIFTLEFT:
615 case NODETYPE_BITSHIFTRIGHT:
616 case NODETYPE_BITROTATELEFT:
617 case NODETYPE_EQUALS:
618 case NODETYPE_NOTEQUALS:
619 case NODETYPE_LESSTHAN:
620 case NODETYPE_GREATERTHAN:
621 case NODETYPE_LESSTHANEQUAL:
622 case NODETYPE_GREATERTHANEQUAL:
624 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
625 if(op1 == ERRPTR) return ERRPTR;
626 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
628 SpiderScript_DereferenceValue(op1);
632 ret = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Type, op1, op2);
634 // Free intermediate objects
635 SpiderScript_DereferenceValue(op1);
636 SpiderScript_DereferenceValue(op2);
641 // AST_RuntimeError(Node, "BUG - SpiderScript AST_ExecuteNode Unimplemented %i", Node->Type);
645 // Reset namespace when no longer needed
646 if( Node->Type != NODETYPE_SCOPE )
647 Block->CurNamespace = NULL;
649 #if TRACE_NODE_RETURNS
650 if(ret && ret != ERRPTR) {
651 AST_RuntimeError(Node, "Ret type of %p %i is %i", Node, Node->Type, ret->Type);
654 AST_RuntimeError(Node, "Ret type of %p %i is %p", Node, Node->Type, ret);
662 tSpiderValue *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value)
666 if( Value->Type == SS_DATATYPE_OBJECT )
671 case NODETYPE_NEGATE: fcnname = "-ve"; break;
672 case NODETYPE_BWNOT: fcnname = "~"; break;
673 default: fcnname = NULL; break;
678 ret = Object_ExecuteMethod(Value->Object, fcnname, );
686 // Integer Operations
687 case SS_DATATYPE_INTEGER:
688 if( Value->ReferenceCount == 1 )
689 SpiderScript_ReferenceValue(ret = Value);
691 ret = SpiderScript_CreateInteger(0);
694 case NODETYPE_NEGATE: ret->Integer = -Value->Integer; break;
695 case NODETYPE_BWNOT: ret->Integer = ~Value->Integer; break;
697 AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Integer unknown op %i", Operation);
698 SpiderScript_DereferenceValue(ret);
703 // Real number Operations
704 case SS_DATATYPE_REAL:
707 case NODETYPE_NEGATE: ret = SpiderScript_CreateInteger( -Value->Real ); break;
709 AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Real unknown op %i", Operation);
716 AST_RuntimeError(NULL, "Invalid operation (%i) on type (%i)", Operation, Value->Type);
724 tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right)
726 tSpiderValue *preCastValue = Right;
730 if( Left && Right && Left->Type != Right->Type )
734 // - Operator overload functions
735 if( Left->Type == SS_DATATYPE_OBJECT )
740 case NODETYPE_ADD: fcnname = "+"; break;
741 case NODETYPE_SUBTRACT: fcnname = "-"; break;
742 case NODETYPE_MULTIPLY: fcnname = "*"; break;
743 case NODETYPE_DIVIDE: fcnname = "/"; break;
744 case NODETYPE_MODULO: fcnname = "%"; break;
745 case NODETYPE_BWAND: fcnname = "&"; break;
746 case NODETYPE_BWOR: fcnname = "|"; break;
747 case NODETYPE_BWXOR: fcnname = "^"; break;
748 case NODETYPE_BITSHIFTLEFT: fcnname = "<<"; break;
749 case NODETYPE_BITSHIFTRIGHT:fcnname = ">>"; break;
750 case NODETYPE_BITROTATELEFT:fcnname = "<<<"; break;
751 default: fcnname = NULL; break;
756 ret = Object_ExecuteMethod(Left->Object, fcnname, Right);
759 // Fall through and try casting (which will usually fail)
764 // If implicit casts are allowed, convert Right to Left's type
765 if(Script->Variant->bImplicitCasts)
767 Right = SpiderScript_CastValueTo(Left->Type, Right);
771 // If statically typed, this should never happen, but catch it anyway
773 AST_RuntimeError(Node, "Implicit cast not allowed (from %i to %i)", Right->Type, Left->Type);
779 if( Left == NULL || Right == NULL ) {
780 if(Right && Right != preCastValue) free(Right);
787 case NODETYPE_EQUALS:
788 case NODETYPE_NOTEQUALS:
789 case NODETYPE_LESSTHAN:
790 case NODETYPE_GREATERTHAN:
791 case NODETYPE_LESSTHANEQUAL:
792 case NODETYPE_GREATERTHANEQUAL: {
798 // - String Compare (does a strcmp, well memcmp)
799 case SS_DATATYPE_STRING:
800 // Call memcmp to do most of the work
802 Left->String.Data, Right->String.Data,
803 (Left->String.Length < Right->String.Length) ? Left->String.Length : Right->String.Length
805 // Handle reaching the end of the string
807 if( Left->String.Length == Right->String.Length )
809 else if( Left->String.Length < Right->String.Length )
816 // - Integer Comparisons
817 case SS_DATATYPE_INTEGER:
818 if( Left->Integer == Right->Integer )
820 else if( Left->Integer < Right->Integer )
825 // - Real Number Comparisons
826 case SS_DATATYPE_REAL:
827 cmp = (Left->Real - Right->Real) / Right->Real * 10000; // < 0.1% difference is equality
830 AST_RuntimeError(Node, "TODO - Comparison of type %i", Left->Type);
838 if(Left->ReferenceCount == 1 && Left->Type != SS_DATATYPE_STRING)
839 SpiderScript_ReferenceValue(ret = Left);
841 ret = SpiderScript_CreateInteger(0);
846 case NODETYPE_EQUALS: ret->Integer = (cmp == 0); break;
847 case NODETYPE_NOTEQUALS: ret->Integer = (cmp != 0); break;
848 case NODETYPE_LESSTHAN: ret->Integer = (cmp < 0); break;
849 case NODETYPE_GREATERTHAN: ret->Integer = (cmp > 0); break;
850 case NODETYPE_LESSTHANEQUAL: ret->Integer = (cmp <= 0); break;
851 case NODETYPE_GREATERTHANEQUAL: ret->Integer = (cmp >= 0); break;
853 AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Operation);
854 SpiderScript_DereferenceValue(ret);
859 if(Right && Right != preCastValue) free(Right);
863 // Fall through and sort by type instead
871 // String Concatenation
872 case SS_DATATYPE_STRING:
875 case NODETYPE_ADD: // Concatenate
876 ret = SpiderScript_StringConcat(Left, Right);
878 // TODO: Support python style 'i = %i' % i ?
879 // Might do it via a function call
880 // Implement it via % with an array, but getting past the cast will be fun
881 // case NODETYPE_MODULUS:
883 // TODO: Support string repititions
884 // case NODETYPE_MULTIPLY:
888 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,String unknown op %i", Operation);
893 // Integer Operations
894 case SS_DATATYPE_INTEGER:
895 if( Left->ReferenceCount == 1 )
896 SpiderScript_ReferenceValue(ret = Left);
898 ret = SpiderScript_CreateInteger(0);
901 case NODETYPE_ADD: ret->Integer = Left->Integer + Right->Integer; break;
902 case NODETYPE_SUBTRACT: ret->Integer = Left->Integer - Right->Integer; break;
903 case NODETYPE_MULTIPLY: ret->Integer = Left->Integer * Right->Integer; break;
904 case NODETYPE_DIVIDE: ret->Integer = Left->Integer / Right->Integer; break;
905 case NODETYPE_MODULO: ret->Integer = Left->Integer % Right->Integer; break;
906 case NODETYPE_BWAND: ret->Integer = Left->Integer & Right->Integer; break;
907 case NODETYPE_BWOR: ret->Integer = Left->Integer | Right->Integer; break;
908 case NODETYPE_BWXOR: ret->Integer = Left->Integer ^ Right->Integer; break;
909 case NODETYPE_BITSHIFTLEFT: ret->Integer = Left->Integer << Right->Integer; break;
910 case NODETYPE_BITSHIFTRIGHT:ret->Integer = Left->Integer >> Right->Integer; break;
911 case NODETYPE_BITROTATELEFT:
912 ret->Integer = (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer));
915 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i", Operation);
916 SpiderScript_DereferenceValue(ret);
923 case SS_DATATYPE_REAL:
924 if( Left->ReferenceCount == 1 )
925 SpiderScript_ReferenceValue(ret = Left);
927 ret = SpiderScript_CreateReal(0);
930 case NODETYPE_ADD: ret->Real = Left->Real + Right->Real; break;
931 case NODETYPE_SUBTRACT: ret->Real = Left->Real - Right->Real; break;
932 case NODETYPE_MULTIPLY: ret->Real = Left->Real * Right->Real; break;
933 case NODETYPE_DIVIDE: ret->Real = Left->Real / Right->Real; break;
935 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation);
936 SpiderScript_DereferenceValue(ret);
943 AST_RuntimeError(Node, "BUG - Invalid operation (%i) on type (%i)", Operation, Left->Type);
948 if(Right && Right != preCastValue) free(Right);
955 * \brief Define a variable
956 * \param Block Current block state
957 * \param Type Type of the variable
958 * \param Name Name of the variable
959 * \return Boolean Failure
961 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value)
963 tAST_Variable *var, *prev = NULL;
965 for( var = Block->FirstVar; var; prev = var, var = var->Next )
967 if( strcmp(var->Name, Name) == 0 ) {
968 AST_RuntimeError(NULL, "Redefinition of variable '%s'", Name);
973 var = malloc( sizeof(tAST_Variable) + strlen(Name) + 1 );
977 if(Value) SpiderScript_ReferenceValue(Value);
978 strcpy(var->Name, Name);
980 if(prev) prev->Next = var;
981 else Block->FirstVar = var;
983 //printf("Defined variable %s (%i)\n", Name, Type);
988 tAST_Variable *Variable_Lookup(tAST_BlockState *Block, tAST_Node *VarNode, int CreateType)
990 tAST_Variable *var = NULL;
993 if( VarNode->BlockState == Block && VarNode->BlockIdent == Block->Ident ) {
994 var = VarNode->ValueCache;
995 #if TRACE_VAR_LOOKUPS
996 AST_RuntimeMessage(VarNode, "debug", "Fast var fetch on '%s' %p (%p:%i)",
997 VarNode->Variable.Name, var,
998 VarNode->BlockState, VarNode->BlockIdent
1004 tAST_BlockState *bs;
1005 for( bs = Block; bs; bs = bs->Parent )
1007 for( var = bs->FirstVar; var; var = var->Next )
1009 if( strcmp(var->Name, VarNode->Variable.Name) == 0 )
1017 if( Block->Script->Variant->bDyamicTyped && CreateType != SS_DATATYPE_UNDEF ) {
1019 var = Variable_Define(Block, CreateType, VarNode->Variable.Name, NULL);
1023 AST_RuntimeError(VarNode, "Variable '%s' is undefined", VarNode->Variable.Name);
1028 #if TRACE_VAR_LOOKUPS
1029 AST_RuntimeMessage(VarNode, "debug", "Saved variable lookup of '%s' %p (%p:%i)",
1030 VarNode->Variable.Name, var,
1031 Block, Block->Ident);
1034 VarNode->ValueCache = var;
1035 VarNode->BlockState = Block;
1036 VarNode->BlockIdent = Block->Ident;
1043 * \brief Set the value of a variable
1044 * \return Boolean Failure
1046 int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value)
1050 var = Variable_Lookup(Block, VarNode, (Value ? Value->Type : SS_DATATYPE_UNDEF));
1052 if( !var ) return -1;
1054 if( !Block->Script->Variant->bDyamicTyped && (Value && var->Type != Value->Type) )
1056 AST_RuntimeError(VarNode, "Type mismatch assigning to '%s'",
1057 VarNode->Variable.Name);
1061 // printf("Assign %p to '%s'\n", Value, var->Name);
1062 SpiderScript_ReferenceValue(Value);
1063 SpiderScript_DereferenceValue(var->Object);
1064 var->Object = Value;
1069 * \brief Get the value of a variable
1071 tSpiderValue *Variable_GetValue(tAST_BlockState *Block, tAST_Node *VarNode)
1073 tAST_Variable *var = Variable_Lookup(Block, VarNode, 0);
1075 if( !var ) return ERRPTR;
1077 SpiderScript_ReferenceValue(var->Object);
1082 * \brief Destorys a variable
1084 void Variable_Destroy(tAST_Variable *Variable)
1086 // printf("Variable_Destroy: (%p'%s')\n", Variable, Variable->Name);
1087 SpiderScript_DereferenceValue(Variable->Object);