YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

a problem about table slot
renshijun
#1 Posted : Wednesday, January 4, 2017 7:41:50 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 12/26/2014(UTC)
Posts: 30
Location: Harbin, China

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
Code:

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "squirrel.h"
#include "sqstdio.h"


static void squirrel_print_last_error(HSQUIRRELVM sqvm)
{
    const SQChar *error;
    sq_getlasterror(sqvm);
    if (SQ_SUCCEEDED(sq_getstring(sqvm, -1, &error))) {
        fprintf(stderr, "sqvm error: %s\n", error);
    }
}

static void squirrel_print_function(HSQUIRRELVM sqvm, const SQChar *format, ...)
{
    va_list args;
    va_start(args, format);
    vfprintf(stdout, format, args);
    va_end(args);
}

int main(int argc, char **argv)
{
    HSQUIRRELVM v = sq_open(1024);

    sq_setprintfunc(v, squirrel_print_function, NULL);
     
    /*    const SQChar *program = "        \
      local A=[]; \
      A.append(1);\
      A.append(1);\
      local i=0;\
      for(i=0;i<10;i++)\
      {\
          A.append(A[A.len()-1]+A[A.len()-2]);    \
      }\
      for(i=0;i<A.len();i++)\
      {\
           print(A[i]);\
           print(\" \");\
      }\
      function ListElement(node,i)\
      {\
          i=i+1\
          if(typeof node=="array")\
          {\
          print("[ ")\
          foreach(aa in node)   \
          {   \
              if(typeof aa=="table")   \
              {   \
                  print("\n    ")   \
              for(local cc=0;cc<i-1;cc++)print("    ")   \
              }   \
              ListElement(aa,i)   \
          }   \
          print("]")   \
          }   \
     ";
*/
   
    const SQChar *program = "\
      local A=[];         \
      A.append(1);         \
      A.append(1);         \
      local i=0;         \
      for(i=0;i<10;i++)         \
      {                        \
          A.append(A[A.len()-1]+A[A.len()-2]);    \
      }                        \
      for(i=0;i<A.len();i++)            \
      {                        \
           print(A[i]);                \
           print(\" \");            \
      }                        \
      function ListElement(node,i)        \
      {                        \
          i=i+1;                \
          if(typeof node==\"array\")        \
          {                        \
                print(\"[ \");                \
                foreach(aa in node)            \
                {                        \
                    if(typeof aa==\"table\")        \
                    {                             \
                        print(\"\\n    \");                 \
                    for(local cc=0;cc<i-1;cc++)print(\"    \");    \
                    }                             \
                    ListElement(aa,i);                     \
                }                                 \
                print(\"]\");                         \
           }                                 \
                                     \
           else if(typeof node==\"table\")                 \
           {                                 \
                print(\"{\\n\");                         \
                foreach(key,keyvalue in node)                 \
                {                                 \
                    for(local cc=0;cc<i;cc++)print(\"    \");         \
                    print(key+\": \");                     \
                    ListElement(keyvalue,i);                 \
                    print(\"\\n\");                     \
                }                                 \
                for(local cc=0;cc<i-1;cc++)print(\"    \");         \
                print(\"}\\n\");                         \
          }                                 \
          else                                 \
              print(node+\" \");                     \
      }                                     \
      local B={a=1,b=[2,3,4,5]};                     \
     ";


    if (SQ_FAILED(sq_compilebuffer(v, program,
                                   //sizeof(SQChar) * strlen(program),
                   strlen(program),
                                   "program", SQFalse))) {
        squirrel_print_last_error(v);
        return 1;
    }
    printf("compiling program ok\n");
    sq_pushroottable(v);
    if (SQ_FAILED(sq_call(v, 1, SQFalse, SQFalse))) {
        squirrel_print_last_error(v);
        return 1;
    }

    printf("\nexec program ok\n");
   
    if (SQ_FAILED(sq_compilebuffer(v, "ListElement(B,0)",
                   strlen("ListElement(B,0)"),
                                   "listelement", SQFalse))) {
        squirrel_print_last_error(v);
        return 1;
    }
   
    printf("compiling list element ok\n");
    sq_pushroottable(v);
    if (SQ_FAILED(sq_call(v, 1, SQFalse, SQFalse))) {
        squirrel_print_last_error(v);
        return 1;
    }
    printf("exec list element ok\n");
    sq_close(v);
    return 0;
}

when I exec string program,it's all ok,but sq_call "ListElement(B,0)" is not. it says index B does not exists. but local B=... is already there. why?

absence
#2 Posted : Tuesday, January 10, 2017 2:29:09 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 109
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
B ceases to exist when the first call is done and returns, because B is local.
what you did would read in squirrel about like that:


local compiled=function(xthis) {
xthis.listelement<-function(...) { ... } ;
local B="whatever"
}
compiled(getroottable()) ;

local compiled=function(xthis) {
{
xthis.listelement(B) ; //B does not exist here
}
compiled(getroottable())= ;


(it's not exactly like that, the "this" object is inherent. However, you should get the idea about locals)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.095 seconds.