Fitting a Forth in 512 bytes (2021)

(compilercrim.es)

61 points | by whereistimbo 20 hours ago ago

8 comments

  • Jun8 5 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?

    • mananaysiempre 4 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

    • simne 2 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.

    • zabzonk 3 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_ 2 hours ago

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

  • dang 2 hours ago

    Discussed at the time:

    Fitting a FORTH in 512 bytes - https://news.ycombinator.com/item?id=27468698 - June 2021 (57 comments)

  • IAmLiterallyAB 4 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 2 hours ago

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