Created
August 10, 2019 05:54
-
-
Save jayrbolton/f92f98a1ec78209c3d11d1f5cfb6dce8 to your computer and use it in GitHub Desktop.
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
group :num | |
:int :float | |
type :bintree:leaf a | |
@val a | |
type :bintree:branch a | |
@left :bintree a | |
@right :bintree a | |
fun !map | |
arg f = :func a b | |
arg vec = :vec a | |
ret :vec b | |
| len <- !len vec | |
| ret <- !:vec len | |
$ foreach idx elem vec | |
| result <- !f elem | |
| ret <- !set result idx vec | |
$ repeat | |
$ return ret | |
fun !sum | |
arg tree <- :bintree :num a | |
ret :num a | |
match tree | |
:bintree:leaf | |
$ return leaf | |
:bintree:branch | |
| left <- @left branch | |
| right <- @right branch | |
| lsum <- !sum left | |
| rsum <- !sum right | |
| sum <- !add lsum rsum | |
$ return sum | |
# bintree constructor | |
fun !mytree | |
arg n <- :num | |
ret :bintree :num | |
| l1 <- !:bintree:leaf n | |
| inc <- !inc n | |
| l2 <- !:bintree:leaf inc | |
| root <- !:create_branch l1 l2 | |
# compiler returns pointer to `root` and keeps structs `root`, `l1`, and `l2` allocated | |
$ return root | |
# creates a bintree, sums it | |
main | |
| tree <- !mytree 10 | |
| sum <- !sum tree | |
> print sum | |
# tree will be deallocated at the end of main, as well as its children (`l1`, `l2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment