mirror of
https://git.sr.ht/~crc_/retroforth
synced 2024-11-16 19:48:56 +01:00
source -> interfaces; remove tanu
FossilOrigin-Name: 4bba4776c0c7e6b1a336592eb50beaff4a81a70d8c20be376e12322ef5bfd16c
This commit is contained in:
parent
9d4392ace7
commit
af66c0079e
8 changed files with 202 additions and 44 deletions
12
Makefile
12
Makefile
|
@ -9,7 +9,6 @@ clean:
|
|||
rm -f bin/repl
|
||||
rm -f bin/extend
|
||||
rm -f bin/muri
|
||||
rm -f bin/tanu
|
||||
|
||||
first:
|
||||
cd tools && make
|
||||
|
@ -21,10 +20,11 @@ then:
|
|||
image:
|
||||
./bin/muri literate/Rx.md
|
||||
./bin/extend literate/RetroForth.md
|
||||
cp ngaImage source
|
||||
cd source && ../bin/extend rre.forth
|
||||
cd source && ../bin/embedimage >image.c
|
||||
rm source/ngaImage
|
||||
cp ngaImage interfaces
|
||||
cd interfaces && ../bin/extend rre.forth
|
||||
cd interfaces && ../bin/embedimage >image.c
|
||||
rm interfaces/ngaImage
|
||||
|
||||
finally:
|
||||
cd source && make
|
||||
cd interfaces && make
|
||||
|
||||
|
|
196
interfaces/rre.forth
Normal file
196
interfaces/rre.forth
Normal file
|
@ -0,0 +1,196 @@
|
|||
~~~
|
||||
:n:to-float (n-_f:-n) #0 `-6000 ;
|
||||
:s:to-float (s-_f:-n) #1 `-6000 ;
|
||||
:f:to-string (f:n-__-s) s:empty dup #2 `-6000 ;
|
||||
:f:+ (f:ab-c) #3 `-6000 ;
|
||||
:f:- (f:ab-c) #4 `-6000 ;
|
||||
:f:* (f:ab-c) #5 `-6000 ;
|
||||
:f:/ (f:ab-c) #6 `-6000 ;
|
||||
:f:floor (f:ab-c) #7 `-6000 ;
|
||||
:f:eq? (f:ab-c) #8 `-6000 ;
|
||||
:f:-eq? (f:ab-c) #9 `-6000 ;
|
||||
:f:lt? (f:ab-c) #10 `-6000 ;
|
||||
:f:gt? (f:ab-c) #11 `-6000 ;
|
||||
:f:depth (-n) #12 `-6000 ;
|
||||
:f:dup (f:a-aa) #13 `-6000 ;
|
||||
:f:drop (f:a-) #14 `-6000 ;
|
||||
:f:swap (f:ab-ba) #15 `-6000 ;
|
||||
:f:over (f:ab-aba) f:to-string f:dup s:to-float f:swap ;
|
||||
:f:tuck (f:ab-bab) f:swap f:over ;
|
||||
:f:positive? #0 n:to-float f:gt? ;
|
||||
:f:negative? #0 n:to-float f:lt? ;
|
||||
:f:negate #-1 n:to-float f:* ;
|
||||
:f:abs f:dup f:negative? [ f:negate ] if ;
|
||||
:f:log (f:ab-c) #16 `-6000 ;
|
||||
:f:power (f:ab-c) #17 `-6000 ;
|
||||
:f:to-number (f:a-__-n) #18 `-6000 ;
|
||||
:prefix:. (s-__f:-f)
|
||||
compiling? [ s:keep ] [ s:temp ] choose &s:to-float class:word ; immediate
|
||||
:putf (f:-) f:to-string puts ;
|
||||
~~~
|
||||
|
||||
~~~
|
||||
:getc (-c) `1001 ;
|
||||
~~~
|
||||
|
||||
RETRO now has Gopher support via `gopher:get`.
|
||||
|
||||
Takes:
|
||||
|
||||
destination
|
||||
server name
|
||||
port
|
||||
selector
|
||||
|
||||
Returns:
|
||||
|
||||
number of characters read
|
||||
|
||||
~~~
|
||||
:gopher:get `-6200 ;
|
||||
~~~
|
||||
|
||||
~~~
|
||||
:sys:argc (-n) `-6100 ;
|
||||
:sys:argv (n-s) s:empty swap `-6101 ;
|
||||
~~~
|
||||
|
||||
This implements words for interfacing with the POSIX file I/O words if
|
||||
you are using an interface supporting them. All of these are in the
|
||||
`file:` namespace.
|
||||
|
||||
These are pretty much direct wrappers for fopen(), fclose(), etc.
|
||||
|
||||
First up, constants for the file modes.
|
||||
|
||||
| # | Used For |
|
||||
| - | ------------------ |
|
||||
| R | Mode for READING |
|
||||
| W | Mode for WRITING |
|
||||
| A | Mode for APPENDING |
|
||||
|
||||
~~~
|
||||
#0 'file:R const
|
||||
#1 'file:W const
|
||||
#2 'file:A const
|
||||
#3 'file:R+ const
|
||||
~~~
|
||||
|
||||
For opening a file, provide the file name and mode. This will return a
|
||||
number identifying the file handle.
|
||||
|
||||
~~~
|
||||
:file:open (sm-h) `118 ;
|
||||
~~~
|
||||
|
||||
Given a file handle, close the file.
|
||||
|
||||
~~~
|
||||
:file:close (h-) `119 ;
|
||||
~~~
|
||||
|
||||
Given a file handle, read a character.
|
||||
|
||||
~~~
|
||||
:file:read (h-c) `120 ;
|
||||
~~~
|
||||
|
||||
Write a character to an open file.
|
||||
|
||||
~~~
|
||||
:file:write (ch-) `121 ;
|
||||
~~~
|
||||
|
||||
Return the current pointer within a file.
|
||||
|
||||
~~~
|
||||
:file:tell (h-n) `122 ;
|
||||
~~~
|
||||
|
||||
Move the file pointer to the specified location.
|
||||
|
||||
~~~
|
||||
:file:seek (nh-) `123 ;
|
||||
~~~
|
||||
|
||||
Return the size of the opened file.
|
||||
|
||||
~~~
|
||||
:file:size (h-n) `124 ;
|
||||
~~~
|
||||
|
||||
Given a file name, delete the file.
|
||||
|
||||
~~~
|
||||
:file:delete (s-) `125 ;
|
||||
~~~
|
||||
|
||||
Flush pending writes to disk.
|
||||
|
||||
~~~
|
||||
:file:flush (f-) `126 ;
|
||||
~~~
|
||||
|
||||
Given a file name, return `TRUE` if it exists or `FALSE` otherwise.
|
||||
|
||||
~~~
|
||||
:file:exists? (s-f)
|
||||
file:R file:open dup n:-zero?
|
||||
[ file:close TRUE ]
|
||||
[ drop FALSE ] choose ;
|
||||
~~~
|
||||
|
||||
With that out of the way, we can begin building higher level functionality.
|
||||
|
||||
The first of these reads a line from the file. This is read to `here`; move
|
||||
it somewhere safe if you need to keep it around.
|
||||
|
||||
The second goes with it. The `for-each-line` word will invoke a combinator
|
||||
once for each line in a file. This makes some things trivial. E.g., a simple
|
||||
'cat' implementation could be as simple as:
|
||||
|
||||
'filename [ puts nl ] file:for-each-line
|
||||
|
||||
~~~
|
||||
{{
|
||||
'FID var
|
||||
'FSize var
|
||||
'Action var
|
||||
'Buffer var
|
||||
:-eof? (-f) @FID file:tell @FSize lt? ;
|
||||
:preserve (q-) &FID [ &FSize [ call ] v:preserve ] v:preserve ;
|
||||
---reveal---
|
||||
:file:read-line (f-s)
|
||||
!FID
|
||||
[ here dup !Buffer buffer:set
|
||||
[ @FID file:read dup buffer:add
|
||||
[ ASCII:CR eq? ] [ ASCII:LF eq? ] [ ASCII:NUL eq? ] tri or or ] until
|
||||
buffer:get drop ] buffer:preserve
|
||||
@Buffer ;
|
||||
|
||||
:file:for-each-line (sq-)
|
||||
[ !Action
|
||||
file:R file:open !FID
|
||||
@FID file:size !FSize
|
||||
[ @FID file:read-line @Action call -eof? ] while
|
||||
@FID file:close
|
||||
] preserve ;
|
||||
}}
|
||||
~~~
|
||||
|
||||
`file:slurp` reads a file into a buffer.
|
||||
|
||||
~~~
|
||||
{{
|
||||
'FID var
|
||||
'Size var
|
||||
---reveal---
|
||||
:file:slurp (as-)
|
||||
[ file:R file:open !FID
|
||||
buffer:set
|
||||
@FID file:size !Size
|
||||
@Size [ @FID file:read buffer:add ] times
|
||||
@FID file:close
|
||||
] buffer:preserve ;
|
||||
}}
|
||||
~~~
|
|
@ -8,12 +8,10 @@ objects:
|
|||
$(CC) $(OPTS) -c extend.c -o extend.o
|
||||
$(CC) $(OPTS) -c unu.c -o unu.o
|
||||
$(CC) $(OPTS) -c muri.c -o muri.o
|
||||
$(CC) $(OPTS) -c tanu.c -o tanu.o
|
||||
|
||||
link:
|
||||
$(CC) $(LIBS) unu.o -o unu
|
||||
$(CC) $(LIBS) muri.o -o muri
|
||||
$(CC) $(LIBS) tanu.o -o tanu
|
||||
$(CC) $(LIBS) embedimage.o -o embedimage
|
||||
$(CC) $(LIBS) extend.o -o extend
|
||||
|
||||
|
@ -22,5 +20,4 @@ finish:
|
|||
mv extend ../bin
|
||||
mv unu ../bin
|
||||
mv muri ../bin
|
||||
mv tanu ../bin
|
||||
rm *.o
|
||||
|
|
35
tools/tanu.c
35
tools/tanu.c
|
@ -1,35 +0,0 @@
|
|||
/* ____ ____ ______ ____ ___
|
||||
|| \\ || | || | || \\ // \\
|
||||
||_// ||== || ||_// (( ))
|
||||
|| \\ ||___ || || \\ \\_//
|
||||
a personal, minimalistic forth
|
||||
|
||||
This converts a text file into a C character array.
|
||||
|
||||
Copyright (c) 2016, 2017 Charles Childers
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
void include_file(char *fname) {
|
||||
int ch;
|
||||
FILE *fp;
|
||||
fp = fopen(fname, "r");
|
||||
if (fp == NULL)
|
||||
return;
|
||||
while (!feof(fp)) {
|
||||
ch = getc(fp);
|
||||
printf("%d, ", ch);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
printf("char %s[] = {", argv[1]);
|
||||
include_file("/dev/stdin");
|
||||
printf("0 };\n");
|
||||
}
|
Loading…
Reference in a new issue