实现Forth语言
Implementing a Forth

原始链接: https://ratfactor.com/forth/implementing

Forth 的魅力在于其极简的核心:构建一个功能完备的语言需要极少的词。虽然 SUBLEQ 只用单个操作就能做到这一点(尽管在概念上如此),但真正的兴趣在于最小的*实用*Forth。 像 PlanckForth(1KB)、SmithForth(1KB)和 sectorforth(512 字节)这样的项目展示了手工编写的机器码实现,它们可以引导更完整的 Forth 系统。milliForth 只有 336 字节,包含 11 个词,展现了功能的极限。StoneKnifeForth 在 2KB 内实现了令人印象深刻的元循环编译壮举。最后,Frank Sergeant 为摩托罗拉芯片设计的 3 指令 Forth 只有惊人的 66 字节,允许在内存受限的系统上快速开发 Forth。这些项目突出了 Forth 的核心原则:从最小的基础构建复杂性,即使在资源匮乏的环境中也能实现强大的编程能力。

This Hacker News thread discusses implementing the Forth programming language, sparked by an article on the topic. Commenters share their experiences building and using Forth, often highlighting its simplicity and educational value for understanding language internals. The discussion touches upon the strengths and weaknesses of Forth. While praised for its ease of implementation, REPL interactivity, and ability to work closely with hardware, concerns are raised about its suitability for large programs due to lack of compile-time error checking and documentation challenges. Some argue that careful DSL design and documentation could address these limitations. Several commenters offer resource suggestions, including the book "Threaded Interpretive Languages" and links to various Forth implementations in different languages, from assembly to Go. The thread also explores alternative uses, like bootstrapping systems, creating minimal-dependency code, and even a Bash-based implementation for filesystem manipulation.

原文

I think one of the most interesting questions in the Forth universe is how small can its core be?

Which is to say, how few words do you need to implement in a host language (or raw machine code, as we’ll see) before you can bootstrap the rest of the language in the Forth itself?

(The most popular one I’ve seen is SUBLEQ, which stands for "SUBtract, branch if Less than/EQual to zero". And since there’s only one, you typically omit it and your program is just the operands for the implied instruction. The "language", then, has no operations and feels to me like the computing equivalent of John Cage’s musical stunt 4'33" (wikipedia.org).)

So I guess a Forth with one word is all you need.

Therefore, it’s not a question of, "How few words would be possible?" but rather, "How few words would be interesting?"

The answer will be personal to you.

The Forth-eV Wiki’s page on "Minimal Word Set" is a great place to start:

In addition to the word set, you can also constrain the total file weight of your core Forth. (Or lines of source or some other arbitary limitation.)

Some really intriguing minimal Forths I’ve encountered:

PlanckForth

I love the hexdump visualization of Koichi Nakamura’s PlanckForth:

binary layout of planckforth as taken from the repo

You’re looking at a complete working Forth implementation in the form of a hand-written ELF binary in less than 1,000 bytes. The rest of PlanckForth is written in PlanckForth to implement a "normal" Forth.

SmithForth

David Smith’s SmithForth is another 1,000 byte, hand-written machine code Forth that bootstraps a more complete Forth.

"My goal with SmithForth is not to stop writing machine code early, but to start writing Forth early."

sectorforth

Cesar Blum’s sectorforth is even smaller, at 512 bytes (the size of a boot sector).

Like other ultra-tiny Forths, it starts with just a handful of Forth primitives with which to bootstrap higher-level constructs.

The included 01-helloworld.f example is 200 lines of well-commented Forth source which starts with making numbers, implements a core of standard Forth words, and then prints "Hello world".

I imagine this is how Ents talk.

"As soon as the whole company was assembled, standing in a wide circle round Treebeard, a curious and unintelligible conversation began." --JRR Tolkein, The Two Towers

milliForth

512 bytes too much for you? How about 336? By my count, fuzzballcat’s milliFORTH implements 11 words and it can do anything, just, you know, painfully. The "Hello World" is 53 lines of pain.

StoneKnifeForth

Not just a hand-written Forth binary, but also a metacircular compiler, Kragen Javier Sitaker’s StoneKnifeForth source comes to a total of 1,902 essential bytes.

"Surprisingly, the language that results is still almost bearable to write a compiler in, although it definitely has the flavor of an assembler."

Many Forths are also compilers and/or assemblers, but rare is the program that can compile its own source in less than 2Kb.

"Three Instruction" Forth

Frank Sergeant’s 3-instruction Forth for an early 1990s Mototorla chip is possibily the smallest of them all?

"How many instructions does it take to make a Forth for target development work? Does memory grow on trees? Does the cost of the development system come out of your own pocket? A 3- instruction Forth makes Forth affordable for target systems with very limited memory. It can be brought up quickly on strange new hardware. You don’t have to do without Forth because of memory or time limitations. It only takes 66 bytes for the Motorola MC68HC11. Full source is provided."

How can you not want to learn about that? I mean, 66 bytes. What?!

"If you do have extra RAM available on the system then you can use the 3-instruction Forth to build and test a full Forth, and then use it."

联系我们 contact @ memedata.com