News for hackers. Stuff that matters.
Re:RetroForth 2021.1 by dreamcompiler on Wednesday February 17, 2021 @05:49PM (#26172880)
The difference is that Forth compilers can be bootstrapped from not-very-much assembler on an embedded system. That's not really true for Lisp and Smalltalk. Forth impresses me because of the degree of leverage that bootstrapping process affords for small computers.
Re:RetroForth 2021.1 by moonchild on Thursday February 18, 2021 @05:35AM (#26178237)
> Forth compilers can be bootstrapped from not-very-much assembler on an embedded system. That's not really true for Lisp and Smalltalk

Sectorlisp[0] is an attempt to make a lisp that can fit in a bootsector (512 bytes). It's currently ~1.5 bootsectors; larger than the smallest forths, but certainly in the same ballpark.

Stage0[1] is an attempt to make a boostrappable seed from a minimum of binary dependencies. I recommend taking a look at what it says[2] on the matter, though it's too long to quote here.

0. https://github.com/jart/sectorlisp

1. https://savannah.nongnu.org/projects/stage0/

2. https://git.savannah.nongnu.org/cgit/stage0.git/tree/README.... skip to ‘stage2’

Re:RetroForth 2021.1 by slaymaker1907 on Wednesday February 17, 2021 @08:39PM (#26174406)
I think that very much depends on the Lisp in question. Even if you want a Scheme style Lisp which really needs some form of GC, you can implement a very rudimentary one using bump allocation. Basically just allocate memory by bumping the stack pointer and clear out everything at specific boundaries (i.e. you have some memory that is fixed at startup with the interpreter, basic functions, and constants but after that have well defined points where you clear out everything).

If you want something closer to assembly, you can just use a normal Lisp and have the Lisp compile itself to an assembly program, but with the benefit of things like variable names whatever other abstractions you can come up with. This approach is taken by Fairlylog[1] which is implemented in Racket but produces Verilog. You are basically writing a new language which compiles to assembly, but with the benefit of better tooling. Racket gives you the benefit of things like syntax-parse for writing the parser in a way that gives good error messages and more flexibility than a parser generator.

[1]: https://pkgs.racket-lang.org/package/Fairylog

Re:RetroForth 2021.1 by MaxBarraclough on Thursday February 18, 2021 @07:44AM (#26179152)
> just allocate memory by bumping the stack pointer and clear out everything at specific boundaries

You've described region-based memory management, also known as arena allocation. [0] This can indeed work nicely in short-lived applications like missile-guidance and even serious compilers. [1] It's not real garbage collection though.

[0] https://en.wikipedia.org/wiki/Region-based_memory_management

[1] https://web.archive.org/web/20190126213344/https://www.drdob...

Re:RetroForth 2021.1 by lhorie on Wednesday February 17, 2021 @06:45PM (#26173475)
> Forth compilers can be bootstrapped from not-very-much assembler

Do you have any resources handy on how one could do that from scratch?

Re:RetroForth 2021.1 by benhoyt on Wednesday February 17, 2021 @06:59PM (#26173601)
A good learning resource is Jonesforth (something of a classic), on GitHub here: https://github.com/nornagon/jonesforth/blob/master/jonesfort...

I wrote several Forth compilers when learning to code as a teenager, one using the A86 assembler, and two fully bootstrapped-from-assembler ones for 8086 and 386. The 8086 source code is here (I didn't write the assembler myself): https://github.com/benhoyt/third (KERNEL.F has the assembler parts)

Re:RetroForth 2021.1 by ampdepolymerase on Wednesday February 17, 2021 @10:59PM (#26175564)
Re:RetroForth 2021.1 by transfire on Thursday February 18, 2021 @04:22AM (#26177714)
Nice find. Thanks.
Re:RetroForth 2021.1 by naters on Wednesday February 17, 2021 @06:57PM (#26173582)
Jonesforth [1] is a classic example of this, and a really good example of “literate” code.

[1] https://github.com/nornagon/jonesforth/blob/master/jonesfort...

Re:RetroForth 2021.1 by dreamcompiler on Wednesday February 17, 2021 @11:53PM (#26175991)
I did this for the TMS34010 graphics processor many years ago but my notes are long gone. I was inspired by Mach 2 Forth, an amazing compiler for the early Mac. My compiler had local variables and tail call detection to take advantage of a special "one-deep" stack register on the chip.

The key point is that Forth can easily incorporate an RPN assembler which you can write in Forth, and with which you can then build a better Forth. I'm not sure if I even started with the official TI assembler for the chip; I probably first wrote a cross assembler in Mach 2 by transcribing the ISA manual and bootstrapped from that.

Re:RetroForth 2021.1 by tailrecursion on Wednesday February 17, 2021 @11:21PM (#26175744)
You requested from scratch, but also look into "metacompilation", which was typically how it was done.
Re:RetroForth 2021.1 by Pet_Ant on Wednesday February 17, 2021 @06:12PM (#26173128)
Forth is sort of live image like Smalltalk, and great for designing languages/DSLs in like Lisp which is also homoiconic.
Re:RetroForth 2021.1 by pjmlp on Thursday February 18, 2021 @12:36AM (#26176325)
Lisp surely can and has been.

Basically start with a s-exp based Assembler, add a macro support subset and go from there.

Re:RetroForth 2021.1 by guerrilla on Thursday February 18, 2021 @01:51AM (#26176786)
I'd like to see that or maybe try it myself but that's not quite the same as Forth fitting in a 512 byte boot sector[1] or on a tinyAVR. I bet it can be done but I suspect it'd require more cleverness than doing the same with Forth.

1. https://news.ycombinator.com/item?id=24604457

Re:RetroForth 2021.1 by pjmlp on Thursday February 18, 2021 @05:00AM (#26178002)
People tend to forget that Lisp was originally developed on an IBM 704, written in Assembly, with the traditional size a 1950 machine filling a server room was capable of, which was just a few KB.

While not the same thing, you can have a look at http://www.ulisp.com/ for AVR.

Re:RetroForth 2021.1 by dreamcompiler on Thursday February 18, 2021 @10:59AM (#26181389)
All the original compilers like Lisp and Fortran were written in assembler. But that doesn't mean it was easy, or that the resulting language was very capable by modern standards. The thing about Forth is that it is easy, and you get a full-featured Forth when you're done.

That's not to say it can't be done in Lisp but Lisp needs a lot more runtime support than Forth. If I was going to bootstrap a Lisp, I'd bootstrap a Forth first and then write the garbage collector for Lisp in Forth.

Re:RetroForth 2021.1 by kazinator on Thursday February 18, 2021 @11:04AM (#26181469)
Writing primitives like cons, car and cdr in assembler, a garbage collector in assembler, and eval in assembler would make a great amount of sense, from a performance and bootstrapping POV.

A Lisp compiler in assembler is just about rationally unjustifiable, though.