]> git.mdlowis.com Git - projs/onward.git/commitdiff
Added more tests and tweaked behavior of the branching words
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 25 Nov 2014 03:15:13 +0000 (22:15 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 25 Nov 2014 03:15:13 +0000 (22:15 -0500)
source/onward/onward.c
tests/main.c
tests/test_interpreter.c

index a1427f259f5bc51cf2f9a3218fdd4b6ae8ee7167..e53ae64839a8b913113f751da64059bd44826704 100755 (executable)
@@ -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 */
index b77f02a2a9f6ea36c8b6586014f63e9bea5ee842..a12b256841cee529656d5fdf9c68ea1d49f2bc61 100755 (executable)
@@ -18,6 +18,7 @@ void state_reset(void) {
     };
     onward_init(&init_data);
     errno = 0;
+    state = 0;
 }
 
 int main(int argc, char** argv)
index 40f3cd6ef4662a6ef11439c1c6e7017199512406..c33241028d546fec55f9f53d5c2d4595a6bfef9c 100644 (file)
@@ -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