Skip to content

Instantly share code, notes, and snippets.

View cosmos72's full-sized avatar

Massimiliano Ghilardi cosmos72

View GitHub Profile
@cosmos72
cosmos72 / gist:f971c172e71d08030f92a1fc5fa52735
Created April 6, 2025 10:33
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 {