13 #define TRACE_VAR_LOOKUPS 0
14 #define TRACE_NODE_RETURNS 0
20 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
21 tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right);
22 tSpiderValue *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value);
24 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value);
25 int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value);
26 tSpiderValue *Variable_GetValue(tAST_BlockState *Block, tAST_Node *VarNode);
27 void Variable_Destroy(tAST_Variable *Variable);
29 void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...);
30 void AST_RuntimeError(tAST_Node *Node, const char *Format, ...);
33 int giNextBlockIdent = 1;
36 tSpiderValue *AST_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Arguments)
42 // Build a block State
46 bs.BaseNamespace = &Script->Variant->RootNamespace;
47 bs.CurNamespace = NULL;
49 bs.Ident = giNextBlockIdent ++;
52 for( i = 0; i < Fcn->ArgumentCount; i ++ )
54 if( i >= NArguments ) break; // TODO: Return gracefully
57 Fcn->Arguments[i].Type, Fcn->Arguments[i].Name,
62 ret = AST_ExecuteNode(&bs, Fcn->ASTFcn);
65 SpiderScript_DereferenceValue(ret); // Dereference output of last block statement
66 ret = bs.RetVal; // Set to return value of block
71 tAST_Variable *nextVar = bs.FirstVar->Next;
72 Variable_Destroy( bs.FirstVar );
73 bs.FirstVar = nextVar;
79 * \brief Execute an AST node and return its value
80 * \param Block Execution context
81 * \param Node Node to execute
83 tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node)
86 tSpiderValue *ret = NULL, *tmpobj;
87 tSpiderValue *op1, *op2; // Binary operations
100 tAST_BlockState blockInfo;
101 blockInfo.Parent = Block;
102 blockInfo.Script = Block->Script;
103 blockInfo.FirstVar = NULL;
104 blockInfo.RetVal = NULL;
105 blockInfo.BaseNamespace = Block->BaseNamespace;
106 blockInfo.CurNamespace = NULL;
107 blockInfo.BreakTarget = NULL;
108 blockInfo.Ident = giNextBlockIdent ++;
110 // Loop over all nodes, or until the return value is set
111 for(node = Node->Block.FirstChild;
112 node && !blockInfo.RetVal && !blockInfo.BreakTarget;
113 node = node->NextSibling )
115 ret = AST_ExecuteNode(&blockInfo, node);
116 if(ret == ERRPTR) break; // Error check
117 if(ret != NULL) SpiderScript_DereferenceValue(ret); // Free unused value
119 // Clean up variables
120 while(blockInfo.FirstVar)
122 tAST_Variable *nextVar = blockInfo.FirstVar->Next;
123 Variable_Destroy( blockInfo.FirstVar );
124 blockInfo.FirstVar = nextVar;
126 // Clear ret if not an error
127 if(ret != ERRPTR) ret = NULL;
129 // Set parent's return value if needed
130 if( blockInfo.RetVal )
131 Block->RetVal = blockInfo.RetVal;
132 if( blockInfo.BreakTarget ) {
133 Block->BreakTarget = blockInfo.BreakTarget;
134 Block->BreakType = blockInfo.BreakType;
137 // TODO: Unset break if break type deontes a block break
143 case NODETYPE_ASSIGN:
144 // TODO: Support assigning to object attributes
145 if( Node->Assign.Dest->Type != NODETYPE_VARIABLE ) {
146 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
149 ret = AST_ExecuteNode(Block, Node->Assign.Value);
150 if(ret == ERRPTR) return ERRPTR;
152 // Perform assignment operation
153 if( Node->Assign.Operation != NODETYPE_NOP )
155 tSpiderValue *varVal, *value;
157 varVal = Variable_GetValue(Block, Node->Assign.Dest);
158 if(varVal == ERRPTR) return ERRPTR;
161 if(varVal && varVal->ReferenceCount == 2) {
162 SpiderScript_DereferenceValue(varVal);
163 // printf("pre: (%s) varVal->ReferenceCount = %i\n",
164 // Node->Assign.Dest->Variable.Name,
165 // varVal->ReferenceCount);
168 value = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Assign.Operation, varVal, ret);
169 if(value == ERRPTR) return ERRPTR;
171 if(ret) SpiderScript_DereferenceValue(ret);
173 if(varVal) SpiderScript_DereferenceValue(varVal);
175 if(varVal && varVal->ReferenceCount == 1) {
176 SpiderScript_ReferenceValue(varVal);
177 // printf("post: varVal->ReferenceCount = %i\n", varVal->ReferenceCount);
178 break; // If varVal was non-null, it has been updated by _BinOp
181 // Else, it was NULL, so has to be assigned
185 // Set the variable value
186 if( Variable_SetValue( Block, Node->Assign.Dest, ret ) ) {
187 SpiderScript_DereferenceValue( ret );
192 // Post increment/decrement
193 case NODETYPE_POSTINC:
194 case NODETYPE_POSTDEC:
196 tSpiderValue *varVal, *value;
197 static tSpiderValue one = {
198 .Type = SS_DATATYPE_INTEGER,
203 // TODO: Support assigning to object attributes
204 if( Node->UniOp.Value->Type != NODETYPE_VARIABLE ) {
205 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
209 // Get values (current variable contents and a static one)
210 varVal = Variable_GetValue(Block, Node->UniOp.Value);
212 if( Node->Type == NODETYPE_POSTDEC )
213 value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_SUBTRACT, varVal, &one);
215 value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_ADD, varVal, &one);
216 if( value == ERRPTR )
221 if( Variable_SetValue( Block, Node->UniOp.Value, value ) ) {
222 SpiderScript_DereferenceValue( ret );
225 SpiderScript_DereferenceValue( value );
230 case NODETYPE_METHODCALL:
231 case NODETYPE_FUNCTIONCALL:
232 case NODETYPE_CREATEOBJECT:
233 // Logical block (used to allocate `params`)
235 tSpiderNamespace *ns = Block->CurNamespace;
236 tSpiderValue *params[Node->FunctionCall.NumArgs];
238 for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling)
240 params[i] = AST_ExecuteNode(Block, node);
241 if( params[i] == ERRPTR ) {
242 while(i--) SpiderScript_DereferenceValue(params[i]);
249 if( !ns ) ns = Block->BaseNamespace;
252 if( Node->Type == NODETYPE_CREATEOBJECT )
254 ret = SpiderScript_CreateObject(Block->Script,
256 Node->FunctionCall.Name,
257 Node->FunctionCall.NumArgs, params
260 else if( Node->Type == NODETYPE_METHODCALL )
262 tSpiderValue *obj = AST_ExecuteNode(Block, Node->FunctionCall.Object);
263 if( !obj || obj == ERRPTR || obj->Type != SS_DATATYPE_OBJECT ) {
264 AST_RuntimeError(Node->FunctionCall.Object,
265 "Type Mismatch - Required SS_DATATYPE_OBJECT for method call");
266 while(i--) SpiderScript_DereferenceValue(params[i]);
270 ret = SpiderScript_ExecuteMethod(Block->Script,
271 obj->Object, Node->FunctionCall.Name,
272 Node->FunctionCall.NumArgs, params
274 SpiderScript_DereferenceValue(obj);
278 ret = SpiderScript_ExecuteFunction(Block->Script,
279 ns, Node->FunctionCall.Name,
280 Node->FunctionCall.NumArgs, params
285 // Dereference parameters
286 while(i--) SpiderScript_DereferenceValue(params[i]);
294 ret = AST_ExecuteNode(Block, Node->If.Condition);
295 if( ret == ERRPTR ) break;
296 if( SpiderScript_IsValueTrue(ret) ) {
297 tmpobj = AST_ExecuteNode(Block, Node->If.True);
300 tmpobj = AST_ExecuteNode(Block, Node->If.False);
302 SpiderScript_DereferenceValue(ret);
303 if( tmpobj == ERRPTR ) return ERRPTR;
304 SpiderScript_DereferenceValue(tmpobj);
311 ret = AST_ExecuteNode(Block, Node->For.Init);
312 if(ret == ERRPTR) break;
314 // Check initial condition
315 if( !Node->For.bCheckAfter )
317 SpiderScript_DereferenceValue(ret);
319 ret = AST_ExecuteNode(Block, Node->For.Condition);
320 if(ret == ERRPTR) return ERRPTR;
321 if(!SpiderScript_IsValueTrue(ret)) {
322 SpiderScript_DereferenceValue(ret);
331 SpiderScript_DereferenceValue(ret);
334 ret = AST_ExecuteNode(Block, Node->For.Code);
335 if(ret == ERRPTR) return ERRPTR;
336 SpiderScript_DereferenceValue(ret);
338 if(Block->BreakTarget)
340 if( Block->BreakTarget[0] == '\0' || strcmp(Block->BreakTarget, Node->For.Tag) == 0 )
343 free((void*)Block->BreakTarget); Block->BreakTarget = NULL;
344 if( Block->BreakType == NODETYPE_CONTINUE ) {
345 // Continue, just keep going
351 break; // Break out of this loop
355 ret = AST_ExecuteNode(Block, Node->For.Increment);
356 if(ret == ERRPTR) return ERRPTR;
357 SpiderScript_DereferenceValue(ret);
360 ret = AST_ExecuteNode(Block, Node->For.Condition);
361 if(ret == ERRPTR) return ERRPTR;
362 if(!SpiderScript_IsValueTrue(ret)) break;
364 SpiderScript_DereferenceValue(ret);
369 case NODETYPE_RETURN:
370 ret = AST_ExecuteNode(Block, Node->UniOp.Value);
371 if(ret == ERRPTR) break;
372 Block->RetVal = ret; // Return value set
373 ret = NULL; // the `return` statement does not return a value
377 case NODETYPE_CONTINUE:
378 Block->BreakTarget = strdup(Node->Variable.Name);
379 Block->BreakType = Node->Type;
383 case NODETYPE_DEFVAR:
384 if( Node->DefVar.InitialValue ) {
385 tmpobj = AST_ExecuteNode(Block, Node->DefVar.InitialValue);
386 if(tmpobj == ERRPTR) return ERRPTR;
391 // TODO: Handle arrays
393 if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name, tmpobj) == ERRPTR )
395 SpiderScript_DereferenceValue(tmpobj);
401 tSpiderNamespace *ns;
403 // Set current namespace if unset
404 if( !Block->CurNamespace )
405 Block->CurNamespace = Block->BaseNamespace;
407 // Empty string means use the root namespace
408 if( Node->Scope.Name[0] == '\0' )
410 ns = &Block->Script->Variant->RootNamespace;
414 // Otherwise scan the current namespace for the element
415 for( ns = Block->CurNamespace->FirstChild; ns; ns = ns->Next )
417 if( strcmp(ns->Name, Node->Scope.Name) == 0 )
422 AST_RuntimeError(Node, "Unknown namespace '%s'", Node->Scope.Name);
426 Block->CurNamespace = ns;
428 ret = AST_ExecuteNode(Block, Node->Scope.Element);
433 case NODETYPE_VARIABLE:
434 ret = Variable_GetValue( Block, Node );
437 // Element of an Object
438 case NODETYPE_ELEMENT:
439 tmpobj = AST_ExecuteNode( Block, Node->Scope.Element );
440 if(tmpobj == ERRPTR) return ERRPTR;
441 if( !tmpobj || tmpobj->Type != SS_DATATYPE_OBJECT )
443 AST_RuntimeError(Node->Scope.Element, "Unable to dereference a non-object");
448 for( i = 0; i < tmpobj->Object->Type->NAttributes; i ++ )
450 if( strcmp(Node->Scope.Name, tmpobj->Object->Type->AttributeDefs[i].Name) == 0 )
452 ret = tmpobj->Object->Attributes[i];
453 SpiderScript_ReferenceValue(ret);
457 if( i == tmpobj->Object->Type->NAttributes )
459 AST_RuntimeError(Node->Scope.Element, "Unknown attribute '%s' of class '%s'",
460 Node->Scope.Name, tmpobj->Object->Type->Name);
465 // Cast a value to another
468 tmpobj = AST_ExecuteNode(Block, Node->Cast.Value);
469 if(tmpobj == ERRPTR) return ERRPTR;
470 ret = SpiderScript_CastValueTo( Node->Cast.DataType, tmpobj );
471 SpiderScript_DereferenceValue(tmpobj);
475 // Index into an array
477 op1 = AST_ExecuteNode(Block, Node->BinOp.Left); // Array
478 if(op1 == ERRPTR) return ERRPTR;
479 op2 = AST_ExecuteNode(Block, Node->BinOp.Right); // Offset
481 SpiderScript_DereferenceValue(op1);
485 if( !op1 || op1->Type != SS_DATATYPE_ARRAY )
487 // TODO: Implement "operator []" on objects
488 AST_RuntimeError(Node, "Indexing non-array");
493 if( (!op2 || op2->Type != SS_DATATYPE_INTEGER) && !Block->Script->Variant->bImplicitCasts ) {
494 AST_RuntimeError(Node, "Array index is not an integer");
499 if( !op2 || op2->Type != SS_DATATYPE_INTEGER )
501 tmpobj = SpiderScript_CastValueTo(SS_DATATYPE_INTEGER, op2);
502 SpiderScript_DereferenceValue(op2);
506 if( op2->Integer >= op1->Array.Length ) {
507 AST_RuntimeError(Node, "Array index out of bounds %i >= %i",
508 op2->Integer, op1->Array.Length);
513 ret = op1->Array.Items[ op2->Integer ];
514 SpiderScript_ReferenceValue(ret);
516 SpiderScript_DereferenceValue(op1);
517 SpiderScript_DereferenceValue(op2);
520 // TODO: Implement runtime constants
521 case NODETYPE_CONSTANT:
522 // TODO: Scan namespace for constant name
523 AST_RuntimeError(Node, "TODO - Runtime Constants");
528 case NODETYPE_STRING:
529 case NODETYPE_INTEGER:
531 ret = &Node->Constant;
532 SpiderScript_ReferenceValue(ret);
535 // --- Operations ---
536 // Boolean Operations
537 case NODETYPE_LOGICALNOT: // Logical NOT (!)
538 op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
539 if(op1 == ERRPTR) return ERRPTR;
540 ret = SpiderScript_CreateInteger( !SpiderScript_IsValueTrue(op1) );
541 SpiderScript_DereferenceValue(op1);
543 case NODETYPE_LOGICALAND: // Logical AND (&&)
544 case NODETYPE_LOGICALOR: // Logical OR (||)
545 case NODETYPE_LOGICALXOR: // Logical XOR (^^)
546 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
547 if(op1 == ERRPTR) return ERRPTR;
548 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
550 SpiderScript_DereferenceValue(op1);
556 case NODETYPE_LOGICALAND:
557 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) && SpiderScript_IsValueTrue(op2) );
559 case NODETYPE_LOGICALOR:
560 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) || SpiderScript_IsValueTrue(op2) );
562 case NODETYPE_LOGICALXOR:
563 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) ^ SpiderScript_IsValueTrue(op2) );
568 // Free intermediate objects
569 SpiderScript_DereferenceValue(op1);
570 SpiderScript_DereferenceValue(op2);
573 // General Unary Operations
574 case NODETYPE_BWNOT: // Bitwise NOT (~)
575 case NODETYPE_NEGATE: // Negation (-)
576 op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
577 if(op1 == ERRPTR) return ERRPTR;
578 ret = AST_ExecuteNode_UniOp(Block->Script, Node, Node->Type, op1);
579 SpiderScript_DereferenceValue(op1);
582 // General Binary Operations
584 case NODETYPE_SUBTRACT:
585 case NODETYPE_MULTIPLY:
586 case NODETYPE_DIVIDE:
587 case NODETYPE_MODULO:
591 case NODETYPE_BITSHIFTLEFT:
592 case NODETYPE_BITSHIFTRIGHT:
593 case NODETYPE_BITROTATELEFT:
594 case NODETYPE_EQUALS:
595 case NODETYPE_LESSTHAN:
596 case NODETYPE_GREATERTHAN:
597 case NODETYPE_LESSTHANEQUAL:
598 case NODETYPE_GREATERTHANEQUAL:
600 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
601 if(op1 == ERRPTR) return ERRPTR;
602 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
604 SpiderScript_DereferenceValue(op1);
608 ret = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Type, op1, op2);
610 // Free intermediate objects
611 SpiderScript_DereferenceValue(op1);
612 SpiderScript_DereferenceValue(op2);
617 // AST_RuntimeError(Node, "BUG - SpiderScript AST_ExecuteNode Unimplemented %i", Node->Type);
621 // Reset namespace when no longer needed
622 if( Node->Type != NODETYPE_SCOPE )
623 Block->CurNamespace = NULL;
625 #if TRACE_NODE_RETURNS
626 if(ret && ret != ERRPTR) {
627 AST_RuntimeError(Node, "Ret type of %p %i is %i", Node, Node->Type, ret->Type);
630 AST_RuntimeError(Node, "Ret type of %p %i is %p", Node, Node->Type, ret);
637 tSpiderValue *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value)
641 if( Value->Type == SS_DATATYPE_OBJECT )
646 case NODETYPE_NEGATE: fcnname = "-ve"; break;
647 case NODETYPE_BWNOT: fcnname = "~"; break;
648 default: fcnname = NULL; break;
653 ret = Object_ExecuteMethod(Value->Object, fcnname, );
661 // Integer Operations
662 case SS_DATATYPE_INTEGER:
663 if( Value->ReferenceCount == 1 )
664 SpiderScript_ReferenceValue(ret = Value);
666 ret = SpiderScript_CreateInteger(0);
669 case NODETYPE_NEGATE: ret->Integer = -Value->Integer; break;
670 case NODETYPE_BWNOT: ret->Integer = ~Value->Integer; break;
672 AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Integer unknown op %i", Operation);
673 SpiderScript_DereferenceValue(ret);
678 // Real number Operations
679 case SS_DATATYPE_REAL:
682 case NODETYPE_NEGATE: ret = SpiderScript_CreateInteger( -Value->Real ); break;
684 AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Real unknown op %i", Operation);
691 AST_RuntimeError(NULL, "Invalid operation (%i) on type (%i)", Operation, Value->Type);
699 tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right)
701 tSpiderValue *preCastValue = Right;
705 if( Left && Right && Left->Type != Right->Type )
709 // - Operator overload functions
710 if( Left->Type == SS_DATATYPE_OBJECT )
715 case NODETYPE_ADD: fcnname = "+"; break;
716 case NODETYPE_SUBTRACT: fcnname = "-"; break;
717 case NODETYPE_MULTIPLY: fcnname = "*"; break;
718 case NODETYPE_DIVIDE: fcnname = "/"; break;
719 case NODETYPE_MODULO: fcnname = "%"; break;
720 case NODETYPE_BWAND: fcnname = "&"; break;
721 case NODETYPE_BWOR: fcnname = "|"; break;
722 case NODETYPE_BWXOR: fcnname = "^"; break;
723 case NODETYPE_BITSHIFTLEFT: fcnname = "<<"; break;
724 case NODETYPE_BITSHIFTRIGHT:fcnname = ">>"; break;
725 case NODETYPE_BITROTATELEFT:fcnname = "<<<"; break;
726 default: fcnname = NULL; break;
731 ret = Object_ExecuteMethod(Left->Object, fcnname, Right);
734 // Fall through and try casting (which will usually fail)
739 // If implicit casts are allowed, convert Right to Left's type
740 if(Script->Variant->bImplicitCasts)
742 Right = SpiderScript_CastValueTo(Left->Type, Right);
746 // If statically typed, this should never happen, but catch it anyway
748 AST_RuntimeError(Node, "Implicit cast not allowed (from %i to %i)", Right->Type, Left->Type);
754 if( Left == NULL || Right == NULL ) {
755 if(Right && Right != preCastValue) free(Right);
762 case NODETYPE_EQUALS:
763 case NODETYPE_LESSTHAN:
764 case NODETYPE_GREATERTHAN:
765 case NODETYPE_LESSTHANEQUAL:
766 case NODETYPE_GREATERTHANEQUAL: {
772 // - String Compare (does a strcmp, well memcmp)
773 case SS_DATATYPE_STRING:
774 // Call memcmp to do most of the work
776 Left->String.Data, Right->String.Data,
777 (Left->String.Length < Right->String.Length) ? Left->String.Length : Right->String.Length
779 // Handle reaching the end of the string
781 if( Left->String.Length == Right->String.Length )
783 else if( Left->String.Length < Right->String.Length )
790 // - Integer Comparisons
791 case SS_DATATYPE_INTEGER:
792 if( Left->Integer == Right->Integer )
794 else if( Left->Integer < Right->Integer )
799 // - Real Number Comparisons
800 case SS_DATATYPE_REAL:
801 cmp = (Left->Real - Right->Real) / Right->Real * 10000; // < 0.1% difference is equality
804 AST_RuntimeError(Node, "TODO - Comparison of type %i", Left->Type);
812 if(Left->ReferenceCount == 1 && Left->Type != SS_DATATYPE_STRING)
813 SpiderScript_ReferenceValue(ret = Left);
815 ret = SpiderScript_CreateInteger(0);
820 case NODETYPE_EQUALS: ret->Integer = (cmp == 0); break;
821 case NODETYPE_LESSTHAN: ret->Integer = (cmp < 0); break;
822 case NODETYPE_GREATERTHAN: ret->Integer = (cmp > 0); break;
823 case NODETYPE_LESSTHANEQUAL: ret->Integer = (cmp <= 0); break;
824 case NODETYPE_GREATERTHANEQUAL: ret->Integer = (cmp >= 0); break;
826 AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Operation);
827 SpiderScript_DereferenceValue(ret);
832 if(Right && Right != preCastValue) free(Right);
836 // Fall through and sort by type instead
844 // String Concatenation
845 case SS_DATATYPE_STRING:
848 case NODETYPE_ADD: // Concatenate
849 ret = SpiderScript_StringConcat(Left, Right);
851 // TODO: Support python style 'i = %i' % i ?
852 // Might do it via a function call
853 // Implement it via % with an array, but getting past the cast will be fun
854 // case NODETYPE_MODULUS:
856 // TODO: Support string repititions
857 // case NODETYPE_MULTIPLY:
861 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,String unknown op %i", Operation);
866 // Integer Operations
867 case SS_DATATYPE_INTEGER:
868 if( Left->ReferenceCount == 1 )
869 SpiderScript_ReferenceValue(ret = Left);
871 ret = SpiderScript_CreateInteger(0);
874 case NODETYPE_ADD: ret->Integer = Left->Integer + Right->Integer; break;
875 case NODETYPE_SUBTRACT: ret->Integer = Left->Integer - Right->Integer; break;
876 case NODETYPE_MULTIPLY: ret->Integer = Left->Integer * Right->Integer; break;
877 case NODETYPE_DIVIDE: ret->Integer = Left->Integer / Right->Integer; break;
878 case NODETYPE_MODULO: ret->Integer = Left->Integer % Right->Integer; break;
879 case NODETYPE_BWAND: ret->Integer = Left->Integer & Right->Integer; break;
880 case NODETYPE_BWOR: ret->Integer = Left->Integer | Right->Integer; break;
881 case NODETYPE_BWXOR: ret->Integer = Left->Integer ^ Right->Integer; break;
882 case NODETYPE_BITSHIFTLEFT: ret->Integer = Left->Integer << Right->Integer; break;
883 case NODETYPE_BITSHIFTRIGHT:ret->Integer = Left->Integer >> Right->Integer; break;
884 case NODETYPE_BITROTATELEFT:
885 ret->Integer = (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer));
888 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i", Operation);
889 SpiderScript_DereferenceValue(ret);
896 case SS_DATATYPE_REAL:
897 if( Left->ReferenceCount == 1 )
898 SpiderScript_ReferenceValue(ret = Left);
900 ret = SpiderScript_CreateReal(0);
903 case NODETYPE_ADD: ret->Real = Left->Real + Right->Real; break;
904 case NODETYPE_SUBTRACT: ret->Real = Left->Real - Right->Real; break;
905 case NODETYPE_MULTIPLY: ret->Real = Left->Real * Right->Real; break;
906 case NODETYPE_DIVIDE: ret->Real = Left->Real / Right->Real; break;
908 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation);
909 SpiderScript_DereferenceValue(ret);
916 AST_RuntimeError(Node, "BUG - Invalid operation (%i) on type (%i)", Operation, Left->Type);
921 if(Right && Right != preCastValue) free(Right);
927 * \brief Define a variable
928 * \param Block Current block state
929 * \param Type Type of the variable
930 * \param Name Name of the variable
931 * \return Boolean Failure
933 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value)
935 tAST_Variable *var, *prev = NULL;
937 for( var = Block->FirstVar; var; prev = var, var = var->Next )
939 if( strcmp(var->Name, Name) == 0 ) {
940 AST_RuntimeError(NULL, "Redefinition of variable '%s'", Name);
945 var = malloc( sizeof(tAST_Variable) + strlen(Name) + 1 );
949 if(Value) SpiderScript_ReferenceValue(Value);
950 strcpy(var->Name, Name);
952 if(prev) prev->Next = var;
953 else Block->FirstVar = var;
955 //printf("Defined variable %s (%i)\n", Name, Type);
960 tAST_Variable *Variable_Lookup(tAST_BlockState *Block, tAST_Node *VarNode, int CreateType)
962 tAST_Variable *var = NULL;
965 if( VarNode->BlockState == Block && VarNode->BlockIdent == Block->Ident ) {
966 var = VarNode->ValueCache;
967 #if TRACE_VAR_LOOKUPS
968 AST_RuntimeMessage(VarNode, "debug", "Fast var fetch on '%s' %p (%p:%i)",
969 VarNode->Variable.Name, var,
970 VarNode->BlockState, VarNode->BlockIdent
977 for( bs = Block; bs; bs = bs->Parent )
979 for( var = bs->FirstVar; var; var = var->Next )
981 if( strcmp(var->Name, VarNode->Variable.Name) == 0 )
989 if( Block->Script->Variant->bDyamicTyped && CreateType != SS_DATATYPE_UNDEF ) {
991 var = Variable_Define(Block, CreateType, VarNode->Variable.Name, NULL);
995 AST_RuntimeError(VarNode, "Variable '%s' is undefined", VarNode->Variable.Name);
1000 #if TRACE_VAR_LOOKUPS
1001 AST_RuntimeMessage(VarNode, "debug", "Saved variable lookup of '%s' %p (%p:%i)",
1002 VarNode->Variable.Name, var,
1003 Block, Block->Ident);
1006 VarNode->ValueCache = var;
1007 VarNode->BlockState = Block;
1008 VarNode->BlockIdent = Block->Ident;
1015 * \brief Set the value of a variable
1016 * \return Boolean Failure
1018 int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value)
1022 var = Variable_Lookup(Block, VarNode, (Value ? Value->Type : SS_DATATYPE_UNDEF));
1024 if( !var ) return -1;
1026 if( !Block->Script->Variant->bDyamicTyped && (Value && var->Type != Value->Type) )
1028 AST_RuntimeError(VarNode, "Type mismatch assigning to '%s'",
1029 VarNode->Variable.Name);
1033 // printf("Assign %p to '%s'\n", Value, var->Name);
1034 SpiderScript_ReferenceValue(Value);
1035 SpiderScript_DereferenceValue(var->Object);
1036 var->Object = Value;
1041 * \brief Get the value of a variable
1043 tSpiderValue *Variable_GetValue(tAST_BlockState *Block, tAST_Node *VarNode)
1045 tAST_Variable *var = Variable_Lookup(Block, VarNode, 0);
1047 if( !var ) return ERRPTR;
1049 SpiderScript_ReferenceValue(var->Object);
1054 * \brief Destorys a variable
1056 void Variable_Destroy(tAST_Variable *Variable)
1058 // printf("Variable_Destroy: (%p'%s')\n", Variable, Variable->Name);
1059 SpiderScript_DereferenceValue(Variable->Object);