commit 3a312bd9d052ceeff9e70c95b096796b236821e4
parent 7d3d6050d090bc5bb759b188deec9a787348de00
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 28 Jun 2022 20:18:39 -0400
Add lib/io, which defines stdin
Also, add [f<] in lib/file which facilitates the override of stdin with an
opened file.
Make cc use stdin as a source input.
We're getting somewhere...
Diffstat:
7 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/fs/cc/cc1.fs b/fs/cc/cc1.fs
@@ -1,9 +1,10 @@
\ C compiler stage 1
\ Requires cc/gen.fs, cc/ast.fs, asm.fs and wordtbl.fs
-\ Compiles input coming from the cc< alias (defaulting to in<) and writes the
+\ Compiles input coming from the stdin alias and writes the
\ result to here. Aborts on error.
: cc1, ( -- )
xhere$ xhere[ parseast curunit _debug if dup printast nl> then ]xhere
gennode ;
-: cc1<< ( -- ) ccopen cc1, ccclose ;
+
+: cc1<< ( -- ) word fopen dup [f<] to stdin cc1, fclose ;
diff --git a/fs/cc/io.fs b/fs/cc/io.fs
@@ -1,9 +1,6 @@
\ C compiler I/O words
-0 value ccfd
-: cc< ccfd fread ;
+alias stdin cc<
0 value putback
: _cc< ( -- c ) putback ?dup if 0 to putback else cc< then ;
-: ccopen word fopen to ccfd ;
-: ccclose ccfd fclose 0 to ccfd ;
diff --git a/fs/init.fs b/fs/init.fs
@@ -3,6 +3,7 @@ f<< lib/core.fs
f<< lib/dict.fs
f<< lib/annotate.fs
f<< sys/doc.fs
+f<< lib/io.fs
f<< sys/scratch.fs
f<< lib/file.fs
f<< lib/nfmt.fs
diff --git a/fs/lib/file.fs b/fs/lib/file.fs
@@ -2,6 +2,11 @@
\ requires sys/scratch
: fopen ( fname -- fd ) str>zstr zfopen ;
+\ 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 )
+ scratch[ litn compile fread exit, ]scratch ;
+
\ Autoloading
\ entries in the floaded list have both a length byte and
\ a nul termination byte. the nul termination is used only
diff --git a/fs/lib/io.fs b/fs/lib/io.fs
@@ -0,0 +1,7 @@
+\ I/O aliases
+
+\ Defines stdin (and soon stdout) which is used by many programs and words as
+\ their main input output. In addition to those words, this subsystem also
+\ implements some convenience words to manage where they point to.
+
+alias in< stdin ( -- c )
diff --git a/fs/sys/scratch.fs b/fs/sys/scratch.fs
@@ -7,6 +7,7 @@
\ at the end of the buffer, it goes back to the beginning.
$4000 value scratchsize
+0 value _here
create scratch( scratchsize allot
: scratch) scratch( scratchsize + ;
\ pointer to current position in buffer
@@ -22,3 +23,9 @@ scratch( value scratch>
\ transforms str into a null-terminated string
: str>zstr ( str -- zstr )
c@+ dup 1+ scratchallot0 ( str+1 u a ) dup >r swap move r> ;
+
+\ Open a scratch area for writing
+: scratch[ ( -- ) here to _here scratch> to here ;
+\ Stop writing to the scratch area and restore here
+\ Returs the address of the beginning of the written area
+: ]scratch ( -- a ) scratch> here to scratch> _here to here ;
diff --git a/fs/tests/cc/ast.fs b/fs/tests/cc/ast.fs
@@ -2,7 +2,7 @@
?f<< cc/cc.fs
testbegin
\ Tests for the C compiler AST
-ccopen tests/cc/test.c parseast ccclose
+S" tests/cc/test.c" fopen dup [f<] to stdin parseast fclose
curunit firstchild dup nodeid AST_FUNCTION #eq ( fnode )
dup ast.func.name S" retconst" s= #