Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Forth implementation in pure Forth?

324 views
Skip to first unread message

Alex Dowad

unread,
Sep 26, 2016, 9:54:38 PM9/26/16
to
Greetings all,

A Google search reveals many free Forths written in C, assembler, Java, etc. But none written in Forth itself. Does anybody have one available?

If your answer is "write one yourself"... well, I've written several, but would like to compare.

Thanks, AD

Michael Barry

unread,
Sep 26, 2016, 11:20:20 PM9/26/16
to
Something like this (I might not have understood your request)?

http://www.camelforth.com/download.php?view.4

Mike B.

Paul Rubin

unread,
Sep 27, 2016, 12:37:11 AM9/27/16
to
Alex Dowad <alexin...@gmail.com> writes:
> But none written in Forth itself. Does anybody have one available?

That's the usual way to do it, I thought. eForth is a good readable
example that's not too fancy. Look also at Brad Rodriguez's web
pages about Forth implementation.

Lars Brinkhoff

unread,
Sep 27, 2016, 2:31:06 AM9/27/16
to
Paul Rubin wrote:
> Alex Dowad wrote:
>> But none written in Forth itself. Does anybody have one available?
> That's the usual way to do it, I thought.

Depends on what you mean. Maybe Alex wanted a Forth implemented with
only high-level Forth, and no assembler CODE words. I think I saw one
of those once.

Elizabeth D. Rather

unread,
Sep 27, 2016, 3:10:51 AM9/27/16
to
Depends on what you mean by "written in Forth." FORTH, Inc. has not used
any compiler or assembler that was not written in Forth since 1971. We
use a meta-compiler with an assmebler (written in Forth) for the new
target to generate the first kernel for a new target.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

Cecil Bayona

unread,
Sep 27, 2016, 3:22:05 AM9/27/16
to
There are versions of eForth for the ep16 and ep32 FPGA CPUs and it's
all Forth, the trick is those two processors the native code is Forth.

It's late here and I'm tired, but I do remember seeing several Forth
that were written in Forth, you used a Meta-compiler to create a working
version. Pygmy is one that comes to mind, it has assembler primitives
but they are words compiled by the Pygmy meta-compiler so its all Forth
no external assembler of C compiler is used, only Pygmy itself..
--
Cecil - k5nwa

Albert van der Horst

unread,
Sep 27, 2016, 8:09:34 AM9/27/16
to
In article <874b78c5-4f2d-4485...@googlegroups.com>,
Alex Dowad <alexin...@gmail.com> wrote:
>Greetings all,
>
>A Google search reveals many free Forths written in C, assembler, Java, etc. But none written in Forth itself. Does anybody have one available?

That is because for some reason one uses the name meta-compilation.
for Forth written in Forth.

>
>If your answer is "write one yourself"... well, I've written several, but would like to compare.
>
>Thanks, AD
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Stephen Pelc

unread,
Sep 27, 2016, 8:12:19 AM9/27/16
to
On Mon, 26 Sep 2016 18:54:37 -0700 (PDT), Alex Dowad
<alexin...@gmail.com> wrote:

>A Google search reveals many free Forths written in C, assembler,
>Java, etc. But none written in Forth itself. Does anybody have one
>available?

A free (beer) Forth and cross compiler can be found in the MPE
MSP430 and ARM Cortex Lite compilers. See:
http://www.mpeforth.com

Stephen

--
Stephen Pelc, steph...@mpeforth.com
MicroProcessor Engineering Ltd - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)23 8063 1441, fax: +44 (0)23 8033 9691
web: http://www.mpeforth.com - free VFX Forth downloads

Albert van der Horst

unread,
Sep 27, 2016, 8:13:43 AM9/27/16
to
In article <86inthe...@molnjunk.nocrew.org>,
And of course that is straight impossible.
Unless you consider Chuck Moores
CODE + 58 C, 5B C, 48 C, 1 C, D8 C, 50 C, 48 C, AD C, FF C, 20 C, END-CODE
a good idea.

Groetjes Albert

Alex Dowad

unread,
Sep 27, 2016, 8:40:25 AM9/27/16
to
A Forth implemented in only high-level Forth (no assembler) is exactly what I am looking for. Sorry for not specifying that.

Alex Dowad

unread,
Sep 27, 2016, 8:42:43 AM9/27/16
to
On Tuesday, 27 September 2016 21:13:43 UTC+9, Albert van der Horst wrote:
> In article <86inthe...@molnjunk.nocrew.org>,
> Lars Brinkhoff <lars...@nocrew.org> wrote:
> >Paul Rubin wrote:
> >> Alex Dowad wrote:
> >>> But none written in Forth itself. Does anybody have one available?
> >> That's the usual way to do it, I thought.
> >
> >Depends on what you mean. Maybe Alex wanted a Forth implemented with
> >only high-level Forth, and no assembler CODE words. I think I saw one
> >of those once.
>
> And of course that is straight impossible.

Of course it's possible. Forth is Turing-complete, so you can write a Forth interpreter/compiler in Forth.

You do need a separate Forth interpreter/compiler to run it on; that is a given.

Albert van der Horst

unread,
Sep 27, 2016, 9:14:47 AM9/27/16
to
In article <d1894ddb-ce19-4aa5...@googlegroups.com>,
That is trivial. You don't need to implement anything in your
compilation system if it already understands Forth.
Why do you summon the ghost of Turing?

Alex

unread,
Sep 27, 2016, 9:25:17 AM9/27/16
to
Well, this:

ALSO MYFORTH DEFINITIONS PREVIOUS
: DUP DUP ;
: SWAP SWAP ;
: FLEA FLEA ;

is the ultimate in Forth on Forth. And yes, there are Forth's that have
code like this*, because : defines DUP in my new Forth's dictionary
MYFORTH, and uses the source system's DUP to do the heavy lifting. The
process is called metacompilation.

* Win32Forth is an example. Version N-1 of Win32Forth is used to build
version N. The code is now so far removed from the original version 0
that it's not possible to compile version N with all prior versions;
just those close enough to the current version.

No assembler is required either, if the source system and the target are
the same; we can copy the code for DUP from the source to the target
system without needing to understand what machine code it consists of.

--
Alex

Alex

unread,
Sep 27, 2016, 9:27:26 AM9/27/16
to
On 27/09/16 14:25, Alex wrote:
> On 27/09/16 13:42, Alex Dowad wrote:
>> On Tuesday, 27 September 2016 21:13:43 UTC+9, Albert van der Horst
>> wrote:
>>> In article <86inthe...@molnjunk.nocrew.org>,
>>> Lars Brinkhoff <lars...@nocrew.org> wrote:
>>>> Paul Rubin wrote:
>>>>> Alex Dowad wrote:
>>>>>> But none written in Forth itself. Does anybody have one available?
>>>>> That's the usual way to do it, I thought.
>>>>
>>>> Depends on what you mean. Maybe Alex wanted a Forth implemented with
>>>> only high-level Forth, and no assembler CODE words. I think I saw one
>>>> of those once.
>>>
>>> And of course that is straight impossible.
>>
>> Of course it's possible. Forth is Turing-complete, so you can write a
>> Forth interpreter/compiler in Forth.
>>
>> You do need a separate Forth interpreter/compiler to run it on; that
>> is a given.
>>
>
> Well, this:
>
> ALSO MYFORTH DEFINITIONS PREVIOUS
> : DUP DUP ;
> : SWAP SWAP ;
> : FLEA FLEA ;

Or, alternatively;

: DUP ['] DUP COMPILE, ;

etc

Lars Brinkhoff

unread,
Sep 27, 2016, 9:47:43 AM9/27/16
to
Alex Dowad <alexin...@gmail.com> writes:
> A Forth implemented in only high-level Forth (no assembler) is exactly
> what I am looking for. Sorry for not specifying that.

I went looking for one, but of course it's impossible to google for
anything Forth. I seem to recall there was one written as an
educational tool.

I did something vaguely like that not long ago. It was an
implementation of a 16-bit Forth inside top of a 32- or 64-bit Forth.
It's on GitHub.

hughag...@gmail.com

unread,
Sep 27, 2016, 2:43:30 PM9/27/16
to
All Forths involve assembler, because machine-code is how processors do stuff, and assemblers are needed to produce machine-code. In many cases the assembler is written in Forth though, which would seem to satisfy your "only high-level Forth" criteria. My MFX was like this (it was a new processor, so there was no assembler available from the chip-maker; I was employed by the chip-maker and writing the development system for the chip was my job).

In other cases, the primitives are written in a traditional assembler. This is typical for threaded systems. My Straight Forth is like this (it is ITC and I use FASM) --- I'm mostly doing this because the VFX assembler is limited to 32-bit, and I need 64-bit --- Straight Forth may be upgraded to STC in the future though.

Why are you asking this? What difference does it make? My read on this is that you are trying to prove that Forth is a toy language that can only be implemented in C (like Lua, except without any support for general-purpose data-structures) --- Rod Pemberton says: "C is the god language" --- is that your agenda here?

Jan Coombs

unread,
Sep 27, 2016, 4:51:27 PM9/27/16
to
On Tue, 27 Sep 2016 11:43:28 -0700 (PDT)
hughag...@gmail.com wrote:

> On Tuesday, September 27, 2016 at 5:40:25 AM UTC-7, Alex Dowad
> wrote:

> > A Forth implemented in only high-level Forth (no assembler)
> > is exactly what I am looking for. Sorry for not specifying
> > that.
>
> All Forths involve assembler, ...

I only needed 2'30" of therapy after reading that:

https://youtu.be/hnTmBjk-M0c

Jan Coombs

Cecil Bayona

unread,
Sep 27, 2016, 5:04:47 PM9/27/16
to
I needed that too, thanks.

I mentioned before eForth for the ep16 and 1p32 processors is all Forth
since the PC executes forth instructions. All Forth all the time.


--
Cecil - k5nwa

Rod Pemberton

unread,
Sep 27, 2016, 9:03:44 PM9/27/16
to
On Mon, 26 Sep 2016 18:54:37 -0700 (PDT)
This idea of "Forth in Forth" should be a FAQ, well, technically, it'd
be a FEC (frequently explained concept).

The best that most seem to be able to do is to write a Forth mostly in
Forth, but not completely. Such Forths usually use threaded-code (DTC
or ITC). They don't compile Forth directly to machine language like
modern Forths do. The threaded-code Forths are able to eliminate all
but a handful of Forth words which must be coded in another language.
These words are mostly low-level words that do the work. They're
usually referred to as "primitives". These can vary from a minimum of
around 9 or so and up to 72 or more. General "consensus" is that you
need around 35 primitives. Fewer than that and the threaded-code Forth
becomes too slow. The reason that you need primitives for
threaded-code Forths is that threaded-code colon-definitions only store
the sequence of operations as an address list. The colon-definitions
don't do any actual work in a threaded interpreter. They just group
Forth words (or instructions) together. So, you must have some Forth
words that do some actual work, i.e., some which aren't coded in
high-level Forth. These are coded in another language, which must be
compiled to machine language, e.g., using assembly or C etc.

eForth by Bill Muench was one of the first to attempt to write a Forth
using only a small set of primitives.

From what I've seen, I'd say that the typical threaded-code Forth
interpreter is usually built up this way:

1) inner or address interpreter (coded in assembly or C etc)
2) 35 primitives (varies from 9 to 72, also in assembly or C etc)
3) outer or text interpreter (pre-compiled threaded-code Forth, also
coded in assembly or C)
4) remainder of dictionary words in high-level (text) Forth

The inner or address interpreter is coded in assembly or C etc and
compiled to machine language. Next, a bunch of low-level Forth words
called "primitives" which do basic work are coded. Then, the outer or
text interpreter is coded using the primitives. This is pre-compiled
Forth code or an address list since the outer or text interpreter,
dictionary, and colon-definitions aren't implemented at this point.
Next, some related words are needed to compile words, and some
bootstrap words may also be needed. Finally, the other Forth words can
be coded in high-level Forth. You can also create more complete
versions of built-in Forth words now by using high-level Forth.


eForth
http://www.forth.org/eforth.html

Forths
https://www.taygeta.com/forthcomp.html

Itsy
http://www.retroprogramming.com/2012/09/itsy-documenting-bit-twiddling-voodoo.html

JonesForth
http://git.annexia.org/?p=jonesforth.git;a=tree;hb=HEAD

An old post of mine with the quantity of primitives per various Forths:
https://groups.google.com/d/msg/comp.lang.forth/KHw28PKZXUk/JrXcjrYshxAJ
(Usenet msg id g4gqv4$qk9$1...@aioe.org)


Rod Pemberton

P.S. Please remind Hugh that some C compilers can produce machine-code
directly without using assembly.
P.P.S. Please remind Hugh that Bradford Rodriguez, PhD, of "Moving
Forth" fame, also recognizes that Forth in C has advantages. See "Part
4: Assemble or Metacompile?" at bottom.

Chris Curl

unread,
Sep 27, 2016, 11:40:03 PM9/27/16
to
It seems to me this is a silly topic. In order to implement a Forth system in
Forth, you need a Forth system in which to implement the new Forth.

At the end of the day, EVERY language needs to resolve eventually into
the CPU'smachine "language". Forth can generate that itself, as can many
other languages that are not assembly.

The only other option is a native Forth stack machine; one with a CPU
that implements the primitive Forth instructions like SWAP and DUP and
so on. My project is along these lines. https://github.com/CCurl/Forth_C

Obviously, if you already have a Forth system, you can generate ANOTHER
Forth system. That is easy, and it is how most mature Forths are built,
so that must not be what you are looking for. Or maybe you just weren't
aware that this is how they are made ... ?

Additionally, it is no problem generating a Forth system for another CPU;
that is also a very common activity.

But if you (AD) want an original, "first generation" Forth system that was
written in itself, then you are either not being clear about what you want,
or are not thinking clearly.

So what exactly are you actually asking about or looking for?

Jan Coombs

unread,
Sep 28, 2016, 2:59:05 AM9/28/16
to
On Mon, 26 Sep 2016 18:54:37 -0700 (PDT)
Alex Dowad <alexin...@gmail.com> wrote:

Perhaps this from EuroForth might be relevant:

Abstract

This document defines a Forth threaded code (inner) interpreter
written entirely in high level standard Forth. For this it
defines a specific threaded code structure of colon definitions,
a compiler from high level Forth to this threaded code and a
corresponding inner interpreter to execute it. This inner
interpreter can run in a stepwise way and so gives the
surrounding environment control of its execution behavior. A
real time environment thus might slice the execution of threaded
code in small pieces and provide an interactive command shell
while still meeting its real time requirements.


nn: Implementing the Forth Inner Interpreter in High Level Forth
http://www.complang.tuwien.ac.at/anton/euroforth/ef16/papers/hoffmann.pdf
Video:
https://wiki.forth-ev.de/doku.php/events:euroforth-2016:interpreter


Jan Coombs

Lars Brinkhoff

unread,
Sep 28, 2016, 4:57:05 AM9/28/16
to
Rod Pemberton <NoHave...@zxdehrnyya.cam> writes:
> The best that most seem to be able to do is to write a Forth mostly in
> Forth, but not completely. [...] The threaded-code Forths are able
> to eliminate all but a handful of Forth words which must be coded in
> another language.

What people usually mean that their Forth is witten in Forth, is that
your "another language" is also implemented in Forth. I.e. the
primitives may be written in an assembler written in Forth. Such an
assembler is more an extension of Forth than a separate language. In
which case one could kinda say that the primitives also are written in
Forth.

Elizabeth D. Rather

unread,
Sep 28, 2016, 5:19:36 AM9/28/16
to
Only the OP can say whether that's what he meant. But, indeed, many
Forths have been written that way. Chuck was writing Forth "in Forth" in
this sense when I met him in 1970, and FORTH, Inc. has never had a Forth
implemented any other way. But as some have pointed out, of course this
chain has to have a beginning that involves not-Forth.

Andrew Haley

unread,
Sep 28, 2016, 5:51:28 AM9/28/16
to
Absolutely so, yes. And a Forth code generator written in Forth is
also a Forth program. Of course it is possible to write Forth in
Forth.

Andrew.

Alex Dowad

unread,
Sep 28, 2016, 6:41:36 AM9/28/16
to
Dear comp.lang.forth,

Thank you for your many and varied replies to my post. It looks like Ulrich Hoffman's paper is just what I was looking for; thanks Jan! I will also look for Lars Brinkhoff's GitHub repo.

I'm surprised at how much confusion the idea of a "Forth implementation written in Forth (sans assembler)" seems to have raised. I was looking for exactly that, no more, no less. A Forth program which reads Forth source text and executes it.

Why would one want such a thing? One reason is for educational/entertainment purposes, something like a quine. An interesting challenge, just for its own sake, or to see how many different ways it can be done. Alex (no surname) noted that this can be done using code like:

: DUP DUP ;
: SWAP SWAP ; \ and so on

...but this is trivial and uninteresting, like this Ruby quine:

print File.read(__FILE__)

...and a null program is even less interesting. Maybe I should revise my request to "a Forth interpreter or compiler written in Forth, *without* any asm, not even using an assembler defined in Forth itself, with an explicit interpreter loop, explicit dictionary, and explicit data and return stacks".

Aside from entertainment value, something like this could be useful as part of a metacompiler. The resulting metacompiler could be executed by any other Forth implementation which happened to be available, fed its own source code as input, and would emit a standalone binary which could then be used to compile other programs.

Thanks again, all... AD

Alex Dowad

unread,
Sep 28, 2016, 6:51:45 AM9/28/16
to
...Just discovered that Lars Hoffman's GitHub account is an absolute gold mine. Lars, consider yourself followed!

rickman

unread,
Sep 28, 2016, 11:13:52 AM9/28/16
to
On 9/27/2016 9:23 AM, Albert van der Horst wrote:
> In article <d1894ddb-ce19-4aa5...@googlegroups.com>,
> Alex Dowad <alexin...@gmail.com> wrote:
>> On Tuesday, 27 September 2016 21:13:43 UTC+9, Albert van der Horst wrote:
>>> In article <86inthe...@molnjunk.nocrew.org>,
>>> Lars Brinkhoff <lars...@nocrew.org> wrote:
>>>> Paul Rubin wrote:
>>>>> Alex Dowad wrote:
>>>>>> But none written in Forth itself. Does anybody have one available?
>>>>> That's the usual way to do it, I thought.
>>>>
>>>> Depends on what you mean. Maybe Alex wanted a Forth implemented with
>>>> only high-level Forth, and no assembler CODE words. I think I saw one
>>>> of those once.
>>>
>>> And of course that is straight impossible.
>>
>> Of course it's possible. Forth is Turing-complete, so you can write a Forth interpreter/compiler in Forth.
>>
>> You do need a separate Forth interpreter/compiler to run it on; that is a given.
>
> That is trivial. You don't need to implement anything in your
> compilation system if it already understands Forth.
> Why do you summon the ghost of Turing?

I believe the idea is to run the Forth defined in Forth on a host system
to produce a Forth system for the target. Isn't that just cross compiling?

--

Rick C

rickman

unread,
Sep 28, 2016, 11:42:01 AM9/28/16
to
On 9/28/2016 5:19 AM, Elizabeth D. Rather wrote:
> On 9/27/16 10:57 PM, Lars Brinkhoff wrote:
>> Rod Pemberton <NoHave...@zxdehrnyya.cam> writes:
>>> The best that most seem to be able to do is to write a Forth mostly in
>>> Forth, but not completely. [...] The threaded-code Forths are able
>>> to eliminate all but a handful of Forth words which must be coded in
>>> another language.
>>
>> What people usually mean that their Forth is witten in Forth, is that
>> your "another language" is also implemented in Forth. I.e. the
>> primitives may be written in an assembler written in Forth. Such an
>> assembler is more an extension of Forth than a separate language. In
>> which case one could kinda say that the primitives also are written in
>> Forth.
>
> Only the OP can say whether that's what he meant. But, indeed, many
> Forths have been written that way. Chuck was writing Forth "in Forth" in
> this sense when I met him in 1970, and FORTH, Inc. has never had a Forth
> implemented any other way. But as some have pointed out, of course this
> chain has to have a beginning that involves not-Forth.

I believe the not-Forth beginning is only needed if you are running on
your target. If running on a host machine, the entire program can be
written in Forth, no? In fact, if appropriate words are defined, there
is no need for an assembler written in Forth in the Forth written in
Forth code. All the target aware code can be on the host system only
including the assembler.

This is really no different from any other language, is it? To write a
C compiler for a target, many would simply write that in C on a host
system (or use one already written) after writing and assembler for the
target in C on that same host system. No need to include the assembler
on the target unless that is what you want.

Hmmm... maybe that is a bad example as most (or all) C compilers produce
assembly code that requires an assembler. lol

--

Rick C

Gerry Jackson

unread,
Sep 28, 2016, 3:42:34 PM9/28/16
to
On 28/09/2016 11:41, Alex Dowad wrote:
> ...and a null program is even less interesting. Maybe I should revise my request to "a Forth interpreter or compiler written in Forth,*without* any asm, not even using an assembler defined in Forth itself, with an explicit interpreter loop, explicit dictionary, and explicit data and return stacks".
>
> Aside from entertainment value, something like this could be useful as part of a metacompiler. The resulting metacompiler could be executed by any other Forth implementation which happened to be available, fed its own source code as input, and would emit a standalone binary which could then be used to compile other programs.
>

You might find Retro interesting. It is built on the Ngaro virtual
machine which is implemented in various languages including Forth. See
http://forthworks.com/retro/

--
Gerry
0 new messages