Easy Forth

(skilldrick.github.io)

62 points | by pkilgore 2 hours ago ago

18 comments

  • 7thaccount an hour ago

    This has showed up here a few times before (example):

    https://news.ycombinator.com/item?id=10634918

    I'm always interested in hearing people's reactions to Forth though and every now and then you get a cool new story on these threads, so I'm not complaining.

    • s-macke 40 minutes ago

      Right. And once again, you’ll also notice that no one is actually coding anything useful in Forth.

      • bxparks 2 minutes ago

        As I like to say: "C is a language that solves a million problems. Forth is a million languages that solve almost nothing." :-P

        I've been reading about Forth for 30-40 years. The dual stack is easy to understand. My problem is that I cannot see how control flow works in Forth, e.g. a simple if-then-else.

        I think that something as fundamental as an if-then-else should be obvious in a useful language. Heck, it's obvious in assembly language. But not in Forth.

      • kragen 8 minutes ago

        Well, I don't know if this qualifies as useful, but I wrote a minimal roguelike in one page of Forth last year: https://asciinema.org/a/672405

      • johnisgood 37 minutes ago

        In Factor though, they do.

        https://factorcode.org/

        • 7thaccount 34 minutes ago

          It looks like it's still being developed, but I feel like the 0.1 version number hasn't changed in 10 years. What cool projects are people making?

          • johnisgood 3 minutes ago

            Check out the website (I linked it) for examples.

            Additionally, check basis/ and extra/ on https://github.com/factor/factor.

            As someone else pointed out, it is 0.100 which was released not that long ago, and if you compile now, it is 0.101 anyways, but regardless of their versioning, it has been actively maintained ever since, they just did not bump the version for a long time.

          • jinwoo68 19 minutes ago

            Its version seems to be 0.100, not 0.1. And it was released last year: https://downloads.factorcode.org/releases/

      • bell-cot 5 minutes ago

        It's a cool little niche language. If you're neither interested in the coolness, nor its little niche - there's no need to be dismissive.

      • kibwen 11 minutes ago

        Eh, that's sort of like saying that nobody's coding anything useful in assembly language. Both the JVM and WASM are stack machines akin to Forth.

        • kragen 9 minutes ago

          They're akin to Forth, but they're not Forth, and they're not implemented in Forth.

          By contrast, assembly language underpins most software people run today: perhaps it's written in Python, which is interpreted by CPython (using a bytecode which is considerably more Forthlike than Wasm, incidentally), which is written in C, which is usually compiled by GCC or LLVM to textual assembly language in order to generate the executable.

  • susam an hour ago

    Glad to see Forth on HN today!

    For anyone who likes playing with small experimental projects, I once made a minimal, esoteric canvas colouring language inspired by Forth and Tixy: https://susam.net/fxyt.html

  • thomascountz an hour ago

    This is a great resource and running in the browser is great for fast feedback. Thanks for sharing!

    I just started learning Forth a month or so ago, and I found this video from Andreas Wagner[1] fun to watch.

    If anyone goes through OP's book and find yourself wanting to see Forth in action, I recommend the video.

    [1]: https://youtu.be/mvrE2ZGe-rs

  • kragen 10 minutes ago

    Forth is very enjoyable, and it's always exciting to see someone new discovering it, but it has three big problems.

    The first is a technical problem: the forte of Forth is self-hosted developer tooling in restricted environments: say, under 256KiB of RAM, no SSD, under 1 MIPS, under 10 megabytes of hard disk or maybe just a floppy. In that kind of environment, you can't really afford to duplicate mechanism very much, and programmers have to adapt themselves to it. So you end up using the same mechanism for fairly disparate purposes, with the attendant compromises. But the results were amazing: on an 8080 with 64KiB of RAM and CP/M you could run F83, which gave you virtual memory, multithreading, a somewhat clumsy WYSIWYG screen editor, a compiler for a language with recursion and structured control flow, an assembler, and a CLI and scripting language for your application.

    Those environments almost don't exist today. But if you're programming, say, an MSP430 (consider as paradigmatic https://www.digikey.com/en/products/detail/texas-instruments...), you have only 2KiB of RAM, and you could use Mecrisp-Stellaris https://mecrisp.sourceforge.net/

    That chip's resources are pretty limited. In a money economy, we measure resources in money; the reason to use a chip with limited resources is to avoid spending money, or to spend less money. That chip costs US$7.40. For US$5.59 you could instead get https://www.digikey.com/en/products/detail/stmicroelectronic...: 100 megahertz, 512MiB of flash, 256KiB of RAM, 50 GPIOs, CAN bus, LINbus, SD/MMC, and so on. And according to Table 33 of https://www.st.com/content/ccc/resource/technical/document/d... it typically uses 1.8μA in standby mode at 25° at 1.7V. That's more than the MSP430's headline 0.1μA from https://www.ti.com/lit/ds/symlink/msp430f248.pdf but it's still low enough for many purposes. That is to say, the niche for such small computers is small and rapidly shrinking.

    Also, while the microcontroller might have only 2KiB of RAM, the keyboard and screen you use to program it are almost certainly connected to a computer with a million times more RAM and a CPU that runs a thousand times faster. So you could just program it in C or C++ or Rust and run your slow and bloated compiler on the faster computer, which will generate more efficient code for the microcontroller. The cases where you have to build the code on the target device itself are few and far between.

    Forth was designed to make easy things easy and hard things possible. The second problem is a social one: as a result of the first problem, the people who used Forth for that have mostly fled to greener pastures. The Forth community today consists mostly of Forth beginners who are looking for an artificial challenge: instead of making hard things possible, they want to make easy things hard. There are a few oldtimers left who keep using Forth because they've been using it since it did make hard things possible. But even those oldtimers are a different population from Forth's user base in its heyday, most of whom switched to C or VHDL. Most of us have never written a real application in Forth, and we've never had the religious-conversion experience where Forth made it possible to write something we couldn't have written without Forth.

    The third problem is also a social one: as a result of the second problem, most Forth tutorials today are written by people who don't really know Forth. I've only briefly skimmed this tutorial, but it seems to be an example of this. For example, I see that it doesn't explain immediate words, much less when to not use immediate words. (If it's ever easier to write something in Forth than in C, it's probably because you can define immediate words, thus extending the language into a DSL for your application in ways that are out of reach of the C preprocessor.) And it doesn't talk about string handling at all, not even the word type, even though string handling is one of the things that Forth beginners stumble over most when they start using Forth (because it doesn't inherently have a heap).

    So, I hope the author continues to learn Forth, and I hope they extend their tutorial to cover more aspects of it.

  • StilesCrisis an hour ago

    The automatic scrolling makes the page basically unusable on Safari.

  • mbfg 43 minutes ago

    >> The thing that separates Forth from most other languages is its use of the stack. In Forth, everything revolves around the stack

    I mean, that's pretty much every language. The main difference is that the programmer's access to it is unconstrained by things like method call definitions.

    • vdupras 30 minutes ago

      Unlike most languages, Forth has two stacks. It sounds trivial, but it changes many things. It allows for a leaner call convention. With a single stack, every function call has to "shovel forward" its arguments over the function return address, where Forth "glides" through its arguments, making function calls significantly lighter.