X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibspiderscript.so_src%2Fexec_ast.c;h=22b2d20e9521723402097759a6601155d509612b;hb=0f81e4f273cdf1032e9bca55b4185ca9113596cc;hp=e0de9ff03248573c0c05351acef06065b72212e6;hpb=5e007006d5b007c29268e4c949a8c9d472233257;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libspiderscript.so_src/exec_ast.c b/Usermode/Libraries/libspiderscript.so_src/exec_ast.c index e0de9ff0..22b2d20e 100644 --- a/Usermode/Libraries/libspiderscript.so_src/exec_ast.c +++ b/Usermode/Libraries/libspiderscript.so_src/exec_ast.c @@ -6,6 +6,9 @@ #include #include "ast.h" +#define TRACE_VAR_LOOKUPS 0 +#define TRACE_NODE_RETURNS 0 + // === IMPORTS === extern tSpiderFunction *gpExports_First; @@ -22,14 +25,19 @@ char *SpiderScript_DumpValue(tSpiderValue *Value); tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node); tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpiderValue *Left, tSpiderValue *Right); +tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, int Operation, tSpiderValue *Value); -tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name); - int Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value); -tSpiderValue *Variable_GetValue(tAST_BlockState *Block, const char *Name); +tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value); + int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value); +tSpiderValue *Variable_GetValue(tAST_BlockState *Block, tAST_Node *VarNode); void Variable_Destroy(tAST_Variable *Variable); +void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...); void AST_RuntimeError(tAST_Node *Node, const char *Format, ...); +// === GLOBALS === + int giNextBlockIdent = 1; + // === CODE === /** * \brief Dereference a created object @@ -123,7 +131,7 @@ tSpiderValue *SpiderScript_CreateString(int Length, const char *Data) /** * \brief Concatenate two strings */ -tSpiderValue *Object_StringConcat(tSpiderValue *Str1, tSpiderValue *Str2) +tSpiderValue *Object_StringConcat(const tSpiderValue *Str1, const tSpiderValue *Str2) { int newLen = 0; tSpiderValue *ret; @@ -155,7 +163,16 @@ tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source) tSpiderValue *ret = ERRPTR; int len = 0; - if( !Source ) return NULL; + if( !Source ) + { + switch(Type) + { + case SS_DATATYPE_INTEGER: return SpiderScript_CreateInteger(0); + case SS_DATATYPE_REAL: return SpiderScript_CreateReal(0); + case SS_DATATYPE_STRING: return SpiderScript_CreateString(4, "null"); + } + return NULL; + } // Check if anything needs to be done if( Source->Type == Type ) { @@ -163,6 +180,20 @@ tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source) return Source; } + #if 0 + { + printf("Casting %i ", Source->Type); + switch(Source->Type) + { + case SS_DATATYPE_INTEGER: printf("0x%lx", Source->Integer); break; + case SS_DATATYPE_STRING: printf("\"%s\"", Source->String.Data); break; + case SS_DATATYPE_REAL: printf("%f", Source->Real); break; + default: break; + } + printf(" to %i\n", Type); + } + #endif + #if 0 if( Source->Type == SS_DATATYPE_OBJECT ) { @@ -213,11 +244,27 @@ tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source) } break; + case SS_DATATYPE_REAL: + ret = malloc(sizeof(tSpiderValue)); + ret->Type = SS_DATATYPE_REAL; + ret->ReferenceCount = 1; + switch(Source->Type) + { + case SS_DATATYPE_STRING: ret->Real = atof(Source->String.Data); break; + case SS_DATATYPE_INTEGER: ret->Real = Source->Integer; break; + default: + AST_RuntimeError(NULL, "Invalid cast from %i to Real", Source->Type); + free(ret); + ret = ERRPTR; + break; + } + break; + case SS_DATATYPE_STRING: switch(Source->Type) { case SS_DATATYPE_INTEGER: len = snprintf(NULL, 0, "%li", Source->Integer); break; - case SS_DATATYPE_REAL: snprintf(NULL, 0, "%f", Source->Real); break; + case SS_DATATYPE_REAL: len = snprintf(NULL, 0, "%g", Source->Real); break; default: break; } ret = malloc(sizeof(tSpiderValue) + len + 1); @@ -227,7 +274,8 @@ tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source) switch(Source->Type) { case SS_DATATYPE_INTEGER: sprintf(ret->String.Data, "%li", Source->Integer); break; - case SS_DATATYPE_REAL: sprintf(ret->String.Data, "%f", Source->Real); break; + case SS_DATATYPE_REAL: + sprintf(ret->String.Data, "%g", Source->Real); break; default: AST_RuntimeError(NULL, "Invalid cast from %i to String", Source->Type); free(ret); @@ -237,7 +285,8 @@ tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source) break; default: - AST_RuntimeError(NULL, "BUG - BUG REPORT: Unimplemented cast target"); + AST_RuntimeError(NULL, "BUG - BUG REPORT: Unimplemented cast target %i", Type); + ret = ERRPTR; break; } @@ -382,20 +431,25 @@ tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script, bs.BaseNamespace = &Script->Variant->RootNamespace; bs.CurNamespace = NULL; bs.Script = Script; + bs.Ident = giNextBlockIdent ++; - + // Parse arguments for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ ) { - // TODO: Type checks - Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name); if( i >= NArguments ) break; // TODO: Return gracefully - Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]); + // TODO: Type checks + Variable_Define(&bs, + arg->DefVar.DataType, arg->DefVar.Name, + Arguments[i]); } // Execute function ret = AST_ExecuteNode(&bs, astFcn->Code); - Object_Dereference(ret); // Dereference output of last block statement - ret = bs.RetVal; // Set to return value of block + if(ret != ERRPTR) + { + Object_Dereference(ret); // Dereference output of last block statement + ret = bs.RetVal; // Set to return value of block + } bFound = 1; while(bs.FirstVar) @@ -555,20 +609,24 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script, bs.BaseNamespace = &Script->Variant->RootNamespace; bs.CurNamespace = NULL; bs.Script = Script; - + bs.Ident = giNextBlockIdent ++; for( arg = astFcn->Arguments; arg; arg = arg->NextSibling, i++ ) { - // TODO: Type checks - Variable_Define(&bs, arg->DefVar.DataType, arg->DefVar.Name); if( i >= NArguments ) break; // TODO: Return gracefully - Variable_SetValue(&bs, arg->DefVar.Name, Arguments[i]); + // TODO: Type checks + Variable_Define(&bs, + arg->DefVar.DataType, arg->DefVar.Name, + Arguments[i]); } // Execute function ret = AST_ExecuteNode(&bs, astFcn->Code); - Object_Dereference(ret); // Dereference output of last block statement - ret = bs.RetVal; // Set to return value of block + if( ret != ERRPTR ) + { + Object_Dereference(ret); // Dereference output of last block statement + ret = bs.RetVal; // Set to return value of block + } bFound = 1; while(bs.FirstVar) @@ -657,7 +715,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) tSpiderValue *ret = NULL, *tmpobj; tSpiderValue *op1, *op2; // Binary operations int cmp; // Used in comparisons - int i=0; + int i; switch(Node->Type) { @@ -668,20 +726,19 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) case NODETYPE_BLOCK: { tAST_BlockState blockInfo; - memcpy(&blockInfo, Block, sizeof(tAST_BlockState)); + blockInfo.Parent = Block; + blockInfo.Script = Block->Script; blockInfo.FirstVar = NULL; blockInfo.RetVal = NULL; - blockInfo.Parent = Block; + blockInfo.BaseNamespace = Block->BaseNamespace; + blockInfo.CurNamespace = NULL; + blockInfo.Ident = giNextBlockIdent ++; ret = NULL; for(node = Node->Block.FirstChild; node && !blockInfo.RetVal; node = node->NextSibling ) { - tmpobj = AST_ExecuteNode(&blockInfo, node); - if(tmpobj == ERRPTR) { // Error check - ret = ERRPTR; - break ; - } - if(tmpobj) Object_Dereference(tmpobj); // Free unused value - tmpobj = NULL; + ret = AST_ExecuteNode(&blockInfo, node); + if(ret == ERRPTR) break; // Error check + if(ret != NULL) Object_Dereference(ret); // Free unused value } // Clean up variables while(blockInfo.FirstVar) @@ -690,7 +747,8 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) Variable_Destroy( blockInfo.FirstVar ); blockInfo.FirstVar = nextVar; } - + if(ret != ERRPTR) + ret = NULL; if( blockInfo.RetVal ) Block->RetVal = blockInfo.RetVal; } @@ -704,134 +762,167 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) return ERRPTR; } ret = AST_ExecuteNode(Block, Node->Assign.Value); - if(ret == ERRPTR) - return ERRPTR; + if(ret == ERRPTR) return ERRPTR; if( Node->Assign.Operation != NODETYPE_NOP ) { - tSpiderValue *varVal = Variable_GetValue(Block, Node->Assign.Dest->Variable.Name); + tSpiderValue *varVal = Variable_GetValue(Block, Node->Assign.Dest); tSpiderValue *value; value = AST_ExecuteNode_BinOp(Block, Node->Assign.Operation, varVal, ret); - if( value == ERRPTR ) - return ERRPTR; + if(value == ERRPTR) return ERRPTR; if(ret) Object_Dereference(ret); - Object_Dereference(varVal); + if(varVal) Object_Dereference(varVal); ret = value; } - if( Variable_SetValue( Block, Node->Assign.Dest->Variable.Name, ret ) ) { + if( Variable_SetValue( Block, Node->Assign.Dest, ret ) ) { Object_Dereference( ret ); return ERRPTR; } break; + case NODETYPE_POSTINC: + case NODETYPE_POSTDEC: + { + tSpiderValue *varVal, *value, *one; + if( Node->UniOp.Value->Type != NODETYPE_VARIABLE ) { + AST_RuntimeError(Node, "LVALUE of assignment is not a variable"); + return ERRPTR; + } + + varVal = Variable_GetValue(Block, Node->Assign.Dest); + one = SpiderScript_CreateInteger(1); + + if( Node->Type == NODETYPE_POSTDEC ) + value = AST_ExecuteNode_BinOp(Block, NODETYPE_SUBTRACT, varVal, one); + else + value = AST_ExecuteNode_BinOp(Block, NODETYPE_ADD, varVal, one); + if( value == ERRPTR ) + return ERRPTR; + Object_Dereference(one); // Free constant one + + ret = varVal; + + if( Variable_SetValue( Block, Node->Assign.Dest, value ) ) { + Object_Dereference( ret ); + return ERRPTR; + } + Object_Dereference( value ); + } + break; + // Function Call case NODETYPE_METHODCALL: case NODETYPE_FUNCTIONCALL: case NODETYPE_CREATEOBJECT: + // Logical block (used to allocate `params`) { - int nParams = 0; - for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling) { - nParams ++; - } - // Logical block (used to allocate `params`) + tSpiderValue *params[Node->FunctionCall.NumArgs]; + i = 0; + for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling) { - tSpiderValue *params[nParams]; - i = 0; - for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling) - { - params[i] = AST_ExecuteNode(Block, node); - if( params[i] == ERRPTR ) { - while(i--) Object_Dereference(params[i]); - ret = ERRPTR; - goto _return; - } - i ++; - } - - if( !Block->CurNamespace ) - Block->CurNamespace = Block->BaseNamespace; - - // Call the function - if( Node->Type == NODETYPE_CREATEOBJECT ) - { - ret = SpiderScript_CreateObject(Block->Script, - Block->CurNamespace, - Node->FunctionCall.Name, - nParams, params - ); - } - else if( Node->Type == NODETYPE_METHODCALL ) - { - tSpiderValue *obj = AST_ExecuteNode(Block, Node->FunctionCall.Object); - if( !obj || obj->Type != SS_DATATYPE_OBJECT ) { - AST_RuntimeError(Node->FunctionCall.Object, - "Type Mismatch - Required SS_DATATYPE_OBJECT for method call"); - while(i--) Object_Dereference(params[i]); - ret = ERRPTR; - break; - } - ret = SpiderScript_ExecuteMethod(Block->Script, - obj->Object, Node->FunctionCall.Name, - nParams, params - ); - Object_Dereference(obj); + params[i] = AST_ExecuteNode(Block, node); + if( params[i] == ERRPTR ) { + while(i--) Object_Dereference(params[i]); + ret = ERRPTR; + goto _return; } - else - { - ret = SpiderScript_ExecuteFunction(Block->Script, - Block->CurNamespace, Node->FunctionCall.Name, - nParams, params - ); + i ++; + } + + if( !Block->CurNamespace ) + Block->CurNamespace = Block->BaseNamespace; + + // Call the function + if( Node->Type == NODETYPE_CREATEOBJECT ) + { + ret = SpiderScript_CreateObject(Block->Script, + Block->CurNamespace, + Node->FunctionCall.Name, + Node->FunctionCall.NumArgs, params + ); + } + else if( Node->Type == NODETYPE_METHODCALL ) + { + tSpiderValue *obj = AST_ExecuteNode(Block, Node->FunctionCall.Object); + if( !obj || obj == ERRPTR || obj->Type != SS_DATATYPE_OBJECT ) { + AST_RuntimeError(Node->FunctionCall.Object, + "Type Mismatch - Required SS_DATATYPE_OBJECT for method call"); + while(i--) Object_Dereference(params[i]); + ret = ERRPTR; + break; } - - - // Dereference parameters - while(i--) Object_Dereference(params[i]); - - // falls out + ret = SpiderScript_ExecuteMethod(Block->Script, + obj->Object, Node->FunctionCall.Name, + Node->FunctionCall.NumArgs, params + ); + Object_Dereference(obj); } + else + { + ret = SpiderScript_ExecuteFunction(Block->Script, + Block->CurNamespace, Node->FunctionCall.Name, + Node->FunctionCall.NumArgs, params + ); + } + + + // Dereference parameters + while(i--) Object_Dereference(params[i]); + + // falls out } break; // Conditional case NODETYPE_IF: ret = AST_ExecuteNode(Block, Node->If.Condition); + if( ret == ERRPTR ) break; if( SpiderScript_IsValueTrue(ret) ) { - Object_Dereference(AST_ExecuteNode(Block, Node->If.True)); + tmpobj = AST_ExecuteNode(Block, Node->If.True); } else { - Object_Dereference(AST_ExecuteNode(Block, Node->If.False)); + tmpobj = AST_ExecuteNode(Block, Node->If.False); } Object_Dereference(ret); + if( tmpobj == ERRPTR ) return ERRPTR; + Object_Dereference(tmpobj); ret = NULL; break; // Loop case NODETYPE_LOOP: ret = AST_ExecuteNode(Block, Node->For.Init); + if(ret == ERRPTR) break; if( Node->For.bCheckAfter ) { do { Object_Dereference(ret); ret = AST_ExecuteNode(Block, Node->For.Code); + if(ret == ERRPTR) return ERRPTR; Object_Dereference(ret); ret = AST_ExecuteNode(Block, Node->For.Increment); + if(ret == ERRPTR) return ERRPTR; Object_Dereference(ret); ret = AST_ExecuteNode(Block, Node->For.Condition); + if(ret == ERRPTR) return ERRPTR; } while( SpiderScript_IsValueTrue(ret) ); } else { Object_Dereference(ret); ret = AST_ExecuteNode(Block, Node->For.Condition); + if(ret == ERRPTR) return ERRPTR; while( SpiderScript_IsValueTrue(ret) ) { Object_Dereference(ret); ret = AST_ExecuteNode(Block, Node->For.Code); + if(ret == ERRPTR) return ERRPTR; Object_Dereference(ret); ret = AST_ExecuteNode(Block, Node->For.Increment); + if(ret == ERRPTR) return ERRPTR; Object_Dereference(ret); ret = AST_ExecuteNode(Block, Node->For.Condition); + if(ret == ERRPTR) return ERRPTR; } } Object_Dereference(ret); @@ -841,6 +932,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) // Return case NODETYPE_RETURN: ret = AST_ExecuteNode(Block, Node->UniOp.Value); + if(ret == ERRPTR) break; Block->RetVal = ret; // Return value set ret = NULL; // the `return` statement does not return a value break; @@ -848,7 +940,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) // Define a variable case NODETYPE_DEFVAR: ret = NULL; - if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name) == ERRPTR ) + if( Variable_Define(Block, Node->DefVar.DataType, Node->DefVar.Name, NULL) == ERRPTR ) ret = ERRPTR; break; @@ -888,13 +980,14 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) // Variable case NODETYPE_VARIABLE: - ret = Variable_GetValue( Block, Node->Variable.Name ); + ret = Variable_GetValue( Block, Node ); break; // Element of an Object case NODETYPE_ELEMENT: tmpobj = AST_ExecuteNode( Block, Node->Scope.Element ); - if( tmpobj->Type != SS_DATATYPE_OBJECT ) + if(tmpobj == ERRPTR) return ERRPTR; + if( !tmpobj || tmpobj->Type != SS_DATATYPE_OBJECT ) { AST_RuntimeError(Node->Scope.Element, "Unable to dereference a non-object"); ret = ERRPTR; @@ -922,6 +1015,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) case NODETYPE_CAST: { tmpobj = AST_ExecuteNode(Block, Node->Cast.Value); + if(tmpobj == ERRPTR) return ERRPTR; ret = SpiderScript_CastValueTo( Node->Cast.DataType, tmpobj ); Object_Dereference(tmpobj); } @@ -930,9 +1024,14 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) // Index into an array case NODETYPE_INDEX: op1 = AST_ExecuteNode(Block, Node->BinOp.Left); // Array + if(op1 == ERRPTR) return ERRPTR; op2 = AST_ExecuteNode(Block, Node->BinOp.Right); // Offset + if(op2 == ERRPTR) { + Object_Dereference(op1); + return ERRPTR; + } - if( op1->Type != SS_DATATYPE_ARRAY ) + if( !op1 || op1->Type != SS_DATATYPE_ARRAY ) { // TODO: Implement "operator []" on objects AST_RuntimeError(Node, "Indexing non-array"); @@ -940,13 +1039,13 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) break; } - if( op2->Type != SS_DATATYPE_INTEGER && !Block->Script->Variant->bImplicitCasts ) { + if( (!op2 || op2->Type != SS_DATATYPE_INTEGER) && !Block->Script->Variant->bImplicitCasts ) { AST_RuntimeError(Node, "Array index is not an integer"); ret = ERRPTR; break; } - if( op2->Type != SS_DATATYPE_INTEGER ) + if( !op2 || op2->Type != SS_DATATYPE_INTEGER ) { tmpobj = SpiderScript_CastValueTo(SS_DATATYPE_INTEGER, op2); Object_Dereference(op2); @@ -975,12 +1074,21 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) break; // Constant Values - case NODETYPE_STRING: ret = SpiderScript_CreateString( Node->String.Length, Node->String.Data ); break; - case NODETYPE_INTEGER: ret = SpiderScript_CreateInteger( Node->Integer ); break; - case NODETYPE_REAL: ret = SpiderScript_CreateReal( Node->Real ); break; + case NODETYPE_STRING: + case NODETYPE_INTEGER: + case NODETYPE_REAL: + ret = &Node->Constant; + Object_Reference(ret); + break; // --- Operations --- // Boolean Operations + case NODETYPE_LOGICALNOT: // Logical NOT (!) + op1 = AST_ExecuteNode(Block, Node->UniOp.Value); + if(op1 == ERRPTR) return ERRPTR; + ret = SpiderScript_CreateInteger( !SpiderScript_IsValueTrue(op1) ); + Object_Dereference(op1); + break; case NODETYPE_LOGICALAND: // Logical AND (&&) case NODETYPE_LOGICALOR: // Logical OR (||) case NODETYPE_LOGICALXOR: // Logical XOR (^^) @@ -1015,18 +1123,29 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) case NODETYPE_EQUALS: case NODETYPE_LESSTHAN: case NODETYPE_GREATERTHAN: + case NODETYPE_LESSTHANEQUAL: + case NODETYPE_GREATERTHANEQUAL: op1 = AST_ExecuteNode(Block, Node->BinOp.Left); if(op1 == ERRPTR) return ERRPTR; op2 = AST_ExecuteNode(Block, Node->BinOp.Right); if(op2 == ERRPTR) { Object_Dereference(op1); - return ERRPTR; + ret = ERRPTR; + break; + } + + if( !op1 || !op2 ) { + AST_RuntimeError(Node, "NULL Comparison (%p and %p)", op1, op2); + if(op1) Object_Dereference(op1); + if(op2) Object_Dereference(op2); + ret = SpiderScript_CreateInteger( !op1 && !op2 ); + break; } // Convert types if( op1->Type != op2->Type ) { // If dynamically typed, convert op2 to op1's type - if(Block->Script->Variant->bDyamicTyped) + if(Block->Script->Variant->bImplicitCasts) { tmpobj = op2; op2 = SpiderScript_CastValueTo(op1->Type, op2); @@ -1038,7 +1157,8 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) } // If statically typed, this should never happen, but catch it anyway else { - AST_RuntimeError(Node, "Statically typed implicit cast"); + AST_RuntimeError(Node, "Statically typed implicit cast %i %i", + op1->Type, op2->Type); ret = ERRPTR; break; } @@ -1073,6 +1193,10 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) else cmp = 1; break; + // - Real Number Comparisons + case SS_DATATYPE_REAL: + cmp = (op1->Real - op2->Real) / op2->Real * 10000; // < 0.1% difference is equality + break; default: AST_RuntimeError(Node, "TODO - Comparison of type %i", op1->Type); ret = ERRPTR; @@ -1093,6 +1217,8 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) case NODETYPE_EQUALS: ret = SpiderScript_CreateInteger(cmp == 0); break; case NODETYPE_LESSTHAN: ret = SpiderScript_CreateInteger(cmp < 0); break; case NODETYPE_GREATERTHAN: ret = SpiderScript_CreateInteger(cmp > 0); break; + case NODETYPE_LESSTHANEQUAL: ret = SpiderScript_CreateInteger(cmp <= 0); break; + case NODETYPE_GREATERTHANEQUAL: ret = SpiderScript_CreateInteger(cmp >= 0); break; default: AST_RuntimeError(Node, "Exec,CmpOp unknown op %i", Node->Type); ret = ERRPTR; @@ -1100,6 +1226,15 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) } break; + // General Unary Operations + case NODETYPE_BWNOT: // Bitwise NOT (~) + case NODETYPE_NEGATE: // Negation (-) + op1 = AST_ExecuteNode(Block, Node->UniOp.Value); + if(op1 == ERRPTR) return ERRPTR; + ret = AST_ExecuteNode_UniOp(Block, Node->Type, op1); + Object_Dereference(op1); + break; + // General Binary Operations case NODETYPE_ADD: case NODETYPE_SUBTRACT: @@ -1130,10 +1265,77 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) //default: // ret = NULL; - // AST_RuntimeError(Node, "BUG - SpiderScript AST_ExecuteNode Unimplemented %i\n", Node->Type); + // AST_RuntimeError(Node, "BUG - SpiderScript AST_ExecuteNode Unimplemented %i", Node->Type); // break; } _return: + #if TRACE_NODE_RETURNS + if(ret && ret != ERRPTR) { + AST_RuntimeError(Node, "Ret type of %p %i is %i", Node, Node->Type, ret->Type); + } + else { + AST_RuntimeError(Node, "Ret type of %p %i is %p", Node, Node->Type, ret); + } + #endif + + return ret; +} + +tSpiderValue *AST_ExecuteNode_UniOp(tAST_BlockState *Block, int Operation, tSpiderValue *Value) +{ + tSpiderValue *ret; + #if 0 + if( Value->Type == SS_DATATYPE_OBJECT ) + { + const char *fcnname; + switch(Operation) + { + case NODETYPE_NEGATE: fcnname = "-ve"; break; + case NODETYPE_BWNOT: fcnname = "~"; break; + default: fcnname = NULL; break; + } + + if( fcnname ) + { + ret = Object_ExecuteMethod(Value->Object, fcnname, ); + if( ret != ERRPTR ) + return ret; + // Fall through and try casting (which will usually fail) + } + } + #endif + switch(Value->Type) + { + // Integer Operations + case SS_DATATYPE_INTEGER: + switch(Operation) + { + case NODETYPE_NEGATE: ret = SpiderScript_CreateInteger( -Value->Integer ); break; + case NODETYPE_BWNOT: ret = SpiderScript_CreateInteger( ~Value->Integer ); break; + default: + AST_RuntimeError(NULL, "SpiderScript internal error: Exec,UniOP,Integer unknown op %i", Operation); + ret = ERRPTR; + break; + } + break; + // Real number Operations + case SS_DATATYPE_REAL: + switch(Operation) + { + case NODETYPE_NEGATE: ret = SpiderScript_CreateInteger( -Value->Real ); break; + default: + AST_RuntimeError(NULL, "SpiderScript internal error: Exec,UniOP,Real unknown op %i", Operation); + ret = ERRPTR; + break; + } + break; + + default: + AST_RuntimeError(NULL, "Invalid operation (%i) on type (%i)", Operation, Value->Type); + ret = ERRPTR; + break; + } + return ret; } @@ -1186,7 +1388,7 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpid } // If statically typed, this should never happen, but catch it anyway else { - AST_RuntimeError(NULL, "Implicit cast not allowed (from %i to %i)\n", Right->Type, Left->Type); + AST_RuntimeError(NULL, "Implicit cast not allowed (from %i to %i)", Right->Type, Left->Type); return ERRPTR; } } @@ -1231,7 +1433,7 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpid ret = SpiderScript_CreateInteger( (Left->Integer << Right->Integer) | (Left->Integer >> (64-Right->Integer)) ); break; default: - AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i\n", Operation); + AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Integer unknown op %i", Operation); ret = ERRPTR; break; } @@ -1241,6 +1443,10 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpid case SS_DATATYPE_REAL: switch(Operation) { + case NODETYPE_ADD: ret = SpiderScript_CreateReal( Left->Real + Right->Real ); break; + case NODETYPE_SUBTRACT: ret = SpiderScript_CreateReal( Left->Real - Right->Real ); break; + case NODETYPE_MULTIPLY: ret = SpiderScript_CreateReal( Left->Real * Right->Real ); break; + case NODETYPE_DIVIDE: ret = SpiderScript_CreateReal( Left->Real / Right->Real ); break; default: AST_RuntimeError(NULL, "SpiderScript internal error: Exec,BinOP,Real unknown op %i", Operation); ret = ERRPTR; @@ -1266,7 +1472,7 @@ tSpiderValue *AST_ExecuteNode_BinOp(tAST_BlockState *Block, int Operation, tSpid * \param Name Name of the variable * \return Boolean Failure */ -tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name) +tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Name, tSpiderValue *Value) { tAST_Variable *var, *prev = NULL; @@ -1281,7 +1487,8 @@ tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Nam var = malloc( sizeof(tAST_Variable) + strlen(Name) + 1 ); var->Next = NULL; var->Type = Type; - var->Object = NULL; + var->Object = Value; + if(Value) Object_Reference(Value); strcpy(var->Name, Name); if(prev) prev->Next = var; @@ -1292,74 +1499,95 @@ tAST_Variable *Variable_Define(tAST_BlockState *Block, int Type, const char *Nam return var; } -/** - * \brief Set the value of a variable - * \return Boolean Failure - */ -int Variable_SetValue(tAST_BlockState *Block, const char *Name, tSpiderValue *Value) -{ - tAST_Variable *var; - tAST_BlockState *bs; - - for( bs = Block; bs; bs = bs->Parent ) +tAST_Variable *Variable_Lookup(tAST_BlockState *Block, tAST_Node *VarNode, int CreateType) +{ + tAST_Variable *var = NULL; + + // Speed hack + if( VarNode->BlockState == Block && VarNode->BlockIdent == Block->Ident ) { + var = VarNode->ValueCache; + #if TRACE_VAR_LOOKUPS + AST_RuntimeMessage(VarNode, "debug", "Fast var fetch on '%s' %p (%p:%i)", + VarNode->Variable.Name, var, + VarNode->BlockState, VarNode->BlockIdent + ); + #endif + } + else { - for( var = bs->FirstVar; var; var = var->Next ) + tAST_BlockState *bs; + for( bs = Block; bs; bs = bs->Parent ) { - if( strcmp(var->Name, Name) == 0 ) + for( var = bs->FirstVar; var; var = var->Next ) { - if( !Block->Script->Variant->bDyamicTyped - && (Value && var->Type != Value->Type) ) - { - AST_RuntimeError(NULL, "Type mismatch assigning to '%s'", Name); - return -2; - } -// printf("Assign %p to '%s'\n", Value, var->Name); - Object_Reference(Value); - Object_Dereference(var->Object); - var->Object = Value; - return 0; + if( strcmp(var->Name, VarNode->Variable.Name) == 0 ) + break; + } + if(var) break; + } + + if( !var ) + { + if( Block->Script->Variant->bDyamicTyped && CreateType != SS_DATATYPE_UNDEF ) { + // Define variable + var = Variable_Define(Block, CreateType, VarNode->Variable.Name, NULL); + } + else + { + AST_RuntimeError(VarNode, "Variable '%s' is undefined", VarNode->Variable.Name); + return NULL; } } + + #if TRACE_VAR_LOOKUPS + AST_RuntimeMessage(VarNode, "debug", "Saved variable lookup of '%s' %p (%p:%i)", + VarNode->Variable.Name, var, + Block, Block->Ident); + #endif + + VarNode->ValueCache = var; + VarNode->BlockState = Block; + VarNode->BlockIdent = Block->Ident; } - if( Block->Script->Variant->bDyamicTyped ) - { - // Define variable - var = Variable_Define(Block, Value->Type, Name); - Object_Reference(Value); - var->Object = Value; - return 0; - } - else - { - AST_RuntimeError(NULL, "Variable '%s' set while undefined", Name); - return -1; - } + return var; } /** - * \brief Get the value of a variable + * \brief Set the value of a variable + * \return Boolean Failure */ -tSpiderValue *Variable_GetValue(tAST_BlockState *Block, const char *Name) +int Variable_SetValue(tAST_BlockState *Block, tAST_Node *VarNode, tSpiderValue *Value) { - tAST_Variable *var; - tAST_BlockState *bs; + tAST_Variable *var = Variable_Lookup(Block, VarNode, Value->Type); - for( bs = Block; bs; bs = bs->Parent ) + if( !var ) return -1; + + if( !Block->Script->Variant->bDyamicTyped && (Value && var->Type != Value->Type) ) { - for( var = bs->FirstVar; var; var = var->Next ) - { - if( strcmp(var->Name, Name) == 0 ) { - Object_Reference(var->Object); - return var->Object; - } - } + AST_RuntimeError(VarNode, "Type mismatch assigning to '%s'", + VarNode->Variable.Name); + return -2; } + +// printf("Assign %p to '%s'\n", Value, var->Name); + Object_Reference(Value); + Object_Dereference(var->Object); + var->Object = Value; + return 0; +} + +/** + * \brief Get the value of a variable + */ +tSpiderValue *Variable_GetValue(tAST_BlockState *Block, tAST_Node *VarNode) +{ + tAST_Variable *var = Variable_Lookup(Block, VarNode, 0); + if( !var ) return ERRPTR; - AST_RuntimeError(NULL, "Variable '%s' used undefined", Name); - - return ERRPTR; + Object_Reference(var->Object); + return var->Object; } /** @@ -1372,18 +1600,29 @@ void Variable_Destroy(tAST_Variable *Variable) free(Variable); } -void AST_RuntimeError(tAST_Node *Node, const char *Format, ...) +void AST_RuntimeMessage(tAST_Node *Node, const char *Type, const char *Format, ...) { va_list args; - fprintf(stderr, "ERROR: "); + if(Node) { + fprintf(stderr, "%s:%i: ", Node->File, Node->Line); + } + fprintf(stderr, "%s: ", Type); va_start(args, Format); vfprintf(stderr, Format, args); va_end(args); fprintf(stderr, "\n"); +} +void AST_RuntimeError(tAST_Node *Node, const char *Format, ...) +{ + va_list args; - if(Node) - { - fprintf(stderr, " at %s:%i\n", Node->File, Node->Line); + if(Node) { + fprintf(stderr, "%s:%i: ", Node->File, Node->Line); } + fprintf(stderr, "error: "); + va_start(args, Format); + vfprintf(stderr, Format, args); + va_end(args); + fprintf(stderr, "\n"); }