Skip to content

Commit

Permalink
linked list almost works now
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Feb 12, 2024
1 parent d2c565d commit 6f2ea83
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
3 changes: 2 additions & 1 deletion FrontEnd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ TESTS = macros.cw \
sexpr.cw \
cast.cw \
comment.cw \
linked_list.cw \
binary_tree.cw \
sizeof.cw

Expand Down Expand Up @@ -97,6 +96,7 @@ tests_lib_a64: \
$(DIR)/sha3_test.lib.a64.exe

tests_emit_x64: \
$(DIR)/linked_list.run.x64.exe \
$(DIR)/hello_world.run.x64.exe \
$(DIR)/fizzbuzz.run.x64.exe \
$(DIR)/sieve.run.x64.exe \
Expand All @@ -106,6 +106,7 @@ tests_emit_x64: \
$(DIR)/wordcount.x64.test

tests_emit_a64: \
$(DIR)/linked_list.run.a64.exe \
$(DIR)/hello_world.run.a64.exe \
$(DIR)/fizzbuzz.run.a64.exe \
$(DIR)/sieve.run.a64.exe \
Expand Down
59 changes: 56 additions & 3 deletions FrontEnd/TestData/linked_list.cw
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@doc "Linked List Example"
(module main [] :

(import test)
(import fmt)

(@wrapped type NoneType void)


Expand All @@ -17,11 +20,61 @@

(fun SumPayload [(param root MaybeNode)] u32 :
(let! sum u32 0)
(let! node auto root)
(let! node MaybeNode root)
(while true :
(fmt::print# sum "\n")
(trylet x (ptr! LinkedListNode) node _ : (break))
(+= sum (. (^ x) payload))
(= node (. (^ x) next)))
(+= sum (-> x payload))
(= node (-> x next)))
(return sum))



(global N u32 10)

(@mut global NodePool auto (array_val N LinkedListNode))

(static_assert (==
(as (sizeof (typeof NodePool)) u32)
(* N 16)))

(fun DumpNode [(param i u32)] void :
(fmt::print# i " " (. (at NodePool i) payload) " " (uniontypetag (. (at NodePool i) next)))
(if (is (. (at NodePool i) next) NoneType) :
(fmt::print# " next: NULL\n")
:
(fmt::print# " next: " (cast (narrowto (. (at NodePool i) next)
(ptr! LinkedListNode))
(ptr void))
"\n")
)


)


@cdecl (fun main [(param argc s32) (param argv (ptr (ptr u8)))] s32 :
(fmt::print# "start: " (cast (front NodePool) (ptr void)) "\n")

(for i 0 N 1 :
(= (. (at NodePool i) payload) i)
(if (== i (- N 1) ) :
(= (. (at NodePool i) next) None)
: (= (. (at NodePool i) next) (&! (at NodePool (+ i 1))) )
)
(shed (DumpNode [i]))
)

(for i 0 N 1 :
(shed (DumpNode [i]))
)
@doc """

(fmt::print# (SumPayload [(front! NodePool)]))
(test::AssertEq# (SumPayload [(front! NodePool)]) 499500_u32)
"""
(test::Success#)
(return 0)
)

)

0 comments on commit 6f2ea83

Please sign in to comment.