Skip to content

Commit

Permalink
add generic binary tree
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Jun 24, 2024
1 parent 077811b commit b81d1bd
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 41 deletions.
30 changes: 21 additions & 9 deletions FrontEnd/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

DIR=build
QEMU=
DIR := build
QEMU :=

TESTS := $(filter-out $(wildcard TestData/*gen.cw), $(wildcard TestData/*.cw))

TESTS = $(wildcard TestData/*cw)
LANG_TESTS = $(wildcard LangTest/*cw)
LIB_TESTS = $(wildcard Lib/*_test.cw)
TEST_TESTS = \
LANG_TESTS := $(wildcard LangTest/*.cw)
LIB_TESTS := $(wildcard Lib/*_test.cw)

TEST_TESTS := \
TestData/binary_tree_test.cw \
TestData/fibonacci.cw \
TestData/fizzbuzz.cw \
TestData/hash_test.cw \
Expand All @@ -16,7 +18,7 @@ TEST_TESTS = \
TestData/sieve.cw \
TestData/sort.cw

TESTS_A32 = \
TESTS_A32 := \
TestData/fibonacci.cw \
TestData/fizzbuzz.cw \
TestData/hello_world.cw \
Expand All @@ -29,7 +31,7 @@ TESTS_A32 = \
Lib/string_test.cw \
Lib/trig_test.cw

TESTS_CONCRETE = $(LANG_TESTS) $(TESTS) \
TESTS_CONCRETE := $(LANG_TESTS) $(TESTS) \
Lib/ansi.cw \
Lib/bitstream.cw \
Lib/bitstream_test.cw \
Expand Down Expand Up @@ -70,15 +72,25 @@ tests_py: \
misc_test_x64 \
misc_test_a64 \
tests_concrete_py
@echo "PASSED $@"

tests_parse_py: $(TESTS:%.cw=$(DIR)/%.cw.parse)
@echo "PASSED $@"

tests_meta_py: $(TESTS:%.cw=$(DIR)/%.cw.meta)
@echo "PASSED $@"

tests_eval_py: $(TESTS:%.cw=$(DIR)/%.cw.eval)
@echo "PASSED $@"

tests_symtab_py: $(TESTS:%.cw=$(DIR)/%.cw.symtab)
@echo "PASSED $@"

tests_pp_py: $(TESTS:%.cw=$(DIR)/%.cw.pp)
@echo "PASSED $@"

tests_concrete_py: $(TESTS_CONCRETE:%.cw=$(DIR)/%.concrete)

@echo "PASSED $@"

tests_all_py: tests_parse_py tests_symtab_py tests_meta_py tests_eval_py tests_pp_py

Expand Down
28 changes: 0 additions & 28 deletions FrontEnd/TestData/binary_tree.cw

This file was deleted.

49 changes: 49 additions & 0 deletions FrontEnd/TestData/binary_tree_gen.cw
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)
)
)
57 changes: 57 additions & 0 deletions FrontEnd/TestData/binary_tree_test.cw
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))
)

7 changes: 3 additions & 4 deletions FrontEnd/emit_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,11 @@ def EmitIRExpr(node, tc: type_corpus.TypeCorpus, id_gen: identifier.IdGenIR) ->
if isinstance(node.callee, cwast.Id):
def_node = node.callee.x_symbol
is_direct = isinstance(def_node, cwast.DefFun)
name = node.callee.x_symbol.name
if is_direct:
print(f"{TAB}bsr {node.callee.x_symbol.name}")
print(f"{TAB}bsr {name}")
else:
print(f">>>>>>>>>>>>>>>> {node.callee.x_type}")
assert False
print(f"{TAB}jsr {node.callee.x_symbol.name}")
print(f"{TAB}jsr {name} {_MakeFunSigName(node.callee.x_type)}")
else:
assert False
if sig.result_type().is_void():
Expand Down

0 comments on commit b81d1bd

Please sign in to comment.