1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- \ Print lines from a text file.
- \ This prints lines from a file, but does not put them on
- \ the stack.
- 256 Constant max-bytes-line
- \ Create a buffer of appropriate size for lines. Add 2 bytes
- \ to the buffer size for CR LF bytes.
- Create line-buffer max-bytes-line 2 + allot
- s" input" r/o open-file throw Value puzzle-input-handle
- : print-line ( line-buffer -- )
- dup max-bytes-line puzzle-input-handle read-line throw
- if
- line-buffer swap
- then ;
- \ given a line-buffer, print-line will print one line per call
- line-buffer
- print-line
- print-line
- print-line
- \ Finally close the file.
- puzzle-input-handle close-file
- \ ================
- \ Interpret a file
- \ ================
- \ This can be useful, if the file contains only words which
- \ are valid Forth words and the input file is
- \ trusted. However, this discards any whitespace. If
- \ whitespace of the input file is significant, this method
- \ will fail.
- s" my-trusted-file" included
- .s
- \ ======================
- \ Read numbers from file
- \ ======================
- 256 Constant max-bytes-line
- Create line-buffer max-bytes-line 2 + allot
- s" input" r/o open-file throw Value puzzle-input-handle
- : add-nums-from-file ( c-addr length -- ??? )
- begin
- \ Read the next line from the puzzle input.
- line-buffer max-bytes-line puzzle-input-handle read-line throw
- \ stack: length eof-flag
- swap
- \ stack: eof-flag length
- \ Check if more than 0 characters were read as a number.
- dup 0 >
- \ stack: eof-flag length more-than-0-chars-flag
- rot
- \ stack: length more-than-0-chars-flag eof-flag
- \ Check that both true.
- and
- \ stack: length flag
- while
- \ stack: length
- \ Put a float at the bottom for >number which requires a
- \ double to be at specific position.
- 0.
- \ stack: length 0. 0
- rot
- \ stack: 0. 0 length
- line-buffer swap
- \ stack: 0. 0 length c-addr
- >number
- 2swap
- \ convert double to one integer number (?)
- d>s
- \ Use the numbers here. For now only dropping them.
- . drop drop
- repeat ;
- puzzle-input-handle close-file
|