Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function Reference parameters in Quirrel #97

Open
Peach1 opened this issue Sep 13, 2024 · 0 comments
Open

Function Reference parameters in Quirrel #97

Peach1 opened this issue Sep 13, 2024 · 0 comments

Comments

@Peach1
Copy link

Peach1 commented Sep 13, 2024

So the Squirrel language already has 'outers' which act like references:

local x = 0; function mod() { x = 5; } mod(); print(x) // x is now 5

x is modified with _OP_SETOUTER; function main() local x gets modified when calling mod() here

Squirrel "Outers" are similar to true references, but Squirrel's outers are limited to locals in the _parent function.

With some modification, how could full references be added to the Quirrel language?

function mod(&x) { x = 5; }  local x = 0; mod(x); print(x);
5

Syntax: Maybe parsing & in CreateFunction could call MarkLocalAsOuter() or something similar?

Then for runtime

In SQVM::CLOSURE_OP() outers seem to get setup on creating the closure:

    if((nouters = func->_noutervalues)) {
        for(SQInteger i = 0; i<nouters; i++) {
            SQOuterVar &v = func->_outervalues[i];
            switch(v._type){
            case otLOCAL: // outer local stack target, possible to use for general references??
                FindOuter(closure->_outervalues[i], &STK(_integer(v._src)));
                break;
            case otOUTER:
                closure->_outervalues[i] = _closure(ci->_closure)->_outervalues[_integer(v._src)];
                break;
            }
        }
    }

To add 'references' to the Quirrel language, "references" would have to be setup right before the function gets called, in _OP_PREPCALL or something? using a modified FindOuter function...

I wish I could create a direct pull request.. I was hoping to first get more insight on how reference parameters would best be implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant