Tuesday, December 30, 2014

More Hand Held Hacking: Forth on Android

I was inspired to try to get a version of Forth running on my cell phone. I've had quite a bit of success developing small Scheme programs on my Galaxy S5 and was curious if I could manage the same thing with Forth.

I installed gforth from Google Play hoping that it would be a solid implementation. Unfortunately, I was met with a non-functional mess:

Besides spitting out the debugging statement log: setComposingRegion, the app didn't do much more. That is, until I switched keyboards. I changed from Swift Key to the keyboard that comes with Terminal IDE. Ahhhh, much better:

Now we were talking!

Poking around, I learned that there's quite a few Forth files to be had in: /sdcard/gforth/site-forth. Between browsing those files, and looking at the gforth manual, I came up with the command:

  require gl-sample.fs

Running that pops up this graphic:

I don't know what to make of this, other than the fact that gforth isn't just some half-baked toy. This is a real forth implementation, with some impressive features (assuming of course that I can unlock them).

All of this was optimistic, but I was still not sure I could turn this into a mobile friendly dev environment. Taking a cue from my Scheme workflow I setup the following:

First, I created a keyboard shortcut that allows me to type Alt-o and be dropped into Forth. With the existing keyboard shortcuts in place, I can type Alt-M to switch to an editor Alt-o to switch me back to gforth.

Next, I created a file named .gforthrc in:


and put the following contents in it:

." BAS init " cr
: lex ( - )
 s" /sdcard/Documents/ex1.fs" included ;

That code doesn't do a whole lot other than announce that the file was executed setup the word lex for me to use. .gforthrc is a magic file, in that it gets loaded anytime gforth is started up.

The lex word, like the scheme equivalent (lex), loads a specific exercise file. In this case, /sdcard/Documents/ex1.fs. This allows me to open this file up in the editor, make changes, switch to gforth and type lex to reload in this file.

I've got a nice edit-eval loop, and when I'm done I can copy ex1.fs into a local git repository for archiving.

I've got to say, the setup truly works!

For now, I've been refreshing my Forth basics by going through the gforth tutorials. Though, I'm eager to tackle the Christmas Programming Praxis challenge, which involves solving 6 'ancient' algorithms. Should be the perfect way to freshen up my Forth skills.

At a minimum I can say this: gforth on Android rocks.


  1. Hey quit having so much fun over there! You are making everyone else feel bad that they aren't hacking on fun stuff too! Just kidding, you are inspiring them. Keep up the good work.

  2. Thanks man! Not to mention, you're the one who triggered this latest hacking fest -- so yeah, you keep up the good work too!

  3. I know this is an old article, but just wanted to say "Thanks, Ben!" Your instructions for setting up a loadable file while inside gforth really helped me continue the "Starting Forth" tutorial. I really appreciate the leg up. Now I can follow the tutorial, build words in QuickEdit then in the gforth window run lex, then test them. Awesome! Take care now.