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
88 int cmp; // Used in comparisons
101 tAST_BlockState blockInfo;
102 blockInfo.Parent = Block;
103 blockInfo.Script = Block->Script;
104 blockInfo.FirstVar = NULL;
105 blockInfo.RetVal = NULL;
106 blockInfo.BaseNamespace = Block->BaseNamespace;
107 blockInfo.CurNamespace = NULL;
108 blockInfo.BreakTarget = NULL;
109 blockInfo.Ident = giNextBlockIdent ++;
111 // Loop over all nodes, or until the return value is set
112 for(node = Node->Block.FirstChild;
113 node && !blockInfo.RetVal && !blockInfo.BreakTarget;
114 node = node->NextSibling )
116 ret = AST_ExecuteNode(&blockInfo, node);
117 if(ret == ERRPTR) break; // Error check
118 if(ret != NULL) SpiderScript_DereferenceValue(ret); // Free unused value
120 // Clean up variables
121 while(blockInfo.FirstVar)
123 tAST_Variable *nextVar = blockInfo.FirstVar->Next;
124 Variable_Destroy( blockInfo.FirstVar );
125 blockInfo.FirstVar = nextVar;
127 // Clear ret if not an error
128 if(ret != ERRPTR) ret = NULL;
130 // Set parent's return value if needed
131 if( blockInfo.RetVal )
132 Block->RetVal = blockInfo.RetVal;
133 if( blockInfo.BreakTarget ) {
134 Block->BreakTarget = blockInfo.BreakTarget;
135 Block->BreakType = blockInfo.BreakType;
138 // TODO: Unset break if break type deontes a block break
144 case NODETYPE_ASSIGN:
145 // TODO: Support assigning to object attributes
146 if( Node->Assign.Dest->Type != NODETYPE_VARIABLE ) {
147 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
150 ret = AST_ExecuteNode(Block, Node->Assign.Value);
151 if(ret == ERRPTR) return ERRPTR;
153 // Perform assignment operation
154 if( Node->Assign.Operation != NODETYPE_NOP )
156 tSpiderValue *varVal, *value;
158 varVal = Variable_GetValue(Block, Node->Assign.Dest);
159 if(varVal == ERRPTR) return ERRPTR;
162 if(varVal && varVal->ReferenceCount == 2) {
163 SpiderScript_DereferenceValue(varVal);
164 // printf("pre: (%s) varVal->ReferenceCount = %i\n",
165 // Node->Assign.Dest->Variable.Name,
166 // varVal->ReferenceCount);
169 value = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Assign.Operation, varVal, ret);
170 if(value == ERRPTR) return ERRPTR;
172 if(ret) SpiderScript_DereferenceValue(ret);
174 if(varVal) SpiderScript_DereferenceValue(varVal);
176 if(varVal && varVal->ReferenceCount == 1) {
177 SpiderScript_ReferenceValue(varVal);
178 // printf("post: varVal->ReferenceCount = %i\n", varVal->ReferenceCount);
179 break; // If varVal was non-null, it has been updated by _BinOp
182 // Else, it was NULL, so has to be assigned
186 // Set the variable value
187 if( Variable_SetValue( Block, Node->Assign.Dest, ret ) ) {
188 SpiderScript_DereferenceValue( ret );
193 // Post increment/decrement
194 case NODETYPE_POSTINC:
195 case NODETYPE_POSTDEC:
197 tSpiderValue *varVal, *value;
198 static tSpiderValue one = {
199 .Type = SS_DATATYPE_INTEGER,
204 // TODO: Support assigning to object attributes
205 if( Node->UniOp.Value->Type != NODETYPE_VARIABLE ) {
206 AST_RuntimeError(Node, "LVALUE of assignment is not a variable");
210 // Get values (current variable contents and a static one)
211 varVal = Variable_GetValue(Block, Node->UniOp.Value);
213 if( Node->Type == NODETYPE_POSTDEC )
214 value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_SUBTRACT, varVal, &one);
216 value = AST_ExecuteNode_BinOp(Block->Script, Node, NODETYPE_ADD, varVal, &one);
217 if( value == ERRPTR )
222 if( Variable_SetValue( Block, Node->UniOp.Value, value ) ) {
223 SpiderScript_DereferenceValue( ret );
226 SpiderScript_DereferenceValue( value );
231 case NODETYPE_METHODCALL:
232 case NODETYPE_FUNCTIONCALL:
233 case NODETYPE_CREATEOBJECT:
234 // Logical block (used to allocate `params`)
236 tSpiderNamespace *ns = Block->CurNamespace;
237 tSpiderValue *params[Node->FunctionCall.NumArgs];
239 for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling)
241 params[i] = AST_ExecuteNode(Block, node);
242 if( params[i] == ERRPTR ) {
243 while(i--) SpiderScript_DereferenceValue(params[i]);
250 if( !ns ) ns = Block->BaseNamespace;
253 if( Node->Type == NODETYPE_CREATEOBJECT )
255 ret = SpiderScript_CreateObject(Block->Script,
257 Node->FunctionCall.Name,
258 Node->FunctionCall.NumArgs, params
261 else if( Node->Type == NODETYPE_METHODCALL )
263 tSpiderValue *obj = AST_ExecuteNode(Block, Node->FunctionCall.Object);
264 if( !obj || obj == ERRPTR || obj->Type != SS_DATATYPE_OBJECT ) {
265 AST_RuntimeError(Node->FunctionCall.Object,
266 "Type Mismatch - Required SS_DATATYPE_OBJECT for method call");
267 while(i--) SpiderScript_DereferenceValue(params[i]);
271 ret = SpiderScript_ExecuteMethod(Block->Script,
272 obj->Object, Node->FunctionCall.Name,
273 Node->FunctionCall.NumArgs, params
275 SpiderScript_DereferenceValue(obj);
279 ret = SpiderScript_ExecuteFunction(Block->Script,
280 ns, Node->FunctionCall.Name,
281 Node->FunctionCall.NumArgs, params
286 // Dereference parameters
287 while(i--) SpiderScript_DereferenceValue(params[i]);
295 ret = AST_ExecuteNode(Block, Node->If.Condition);
296 if( ret == ERRPTR ) break;
297 if( SpiderScript_IsValueTrue(ret) ) {
298 tmpobj = AST_ExecuteNode(Block, Node->If.True);
301 tmpobj = AST_ExecuteNode(Block, Node->If.False);
303 SpiderScript_DereferenceValue(ret);
304 if( tmpobj == ERRPTR ) return ERRPTR;
305 SpiderScript_DereferenceValue(tmpobj);
312 ret = AST_ExecuteNode(Block, Node->For.Init);
313 if(ret == ERRPTR) break;
315 // Check initial condition
316 if( !Node->For.bCheckAfter )
318 SpiderScript_DereferenceValue(ret);
320 ret = AST_ExecuteNode(Block, Node->For.Condition);
321 if(ret == ERRPTR) return ERRPTR;
322 if(!SpiderScript_IsValueTrue(ret)) {
323 SpiderScript_DereferenceValue(ret);
332 SpiderScript_DereferenceValue(ret);
335 ret = AST_ExecuteNode(Block, Node->For.Code);
336 if(ret == ERRPTR) return ERRPTR;
337 SpiderScript_DereferenceValue(ret);
339 if(Block->BreakTarget)
341 if( Block->BreakTarget[0] == '\0' || strcmp(Block->BreakTarget, Node->For.Tag) == 0 )
344 free((void*)Block->BreakTarget); Block->BreakTarget = NULL;
345 if( Block->BreakType == NODETYPE_CONTINUE ) {
346 // Continue, just keep going
352 break; // Break out of this loop
356 ret = AST_ExecuteNode(Block, Node->For.Increment);
357 if(ret == ERRPTR) return ERRPTR;
358 SpiderScript_DereferenceValue(ret);
361 ret = AST_ExecuteNode(Block, Node->For.Condition);
362 if(ret == ERRPTR) return ERRPTR;
363 if(!SpiderScript_IsValueTrue(ret)) break;
365 SpiderScript_DereferenceValue(ret);
370 case NODETYPE_RETURN:
371 ret = AST_ExecuteNode(Block, Node->UniOp.Value);
372 if(ret == ERRPTR) break;
373 Block->RetVal = ret; // Return value set
374 ret = NULL; // the `return` statement does not return a value
378 case NODETYPE_CONTINUE:
379 Block->BreakTarget = strdup(Node->Variable.Name);
380 Block->BreakType = Node->Type;
384 case NODETYPE_DEFVAR:
385 if( Node->DefVar.InitialValue ) {
386 tmpobj = AST_ExecuteNode(Block, Node->DefVar.InitialValue);
387 if(tmpobj == ERRPTR) return ERRPTR;
392 // TODO: Handle arrays
394 if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name, tmpobj) == ERRPTR )
396 SpiderScript_DereferenceValue(tmpobj);
402 tSpiderNamespace *ns;
404 // Set current namespace if unset
405 if( !Block->CurNamespace )
406 Block->CurNamespace = Block->BaseNamespace;
408 // Empty string means use the root namespace
409 if( Node->Scope.Name[0] == '\0' )
411 ns = &Block->Script->Variant->RootNamespace;
415 // Otherwise scan the current namespace for the element
416 for( ns = Block->CurNamespace->FirstChild; ns; ns = ns->Next )
418 if( strcmp(ns->Name, Node->Scope.Name) == 0 )
423 AST_RuntimeError(Node, "Unknown namespace '%s'", Node->Scope.Name);
427 Block->CurNamespace = ns;
429 ret = AST_ExecuteNode(Block, Node->Scope.Element);
434 case NODETYPE_VARIABLE:
435 ret = Variable_GetValue( Block, Node );
438 // Element of an Object
439 case NODETYPE_ELEMENT:
440 tmpobj = AST_ExecuteNode( Block, Node->Scope.Element );
441 if(tmpobj == ERRPTR) return ERRPTR;
442 if( !tmpobj || tmpobj->Type != SS_DATATYPE_OBJECT )
444 AST_RuntimeError(Node->Scope.Element, "Unable to dereference a non-object");
449 for( i = 0; i < tmpobj->Object->Type->NAttributes; i ++ )
451 if( strcmp(Node->Scope.Name, tmpobj->Object->Type->AttributeDefs[i].Name) == 0 )
453 ret = tmpobj->Object->Attributes[i];
454 SpiderScript_ReferenceValue(ret);
458 if( i == tmpobj->Object->Type->NAttributes )
460 AST_RuntimeError(Node->Scope.Element, "Unknown attribute '%s' of class '%s'",
461 Node->Scope.Name, tmpobj->Object->Type->Name);
466 // Cast a value to another
469 tmpobj = AST_ExecuteNode(Block, Node->Cast.Value);
470 if(tmpobj == ERRPTR) return ERRPTR;
471 ret = SpiderScript_CastValueTo( Node->Cast.DataType, tmpobj );
472 SpiderScript_DereferenceValue(tmpobj);
476 // Index into an array
478 op1 = AST_ExecuteNode(Block, Node->BinOp.Left); // Array
479 if(op1 == ERRPTR) return ERRPTR;
480 op2 = AST_ExecuteNode(Block, Node->BinOp.Right); // Offset
482 SpiderScript_DereferenceValue(op1);
486 if( !op1 || op1->Type != SS_DATATYPE_ARRAY )
488 // TODO: Implement "operator []" on objects
489 AST_RuntimeError(Node, "Indexing non-array");
494 if( (!op2 || op2->Type != SS_DATATYPE_INTEGER) && !Block->Script->Variant->bImplicitCasts ) {
495 AST_RuntimeError(Node, "Array index is not an integer");
500 if( !op2 || op2->Type != SS_DATATYPE_INTEGER )
502 tmpobj = SpiderScript_CastValueTo(SS_DATATYPE_INTEGER, op2);
503 SpiderScript_DereferenceValue(op2);
507 if( op2->Integer >= op1->Array.Length ) {
508 AST_RuntimeError(Node, "Array index out of bounds %i >= %i",
509 op2->Integer, op1->Array.Length);
514 ret = op1->Array.Items[ op2->Integer ];
515 SpiderScript_ReferenceValue(ret);
517 SpiderScript_DereferenceValue(op1);
518 SpiderScript_DereferenceValue(op2);
521 // TODO: Implement runtime constants
522 case NODETYPE_CONSTANT:
523 // TODO: Scan namespace for constant name
524 AST_RuntimeError(Node, "TODO - Runtime Constants");
529 case NODETYPE_STRING:
530 case NODETYPE_INTEGER:
532 ret = &Node->Constant;
533 SpiderScript_ReferenceValue(ret);
536 // --- Operations ---
537 // Boolean Operations
538 case NODETYPE_LOGICALNOT: // Logical NOT (!)
539 op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
540 if(op1 == ERRPTR) return ERRPTR;
541 ret = SpiderScript_CreateInteger( !SpiderScript_IsValueTrue(op1) );
542 SpiderScript_DereferenceValue(op1);
544 case NODETYPE_LOGICALAND: // Logical AND (&&)
545 case NODETYPE_LOGICALOR: // Logical OR (||)
546 case NODETYPE_LOGICALXOR: // Logical XOR (^^)
547 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
548 if(op1 == ERRPTR) return ERRPTR;
549 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
551 SpiderScript_DereferenceValue(op1);
557 case NODETYPE_LOGICALAND:
558 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) && SpiderScript_IsValueTrue(op2) );
560 case NODETYPE_LOGICALOR:
561 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) || SpiderScript_IsValueTrue(op2) );
563 case NODETYPE_LOGICALXOR:
564 ret = SpiderScript_CreateInteger( SpiderScript_IsValueTrue(op1) ^ SpiderScript_IsValueTrue(op2) );
569 // Free intermediate objects
570 SpiderScript_DereferenceValue(op1);
571 SpiderScript_DereferenceValue(op2);
575 case NODETYPE_EQUALS:
576 case NODETYPE_LESSTHAN:
577 case NODETYPE_GREATERTHAN:
578 case NODETYPE_LESSTHANEQUAL:
579 case NODETYPE_GREATERTHANEQUAL:
580 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
581 if(op1 == ERRPTR) return ERRPTR;
582 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
584 SpiderScript_DereferenceValue(op1);
590 AST_RuntimeError(Node, "NULL Comparison (%p and %p)", op1, op2);
591 if(op1) SpiderScript_DereferenceValue(op1);
592 if(op2) SpiderScript_DereferenceValue(op2);
593 ret = SpiderScript_CreateInteger( !op1 && !op2 );
598 if( op1->Type != op2->Type ) {
599 // If dynamically typed, convert op2 to op1's type
600 if(Block->Script->Variant->bImplicitCasts)
603 op2 = SpiderScript_CastValueTo(op1->Type, op2);
604 SpiderScript_DereferenceValue(tmpobj);
606 SpiderScript_DereferenceValue(op1);
610 // If statically typed, this should never happen, but catch it anyway
612 AST_RuntimeError(Node, "Statically typed implicit cast %i <op> %i",
613 op1->Type, op2->Type);
621 // - String Compare (does a strcmp, well memcmp)
622 case SS_DATATYPE_STRING:
623 // Call memcmp to do most of the work
625 op1->String.Data, op2->String.Data,
626 (op1->String.Length < op2->String.Length) ? op1->String.Length : op2->String.Length
628 // Handle reaching the end of the string
630 if( op1->String.Length == op2->String.Length )
632 else if( op1->String.Length < op2->String.Length )
639 // - Integer Comparisons
640 case SS_DATATYPE_INTEGER:
641 if( op1->Integer == op2->Integer )
643 else if( op1->Integer < op2->Integer )
648 // - Real Number Comparisons
649 case SS_DATATYPE_REAL:
650 cmp = (op1->Real - op2->Real) / op2->Real * 10000; // < 0.1% difference is equality
653 AST_RuntimeError(Node, "TODO - Comparison of type %i", op1->Type);
658 // Free intermediate objects
659 SpiderScript_DereferenceValue(op1);
660 SpiderScript_DereferenceValue(op2);
669 case NODETYPE_EQUALS: ret = SpiderScript_CreateInteger(cmp == 0); break;
670 case NODETYPE_LESSTHAN: ret = SpiderScript_CreateInteger(cmp < 0); break;
671 case NODETYPE_GREATERTHAN: ret = SpiderScript_CreateInteger(cmp > 0); break;
672 case NODETYPE_LESSTHANEQUAL: ret = SpiderScript_CreateInteger(cmp <= 0); break;
673 case NODETYPE_GREATERTHANEQUAL: ret = SpiderScript_CreateInteger(cmp >= 0); break;
675 AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Node->Type);
681 // General Unary Operations
682 case NODETYPE_BWNOT: // Bitwise NOT (~)
683 case NODETYPE_NEGATE: // Negation (-)
684 op1 = AST_ExecuteNode(Block, Node->UniOp.Value);
685 if(op1 == ERRPTR) return ERRPTR;
686 ret = AST_ExecuteNode_UniOp(Block->Script, Node, Node->Type, op1);
687 SpiderScript_DereferenceValue(op1);
690 // General Binary Operations
692 case NODETYPE_SUBTRACT:
693 case NODETYPE_MULTIPLY:
694 case NODETYPE_DIVIDE:
695 case NODETYPE_MODULO:
699 case NODETYPE_BITSHIFTLEFT:
700 case NODETYPE_BITSHIFTRIGHT:
701 case NODETYPE_BITROTATELEFT:
703 op1 = AST_ExecuteNode(Block, Node->BinOp.Left);
704 if(op1 == ERRPTR) return ERRPTR;
705 op2 = AST_ExecuteNode(Block, Node->BinOp.Right);
707 SpiderScript_DereferenceValue(op1);
711 ret = AST_ExecuteNode_BinOp(Block->Script, Node, Node->Type, op1, op2);
713 // Free intermediate objects
714 SpiderScript_DereferenceValue(op1);
715 SpiderScript_DereferenceValue(op2);
720 // AST_RuntimeError(Node, "BUG - SpiderScript AST_ExecuteNode Unimplemented %i", Node->Type);
724 // Reset namespace when no longer needed
725 if( Node->Type != NODETYPE_SCOPE )
726 Block->CurNamespace = NULL;
728 #if TRACE_NODE_RETURNS
729 if(ret && ret != ERRPTR) {
730 AST_RuntimeError(Node, "Ret type of %p %i is %i", Node, Node->Type, ret->Type);
733 AST_RuntimeError(Node, "Ret type of %p %i is %p", Node, Node->Type, ret);
740 tSpiderValue *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value)
744 if( Value->Type == SS_DATATYPE_OBJECT )
749 case NODETYPE_NEGATE: fcnname = "-ve"; break;
750 case NODETYPE_BWNOT: fcnname = "~"; break;
751 default: fcnname = NULL; break;
756 ret = Object_ExecuteMethod(Value->Object, fcnname, );
764 // Integer Operations
765 case SS_DATATYPE_INTEGER:
766 if( Value->ReferenceCount == 1 )
767 SpiderScript_ReferenceValue(ret = Value);
769 ret = SpiderScript_CreateInteger(0);
772 case NODETYPE_NEGATE: ret->Integer = -Value->Integer; break;
773 case NODETYPE_BWNOT: ret->Integer = ~Value->Integer; break;
775 AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Integer unknown op %i", Operation);
776 SpiderScript_DereferenceValue(ret);
781 // Real number Operations
782 case SS_DATATYPE_REAL:
785 case NODETYPE_NEGATE: ret = SpiderScript_CreateInteger( -Value->Real ); break;
787 AST_RuntimeError(Node, "SpiderScript internal error: Exec,UniOP,Real unknown op %i", Operation);
794 AST_RuntimeError(NULL, "Invalid operation (%i) on type (%i)", Operation, Value->Type);
802 tSpiderValue *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right)
804 tSpiderValue *preCastValue = Right;
808 if( Left && Right && Left->Type != Right->Type )
812 // - Operator overload functions
813 if( Left->Type == SS_DATATYPE_OBJECT )
818 case NODETYPE_ADD: fcnname = "+"; break;
819 case NODETYPE_SUBTRACT: fcnname = "-"; break;
820 case NODETYPE_MULTIPLY: fcnname = "*"; break;
821 case NODETYPE_DIVIDE: fcnname = "/"; break;
822 case NODETYPE_MODULO: fcnname = "%"; break;
823 case NODETYPE_BWAND: fcnname = "&"; break;
824 case NODETYPE_BWOR: fcnname = "|"; break;
825 case NODETYPE_BWXOR: fcnname = "^"; break;
826 case NODETYPE_BITSHIFTLEFT: fcnname = "<<"; break;
827 case NODETYPE_BITSHIFTRIGHT:fcnname = ">>"; break;
828 case NODETYPE_BITROTATELEFT:fcnname = "<<<"; break;
829 default: fcnname = NULL; break;
834 ret = Object_ExecuteMethod(Left->Object, fcnname, Right);
837 // Fall through and try casting (which will usually fail)
842 // If implicit casts are allowed, convert Right to Left's type
843 if(Script->Variant->bImplicitCasts)
845 Right = SpiderScript_CastValueTo(Left->Type, Right);
849 // If statically typed, this should never happen, but catch it anyway
851 AST_RuntimeError(Node, "Implicit cast not allowed (from %i to %i)", Right->Type, Left->Type);
857 if( Left == NULL || Right == NULL ) {
858 if(Right && Right != preCastValue) free(Right);
865 // String Concatenation
866 case SS_DATATYPE_STRING:
869 case NODETYPE_ADD: // Concatenate
870 ret = SpiderScript_StringConcat(Left, Right);
872 // TODO: Support python style 'i = %i' % i ?
873 // Might do it via a function call
874 // Implement it via % with an array, but getting past the cast will be fun
875 // case NODETYPE_MODULUS:
877 // TODO: Support string repititions
878 // case NODETYPE_MULTIPLY:
882 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,String unknown op %i", Operation);
887 // Integer Operations
888 case SS_DATATYPE_INTEGER:
889 if( Left->ReferenceCount == 1 )
890 SpiderScript_ReferenceValue(ret = Left);
892 ret = SpiderScript_CreateInteger(0);
895 case NODETYPE_ADD: ret->Integer = Left->Integer + Right->Integer; break;
896 case NODETYPE_SUBTRACT: ret->Integer = Left->Integer - Right->Integer; break;
897 case NODETYPE_MULTIPLY: ret->Integer = Left->Integer * Right->Integer; break;
898 case NODETYPE_DIVIDE: ret->Integer = Left->Integer / Right->Integer; break;
899 case NODETYPE_MODULO: ret->Integer = Left->Integer % Right->Integer; break;
900 case NODETYPE_BWAND: ret->Integer = Left->Integer & Right->Integer; break;
901 case NODETYPE_BWOR: ret->Integer = Left->Integer | Right->Integer; break;
902 case NODETYPE_BWXOR: ret->Integer = Left->Integer ^ Right->Integer; break;
903 case NODETYPE_BITSHIFTLEFT: ret->Integer = Left->Integer << Right->Integer; break;
904 case NODETYPE_BITSHIFTRIGHT:ret->Integer = Left->Integer >> Right->Integer; break;
905 case NODETYPE_BITROTATELEFT:
906 ret->Integer = (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer));
909 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i", Operation);
910 SpiderScript_DereferenceValue(ret);
917 case SS_DATATYPE_REAL:
918 if( Left->ReferenceCount == 1 )
919 SpiderScript_ReferenceValue(ret = Left);
921 ret = SpiderScript_CreateReal(0);
924 case NODETYPE_ADD: ret->Real = Left->Real + Right->Real; break;
925 case NODETYPE_SUBTRACT: ret->Real = Left->Real - Right->Real; break;
926 case NODETYPE_MULTIPLY: ret->Real = Left->Real * Right->Real; break;
927 case NODETYPE_DIVIDE: ret->Real = Left->Real / Right->Real; break;
929 AST_RuntimeError(Node, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation);
930 SpiderScript_DereferenceValue(ret);
937 AST_RuntimeError(Node, "BUG - Invalid operation (%i) on type (%i)", Operation, Left->Type);
942 if(Right && Right != preCastValue) free(Right);
948 * \brief Define a variable
949 * \param Block Current block state
950 * \param Type Type of the variable
951 * \param Name Name of the variable
952 * \return Boolean Failure
954 tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value)
956 tAST_Variable *var, *prev = NULL;
958 for( var = Block->FirstVar; var; prev = var, var = var->Next )
960 if( strcmp(var->Name, Name) == 0 ) {
961 AST_RuntimeError(NULL, "Redefinition of variable '%s'", Name);
966 var = malloc( sizeof(tAST_Variable) + strlen(Name) + 1 );
970 if(Value) SpiderScript_ReferenceValue(Value);
971 strcpy(var->Name, Name);
973 if(prev) prev->Next = var;
974 else Block->FirstVar = var;
976 //printf("Defined variable %s (%i)\n", Name, Type);
981 tAST_Variable *Variable_Lookup(tAST_BlockState *Block, tAST_Node *VarNode, int CreateType)
983 tAST_Variable *var = NULL;
986 if( VarNode->BlockState == Block && VarNode->BlockIdent == Block->Ident ) {
987 var = VarNode->ValueCache;
988 #if TRACE_VAR_LOOKUPS
989 AST_RuntimeMessage(VarNode, "debug", "Fast var fetch on '%s' %p (%p:%i)",
990 VarNode->Variable.Name, var,
991 VarNode->BlockState, VarNode->BlockIdent
998 for( bs = Block; bs; bs = bs->Parent )
1000 for( var = bs->FirstVar; var; var = var->Next )
1002 if( strcmp(var->Name, VarNode->Variable.Name) == 0 )
1010 if( Block->Script->Variant->bDyamicTyped && CreateType != SS_DATATYPE_UNDEF ) {
1012 var = Variable_Define(Block, CreateType, VarNode->Variable.Name, NULL);
1016 AST_RuntimeError(VarNode, "Variable '%s' is undefined", VarNode->Variable.Name);
1021 #if TRACE_VAR_LOOKUPS
1022 AST_RuntimeMessage(VarNode, "debug", "Saved variable lookup of '%s' %p (%p:%i)",
1023 VarNode->Variable.Name, var,
1024 Block, Block->Ident);
1027 VarNode->ValueCache = var;
1028 VarNode->BlockState = Block;
1029 VarNode->BlockIdent = Block->Ident;
1036 * \brief Set the value of a variable
1037 * \return Boolean Failure
1039 int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value)
1043 var = Variable_Lookup(Block, VarNode, (Value ? Value->Type : SS_DATATYPE_UNDEF));
1045 if( !var ) return -1;
1047 if( !Block->Script->Variant->bDyamicTyped && (Value && var->Type != Value->Type) )
1049 AST_RuntimeError(VarNode, "Type mismatch assigning to '%s'",
1050 VarNode->Variable.Name);
1054 // printf("Assign %p to '%s'\n", Value, var->Name);
1055 SpiderScript_ReferenceValue(Value);
1056 SpiderScript_DereferenceValue(var->Object);
1057 var->Object = Value;
1062 * \brief Get the value of a variable
1064 tSpiderValue *Variable_GetValue(tAST_BlockState *Block, tAST_Node *VarNode)
1066 tAST_Variable *var = Variable_Lookup(Block, VarNode, 0);
1068 if( !var ) return ERRPTR;
1070 SpiderScript_ReferenceValue(var->Object);
1075 * \brief Destorys a variable
1077 void Variable_Destroy(tAST_Variable *Variable)
1079 // printf("Variable_Destroy: (%p'%s')\n", Variable, Variable->Name);
1080 SpiderScript_DereferenceValue(Variable->Object);