-
Notifications
You must be signed in to change notification settings - Fork 17
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
cps: experimental support for for-loops #84
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! 👍
cps.nim
Outdated
n[1] = getTypeInst n[2] | ||
else: | ||
# get the type from the symbol as the last resort. | ||
# walkaround for #48. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still workaround
@@ -854,13 +870,21 @@ proc cpsXfrm(T: NimNode, n: NimNode): NimNode = | |||
result = copy n | |||
result = workaroundRewrites(result) | |||
|
|||
macro cps*(T: typed, n: typed): untyped = | |||
macro cps2(T: typed, n: typed): untyped = | |||
# I hate doing stuff inside macros, call the proc to do the work |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# I hate doing stuff inside macros, call the proc to do the work | |
## We used `getImplTransformed` to make a pre-pass whatfer transforming `for` and `defer` | |
## into the input to this macro, where we perform the meat of the CPS transform. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could use a for loop macro to remove part of the getImplTransformed dependency, I still need to think about the defer one.
Not a blocker
when defined(nimdoc): | ||
result = n | ||
else: | ||
result = cpsXfrm(T, n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This when
should move into cps()
.
# I hate doing stuff inside macros, call the proc to do the work | ||
when defined(nimdoc): | ||
result = n | ||
else: | ||
result = cpsXfrm(T, n) | ||
|
||
macro cps*(T: typed, n: typed): untyped = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
macro cps*(T: typed, n: typed): untyped = | |
macro cps*(T: typed, n: typed): untyped = | |
## Rewrite procedure `n` in Continuation-Passing Style, extending type `T` to store any transient locals. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted the for loop macro idea for posterity. @disruptek already highlighted the doc bits, lgtm.
For-loops are inlined using block statements, which messes with their control flow due to nim-works#76. Breaks a test in tzevv due to defer being rewritten into try-finally, which doesn't work. Co-authored-by: Andy Davidoff <[email protected]>
Rebased, compiler issues that I found:
case true
of true:
echo "true"
of false:
echo "failed" transformed to: case true
of true:
echo "true"
of false:
echo "failed"
else:
nil And this stuff don't pass sem, obviously.
a & b & c transformed to
And it also doesn't pass sem because a I'd say we should perform this transform ourselves. |
For-loops are inlined using block statements, which messes with their
control flow due to #76.
Breaks "for loop with continue, break" test. On the flip side,
splitting now functions on them.
Also breaks a test in tzevv due to defer being rewritten into
try-finally, which doesn't work due to #80.