albertodemichelis / squirrel

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

How to recursively traverse a table #278

Closed Lenheart357 closed 11 months ago

Lenheart357 commented 11 months 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 11 months ago

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

Lenheart357 commented 11 months 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 11 months ago
    case OT_TABLE:
    {
        sq_get(v, -1);
        Jso += EncodeTABLE(v, Jso);
    }
        break;
Lenheart357 commented 11 months 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