From 21888f80d7a8e603e45849c52f3191e71a8f6661 Mon Sep 17 00:00:00 2001 From: inglorion Date: Sat, 30 Jan 2016 23:13:21 -0800 Subject: [PATCH] added block_iterator --- bootstrap/src/antimony/block_sb.voo | 40 ++++++++++++++++++++++++++++++ configure | 1 + src/antimony/block_sb.sb | 20 +++++++++++++++ src/test/test_block.out | 2 ++ src/test/test_block.sb | 49 ++++++++++++++++++++++++------------- 5 files changed, 95 insertions(+), 17 deletions(-) create mode 100644 bootstrap/src/antimony/block_sb.voo create mode 100644 src/antimony/block_sb.sb rewrite src/test/test_block.sb (63%) diff --git a/bootstrap/src/antimony/block_sb.voo b/bootstrap/src/antimony/block_sb.voo new file mode 100644 index 0000000..662f6cf --- /dev/null +++ b/bootstrap/src/antimony/block_sb.voo @@ -0,0 +1,40 @@ +section data +import sbJ___log +section functions +export sbJ_block_iterator +import sbJ_allocate_words sbJ_block_length sbJ_block_nth sbJ_false sbJ_make_iterator sbJ_lt sbJ_true +align +sbJ_block_iterator: +function sbJ_blk + block + let sbJ_state call sbJ_allocate_words 4 + set-word sbJ_state 1 sbJ_blk + let sbJ__sb_G1 call sbJ_block_length sbJ_blk + set-word sbJ_state 2 sbJ__sb_G1 + set-word sbJ_state 3 0 + let sbJ__sb_G2 call sbJ_make_iterator sbJ_block_iterator_next sbJ_state + return sbJ__sb_G2 + end block +end function + +align +sbJ_block_iterator_next: +function sbJ_it sbJ_state sbJ_setJ2diteratorJ2dvalue + block + let sbJ_i get-word sbJ_state 3 + let sbJ__sb_G3 get-word sbJ_state 2 + let sbJ__sb_G4 call sbJ_lt sbJ_i sbJ__sb_G3 + ifeq sbJ__sb_G4 @sbJ_true + block + let sbJ__sb_G5 add sbJ_i 1 + set-word sbJ_state 3 sbJ__sb_G5 + let sbJ__sb_G6 get-word sbJ_state 1 + let sbJ__sb_G7 call sbJ_block_nth sbJ__sb_G6 sbJ_i + let sbJ__sb_G8 call sbJ_setJ2diteratorJ2dvalue sbJ_it sbJ__sb_G7 + return sbJ__sb_G8 + end block + end if + return @sbJ_false + end block +end function + diff --git a/configure b/configure index 5727090..8dbc037 100755 --- a/configure +++ b/configure @@ -60,6 +60,7 @@ allocate blob blob_sb block +block_sb boolean comment condition diff --git a/src/antimony/block_sb.sb b/src/antimony/block_sb.sb new file mode 100644 index 0000000..f334e02 --- /dev/null +++ b/src/antimony/block_sb.sb @@ -0,0 +1,20 @@ +section functions +export block_iterator +import allocate_words block_length block_nth false make_iterator lt true + +function block_iterator blk { + let state allocate_words 4 + set-word state 1 blk + set-word state 2 (block_length blk) + set-word state 3 0 + return (make_iterator block_iterator_next state) +} + +function block_iterator_next it state set-iterator-value { + let i get-word state 3 + if (lt i (get-word state 2)) { + set-word state 3 (add i 1) + return (set-iterator-value it (block_nth (get-word state 1) i)) + } + return @false +} diff --git a/src/test/test_block.out b/src/test/test_block.out index 48cf2ad..68eb380 100644 --- a/src/test/test_block.out +++ b/src/test/test_block.out @@ -1 +1,3 @@ block +foo +bar diff --git a/src/test/test_block.sb b/src/test/test_block.sb dissimilarity index 63% index 9e35080..6c1e32a 100644 --- a/src/test/test_block.sb +++ b/src/test/test_block.sb @@ -1,17 +1,32 @@ -section functions -export core.main -import eq true -import array block block_t make_blob sb_init standard_output type_of \ - write_blob_to_stream write_newline_and_flush_stream - -function core.main argc argv { - sb_init - var items = auto-words 1 - set-word items 0 "foo" - var blk = block (array 1 items) - if (eq (type_of blk) block_t) { - write_blob_to_stream "block" standard_output - write_newline_and_flush_stream standard_output - } - return 0 -} +section functions +export core.main +import eq true +import array block block_t block_iterator false iterator_step iterator_value \ + make_blob sb_init standard_output type_of write_blob_to_stream \ + write_newline_and_flush_stream + +function puts blob { + write_blob_to_stream blob standard_output + write_newline_and_flush_stream standard_output + return 0 +} + +function core.main argc argv { + sb_init + var items = auto-words 2 + set-word items 0 "foo" + set-word items 1 "bar" + var blk = block (array 2 items) + if (eq (type_of blk) block_t) { + puts "block" + } + + loop { + let it block_iterator blk + do + while (iterator_step it) + puts (iterator_value it) + } + + return 0 +} -- 2.11.4.GIT