From: John Hodge Date: Sun, 3 Apr 2011 09:03:29 +0000 (+0800) Subject: SpiderScript - Fixed namespace handling (and unset the . hack) X-Git-Tag: rel0.10~128 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=3bf0a5e914ee87da14a3255ad4b706994d54f0d6;p=tpg%2Facess2.git SpiderScript - Fixed namespace handling (and unset the . hack) - Also improved the parse/run output (by way of an elegant hack with storing the filename) --- diff --git a/Usermode/Libraries/libspiderscript.so_src/ast.c b/Usermode/Libraries/libspiderscript.so_src/ast.c index c1d7a46e..5fbdf77d 100644 --- a/Usermode/Libraries/libspiderscript.so_src/ast.c +++ b/Usermode/Libraries/libspiderscript.so_src/ast.c @@ -294,6 +294,11 @@ void AST_FreeNode(tAST_Node *Node) if(!Node) return ; + // Referenced counted file name + (*(int*)(Node->File - sizeof(int))) -= 1; + if( *(int*)(Node->File - sizeof(int)) == 0 ) + free( (void*)(Node->File - sizeof(int)) ); + switch(Node->Type) { // Block of code @@ -411,7 +416,7 @@ tAST_Node *AST_int_AllocateNode(tParser *Parser, int Type, int ExtraSize) { tAST_Node *ret = malloc( sizeof(tAST_Node) + ExtraSize ); ret->NextSibling = NULL; - ret->File = ""; + ret->File = Parser->Filename; *(int*)(Parser->Filename - sizeof(int)) += 1; ret->Line = Parser->CurLine; ret->Type = Type; diff --git a/Usermode/Libraries/libspiderscript.so_src/exec_ast.c b/Usermode/Libraries/libspiderscript.so_src/exec_ast.c index 22469798..480ecc19 100644 --- a/Usermode/Libraries/libspiderscript.so_src/exec_ast.c +++ b/Usermode/Libraries/libspiderscript.so_src/exec_ast.c @@ -508,7 +508,8 @@ tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script, // Not found? if(!bFound) { - fprintf(stderr, "Undefined reference to function '%s'\n", Function); + fprintf(stderr, "Undefined reference to function '%s' (ns='%s')\n", + Function, Namespace->Name); return ERRPTR; } @@ -650,6 +651,9 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script, { class = NULL; // Just to allow the below code to be neat + //if( !Namespace ) + // Namespace = &Script->Variant->RootNamespace; + // Second: Scan current namespace if( !class && Namespace ) { @@ -843,6 +847,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) case NODETYPE_CREATEOBJECT: // Logical block (used to allocate `params`) { + tSpiderNamespace *ns = Block->CurNamespace; tSpiderValue *params[Node->FunctionCall.NumArgs]; i = 0; for(node = Node->FunctionCall.FirstArg; node; node = node->NextSibling) @@ -856,14 +861,13 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) i ++; } - if( !Block->CurNamespace ) - Block->CurNamespace = Block->BaseNamespace; + if( !ns ) ns = Block->BaseNamespace; // Call the function if( Node->Type == NODETYPE_CREATEOBJECT ) { ret = SpiderScript_CreateObject(Block->Script, - Block->CurNamespace, + ns, Node->FunctionCall.Name, Node->FunctionCall.NumArgs, params ); @@ -887,7 +891,7 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) else { ret = SpiderScript_ExecuteFunction(Block->Script, - Block->CurNamespace, Node->FunctionCall.Name, + ns, Node->FunctionCall.Name, Node->FunctionCall.NumArgs, params ); } @@ -1303,6 +1307,10 @@ tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node) // break; } _return: + // Reset namespace when no longer needed + if( Node->Type != NODETYPE_SCOPE ) + Block->CurNamespace = NULL; + #if TRACE_NODE_RETURNS if(ret && ret != ERRPTR) { AST_RuntimeError(Node, "Ret type of %p %i is %i", Node, Node->Type, ret->Type); diff --git a/Usermode/Libraries/libspiderscript.so_src/lex.c b/Usermode/Libraries/libspiderscript.so_src/lex.c index 610c2f2b..62d6c3f7 100644 --- a/Usermode/Libraries/libspiderscript.so_src/lex.c +++ b/Usermode/Libraries/libspiderscript.so_src/lex.c @@ -7,10 +7,6 @@ #include #include -// Make the scope character ('.') be a symbol, otherwise it's just -// a ident character -#define USE_SCOPE_CHAR 0 - #define DEBUG 0 #define ARRAY_SIZE(x) ((sizeof(x))/(sizeof((x)[0]))) diff --git a/Usermode/Libraries/libspiderscript.so_src/parse.c b/Usermode/Libraries/libspiderscript.so_src/parse.c index b3a1e82c..12f26747 100644 --- a/Usermode/Libraries/libspiderscript.so_src/parse.c +++ b/Usermode/Libraries/libspiderscript.so_src/parse.c @@ -77,7 +77,11 @@ tAST_Script *Parse_Buffer(tSpiderVariant *Variant, const char *Buffer, const cha parser.CurLine = 1; parser.BufStart = Buffer; parser.CurPos = Buffer; - parser.Filename = Filename; + // hackery to do reference counting + parser.Filename = malloc(sizeof(int)+strlen(Filename)+1); + strcpy(parser.Filename + sizeof(int), Filename); + *(int*)(parser.Filename) = 0; // Set reference count + parser.Filename += sizeof(int); // Move filename parser.ErrorHit = 0; ret = AST_NewScript(); @@ -931,7 +935,7 @@ tAST_Node *Parse_GetIdent(tParser *Parser, int bObjectCreate) #if USE_SCOPE_CHAR if( GetToken(Parser) == TOK_SCOPE ) { - ret = AST_NewScopeDereference( Parser, Parse_GetIdent(Parser, bObjectCreate), name ); + ret = AST_NewScopeDereference( Parser, name, Parse_GetIdent(Parser, bObjectCreate) ); free(name); return ret; } diff --git a/Usermode/Libraries/libspiderscript.so_src/tokens.h b/Usermode/Libraries/libspiderscript.so_src/tokens.h index 8d041e97..4c6fae3f 100644 --- a/Usermode/Libraries/libspiderscript.so_src/tokens.h +++ b/Usermode/Libraries/libspiderscript.so_src/tokens.h @@ -5,6 +5,10 @@ #include +// Make the scope character ('.') be a symbol, otherwise it's just +// a ident character +#define USE_SCOPE_CHAR 1 + // === TYPES === typedef struct { @@ -12,7 +16,7 @@ typedef struct const char *BufStart; const char *CurPos; - const char *Filename; + char *Filename; int LastLine; int LastToken, LastTokenLen;