commit 536e723f9eac7b081bdb30773f5afea9a9c0606d
parent c0619bcc7fbd7cdfe3fec9f396f99ec3356bd4f4
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 1 Jun 2022 16:22:02 -0400
Simplify in< by removing in<?
Also, replace readfile with a more flexible fopen+f< mechanism, which then
overrides in< instead of key.
Diffstat:
5 files changed, 53 insertions(+), 32 deletions(-)
diff --git a/aliases.txt b/aliases.txt
@@ -30,7 +30,6 @@ in> inptr
<< shl
>> shr
boot< bootrd
-in<? inrdc
in< inrd
[c]? findchar
[]= rangeeq
@@ -39,3 +38,4 @@ in< inrd
' apos
; compstop
: docolon
+f< fread
diff --git a/boot.fs b/boot.fs
@@ -44,22 +44,27 @@ current to (psufl)
S" -- " stype stack? psdump ;
: create entry compile (cell) ;
: value entry compile (val) , ;
-64 value LNSZ
+64 value LNSZ
create in( LNSZ allot
: in) in( 64 + ;
: bs? BS over = swap $7f = or ;
+\ only emit c if it's within the visible ascii range
+: emitv ( c -- ) dup SPC - $5f < if emit else drop then ;
: lntype ( ptr c -- ptr+1 f )
dup bs? if ( ptr c )
drop dup in( > if 1- BS emit then spc> BS emit 0
else ( ptr c ) \ non-BS
- dup SPC < if drop dup in) over - 0 fill 1 else
- tuck emit c!+ dup in) = then then ;
-: rdln S" ok" stype nl> in( begin key lntype until drop nl> ;
+ dup emitv dup rot c!+ ( c ptr+1 ) dup in) = rot SPC < or ( ptr+1 f )
+ then ;
+: rdln
+ in( LNSZ SPC fill S" ok" stype nl>
+ in( begin key lntype until drop nl> ;
: rdln<? ( -- c-or-0 )
in> in) < if in> c@+ swap to in> else 0 then ;
: rdln< ( -- c ) rdln<? ?dup not if
rdln in( to in> SPC then ;
-: rdln$ ['] rdln< to in< ['] rdln<? to in<?
- in) to in> 'curword 6 0 fill ;
+: rdln$ ['] rdln< to in< in) to in> ;
+: fin< f< ?dup not if ['] rdln< to in< SPC then ;
+: f<< word fopen not if S" Can't open" stype abort then ['] fin< to in< ;
: init S" Dusk OS" stype rdln$ ;
init
diff --git a/dusk.asm b/dusk.asm
@@ -106,7 +106,6 @@ compiling: resd 1
curword: resb 0x20 ; 1b len, then contents
inptr: resd 1 ; in>
inrd: resd 1 ; in<
-inrdc: resd 1 ; in<?
wnf: resd 1 ; (wnf)
psufl: resd 1 ; (psufl)
curfd: resd 1 ; file descriptor being read by key
@@ -128,10 +127,9 @@ _start:
mov dword [current], word_lastxcomp
mov dword [inptr], bootsrc
mov dword [inrd], word_bootrd
- mov dword [inrdc], word_bootrd
mov dword [wnf], word_bye
mov dword [psufl], word_bye
- mov dword [curfd], 0 ; stdin
+ mov dword [curfd], 0 ; nothing opened
mov eax, SYSCALL_CHDIR
mov ebx, rootfspath
int 0x80
@@ -229,30 +227,29 @@ defword 'emit', 4, word_emit, word_nextroutine
defword 'key', 3, word_key, word_emit
pspush 0
mov eax, SYSCALL_READ
- mov ebx, [curfd]
+ mov ebx, 0 ; stdin
mov ecx, ebp ; buffer
mov edx, 1 ; len
int 0x80
- dec eax ; close and back to stdin if eax is 0 (eof) or -1
- jns _key_success
- ; error, close and go back to stdin
+ ret
+
+; ( sa sl -- f )
+; open specified file in rootfs and set curfd to its file descriptor
+; if there was a file opened, close it.
+; f is false if there was an error during open().
+defword 'fopen', 5, word_fopen, word_key
+ test dword [curfd], -1
+ jz _fopen_noclose
mov eax, SYSCALL_CLOSE
mov ebx, [curfd]
int 0x80
mov dword [curfd], 0
-_key_success:
- ret
-
-; ( sa sl -- f )
-; open specified file in rootfs and have "key" go through it all, then go back
-; to stdin once it's through. f is false if there was an error during open().
-defword 'readfile', 8, word_readfile, word_key
- mov esi, scratchpad ; dst
- mov eax, [ebp] ; sl
- mov byte [esi+eax], 0 ; null terminate
- mov [ebp], esi ; dst
- pspush eax
- call word_move
+_fopen_noclose:
+ pspop ecx ; sl
+ pspop esi ; src
+ mov edi, scratchpad ; dst
+ mov byte [edi+ecx], 0 ; null terminate
+ rep movsb
mov eax, SYSCALL_OPEN
mov ebx, scratchpad
xor ecx, ecx ; flags
@@ -260,14 +257,32 @@ defword 'readfile', 8, word_readfile, word_key
int 0x80
xor ebx, ebx ; f value
test eax, eax
- js _readfile_error
+ js _fopen_error
mov [curfd], eax
inc ebx
-_readfile_error:
+_fopen_error:
pspush ebx
ret
-defword 'drop', 4, word_drop, word_readfile
+defword 'f<', 2, word_fread, word_fopen
+ pspush 0
+ test dword [curfd], -1
+ jnz _fread_hasfd
+ ret ; no fd, no read!
+_fread_hasfd:
+ mov eax, SYSCALL_READ
+ mov ebx, [curfd]
+ mov ecx, ebp ; buffer
+ mov edx, 1 ; len
+ int 0x80
+ test eax, eax
+ jns _fread_success
+ mov dword [ebp], 0 ; return 0 if negative
+_fread_success:
+ ret
+
+
+defword 'drop', 4, word_drop, word_fread
add ebp, 4
ret
diff --git a/fs/hello.fs b/fs/hello.fs
@@ -0,0 +1,2 @@
+42 .x
+
diff --git a/xcomp.txt b/xcomp.txt
@@ -31,12 +31,11 @@ alias (psufl)
: move, ( a u -- ) here over allot swap move ;
: ws? SPC <= ;
: boot< in> c@+ swap to in> ;
-alias in<?
alias in<
: curword ( -- sa sl ) 'curword 1+ 'curword c@ ;
: word ( -- sa sl )
A>r 0 begin drop in< dup ws? not until ( c )
- 'curword 1+ >A begin ( c ) Ac!+ in<? dup ws? until drop
+ 'curword 1+ >A begin ( c ) Ac!+ in< dup ws? until drop
A> 'curword - 1- ( len ) 'curword c! curword r>A ;
: [c]? ( c a u -- i )
?dup not if 2drop -1 exit then A>r over >r >r >A ( c )