Skip to content

Instantly share code, notes, and snippets.

@jayrbolton
Last active July 30, 2019 20:26
Show Gist options
  • Save jayrbolton/6984973933c160a4c89b69e506b6c6da to your computer and use it in GitHub Desktop.
Save jayrbolton/6984973933c160a4c89b69e506b6c6da to your computer and use it in GitHub Desktop.
# Register-based -- all varaibles are stored in registers. Sub-function calls are templated and expanded inline
# Compute the nth factorial
fun factorial n:int -> int
| count <- n
$ loop while (gt count 1)
| count <- dec count
| n <- mul count n
$ repeat
$ return n
# Get the nth fibonacci number
fn fibonacci n:int -> int
| x <- 0
| y <- 1
| swp:int
$ loop while (gt n 1)
| swp <- x
| x <- y
| y <- add swp y
| n <- dec n
$ repeat
$ return y
# stack-based; every line shows the full stack
fn factorial n:int -> int
| n
| push 0 -> y n
| push 1 -> x y n
loop
| gt n 1 -> b:bool x y n
breakif! -> x y n
| add x y -> newy x y n
| swap -> x newy y n
| pop -> newy y n
| swap -> y newy n
| rename -> x y n
repeat
| x y n
| pop -> y n
| swap -> n y
| pop -> y
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment