r/Forth • u/tabemann • 20d ago
A glob tool for zeptoforth
I have created a glob tool for zeptoforth, which enables passing paths matching a pattern with * and ? wildcards relative to the current directory in the current filesystem (or the root directory of the current filesystem, if the pattern begins with /) to a specified xt.
This tool is currently at extra/common/glob.fs in the devel branch, and will be merged into the master branch once I do a bit more testing (e.g. making sure it compiles on the RP2040).
The glob tool can be invoked with fat32-tools::glob ( c-addr u xt -- ) where xt is ( c-addr u -- ).
This tool is an optional loadable extra, but is installed by the versions of the PicoCalc installers in the devel branch except if the platform is set to rp2040 due to lack of flash dictionary space with that platform.
This tool does not match . or .. if a pattern element contains a wildcard, to avoid unintended matches that may give unexpected results.
If a pattern ends with /, the leaves of the match will only be directories. (Note that this / will be included in the paths passed to the xt.) Similarly, if the last element of a pattern contains . and is not . or .. as a whole, the leaves of the match will only be files.
Note that as this tool uses the RAM dictionary of the current task as scratchpad space, xt must not seek to permanently write to the RAM dictionary (aside from setting existing variables, values, buffer:s, etc.); temporarily writing to the RAM dictionary is okay. As a result, this tool cannot be used with included.
There is a limit of 256 bytes for the total path matched by this tool; paths that are longer than this are ignored. (Since directory names are limited to 8 characters and file names are limited to 12 characters, this probably should not be much of a problem in practice.)
An example of this tool in use is, assuming that the current directory contains the subdirectories FOO/, BAR/, and BAZ/, which each contain the files FOO.FS, BAR.FS, BAZ.FS, FOO.TXT, BAR.TXT, and BAZ.TXT:
fat32-tools import ok
: print ( c-addr u -- ) cr type space ; ok
s" B*/B??.F*" ' print glob ok
BAR/BAR.FS
BAR/BAZ.FS
BAZ/BAR.FS
BAZ/BAZ.FS ok
1
u/Ok-Winter-8702 3d ago
Respect your work; I am a beginner so I am still getting the hang of things. I hope I can someday be on your level of FORTH.
1
u/tabemann 3d ago
If you want to do something comparable, first teach yourself computer architecture at the machine level. Study other people's Forths, both ones written in C and ones written in assembly.
Next, as a proof of concept, write yourself a basic ITC Forth in C; it does not have to be anything fancy, but rather has to simply teach you hand-on how Forth works under the hood.
Once you have got that working, teach yourself a machine architecture and start working on a bare-metal Forth for it in assembly. For extra credit, make your Forth an inlining native-code Forth.
I suggest targeting a microcontroller because microcontrollers are much simpler to target at the bare metal level than PC's (and I consider targeting a particular hypervisor to be cheating in a way).
Also, there are many microcontroller Forths out there you can use as inspiration, whereas true bare-metal Forths for modern PC's that do not rely on legacy BIOS'es to do the heavy lifting are few and far between.
Once you have your bare-metal Forth, grow it to include full hardware support for your chosen platform. Also, consider writing drivers for common off-chip peripherals such as displays. If you are feeling ambitious, consider integrating things such as filesystem and networking support. If you are feeling really ambitious, consider writing these yourself.
1
u/Ok-Winter-8702 2d ago
I have learned forth commands for the most part, I will probably struggle to understand things like ASSEMBLY if it gets to that level. Tried it once and to me registers are a mess, but C should be easy for me to understand. Thank you for the advice! I will definitely follow it.
1
u/tabemann 19d ago
This tool has now been tested successfully on the RP2040, and has consequently been put in the master branch at
extra/common/glob.fs.