squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

missing goto break/continue n ?

Open mingodad opened this issue 8 years ago • 7 comments

Hello ! How hard it would be to add "goto" and/or "break/continue x" ?

Cheers !

mingodad avatar May 13 '16 18:05 mingodad

due to the fact that squirrel's compiler doesn't build an AST it would be quite tricky.

albertodemichelis avatar May 14 '16 10:05 albertodemichelis

I modified the lexer to manage one token lookahead to facilitate parsing some statements, added the "goto" keyword and made the parser/lexer accept it (including labels) and emit a warning for now. It's necessary to create code to manage it (goto/labels) properly now.

mingodad avatar May 15 '16 21:05 mingodad

Hello ! I need some help to fix my implementation of "goto" in SquiLu https://github.com/mingodad/squilu/commit/ba83378ac10aa9ece901ec4d70dc49f0c935ec37 , right now I can parse labels/goto statements check if they/exists/duplicate and it works for a very simple test but I need to find a way to add the "_OP_POPTRAP", looking at how "break/continue" is implemented it seems that I need to add one if one of this conditions are true "_fs->_breaktargets.top() > 0 / _fs->_continuetargets.top() > 0", but probably I also need to check if the jump goes outside of "try/catch/for/while" instructions.

Can someone give any help on this ? Cheers !

mingodad avatar Jun 12 '16 20:06 mingodad

Trying to solve the goto _OP_POPTRAP RESOLVE_OUTERS I introduced a new opcode _OP_NOP (no operation) to use as placeholder to be edited if needed, the idea is that not all gotos will cross try/catch/loops boundaries so some will need to pop traps and resolve outers and others don't.

Looking at how TK_RETURN manage this situation (pop all traps, but it's not clear about how it resolve outers "_fs->AddInstruction(_OP_RETURN, 1, _fs->PopTarget(),_fs->GetStackSize());").

Right now I'm leaving 2 _OP_NOP placeholders before the _OP_JUMP to be resolved at the end of function parsing with a call to ResolveGotos, then I need to calculate (depending on the label to jump being before or after the actual goto) how many try/catch/loops the jump will cross and edit the placeholders with that information (for try one _OP_NOP will be converted to _OP_POPTRAP with the number to be poped, for the RESOLVE_OUTERS the other _OP_NOP will be converted to _OP_CLOSE).

Can someone give any help here ? Cheers !

mingodad avatar Jun 13 '16 09:06 mingodad

I could get the "goto" implementation in a working state but still need more test for corner cases.

mingodad avatar Jun 14 '16 18:06 mingodad

I did a simplification of the "goto" implementation and can be used with care now. The remaining problem is to detect cross declaration/initialization:

local function test10(x)
{
    goto done2;
    local zz = 3; //this should be an error cross declaration/initialization
    try {goto done;}catch(e){}
done:
    return "done";
done2:
    return "done2";
}

print(test10(23));

Any feedback is welcome !

mingodad avatar Jun 15 '16 13:06 mingodad

With a bit more minor fixes SquiLu can now run the program bellow and most any valid squirrel. The C/C++ syntax are mostly accepted and discarded/alias to Squirrel/SquiLu substitutes.

typedef string_t string;
typedef int_t int;
typedef any_t T;

template<typename T>
string doIt(T x)
{
    int z = 3;
    z = (int)x;
    print(z);
    for(int i=0; i < 10; ++i)
    {
        while(i < 10)
        {
            try
            {
                print("i =", i);
                if(i == 0) goto done;
            }
            catch(e)
            {
            }
        }
    }
done:
    return "done with " + x.tostring();
}

class Klass
{
    template<typename T>
    string doIt(T x)
    {
        return x.tostring();
    }
}

Klass k = new Klass();
print(k.doIt(10));
print(doIt(20));

Output:

10
20
i = 0
done with 20

mingodad avatar Jun 15 '16 21:06 mingodad