Skip to content

Instantly share code, notes, and snippets.

@cosmos72
Created April 6, 2025 10:33
Show Gist options
  • Select an option

  • Save cosmos72/f971c172e71d08030f92a1fc5fa52735 to your computer and use it in GitHub Desktop.

Select an option

Save cosmos72/f971c172e71d08030f92a1fc5fa52735 to your computer and use it in GitHub Desktop.
tree-of-closures example for fast interpreters
/* 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