Created
April 6, 2025 10:33
-
-
Save cosmos72/f971c172e71d08030f92a1fc5fa52735 to your computer and use it in GitHub Desktop.
tree-of-closures example for fast interpreters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* return a closure that computes x + y when executed */ | |
| func compile_add(x Closure, y Closure) -> Closure { | |
| if (x.Type == y.Type) { | |
| switch (x.Type) { | |
| case types.Int: | |
| /* both closures actually return an int, downcast them */ | |
| var xe = type_cast(x, func () -> int) | |
| var ye = type_cast(y, func () -> int) | |
| /* create a closure that calls xe() and ye() and returns their sum */ | |
| return func () -> int { | |
| return xe() + ye() | |
| } | |
| } | |
| } | |
| /* ... */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment