First Try at a Forth interpreter in Anvil

I like strange programming languages, and have been a fan of Forth for quite a number of years now. When I teach the programming languages course I force students to learn it just so I can have a reason to work with Forth.

After writing the Python Duck Coding IDE for my students who are learning Python, I thought it’d be fun to use the same infrastructure to create some small games that can be solved using Forth code.

Plus, I’ve been sick and unable to do much else, and I like to program strange things when I don’t have anything else to do.

Here’s the alpha version of the concept: https://forth-games.anvil.app

If you don’t know Forth, you’ll need to at a minimum look up basic syntax and how to use if/else statements to get any traction.

The idea is that you write Forth code in the editor. When you click the run icon in the toolbar, it runs the Forth code and then redraws the frame. Then your Forth code runs again, the frame updates, etc. Your code runs completely each frame, and continues to run each frame until you either stop it using the stop icon in the toolbar, or the code accomplishes the goal in the game.

Scroll down in the right hand pane to see the goal for the game and the game specific words available.

If you want to see it running but don’t care to learn any Forth, this code will solve the first (and only so far) game:

Forth Solution
wall@
if left else move then

If I take it farther then there will be more games that require increasingly more sophisticated use of Forth to solve. Not featured in the first game is the ability to have multiple copies of your Forth code running in parallel.

2 Likes

OMG :exploding_head:

I haven’t touched FORTH since MMS Forth on the TRS-80! But I’ve followed other RPN languages, incl. a selection of high-end HP calculators (some doing incredible math – complex numbers, matrix math and linear algebra; differential and integral calculus – with UNITS!!!). Also PostScript (yes, the printer page-description language).

You’ll want to add a little documentation, I think. The goal is probably guessable (get the bird to the target), but some other things are not.

Words like if, else, and then are Forth-standard, but other words (variable wall, procedures left and move) are not, and should be described to the end-user. Otherwise, they’ll have no idea that these words are available to them, or what they will do.

Also, you have an implicit loop, which is not expressed in the user-supplied Forth code. I half expected to write my own – but it’s been too long, and I’ve forgotten the standard syntax, so maybe it’s better this way, after all.

Hats off to a terrific (and fun) start!

1 Like

Yeah, if I continue this it’ll get far more like a Forth tutorial, with more steps along the way to explain how it all works. Consider this as a proof of concept for a potential future tool to help students learn Forth.

The basic architecture allows plugging in multiple games, with each game able to define its own words available to Forth. I’m particularly excited about allowing the use of the clone word to spin off parallel copies of the code, so you’re controlling multiple objects in the world.

Neat! I’ve had the idea of rewriting an old Rebol code builder/IDE that I did years ago, in Anvil. This motivates me :slight_smile:

1 Like