From: Michael D. Lowis Date: Tue, 25 Nov 2014 03:15:13 +0000 (-0500) Subject: Added more tests and tweaked behavior of the branching words X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=bbf682055403fc66bbcd05fe52bc460d9ac11f1b;p=projs%2Fonward.git Added more tests and tweaked behavior of the branching words --- diff --git a/source/onward/onward.c b/source/onward/onward.c index a1427f2..e53ae64 100755 --- a/source/onward/onward.c +++ b/source/onward/onward.c @@ -243,14 +243,16 @@ defcode("'", tick, &semicolon, 0u) { /** Branch unconditionally to the offset specified by the next instruction */ defcode("br", br, &tick, 0u) { - pc += (*((value_t*)pc) * sizeof(value_t)); + pc += *((value_t*)pc); } /** Branch to the offset specified by the next instruction if the top item on * the stack is 0 */ defcode("0br", zbr, &br, 0u) { if (!onward_aspop()) - pc += (*((value_t*)pc) * sizeof(value_t)); + pc += *((value_t*)pc); + else + pc += sizeof(intptr_t); } /** Take the input string, tokenize it, and execute or compile each word */ diff --git a/tests/main.c b/tests/main.c index b77f02a..a12b256 100755 --- a/tests/main.c +++ b/tests/main.c @@ -18,6 +18,7 @@ void state_reset(void) { }; onward_init(&init_data); errno = 0; + state = 0; } int main(int argc, char** argv) diff --git a/tests/test_interpreter.c b/tests/test_interpreter.c index 40f3cd6..c332410 100644 --- a/tests/test_interpreter.c +++ b/tests/test_interpreter.c @@ -308,6 +308,22 @@ TEST_SUITE(Interpreter) { //------------------------------------------------------------------------- // Testing: : //------------------------------------------------------------------------- + TEST(Verify_colon_creates_a_new_word_and_switches_to_compile_mode) + { + state_reset(); + input = (intptr_t)" foo "; + onward_aspush((intptr_t)&colon); + ((primitive_t)exec.code)(); + word_t* old_word = (word_t*)latest; + onward_aspush((intptr_t)"foo"); + ((primitive_t)create.code)(); + word_t* new_word = (word_t*)latest; + CHECK(F_HIDDEN_MSK == new_word->flags); + CHECK((intptr_t*)here == new_word->code); + CHECK(old_word == new_word->link); + CHECK(0 == *(intptr_t*)here); + CHECK(1 == state); + } //------------------------------------------------------------------------- // Testing: ' @@ -324,10 +340,37 @@ TEST_SUITE(Interpreter) { //------------------------------------------------------------------------- // Testing: br //------------------------------------------------------------------------- + TEST(Verify_br_adds_the_offset_to_the_program_counter_uncoditionally) + { + state_reset(); + intptr_t code[] = { sizeof(intptr_t)*2, 0, 0 }; + pc = (intptr_t)code; + ((primitive_t)br.code)(); + CHECK((intptr_t)&code[2] == pc); + } //------------------------------------------------------------------------- // Testing: 0br //------------------------------------------------------------------------- + TEST(Verify_br0_adds_the_offset_to_the_program_counter_if_the_top_of_the_stack_is_zero) + { + state_reset(); + intptr_t code[] = { sizeof(intptr_t)*2, 0, 0 }; + pc = (intptr_t)code; + onward_aspush(0); + ((primitive_t)zbr.code)(); + CHECK((intptr_t)&code[2] == pc); + } + + TEST(Verify_br0_should_skip_past_the_branch_offset_if_top_of_the_stack_is_non_zero) + { + state_reset(); + intptr_t code[] = { sizeof(intptr_t)*2, 0, 0 }; + pc = (intptr_t)code; + onward_aspush(1); + ((primitive_t)zbr.code)(); + CHECK((intptr_t)&code[1] == pc); + } //------------------------------------------------------------------------- // Testing: interp