-
Notifications
You must be signed in to change notification settings - Fork 0
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
22a Scoping: Recursion #56
Conversation
Testing recusionSince we have a testing framework using Test code: pseudo-9608/tests/test_recursion.py Lines 7 to 15 in c9076f8
The tests: pseudo-9608/tests/test_recursion.py Lines 28 to 38 in c9076f8
It obviously fails:
And that's because |
Accessing globalsNow, one way to resolve this without adding new features is to iterate through Instead, let's extend pseudo-9608/pseudocode/lang.py Lines 129 to 131 in 54c4399
What can we do with an Let's add a pseudo-9608/pseudocode/lang.py Lines 163 to 167 in db9d819
|
Resolving in outerNow let's make this happen in the resolver: we want to insert the frame containing the name when we resolve a Get expr. That happens in the pseudo-9608/pseudocode/resolver.py Lines 111 to 119 in 959864a
And we use our shiny new Resolving callables for recursionNext, pseudo-9608/pseudocode/resolver.py Lines 121 to 137 in cdc6a97
After we correctly insert the frame (with This still doesn't work yet, because in pseudo-9608/pseudocode/resolver.py Lines 231 to 252 in cdc6a97
We'll have to rearrange things a little to make it work: pseudo-9608/pseudocode/resolver.py Lines 208 to 229 in 05946ee
pseudo-9608/pseudocode/resolver.py Lines 231 to 252 in 05946ee
The workflow here is:
|
Resolving recursive callsA few more minor tweaks and fixes before this will work: pseudo-9608/pseudocode/resolver.py Lines 139 to 147 in bfb5957
Because the resolver isn't concerned with retrieving values (that is the interpreter's work), and we don't have any helper functions/functions/evaluators to do that, we'll have to do it manually:
Test fixesAnd then a couple of fixes for our test code: a wrong procedure call, then some jankiness around terminal line breaks (which we'll And now our recursion tests pass! |
The next thing I want to do actually is to implement some built-in functions, like
RND()
andRANDOMBETWEEN()
. But something more pressing awaits: while pseudo alloss us to declare FUNCTIONs and PROCEDUREs in global space, we can't actually call them from within a PROCEDURE or FUNCTION yet.And that is because so far, the local scopes only declare whatever parameters they are defined with. We have yet to introduce the wider notion of global scope, where any names declared are available to all function/procedure calls. And we'll need this because ... where else are we going to put built-in functions?
And, in an oblique roundabout way, we are going to approach this by tackling recursion first. After all, recursion involves a procedure/function calling itself, which has to first be declared in the global scope right? Solve that and almost everything else will work.