SpiderScript - Fixed namespace handling (and unset the . hack)
authorJohn Hodge <[email protected]>
Sun, 3 Apr 2011 09:03:29 +0000 (17:03 +0800)
committerJohn Hodge <[email protected]>
Sun, 3 Apr 2011 09:03:29 +0000 (17:03 +0800)
- Also improved the parse/run output (by way of an elegant hack
  with storing the filename)

Usermode/Libraries/libspiderscript.so_src/ast.c
Usermode/Libraries/libspiderscript.so_src/exec_ast.c
Usermode/Libraries/libspiderscript.so_src/lex.c
Usermode/Libraries/libspiderscript.so_src/parse.c
Usermode/Libraries/libspiderscript.so_src/tokens.h

index c1d7a46..5fbdf77 100644 (file)
@@ -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 = "<unk>";
+       ret->File = Parser->Filename;   *(int*)(Parser->Filename - sizeof(int)) += 1;
        ret->Line = Parser->CurLine;
        ret->Type = Type;
        
index 2246979..480ecc1 100644 (file)
@@ -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);
index 610c2f2..62d6c3f 100644 (file)
@@ -7,10 +7,6 @@
 #include <stdio.h>
 #include <string.h>
 
-// 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])))
index b3a1e82..12f2674 100644 (file)
@@ -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;
        }
index 8d041e9..4c6fae3 100644 (file)
@@ -5,6 +5,10 @@
 
 #include <setjmp.h>
 
+// 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;

UCC git Repository :: git.ucc.asn.au