eofFlag = endswith(line, '\n') ? 0 : -1
line = chomp(line)
- println("Reading: $line");
+ #println("Reading: $line");
putString(line, addr, maxSize)
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()
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)
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
: ( 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
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
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 )