~vdupras/duskos

0c9a2b553c59f74be6b0e158b41249edb0e7276c — Virgil Dupras a month ago f7c3ef6
getc now returns -1 when EOF is reached

This whole "c-or-0" pattern works as long as we don't deal with binary files.
But otherwise, this pattern doesn't work.
7 files changed, 27 insertions(+), 10 deletions(-)

M dusk.asm
M fs/cc/tok.fs
M fs/fs/boot.fs
M fs/init.fs
R fs/{lib/file.fs => sys/file.fs}
M fs/tests/fs/boot.fs
M fs/xcomp/bootlo.fs
M dusk.asm => dusk.asm +2 -2
@@ 656,8 656,8 @@ defword 'maybeword', 9, word_maybeword
_word_loop1:
	call [inrd]	; ( -- c )
	pspop eax
	cmp eax, 0x05	; is EOF?
	jc _word_eof
	test eax, eax
	js _word_eof
	cmp eax, 0x21	; is ws?
	jc _word_loop1
	mov ebx, curword+1

M fs/cc/tok.fs => fs/cc/tok.fs +1 -1
@@ 53,7 53,7 @@ create _ 10 c, ," 09AZaz__$$"
\ advance to the next non-whitespace and return the char encountered.
\ if end of stream is reached, c is 0
: tonws ( -- c ) begin ( )
    _cc< dup EOF > while dup ws? while drop repeat
    _cc< dup 0>= while dup ws? while drop repeat
      ( c ) else ( EOF ) drop 0 then ;

: _writesym ( c3? c2? c1 len -- str )

M fs/fs/boot.fs => fs/fs/boot.fs +2 -2
@@ 135,8 135,8 @@ create fcursors( FCursorSize FCURSORCNT * allot0

: fat16open ( path -- fcursor ) findpath openfile ;

: fat16getc ( fcursor -- c-or-0 )
  dup FCUR_pos over FCUR_size = if drop 0 exit then
: fat16getc ( fcursor -- c )
  dup FCUR_pos over FCUR_size = if drop -1 exit then
  dup FCUR_pos+ ClusterSize mod over FCUR_buf( + c@ ( fc c )
  over FCUR_pos ClusterSize mod not if ( fc c ) \ end of cluster, read next
    over FCUR_cluster nextcluster ( fc c cluster )

M fs/init.fs => fs/init.fs +1 -1
@@ 10,7 10,7 @@ RAMDRVSECSZ to drvblksz
fatfs( to ramdrv(
' ramdrv@ to (drv@)
' ramdrv! to (drv!)
f<< lib/file.fs
f<< sys/file.fs
f<< lib/nfmt.fs
f<< lib/diag.fs
f<< sys/xhere.fs

R fs/lib/file.fs => fs/sys/file.fs +19 -1
@@ 1,5 1,23 @@
\ File I/O
\ File subsystem
\ requires sys/scratch

\ This subsystems defines a "filesystem" protocol and upon it defines
\ convenience word around files. This subsystem is a bit weird because the
\ boot sequence has already defined 3 of those words from the protocol. They
\ are:

\ fopen ( path -- fcursor )
\   Open file at path and return a cursor through which other file-related word
\   identify the target file. Once a file isn't used anymore, it should be
\   closed with fclose. Aborts on error.

\ fclose ( fcursor -- )
\   Close cursor fcursor and free its ressources.

\ fgetc ( fcursor -- c )
\   Read a single character from fcursor. If the end of file (EOF) is reached,
\   returns -1.

\ This creates a "f<" reader with the file descriptor embedded in it. This
\ allows for a straightforward override of input/output words.
: [f<] ( curfd -- word )

M fs/tests/fs/boot.fs => fs/tests/fs/boot.fs +1 -1
@@ 18,6 18,6 @@ dup $fd readN ( fcursor )
dup fat16getc 'b' #eq
dup $dfc readN ( fcursor )
dup fat16getc 'E' #eq dup fat16getc 'O' #eq dup fat16getc 'F' #eq
dup fat16getc 0 #eq
dup fat16getc -1 #eq
fat16close
testend

M fs/xcomp/bootlo.fs => fs/xcomp/bootlo.fs +1 -2
@@ 59,8 59,7 @@
: =><= ( n l h -- f ) over - rot> ( h n l ) - >= ;

\ Emitting
$20 const SPC $0d const CR $0a const LF
$08 const BS  $04 const EOF
$20 const SPC $0d const CR $0a const LF $08 const BS
: nl> CR emit LF emit ; : spc> SPC emit ;
\ emit all chars of "str"
: stype ( str -- ) c@+ rtype ;