Jun8 14 hours ago

Forth has been sitting on my list of cool thing to learn when I have time for the past 20 yrs or so. What would be a compelling use case and setup?

  • jlokier an hour ago

    I looked into tiny Forth and Lisp interpreters as the foundation for bootstrapping bigger things.

    I expected Forth to be better for this because it seems like a smaller language, minimal even, that would have the smallest interpreter.

    Forth feels lower level and simpler. After all, on the face of it, Forth is mostly a flat sequence of tokens and simple commands, run one after another liike assembly language.

    Whereas Lisp has obvious tree data structures up front in a prettier syntax, like higher level languages, and you're encouraged to use them. Even lexical scope, closures and macros if you want them. That seemed like it must be bigger and heavier.

    But I found the small Forth and Lisp interpreters came out about the same size.

    This is backed up by the existence of 512-byte boot sector implementations of both Forth and Lisp.

    So I decided for applications needing a tiny interpreter, or for bootstrapping, to not pursue Forth any more for those things, as a tiny Lisp does the job with (in my opinion) better ergonomics and versatility, and negligible cost difference.

  • mananaysiempre 13 hours ago

    One compelling use case is getting a REPL on any microcontroller with a UART and, say, ≥64K RAM (I remember lbForth[1] being particularly portable, and there are other implementations you could use as well).

    I don’t know if you’d want to have that be your first experience with the Forth itself, though: there’s inherent fiddliness involved in bringing up hardware; the win is that Forth doesn’t really add any of its own once you’re vaguely familliar with the internals. If you can get it to boot and send and receive bytes, you can get an interactive Forth on it—or if the available resources don’t permit that, on an imaginary machine spanning it and your PC (a “tethered” Forth).

    [1] https://github.com/larsbrinkhoff/lbForth

    • NonEUCitizen 4 hours ago

      I think you meant ≤ instead of ≥ 64K RAM

  • zabzonk 12 hours ago

    Some of the things I learned from implementing a FORTH on a CP/M box back in the early 1980s were

    - writing Z80 assembler

    - using the assembler and linker

    - getting a clue about how the CP/M file system worked (it didn't, very well)

    - writing a number of utilities (VT52 emulator, PacMan clone & stuff) in FORTH, which was fun

    - macro programming at compile/runtime using things like BUILDS/DOES

    It's a lot of fun and you don't need to invest much time in it to get things done.

  • astrobe_ 11 hours ago

    Application scripting. Single file, low footprint, straightforward script/application interface. Can help with debugging.

    • irq-1 8 hours ago

      Is there a Forth made for scripting/embedding in an application?

  • simne 11 hours ago

    Forth is known for extremely compact representation of code, and very portable, because standard have very few registers and work on stack machine. Unfortunately all these at cost of slow execution and not easy to make serious projects (hard to deal with large codebase chaos, but read more).

    So it definitely, platform for slow embedded applications, keyword Arduino, may be Raspberry.

    Sure, compactness is huge advantage in some other cases, for example, known boot loader shell for UEFI is written on Forth, so you could write applications for it (and yes, for this use case any modern motherboard except Apple).

    For large codebase works, some Forth people learn Scheme and technically switched to Lisp programming paradigm. Yes, it interest phenomena, Lisp techniques work with Forth good enough if developer disciplined enough. Sure, Lisp will open for you whole new world.

IAmLiterallyAB 13 hours ago

I have a side project to try to make an extreme minimal size compiler for a Forth inspired language and implementation. It's a compiler that generates a Forth like interpreter and byte code. The bytecode is huffman encoded. Nothing works yet but its an idea.

  • 082349872349872 11 hours ago

    Have you seen ColorForth? Moore huffman encoded (or similar) his character set, so he could do dictionary lookup with just REPNZ SCAS.