User Tools

Site Tools


en:pfw:library

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:pfw:library [2025-03-20 16:45] – [A little extra] willemen:pfw:library [2025-04-01 15:56] (current) – [noForth t library use] willem
Line 1: Line 1:
-====== A library for Forth ======+====== A library for (no)Forth ======
  
-*W.O.*+*W.O. 2025*
  
  
Line 14: Line 14:
  
 An optional word as suggested by Ulrich Hoffmann An optional word as suggested by Ulrich Hoffmann
-  * ''%%FROM%%'' ''%%( "name" -- i*x )%%'' Open the library "name" for use with ''%%NEED%%''+  * ''%%FROM%%'' ''%%( "name" -- )%%'' Open the library "name" for use with ''%%NEED%%''
  
 Optional words as suggested by Willem Ouwerkerk Optional words as suggested by Willem Ouwerkerk
   * ''%%NEEDED%%''  ''%%( i*x a b -- j*x )%%'' Make sure that name represented by the string a b is present in the dictionary. If not, load it from a library. If name already exists, do nothing.   * ''%%NEEDED%%''  ''%%( i*x a b -- j*x )%%'' Make sure that name represented by the string a b is present in the dictionary. If not, load it from a library. If name already exists, do nothing.
   * ''%%RUN%%'' ''%%( i*x "name" -- j*x )%%'' Find "name" in the library and load & run the script from a library.   * ''%%RUN%%'' ''%%( i*x "name" -- j*x )%%'' Find "name" in the library and load & run the script from a library.
-  * ''%%CHAPTERS%%''  ''%%( -- )%%'' Show all section/chapter headers of the current library in one or more columns +  * ''%%CHAPTERS%%''  ''%%( -- )%%'' Show all chapter headers of the current library 
-  * ''%%LOOK-AT%%'' ''%%( "name" -- )%%'' Look at the code from the library, that belongs to "name".+  * ''%%LOOK-AT%%'' ''%%( "name" -- )%%'' Look at the code of chapter “name” from the active library.
  
 ===== Pseudo code ===== ===== Pseudo code =====
Line 161: Line 161:
 <code forth> <code forth>
   create KEYWORD  20 allot  \ Hold keyword   create KEYWORD  20 allot  \ Hold keyword
-  1008,1000 constant LIB    \ Library start address+  1008,1000 value LIB       \ Library start address
   LIB value LIBHERE         \ Current end address of library   LIB value LIBHERE         \ Current end address of library
  
-LIB-FIND    a +n –sa ) \ a,+n=name, sa=begin-source-section +FIND-CHAPTER adr len -- chap-adr )  Find chapter start address (A.N.) 
-    keyword place           Save keyword +    >r >r 
-    keyword count upper     \ Convert to uppercase +    libhere lib 
-    libhere  lib            \ Library address range +    begin begin begin 
-    begin +        scan                      To begin chapter 
-        2dup > 0= throw 2dup \ Nothing found? +        2dup < throw                    End library
-        0D scan nip over 1+ \ Get line with keywords, skip \ +        dup c@ r@ c@ = until            First letter ok? 
-        begin +        dup 2r@ over s<> 0= until       Name ok? 
-        2dup > while        Line not done+        dup 2r@ drop c@ ch ! < until  Length ok? 
-            bl skip dup > \ Skip leading spaces +    nip  2 - rdrop  rdrop ;
-            bl scan         After keyword +
-            r over r> -   \ Keyword length +
-            keyword count s<> 0= \ Keyword found, leave source address +
-            if  2drop nip exit +
-            then +
-        repeat +
-        2drop  09 scan  1+  \ Find library section, skip 09 +
-    again ;+
  
 : LIB-REFILL  ( source-id –- f )  : LIB-REFILL  ( source-id –- f ) 
Line 200: Line 192:
 : NEEDED      ( a +n –- i*x )  : NEEDED      ( a +n –- i*x ) 
     ['] lib-refill  to 'refill   \ Add library REFILL     ['] lib-refill  to 'refill   \ Add library REFILL
-    LIB-FIND keyword find nip 0= \ in the current forth search order+    find-chapter keyword find nip 0= \ in the current forth search order
     and lib-load ;     and lib-load ;
  
Line 263: Line 255:
  
 : OPEN-LIB      ( -- )  \ Open a lib. for writing, read incomplete lib. sector too! : OPEN-LIB      ( -- )  \ Open a lib. for writing, read incomplete lib. sector too!
-    libhere lib <> if               \ Lib. not empty? +    libhere lib <> if                     \ Lib. not empty? 
-        libhere 100 /mod  100 *     \ Get previous sector & ptr length +        libhere 100 /mod  100 *           \ Get previous sector & ptr length 
-        dup to libhere              \ Correct lib. pointer & calc. XIP address+        dup to libhere                    \ Correct lib. pointer & calc. XIP address
         buffer 100 move  to ptr  {w  exit \ Read uncomplete sector to buffer, open flash         buffer 100 move  to ptr  {w  exit \ Read uncomplete sector to buffer, open flash
-    then  0 to ptr  09 lc,  {W    \ First lib. entry, open flash+    then  0 to ptr  {W  09 lc, ;          \ First lib. entry, open flash
  
 : CLOSE-LIB     ( -- )      \ Close library, save unfinshed buffer too : CLOSE-LIB     ( -- )      \ Close library, save unfinshed buffer too
Line 303: Line 295:
     repeat  drop ;     repeat  drop ;
  
-: LOOK-AT   ( "name" -- )       bl word count  lib-find libtype ;+: LOOK-AT   ( "name" -- )       bl word count  find-chapter libtype ;
  
 </code> </code>
Line 374: Line 366:
 ===== The library is just bare ASCII ===== ===== The library is just bare ASCII =====
  
-When displayed the library chapters look like this:\\+When opened in an editor the library chapters look like this:\\
  
 <code forth> <code forth>
-\ VERSION VERSIE VSN+    \ VERSION VERSIE VSN
 cr .(    NEED version 0.33     ) cr .(    NEED version 0.33     )
 cr .(   Library version 0.22   ) cr .(   Library version 0.22   )
Line 441: Line 433:
 </code> </code>
  
-Loading a part from any library. This function has two parameter strings, therefore you can select the library where to code is taken from. This is a sample implementation:+Loading and viewing code chapter from any library.\\ 
 +This function has two strings as parameter, therefore you can select the library where to code is taken from too.\\ 
 +This is just a sample implementation:
  
 <code forth> <code forth>
 : NEED-FROM  ( i*x "library" "chapter" -- j*x ) : NEED-FROM  ( i*x "library" "chapter" -- j*x )
-    lib >r  from            \ Save library pointers, select library +    lib libhere 2>r  from          \ Save library pointers, select library 
-    ['] need  catch         \ Load code from that library, catch errors +    ['] need  catch                \ Load code from that library, catch errors 
-    r> to lib  throw ;      \ Restore saved library & throw error number+    2rto libhere to lib  throw ; \ Restore saved library & throw error number
 </code> </code>
  
 +Viewing code chapter from any library. It needs the library name as string parameter.\\
  
 +<code forth>
 +: CHAPTERS-FROM ( "library" -- )   \ Show chapters from any given library
 +    lib libhere 2> from          \ Save library pointers, select library
 +    chapters                       \ Show code chapters from that library
 +    2r> to libhere to lib ;        \ Restore saved library
 +</code>
  
 ==== More ==== ==== More ====
en/pfw/library.1742485522.txt.gz · Last modified: 2025-03-20 16:45 by willem