[HN Gopher] StoneKnifeForth (With a Metacircular Compiler)
       ___________________________________________________________________
        
       StoneKnifeForth (With a Metacircular Compiler)
        
       Author : guerrilla
       Score  : 74 points
       Date   : 2021-02-18 10:51 UTC (12 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | transfire wrote:
       | I very much enjoyed this, in explaining StoneKnifeForth it is a
       | nice article on minimalism in computer language design.
        
       | notriskfree wrote:
       | Very nice. Building a new FORTH using FORTH might have been
       | easier; FORTH is traditionally very good at rebuilding itself.
       | e.g. WimpForth on a PI400 recompiles its own kernel to an
       | absolute image (executable) in 00:00:007. FORTH week on HN is
       | great. The main trouble with FORTH is remembering to stop playing
       | with FORTH and actually write the App.
        
       | tekknolagi wrote:
       | I have a C++ fork of this project in case Python isn't an option:
       | https://github.com/tekknolagi/stoneknifecpp
        
       | orthoxerox wrote:
       | I love that it produces actual runnable binaries instead of
       | emitting assembly or C, like so many tiny compilers do. I know
       | it's technically the job of the assembler and the linker, but
       | without these parts it kinda feels like cheating.
        
         | carapace wrote:
         | For this sort of thing I bounce back and forth between
         | targeting C vs ASM. C is so universal and there are so many
         | tools you can use to harden it. On the other hand, you're still
         | using a big ol' abstraction layer between C and the machine. On
         | the gripping hand, CPUs are doing so many things under the hood
         | now that ASM can be considered an abstraction layer obscuring
         | the actual machine, so you kind of need a modern compiler to
         | have a hope of taking advantage of all that without doing a ton
         | of research.
         | 
         | Over on the "actually pdrtable executable" page (
         | https://justine.lol/ape.html ) there's a subsection called
         | "x86-64 Linux ABI Makes a Pretty Good Lingua Franca" it seems
         | to me to be a reasonable suggestion (although I've never like
         | x86 myself.) You can bundle an x86 emulator with your code to
         | run on other architectures.
         | 
         | I like the Project Oberon 32-bit RISC developed by Prof. Wirth
         | as a target. It's really simple for educational purposes.
         | (Meaning you can more easily teach more people Oberon RISC
         | than, say, RISC-V. ALthough see the "Selfie" project:
         | https://selfie.cs.uni-salzburg.at/ "An educational software
         | system of a tiny self-compiling C compiler, a tiny self-
         | executing RISC-V emulator, and a tiny self-hosting RISC-V
         | hypervisor.")
         | 
         | There are many emulators for Oberon RISC already, including one
         | in Javascript, so you can run the old Oberon OS in your
         | browser! Launch an emulator with a disk image from here:
         | https://schierlm.github.io/OberonEmulator/
        
           | astrobe_ wrote:
           | I have done something along the lines of TFA a long time ago,
           | a system that could boot from a floppy and recompile itself
           | from source (never did anything useful with it), but today I
           | daily use a Forth dialect written in C that is designed to
           | play nice with C and the OS.
           | 
           | C is a sure bet. Interpreters that rely too much on JIT are
           | simply not available on processors where the JIT is not
           | available (duh), which was often the case for ARM before it
           | became really popular. Same thing with relying on an emulator
           | (also, you are running a VM in another kind of VM; it's
           | usually bad for performance).
           | 
           | As far as speed is concerned, if your Forth implementation
           | makes it really easy to extend, you can always get something
           | fast enough by re-coding the critical parts in C. That's why
           | I stopped worrying about which implementation technique is
           | the fastest and chose the one that made it the most trivial
           | to extend the interpreter.
        
             | guerrilla wrote:
             | Is that dialect written in C available online? I'm
             | collecting implementations at the moment. If you know any
             | other C implementations besides the popular ones I'd be
             | grateful for pointers to those too.
        
       | aarchi wrote:
       | I built an AOT compiler for Whitespace with its own SSA-form
       | intermediate representation that lowers to LLVM IR. Whitespace is
       | very similar to a minimal Forth, except, well, syntax. Since
       | StoneKnifeForth has no filesystem access, only stdin and stdout,
       | the principles could be easily adapted for Whitespace.
       | 
       | https://github.com/andrewarchi/nebula
        
       | mcguire wrote:
       | " _But if you try to implement a Lisp interpreter in a low-level
       | language by translating that metacircular interpreter into it (as
       | I did later that year) you run into a problem. The metacircular
       | interpreter glosses over a large number of things that turn out
       | to be nontrivial to implement -- indeed, about half of the code
       | is devoted to things outside of the scope of the metacircular
       | interpreter. Here's a list of issues that the Lisp 1.5
       | metacircular interpreter neglects, some semantic and some merely
       | implementational:..._ "
       | 
       | Amen! Those metacircular interpreters are neat, and very useful
       | from a learning-programming-languages standpoint, but they gloss
       | over about 90% of the details and pain of language
       | implementation.
       | 
       | I was really looking forward to Paul Wilson's _An Introduction to
       | Scheme and its Implementation_ (http://www.aboulsaadat.com/wael/t
       | eaching/324/exdocs/schintro...), but at the time it was really
       | incomplete. Looks like there's more now, but still no garbage
       | collection as far as I can see.
        
       | [deleted]
        
       | rwmj wrote:
       | Is it Forth week on HN :-? I like this but only allowing single
       | letter identifiers is an unfortunate choice - although I'm sure
       | it made parsing much simpler.
        
         | todd8 wrote:
         | How strange, I just picked up a book from my bookshelf, my old
         | copy of _Starting Forth_ by Leo Brodie. I was trying to
         | remember if Forth was created by Charles Moore or Calvin
         | Moores. Charles Moore invented Forth.
         | 
         | I really enjoyed reading _Starting Forth_ when it came out. I
         | 've used _Starting Forth_ together with _Threaded Interpretive
         | Languages: Their Design and Implementation_ by R. G. Loeliger
         | to create my own Forth system. This second book is not about
         | "threads" as we know them today; threading is a way to very
         | efficiently dispatch the execution of Forth primitives.
         | 
         | Calvin Moores is a language inventor too. I've also implemented
         | a version of his language TRAC. TRAC is mentioned prominently
         | in a book that I really enjoyed while in grad school in the
         | 70's, _Computer Lib_ by Ted Nelson. Described by Wikipedia as
         | the first book on personal computers.
         | 
         | The appeal of Forth, and even more so of TRAC, is that they are
         | very simple languages to implement. It is completely possible
         | to implement either language in assembly language, but C would
         | be a bit easier. As a project to learn Perl, back when it was
         | new, I wrote a TRAC interpreter over a couple of days.
         | 
         | TRAC, like the roughly contemporaneous language GPM invented by
         | Christopher Strachey, is a macro processor. GPM and TRAC are
         | "Turing complete" macro processors. So they can be used to
         | write arbitrary programs and are in fact designed for this
         | purpose.
         | 
         | Some links/references:
         | 
         | Leo Brodie, Starting FORTH : an introduction to the FORTH
         | language and operating system for beginners and professionals,
         | Prentice-Hall, 1987. https://www.forth.com/starting-forth/
         | 
         | Martin Campbell-Kelly, _Christopher Strachey, 1916-1975: A
         | Biographical Note_ , IEEE Annals of the History of Computing,
         | 1985,
         | https://www.computer.org/csdl/magazine/an/1985/01/man1985010...
         | 
         | R. G. Loeliger, _Threaded Interpretive Languages: Their Design
         | and Implementation_ , BYTE Books, 1979,
         | https://www.abebooks.com/servlet/BookDetailsPL?bi=2253615473...
         | 
         | Calvin Moores, _TRAC, A Procedure-Describing Language for the
         | Reactive Typewriter_ , CACM Vol 9, Num 3, March 1966,
         | https://dl.acm.org/doi/10.1145/365230.365270
         | 
         | Ted Nelson, _Computer Lib /Dream Machines_, self-published,
         | 1974, https://en.wikipedia.org/wiki/Computer_Lib/Dream_Machines
        
           | rwmj wrote:
           | > It is completely possible to implement [FORTH] in assembly
           | language
           | 
           | I know because I did :-)
           | https://news.ycombinator.com/item?id=10187248
        
             | mikewarot wrote:
             | Me Too - For OS/2, and it was direct threaded code
             | 
             | https://sourceforge.net/p/forth2/wiki/Home/
        
             | guerrilla wrote:
             | Thanks for that by the away. It's an amazing educational
             | resource.
        
             | alatz wrote:
             | I also wanted to say thank you for Jonesforth. Using it as
             | a guide while implementing a Forth in 6502 assembly is the
             | most fun I've had programming.
        
         | agumonkey wrote:
         | Yeah I wonder what propped this flood of Forth threads. Not
         | that I'm against it.. just a bit odd.
        
           | guerrilla wrote:
           | Too much? I've been posting things I think other people might
           | find interesting while researching implementing a Forth
           | compiler after having just done a Scheme.
        
             | agumonkey wrote:
             | Take it easy, I'm not complaining at all. It is
             | interesting. It's just odd (nothing more), like 5 articles
             | on Forth the same day.
             | 
             | Think running into 5 kangaroos in a row in NYC.
        
             | rwmj wrote:
             | Don't worry, can't have too much Forth (or Scheme for that
             | matter).
        
               | agumonkey wrote:
               | or scheme in forth
        
               | guerrilla wrote:
               | That'd be my next project if I complete this one to my
               | satisfaction.
        
         | vidarh wrote:
         | These things tend to happen on HN - someone discovers something
         | new or new to them and posts something and triggers others to
         | read up on the subject and/or reminds people of related
         | projects, and suddenly you have a slew of submissions about
         | similar things.
        
         | mindcrime wrote:
         | Speaking as somebody who submitted a couple of Forth links (but
         | not this one) - I saw there were two Forth related links on the
         | front-page at one point yesterday, was reminded of the famous
         | "Erlang Day" incident in HN history and thought "wouldn't it be
         | cool if HN were flooded with Forth links for a little while?"
         | and submitted a couple. Can't speak for anybody else, but I'm
         | guessing other people had a similar reaction.
        
       | Klwohu wrote:
       | >If you want to counter Ken Thompson's "Trusting Trust" attack,
       | you would want to start with a minimal compiler on a minimal
       | chip; StoneKnifeForth might be a good approach.
       | 
       | Some people have been working on fairly good Forths for
       | different, minimal hardware including the Parallax boards.
       | 
       | https://github.com/prof-braino/PropForth5.5
       | 
       | There's also Chuck Moore's GreenArrays GA144 if you want a high
       | performance Forth machine which includes a proto area on the
       | board.
        
       | pmiller2 wrote:
       | Someone needs to write a library / framework for this thing and
       | call it "bear skin."
       | 
       | https://wiki.c2.com/?BearSkinsAndStoneKnives
        
       ___________________________________________________________________
       (page generated 2021-02-18 23:01 UTC)