en:pfw:library
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:pfw:library [2024-08-25 20:21] – [The library is just bare ASCII] willem | en:pfw:library [2025-02-06 17:59] (current) – [A library for Forth] willem | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== A library for Forth ====== | ====== A library for Forth ====== | ||
+ | |||
+ | *W.O.* | ||
Line 9: | Line 11: | ||
The interface consists of the following word: '' | The interface consists of the following word: '' | ||
- | * '' | + | * '' |
An optional word as suggested by Ulrich Hoffmann | An optional word as suggested by Ulrich Hoffmann | ||
Line 15: | Line 17: | ||
Optional words as suggested by Willem Ouwerkerk | Optional words as suggested by Willem Ouwerkerk | ||
- | * '' | + | * '' |
+ | * '' | ||
* '' | * '' | ||
+ | * '' | ||
===== Pseudo code ===== | ===== Pseudo code ===== | ||
Line 34: | Line 38: | ||
Function: .LIB ( -- ) | Function: .LIB ( -- ) | ||
Print all chapter headers in a human readable form. | Print all chapter headers in a human readable form. | ||
+ | | ||
+ | Function: VIEW ( " | ||
+ | When the keyword " | ||
+ | otherwise issue an error message | ||
| | ||
Function: FROM ( " | Function: FROM ( " | ||
Line 60: | Line 68: | ||
They act as separators and are not part of the text itself. | They act as separators and are not part of the text itself. | ||
- | The '' | + | The '' |
- | Note that: The library starts with a dummy '' | + | |
- | * '' | + | * chapter-1 '' |
The '' | The '' | ||
* line-1 '' | * line-1 '' | ||
- | The first line of each library chapter starts with case insensitive keyword(s) separated by spaces. The word '' | + | The first line of each library chapter starts with a case insensitive keyword. The word '' |
backslash to prevent the keywords for being executed while loading a chapter. Then it adds all source code until the ''< | backslash to prevent the keywords for being executed while loading a chapter. Then it adds all source code until the ''< | ||
The end of the library is kept in a pointer that is updated when we extend the library.\\ | The end of the library is kept in a pointer that is updated when we extend the library.\\ | ||
Line 95: | Line 103: | ||
: 2TUCK 2swap 2over ; ( x1 x2 x3 x4 -- x3 x4 x1 x2 x3 x4 )<0D> | : 2TUCK 2swap 2over ; ( x1 x2 x3 x4 -- x3 x4 x1 x2 x3 x4 )<0D> | ||
<09> | <09> | ||
- | |||
\ 2ROT< | \ 2ROT< | ||
\ Rotate third double to top of stack< | \ Rotate third double to top of stack< | ||
: 2ROT 2>r 2swap 2r> 2swap ;<0D> | : 2ROT 2>r 2swap 2r> 2swap ;<0D> | ||
<09> | <09> | ||
- | |||
\ -2ROT< | \ -2ROT< | ||
\ Rotate top double to third double position on the stack< | \ Rotate top double to third double position on the stack< | ||
Line 110: | Line 116: | ||
< | < | ||
- | Function: NEEDED | + | Function: NEEDED |
a) Save the string a +n and make the string uppercase | a) Save the string a +n and make the string uppercase | ||
b) If this string is already present in the dictionary, do nothing | b) If this string is already present in the dictionary, do nothing | ||
Line 117: | Line 123: | ||
e) When a keyword is not found issue an error message | e) When a keyword is not found issue an error message | ||
| | ||
- | Function: NEED ( " | + | Function: NEED |
a) Parse the next word form the input stream and perform the function of NEEDED | a) Parse the next word form the input stream and perform the function of NEEDED | ||
Line 273: | Line 279: | ||
</ | </ | ||
+ | ===== A viewer for the library ===== | ||
+ | |||
+ | This viewer shows 16 or less lines at a time. | ||
+ | It stops at a <09> character, prints a divider line and waits for user input. | ||
+ | When the spacebar was hit, it goes on displaying the next chapter. | ||
+ | Any other key leaves the viewer. | ||
+ | |||
+ | <code forth> | ||
+ | ( A library consists of 09 {tab}, 0D {cr} and ASCII chars ) | ||
+ | ( ranging from blank to ~ others chars are not allowed ) | ||
+ | \ Show the library source code of a chapter | ||
+ | : .LINE ( a1 -- a2 ch ) begin c@+ dup BL < 0= while emit repeat ; | ||
+ | : .DIVIDER | ||
+ | |||
+ | : LIBTYPE | ||
+ | begin | ||
+ | 10 0 do \ Max. 16 lines at a time | ||
+ | cr .line 09 = \ End of source chapter? | ||
+ | if .divider leave then \ Show divider & ready | ||
+ | loop | ||
+ | dup libhere < while \ More library source to be done? | ||
+ | key BL <> if drop exit then \ Yes, ask key, stop on non space | ||
+ | repeat | ||
+ | |||
+ | : VIEW ( " | ||
+ | |||
+ | </ | ||
===== A sample library source ===== | ===== A sample library source ===== | ||
This examples shows how many different code parts can be used inside the library.\\ | This examples shows how many different code parts can be used inside the library.\\ | ||
Line 291: | Line 324: | ||
chapter RESTORE-LIB | chapter RESTORE-LIB | ||
v: inside | v: inside | ||
- | | + | |
cr .( Libhere ) libhere u. | cr .( Libhere ) libhere u. | ||
cr .( Size ) libhere lib - dm . .( bytes ) | cr .( Size ) libhere lib - dm . .( bytes ) | ||
- | freeze | ||
v: fresh | v: fresh | ||
%% | %% | ||
Line 352: | Line 384: | ||
\ RESTORE-LIB | \ RESTORE-LIB | ||
v: inside | v: inside | ||
- | | + | |
cr .( Libhere ) libhere u. | cr .( Libhere ) libhere u. | ||
cr .( Size ) libhere lib - dm . .( bytes ) | cr .( Size ) libhere lib - dm . .( bytes ) | ||
- | freeze | ||
v: fresh | v: fresh | ||
Line 397: | Line 428: | ||
==== A little extra ==== | ==== A little extra ==== | ||
- | Loading a lot of library code in one go. it is used like this: '' | + | Loading a lot of library code in one go. It must be used like this: '' |
Note that '' | Note that '' | ||
en/pfw/library.1724610098.txt.gz · Last modified: 2024-08-25 20:21 by willem