From: Michael D. Lowis Date: Tue, 2 Dec 2014 21:27:24 +0000 (-0500) Subject: Started re-organizing and debugging defined words dictionary X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=41c5fa849f8d39e5ec8c11625608b9c8123b855d;p=projs%2Fonward.git Started re-organizing and debugging defined words dictionary --- diff --git a/source/onward.c b/source/onward.c index 402a2f5..76c72b3 100755 --- a/source/onward.c +++ b/source/onward.c @@ -651,6 +651,11 @@ static syscall_fn_t System_Calls[7] = { #ifdef STANDALONE #include +static bool Newline_Consumed = false; +value_t Argument_Stack[ARG_STACK_SZ]; +value_t Return_Stack[RET_STACK_SZ]; +value_t Word_Buffer[WORD_BUF_SZ]; + defvar("infile", infile, 0u, LATEST_BUILTIN); defvar("outfile", outfile, 0u, &infile_word); defvar("errfile", errfile, 0u, &outfile_word); @@ -658,8 +663,6 @@ defcode("syscall", syscall, &errfile_word, 0u) { System_Calls[onward_aspop()](); } -static bool Newline_Consumed = false; - value_t fetch_char(void) { value_t ch = (value_t)fgetc((FILE*)infile); diff --git a/source/onward.ft b/source/onward.ft index 93b39fd..bec0bcb 100644 --- a/source/onward.ft +++ b/source/onward.ft @@ -1,74 +1,99 @@ +\ Compiler Words +\ ----------------------------------------------------------------------------- : immediate latest @ \ Get the latest word CELLSZ + \ Add offset to get to the flags field dup @ \ Fetch the current value F_IMMEDIATE | ! \ Set the immediate bit -; immediate +; \ immediate -: [compile] immediate - word find , -; +\ : [compile] immediate +\ word find , +\ ; -: # [compile] \ ; -: #! [compile] \ ; +\ : recurse immediate +\ latest @ , +\ ; -: recurse immediate - latest @ , -; +\ Conditional Words: if, then, else +\ ----------------------------------------------------------------------------- +\ : if immediate +\ ' 0br , \ compile 0branch +\ here @ \ save location of the offset on the stack +\ 0 , \ compile a dummy offset +\ ; -: if immediate - ' 0br , \ compile 0branch - here @ \ save location of the offset on the stack - 0 , \ compile a dummy offset -; +\ : then immediate +\ dup +\ here @ swap - \ calculate the offset from the address saved on the stack +\ ! +\ ; -: then immediate - dup - here @ swap - \ calculate the offset from the address saved on the stack - ! -; +\ : else immediate +\ ' br , \ definite branch to just over the false-part +\ here @ \ save location of the offset on the stack +\ 0 , \ compile a dummy offset +\ swap \ now back-fill the original (if) offset +\ dup \ same as for then word above +\ here @ swap - \ calculate the offset from the address saved on the stack +\ ! +\ ; -: else immediate - ' br , \ definite branch to just over the false-part - here @ \ save location of the offset on the stack - 0 , \ compile a dummy offset - swap \ now back-fill the original (if) offset - dup \ same as for then word above - here @ swap - \ calculate the offset from the address saved on the stack - ! -; +\ Looping Words +\ ----------------------------------------------------------------------------- +\ : begin immediate here @ ; -: begin immediate here @ ; +\ : until immediate +\ ' 0br , \ compile 0BRANCH +\ here @ - \ calculate the offset from the address saved on the stack +\ , \ compile the offset here +\ ; -: until immediate - ' 0br , \ compile 0BRANCH - here @ - \ calculate the offset from the address saved on the stack - , \ compile the offset here -; +\ : again immediate +\ ' br , \ compile branch +\ here @ - \ calculate the offset back +\ , \ compile the offset here +\ ; -: again immediate - ' br , \ compile branch - here @ - \ calculate the offset back - , \ compile the offset here -; +\ : while immediate +\ ' 0br , \ compile 0branch +\ here @ \ save location of the offset2 on the stack +\ 0 , \ compile a dummy offset2 +\ ; -: while immediate - ' 0br , \ compile 0branch - here @ \ save location of the offset2 on the stack - 0 , \ compile a dummy offset2 -; +\ : repeat immediate +\ ' br , \ compile branch +\ swap \ get the original offset (from begin) +\ here @ - , \ and compile it after branch +\ dup +\ here @ swap - \ calculate the offset2 +\ ! \ and back-fill it in the original location +\ ; -: repeat immediate - ' br , \ compile branch - swap \ get the original offset (from begin) - here @ - , \ and compile it after branch - dup - here @ swap - \ calculate the offset2 - ! \ and back-fill it in the original location -; +\ : unless immediate +\ ' not , \ compile not (to reverse the test) +\ [compile] if \ continue by calling the normal if +\ ; + +\ Comment Words +\ ----------------------------------------------------------------------------- +\ : # [compile] \ ; +\ : #! [compile] \ ; + +\ : ( immediate +\ 1 \ allowed nested parens by keeping track of depth +\ begin +\ key \ read next character +\ dup 0x28 = if \ open paren? +\ drop \ drop the open paren +\ 1 + \ depth increases +\ else +\ 0x29 = if \ close paren? +\ 1 - \ depth decreases +\ then +\ then +\ dup 0 = until \ continue until we reach matching close paren, depth 0 +\ drop \ drop the depth counter +\ ; -: unless immediate - ' not , \ compile not (to reverse the test) - [compile] if \ continue by calling the normal if -; diff --git a/source/onward_sys.h b/source/onward_sys.h index 622c3c9..ea40007 100644 --- a/source/onward_sys.h +++ b/source/onward_sys.h @@ -21,11 +21,11 @@ #define WORD_BUF_SZ (8192 / sizeof(value_t)) #endif -value_t Argument_Stack[ARG_STACK_SZ]; +extern value_t Argument_Stack[ARG_STACK_SZ]; -value_t Return_Stack[RET_STACK_SZ]; +extern value_t Return_Stack[RET_STACK_SZ]; -value_t Word_Buffer[WORD_BUF_SZ]; +extern value_t Word_Buffer[WORD_BUF_SZ]; value_t fetch_char(void); diff --git a/tests/main.c b/tests/main.c index 29d7255..f5bf5e3 100755 --- a/tests/main.c +++ b/tests/main.c @@ -1,8 +1,11 @@ #include "atf.h" #include "onward.h" +#include "onward_sys.h" char* input = ""; -value_t Word_Buffer[1024 / sizeof(value_t)]; +value_t Argument_Stack[ARG_STACK_SZ]; +value_t Return_Stack[RET_STACK_SZ]; +value_t Word_Buffer[WORD_BUF_SZ]; value_t fetch_char(void) {