-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
077811b
commit b81d1bd
Showing
5 changed files
with
130 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
@doc "Binary Tree Example" | ||
(module BinaryTree [ | ||
@doc "the payload type" | ||
(modparam $type TYPE) | ||
@doc "the less-than function ($type x $type) -> bool" | ||
(modparam $lt CONST_EXPR)] : | ||
|
||
(import fmt) | ||
|
||
@pub (global Leaf auto void_val) | ||
|
||
|
||
@pub (defrec Node : | ||
(field left (union [void (ptr! Node)])) | ||
(field right (union [void (ptr! Node)])) | ||
(field payload $type)) | ||
|
||
@doc "same as above for left and right" | ||
@pub (type MaybeNode (union [void (ptr! Node)])) | ||
|
||
|
||
(type Visitor (sig [(param node (ptr $type))] void)) | ||
|
||
|
||
@pub (fun InorderTraversal [(param root MaybeNode) (param visitor Visitor)] void : | ||
(trylet node (ptr! Node) root _ : | ||
(return)) | ||
(do (InorderTraversal [(^. node left) visitor])) | ||
(do (visitor [(& (^. node payload))])) | ||
(do (InorderTraversal [(^. node right) visitor]))) | ||
|
||
@doc "returns the new root" | ||
@pub (fun Insert [(param root MaybeNode) (param node (ptr! Node))] (ptr! Node): | ||
(= (^. node left) Leaf) | ||
(= (^. node right) Leaf) | ||
(if (is root void) : | ||
(return node) | ||
:) | ||
(let! curr auto (narrowto root (ptr! Node))) | ||
(if (call $lt [(& (^. node payload)) (&(^. curr payload))]) : | ||
(let x auto (Insert [(^. curr left) node])) | ||
(= (^. curr left) x) | ||
: | ||
(let x auto (Insert [(^. curr right) node])) | ||
(= (^. curr right) x) | ||
) | ||
(return curr) | ||
) | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
@doc "Binary Tree Example" | ||
(module main [] : | ||
(import test) | ||
|
||
(import fmt) | ||
|
||
|
||
(fun lt [(param a (ptr u32)) (param b (ptr u32))] bool : | ||
(return (< (^ a) (^ b)))) | ||
|
||
(import bt ./binary_tree_gen [u32 lt]) | ||
|
||
|
||
(global N u32 64) | ||
|
||
|
||
(global! NodePool auto (array_val N bt::Node)) | ||
|
||
|
||
(global! NodePoolFreeIndex uint 0) | ||
|
||
|
||
(fun alloc [(param p u32)] (ptr! bt::Node) : | ||
(let out auto (&! (at NodePool NodePoolFreeIndex))) | ||
(+= NodePoolFreeIndex 1) | ||
(= (^. out payload) p) | ||
(return out)) | ||
|
||
|
||
(fun reverse_bits [(param bits u32) (param width u32)] u32 : | ||
(let! x u32 bits) | ||
(let! out u32 0) | ||
(for i 0 width 1 : | ||
(<<= out 1) | ||
(if (== (and x 1) 0) : | ||
(or= out 1) | ||
:) | ||
(>>= x 1)) | ||
(return out)) | ||
|
||
|
||
(fun DumpNode [(param payload (ptr u32))] void : | ||
(fmt::print# (^ payload) "\n")) | ||
|
||
|
||
@cdecl (fun main [(param argc s32) (param argv (ptr (ptr u8)))] s32 : | ||
(let! root bt::MaybeNode bt::Leaf) | ||
(for i 0 N 1 : | ||
(let node auto (alloc [(reverse_bits [i 6])])) | ||
@doc """(fmt::print# "before insert " i "\n")""" | ||
(let x auto (bt::Insert [root node])) | ||
(= root x)) | ||
(do (bt::InorderTraversal [root DumpNode])) | ||
(test::Success#) | ||
(return 0)) | ||
) | ||
|
This file contains 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