From c52194127d604bd365f7222936b46f34f62a4814 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Tue, 11 Oct 2016 21:20:08 +1300 Subject: [PATCH] Updated library code to use IB and QUERY-INPUT. --- src/forth.jl | 20 ++++++++++++++------ src/lib_1_basic.4th | 1 - src/lib_3_comments.4th | 12 ++++++------ src/lib_5_strings.4th | 12 ++++++------ 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/forth.jl b/src/forth.jl index 97dcc57..c540313 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -683,7 +683,7 @@ READ_LINE_CFA = defPrimWord("READ-LINE", () -> begin eofFlag = endswith(line, '\n') ? 0 : -1 line = chomp(line) - println("Reading: $line"); + #println("Reading: $line"); putString(line, addr, maxSize) @@ -999,15 +999,22 @@ QUERY_CFA = defWord("QUERY", LIT_CFA, 0, TOIN_CFA, STORE_CFA, EXIT_CFA]) -# ( fid -- flag ) -# flag set to true if EOF is reached +EOF_FLAG, EOF_FLAG_CFA = defNewVar("EOF-FLAG", 0) + +# ( fid -- ) +# EOF-FLAG set to true if EOF is reached QUERY_FILE_CFA = defWord("QUERY-FILE", [FIB_CFA, LIT_CFA, 160, ROT_CFA, READ_LINE_CFA, - DROP_CFA, SWAP_CFA, + DROP_CFA, EOF_FLAG_CFA, STORE_CFA, NUMFIB_CFA, STORE_CFA, LIT_CFA, 0, TOIN_CFA, STORE_CFA, EXIT_CFA]) +QUERY_INPUT_CFA = defWord("QUERY-INPUT", + [SOURCE_ID_CFA, FETCH_CFA, QDUP_CFA, ZBRANCH_CFA, 3, + QUERY_FILE_CFA, EXIT_CFA, + QUERY_CFA, EXIT_CFA]) + WORD_CFA = defPrimWord("WORD", () -> begin delim = popPS() @@ -1041,7 +1048,7 @@ WORD_CFA = defPrimWord("WORD", () -> begin mem[countAddr] = count pushPS(countAddr) - println("Processing word: '$(getString(countAddr+1,mem[countAddr]))' (state $(mem[STATE]))") + #println("Processing word: '$(getString(countAddr+1,mem[countAddr]))' (state $(mem[STATE]))") return NEXT end) @@ -1241,10 +1248,11 @@ INCLUDED_CFA = defWord("INCLUDED", SOURCE_ID_CFA, FETCH_CFA, SWAP_CFA, # Store current source on stack SOURCE_ID_CFA, STORE_CFA, # Mark this as the current source SOURCE_ID_CFA, FETCH_CFA, QUERY_FILE_CFA, # Read line from file + EOF_FLAG_CFA, FETCH_CFA, NUMFIB_CFA, FETCH_CFA, ZE_CFA, AND_CFA, # Test for EOF and empty line INVERT_CFA, ZBRANCH_CFA, 4, # Break out if EOF INTERPRET_CFA, # Interpret line - BRANCH_CFA, -12, # Loop + BRANCH_CFA, -14, # Loop SOURCE_ID_CFA, FETCH_CFA, CLOSE_FILE_CFA, DROP_CFA, # Close file SOURCE_ID_CFA, STORE_CFA, # Restore input source diff --git a/src/lib_1_basic.4th b/src/lib_1_basic.4th index bc21965..a5bb701 100644 --- a/src/lib_1_basic.4th +++ b/src/lib_1_basic.4th @@ -75,4 +75,3 @@ LINK> \ get the codeword , \ compile it ; - diff --git a/src/lib_3_comments.4th b/src/lib_3_comments.4th index a5b695b..1c0be39 100644 --- a/src/lib_3_comments.4th +++ b/src/lib_3_comments.4th @@ -3,17 +3,17 @@ : ( IMMEDIATE 1 \ allowed nested parens by keeping track of depth BEGIN - >IN @ #TIB @ >= IF \ End of TIB? - QUERY \ Get next line + >IN @ #IB @ >= IF \ End of TIB? + QUERY-INPUT \ Get next line THEN - TIB >IN @ + @ 1 >IN +! - DUP [CHAR] ( = IF \ open paren? + IB >IN @ + @ 1 >IN +! + DUP [CHAR] ( = IF \ open paren? DROP \ drop the open paren 1+ \ depth increases ELSE - [CHAR] ) = IF \ close paren? - 1- \ depth decreases + [CHAR] ) = IF \ close paren? + 1- \ depth decreases THEN THEN DUP 0= UNTIL \ continue until we reach matching close paren, depth 0 diff --git a/src/lib_5_strings.4th b/src/lib_5_strings.4th index bc2d69c..56a2174 100644 --- a/src/lib_5_strings.4th +++ b/src/lib_5_strings.4th @@ -45,11 +45,11 @@ 0 , ( dummy length - we don't know what it is yet ) BEGIN - >IN @ #TIB @ >= IF \ End of TIB? - QUERY \ Get next line + >IN @ #IB @ >= IF \ End of TIB? + QUERY-INPUT \ Get next line THEN - TIB >IN @ + @ 1 >IN +! \ Get char from TIB + IB >IN @ + @ 1 >IN +! \ Get char from TIB DUP [CHAR] " <> WHILE @@ -73,11 +73,11 @@ right-paren to the terminal. ) : .( BEGIN - >IN @ #TIB @ >= IF \ End of TIB? - QUERY \ Get next line + >IN @ #IB @ >= IF \ End of TIB? + QUERY-INPUT \ Get next line THEN - TIB >IN @ + @ 1 >IN +! \ Get char from TIB + IB >IN @ + @ 1 >IN +! \ Get char from TIB DUP [CHAR] ) = IF DROP ( drop the double quote character ) -- 2.20.1