From: John Hodge Date: Sat, 2 Apr 2011 14:45:22 +0000 (+0800) Subject: SpiderScript - Moar fixes, mostly speedups (caching values and lookups) X-Git-Tag: rel0.10~133 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=0f81e4f273cdf1032e9bca55b4185ca9113596cc;p=tpg%2Facess2.git SpiderScript - Moar fixes, mostly speedups (caching values and lookups) --- diff --git a/Usermode/Libraries/libspiderscript.so_src/ast.c b/Usermode/Libraries/libspiderscript.so_src/ast.c index 5aa6e3dc..1ea7e3e5 100644 --- a/Usermode/Libraries/libspiderscript.so_src/ast.c +++ b/Usermode/Libraries/libspiderscript.so_src/ast.c @@ -258,14 +258,14 @@ size_t AST_WriteNode(void *Buffer, size_t Offset, tAST_Node *Node) WRITE_STR(Buffer, Offset, Node->Variable.Name); break; case NODETYPE_STRING: - WRITE_32(Buffer, Offset, Node->String.Length); - WRITE_N(Buffer, Offset, Node->String.Length, Node->String.Data); + WRITE_32(Buffer, Offset, Node->Constant.String.Length); + WRITE_N(Buffer, Offset, Node->Constant.String.Length, Node->Constant.String.Data); break; case NODETYPE_INTEGER: - WRITE_64(Buffer, Offset, Node->Integer); + WRITE_64(Buffer, Offset, Node->Constant.Integer); break; case NODETYPE_REAL: - WRITE_REAL(Buffer, Offset, Node->Real); + WRITE_REAL(Buffer, Offset, Node->Constant.Real); break; //default: @@ -385,21 +385,38 @@ void AST_FreeNode(tAST_Node *Node) case NODETYPE_NOP: break; case NODETYPE_VARIABLE: break; case NODETYPE_CONSTANT: break; - case NODETYPE_STRING: break; - case NODETYPE_INTEGER: break; - case NODETYPE_REAL: break; + + case NODETYPE_STRING: + case NODETYPE_INTEGER: + case NODETYPE_REAL: + if( Node->ValueCache ) + Object_Dereference(Node->ValueCache); + Node->ValueCache = NULL; + break; } free( Node ); } -tAST_Node *AST_NewCodeBlock(tParser *Parser) +tAST_Node *AST_int_AllocateNode(tParser *Parser, int Type, int ExtraSize) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); - + tAST_Node *ret = malloc( sizeof(tAST_Node) + ExtraSize ); ret->NextSibling = NULL; - ret->File = NULL; + ret->File = ""; ret->Line = Parser->CurLine; - ret->Type = NODETYPE_BLOCK; + ret->Type = Type; + + // Runtime Caching + ret->BlockState = NULL; + ret->BlockIdent = 0; + ret->ValueCache = NULL; + + return ret; +} + +tAST_Node *AST_NewCodeBlock(tParser *Parser) +{ + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_BLOCK, 0 ); + ret->Block.FirstChild = NULL; ret->Block.LastChild = NULL; @@ -437,11 +454,7 @@ void AST_AppendNode(tAST_Node *Parent, tAST_Node *Child) tAST_Node *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAST_Node *False) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_IF; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_IF, 0); ret->If.Condition = Condition; ret->If.True = True; ret->If.False = False; @@ -450,11 +463,7 @@ tAST_Node *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAS tAST_Node *AST_NewLoop(tParser *Parser, tAST_Node *Init, int bPostCheck, tAST_Node *Condition, tAST_Node *Increment, tAST_Node *Code) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_LOOP; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_LOOP, 0); ret->For.Init = Init; ret->For.bCheckAfter = !!bPostCheck; ret->For.Condition = Condition; @@ -465,12 +474,8 @@ tAST_Node *AST_NewLoop(tParser *Parser, tAST_Node *Init, int bPostCheck, tAST_No tAST_Node *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_Node *Value) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_ASSIGN, 0); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_ASSIGN; ret->Assign.Operation = Operation; ret->Assign.Dest = Dest; ret->Assign.Value = Value; @@ -480,12 +485,8 @@ tAST_Node *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_N tAST_Node *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CAST, 0); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_CAST; ret->Cast.DataType = Target; ret->Cast.Value = Value; @@ -494,12 +495,8 @@ tAST_Node *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value) tAST_Node *AST_NewBinOp(tParser *Parser, int Operation, tAST_Node *Left, tAST_Node *Right) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); + tAST_Node *ret = AST_int_AllocateNode(Parser, Operation, 0); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = Operation; ret->BinOp.Left = Left; ret->BinOp.Right = Right; @@ -510,12 +507,8 @@ tAST_Node *AST_NewBinOp(tParser *Parser, int Operation, tAST_Node *Left, tAST_No */ tAST_Node *AST_NewUniOp(tParser *Parser, int Operation, tAST_Node *Value) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); + tAST_Node *ret = AST_int_AllocateNode(Parser, Operation, 0); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = Operation; ret->UniOp.Value = Value; return ret; @@ -523,12 +516,7 @@ tAST_Node *AST_NewUniOp(tParser *Parser, int Operation, tAST_Node *Value) tAST_Node *AST_NewNop(tParser *Parser) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); - - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_NOP; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_NOP, 0); return ret; } @@ -538,15 +526,13 @@ tAST_Node *AST_NewNop(tParser *Parser) */ tAST_Node *AST_NewString(tParser *Parser, const char *String, int Length) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + Length + 1 ); + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_STRING, Length + 1); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_STRING; - ret->String.Length = Length; - memcpy(ret->String.Data, String, Length); - ret->String.Data[Length] = '\0'; + ret->Constant.Type = SS_DATATYPE_STRING; + ret->Constant.ReferenceCount = 1; + ret->Constant.String.Length = Length; + memcpy(ret->Constant.String.Data, String, Length); + ret->Constant.String.Data[Length] = '\0'; return ret; } @@ -556,12 +542,10 @@ tAST_Node *AST_NewString(tParser *Parser, const char *String, int Length) */ tAST_Node *AST_NewInteger(tParser *Parser, int64_t Value) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_INTEGER; - ret->Integer = Value; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_INTEGER, 0); + ret->Constant.Type = SS_DATATYPE_INTEGER; + ret->Constant.ReferenceCount = 1; + ret->Constant.Integer = Value; return ret; } @@ -570,12 +554,10 @@ tAST_Node *AST_NewInteger(tParser *Parser, int64_t Value) */ tAST_Node *AST_NewReal(tParser *Parser, double Value) { - tAST_Node *ret = malloc( sizeof(tAST_Node) ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_REAL; - ret->Real = Value; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_REAL, 0); + ret->Constant.Type = SS_DATATYPE_REAL; + ret->Constant.ReferenceCount = 1; + ret->Constant.Real = Value; return ret; } @@ -584,11 +566,7 @@ tAST_Node *AST_NewReal(tParser *Parser, double Value) */ tAST_Node *AST_NewVariable(tParser *Parser, const char *Name) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_VARIABLE; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_VARIABLE, strlen(Name) + 1 ); strcpy(ret->Variable.Name, Name); return ret; } @@ -598,14 +576,12 @@ tAST_Node *AST_NewVariable(tParser *Parser, const char *Name) */ tAST_Node *AST_NewDefineVar(tParser *Parser, int Type, const char *Name) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_DEFVAR; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_DEFVAR, strlen(Name) + 1 ); + ret->DefVar.DataType = Type; ret->DefVar.LevelSizes = NULL; strcpy(ret->DefVar.Name, Name); + return ret; } @@ -614,12 +590,10 @@ tAST_Node *AST_NewDefineVar(tParser *Parser, int Type, const char *Name) */ tAST_Node *AST_NewConstant(tParser *Parser, const char *Name) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_CONSTANT; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CONSTANT, strlen(Name) + 1 ); + strcpy(ret->Variable.Name, Name); + return ret; } @@ -629,48 +603,39 @@ tAST_Node *AST_NewConstant(tParser *Parser, const char *Name) */ tAST_Node *AST_NewFunctionCall(tParser *Parser, const char *Name) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_FUNCTIONCALL, strlen(Name) + 1 ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_FUNCTIONCALL; ret->FunctionCall.Object = NULL; ret->FunctionCall.FirstArg = NULL; ret->FunctionCall.LastArg = NULL; ret->FunctionCall.NumArgs = 0; strcpy(ret->FunctionCall.Name, Name); + return ret; } tAST_Node *AST_NewMethodCall(tParser *Parser, tAST_Node *Object, const char *Name) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_METHODCALL, strlen(Name) + 1 ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_METHODCALL; ret->FunctionCall.Object = Object; ret->FunctionCall.FirstArg = NULL; ret->FunctionCall.LastArg = NULL; ret->FunctionCall.NumArgs = 0; strcpy(ret->FunctionCall.Name, Name); + return ret; } tAST_Node *AST_NewCreateObject(tParser *Parser, const char *Name) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CREATEOBJECT, strlen(Name) + 1 ); - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_CREATEOBJECT; ret->FunctionCall.Object = NULL; ret->FunctionCall.FirstArg = NULL; ret->FunctionCall.LastArg = NULL; ret->FunctionCall.NumArgs = 0; strcpy(ret->FunctionCall.Name, Name); + return ret; } @@ -703,12 +668,7 @@ void AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg) */ tAST_Node *AST_NewScopeDereference(tParser *Parser, const char *Name, tAST_Node *Child) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); - - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_SCOPE; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_SCOPE, strlen(Name) + 1 ); ret->Scope.Element = Child; strcpy(ret->Scope.Name, Name); return ret; @@ -719,12 +679,7 @@ tAST_Node *AST_NewScopeDereference(tParser *Parser, const char *Name, tAST_Node */ tAST_Node *AST_NewClassElement(tParser *Parser, tAST_Node *Object, const char *Name) { - tAST_Node *ret = malloc( sizeof(tAST_Node) + strlen(Name) + 1 ); - - ret->NextSibling = NULL; - ret->File = NULL; - ret->Line = Parser->CurLine; - ret->Type = NODETYPE_ELEMENT; + tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_ELEMENT, strlen(Name) + 1 ); ret->Scope.Element = Object; strcpy(ret->Scope.Name, Name); return ret; diff --git a/Usermode/Libraries/libspiderscript.so_src/ast.h b/Usermode/Libraries/libspiderscript.so_src/ast.h index a9cf1db2..680299b6 100644 --- a/Usermode/Libraries/libspiderscript.so_src/ast.h +++ b/Usermode/Libraries/libspiderscript.so_src/ast.h @@ -105,6 +105,10 @@ struct sAST_Node const char *File; int Line; + void *BlockState; //!< BlockState pointer (for cache integrity) + int BlockIdent; //!< Ident (same as above) + void *ValueCache; //!< Cached value / pointer + union { struct { @@ -175,13 +179,8 @@ struct sAST_Node tAST_Node *Value; } Cast; - struct { - int Length; - char Data[]; - } String; - - uint64_t Integer; - double Real; + // Used for NODETYPE_REAL, NODETYPE_INTEGER and NODETYPE_STRING + tSpiderValue Constant; }; }; @@ -196,6 +195,7 @@ struct sAST_BlockState tSpiderValue *RetVal; tSpiderNamespace *BaseNamespace; //!< Base namespace (for entire block) tSpiderNamespace *CurNamespace; //!< Currently selected namespace + int Ident; }; struct sAST_Variable diff --git a/Usermode/Libraries/libspiderscript.so_src/exec_ast.c b/Usermode/Libraries/libspiderscript.so_src/exec_ast.c index 74d63835..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; @@ -24,13 +27,17 @@ 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 @@ -124,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; @@ -424,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) @@ -597,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) @@ -716,14 +732,13 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) blockInfo.RetVal = NULL; blockInfo.BaseNamespace = Block->BaseNamespace; blockInfo.CurNamespace = NULL; + blockInfo.Ident = giNextBlockIdent ++; ret = NULL; for(node = Node->Block.FirstChild; node && !blockInfo.RetVal; node = node->NextSibling ) { ret = AST_ExecuteNode(&blockInfo, node); - if(ret == ERRPTR) { // Error check - break ; - } - if(ret) Object_Dereference(ret); // Free unused value + if(ret == ERRPTR) break; // Error check + if(ret != NULL) Object_Dereference(ret); // Free unused value } // Clean up variables while(blockInfo.FirstVar) @@ -747,22 +762,20 @@ 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); 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; } @@ -777,7 +790,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) return ERRPTR; } - varVal = Variable_GetValue(Block, Node->Assign.Dest->Variable.Name); + varVal = Variable_GetValue(Block, Node->Assign.Dest); one = SpiderScript_CreateInteger(1); if( Node->Type == NODETYPE_POSTDEC ) @@ -790,7 +803,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) ret = varVal; - if( Variable_SetValue( Block, Node->Assign.Dest->Variable.Name, value ) ) { + if( Variable_SetValue( Block, Node->Assign.Dest, value ) ) { Object_Dereference( ret ); return ERRPTR; } @@ -832,7 +845,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) else if( Node->Type == NODETYPE_METHODCALL ) { tSpiderValue *obj = AST_ExecuteNode(Block, Node->FunctionCall.Object); - if( !obj || obj->Type != SS_DATATYPE_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]); @@ -864,41 +877,52 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) // 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); @@ -908,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; @@ -915,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; @@ -955,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; @@ -989,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); } @@ -997,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"); @@ -1007,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); @@ -1042,9 +1074,12 @@ 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 @@ -1100,7 +1135,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) } if( !op1 || !op2 ) { - AST_RuntimeError(Node, "NULL Comparison (%p and %p)\n", 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 ); @@ -1110,7 +1145,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) // 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); @@ -1122,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; } @@ -1229,10 +1265,19 @@ 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; } @@ -1343,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; } } @@ -1388,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; } @@ -1427,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; @@ -1442,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; @@ -1453,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); + + if( !var ) return -1; - for( bs = Block; bs; bs = bs->Parent ) + 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; } /** @@ -1533,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"); } diff --git a/Usermode/Libraries/libspiderscript.so_src/lex.c b/Usermode/Libraries/libspiderscript.so_src/lex.c index be3a15b4..610c2f2b 100644 --- a/Usermode/Libraries/libspiderscript.so_src/lex.c +++ b/Usermode/Libraries/libspiderscript.so_src/lex.c @@ -306,7 +306,8 @@ int GetToken(tParser *File) if( isdigit(*File->CurPos) ) { ret = TOK_INTEGER; - if( *File->CurPos == '0' && File->CurPos[1] == 'x' ) { + if( *File->CurPos == '0' && File->CurPos[1] == 'x' ) + { File->CurPos += 2; while(('0' <= *File->CurPos && *File->CurPos <= '9') || ('A' <= *File->CurPos && *File->CurPos <= 'F') @@ -315,10 +316,13 @@ int GetToken(tParser *File) File->CurPos ++; } } - else { + else + { while( isdigit(*File->CurPos) ) File->CurPos ++; +// printf("*File->CurPos = '%c'\n", *File->CurPos); + // Decimal if( *File->CurPos == '.' ) { @@ -337,6 +341,8 @@ int GetToken(tParser *File) while( isdigit(*File->CurPos) ) File->CurPos ++; } + +// printf(" ret = %i\n", ret); } break; } diff --git a/Usermode/Libraries/libspiderscript.so_src/parse.c b/Usermode/Libraries/libspiderscript.so_src/parse.c index e04bc254..c3a68248 100644 --- a/Usermode/Libraries/libspiderscript.so_src/parse.c +++ b/Usermode/Libraries/libspiderscript.so_src/parse.c @@ -604,8 +604,11 @@ tAST_Node *Parse_DoValue(tParser *Parser) switch(tok) { - case TOK_STR: return Parse_GetString(Parser); - case TOK_INTEGER: return Parse_GetNumeric(Parser); + case TOK_STR: + return Parse_GetString(Parser); + case TOK_INTEGER: + return Parse_GetNumeric(Parser); + case TOK_REAL: GetToken(Parser); return AST_NewReal( Parser, atof(Parser->TokenStr) );