Tails, A Fast C++ Forth Core

Tails is a minimal, fast Forth-like interpreter core. It uses no assembly code, only C++, but an elegant tail-recursion technique inspired by Wasm3 makes it nearly as efficient as hand-written assembly. I created it as a one-day hack to celebrate May Forth 2021 … then kept going because it’s fun.

It started out as tiny but functional. The magic core code (NEXT, INTERP, RETURN, LITERAL, DUP, etc.) is about 200SLOC and compiles to a few hundred bytes, many of which are NOPs the compiler adds for padding. The parser and compiler add a few KB more.

It’s grown significantly since then: there’s a parser; a stack-checking & type-checking compiler; multiple value types including strings and arrays; “quotations” (i.e. lambdas or blocks); garbage collection … but the simple core can still be extracted and used if something more minimal is needed.

Tails doesn’t follow the usual Forth implementation strategy of starting with a minimal assembly-language core and then building as much as possible in Forth itself. That makes sense for a system where you’re going to be writing applications entirely in Forth; but for my purposes I’m more interested in having an embedded language to use for small tasks inside an application written in a more traditional compiled language like C++.

Read in full here:

This thread was posted by one of our members via one of our news source trackers.

2 Likes

Corresponding tweet for this thread:

Share link for this tweet.

1 Like

Heh, FORTH’s are always fun, I’ve implemented at last a dozen as a learning task in a variety of languages. ^.^

2 Likes