From: Michael D. Lowis Date: Tue, 22 Apr 2014 02:19:55 +0000 (-0400) Subject: CHECKPOINT: IF and THEN implementations X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ba1d3a4c65034e1b2e02616bcac29d63b72510d4;p=proto%2Fsclpl.git CHECKPOINT: IF and THEN implementations --- diff --git a/source/slvm/main.c b/source/slvm/main.c index 7daa829..be2af88 100644 --- a/source/slvm/main.c +++ b/source/slvm/main.c @@ -394,7 +394,7 @@ defcode("quit", quit, 0, 0, &interpret){ for(i = (stacksz > 5) ? 4 : stacksz-1; i >= 0; i--) { - printf("%lu ", *(ArgStackPtr-i)); + printf("%ld ", *(ArgStackPtr-i)); } printf(")\n%s ", (state_val == 0) ? "=>" : ".."); Line_Read = 0; @@ -571,23 +571,41 @@ defcode("bmove", bytemove, 0, 0, &bytecopy){ /* Control Flow Words *****************************************************************************/ -defcode("if", _if, 1, 0, &bytemove){ +defcode("if", _if, 0, 1, &bytemove){ // : IF IMMEDIATE // ' 0BRANCH , \ compile 0BRANCH + ArgStackPtr++; + *(ArgStackPtr) = (long)&zbranch; + EXEC(comma); // HERE @ \ save location of the offset on the stack + ArgStackPtr++; + *(ArgStackPtr) = here_val; // 0 , \ compile a dummy offset + ArgStackPtr++; + *(ArgStackPtr) = 0; + EXEC(comma); // ; } -defcode("then", _then, 1, 0, &_if){ +defcode("then", _then, 0, 1, &_if){ // : THEN IMMEDIATE // DUP + EXEC(dup); // HERE @ SWAP - \ calculate the offset from the address saved on the stack + ArgStackPtr++; + *(ArgStackPtr) = here_val; + EXEC(swap); + EXEC(sub); // SWAP ! \ store the offset in the back-filled location + EXEC(swap); + //EXEC(store); + printf("STACK: %ld %ld\n", *(ArgStackPtr-1), *(ArgStackPtr)); + //*((long*)*ArgStackPtr) = *(ArgStackPtr-1); + //ArgStackPtr -= 2; // ; } -defcode("else", _else, 1, 0, &_then){ +defcode("else", _else, 0, 1, &_then){ // : ELSE IMMEDIATE // ' BRANCH , \ definite branch to just over the false-part // HERE @ \ save location of the offset on the stack @@ -617,7 +635,12 @@ defcode("printw", printw, 0, 0, &_else){ if (*bytecode == (long)&literal) { bytecode++; - printf("\tlit %lu\n", *bytecode); + printf("\tlit %ld\n", *bytecode); + } + else if (*bytecode == (long)&zbranch) + { + bytecode++; + printf("\t0br %ld\n", *bytecode); } else { @@ -655,7 +678,7 @@ defcode("printdefw", printdefw, 0, 0, &printallw){ const word_t* word = (word_t*)latest_val; while(word != &printdefw) { - printf("%s\t%lu %lu", + printf("%s\t%ld %ld", word->name, word->flags.f_immed, word->flags.f_hidden);