albertodemichelis / squirrel

Official repository for the programming language Squirrel
http://www.squirrel-lang.org
MIT License
926 stars 161 forks source link

How to recursively traverse a table #278

Closed Lenheart357 closed 1 year ago

Lenheart357 commented 1 year ago

If I want to traverse the table

sq_pushnull(v) while(SQ_SUCCEEDED(sq_next(v,-2))) { //-1 value -2key }

But how should I traverse value when it is another table

zeromus commented 1 year ago

by calling your table traverser recursively? this is a programming textbook homework question.

Lenheart357 commented 1 year ago

I did use recursion, but it seems like I won't be able to retrieve the table on the stack the next time I call it

Lenheart357 commented 1 year ago
    case OT_TABLE:
    {
        sq_get(v, -1);
        Jso += EncodeTABLE(v, Jso);
    }
        break;
Lenheart357 commented 1 year ago
        SQInteger Top = sq_gettop(v);
        SQObject obj;
        sq_getstackobj(v, -1, &obj);
        sq_pushobject(v, obj);
        Jso += EncodeTABLE(v, Jso);
        sq_settop(v, Top);

I solved it
Do not use sq_get use sq_getstackobj