Friday, December 28, 2007

Forth in the .Net 3.5 Framework

A project containing a Forth API in C# 3.0, extendable using extension methods, and a Forth to .Net compiler/interpreter

This effort began as an experiment on how extension methods can be used to extend the functionality of a .Net class. What I wanted was an interesting test platform. I had experimented with Forth in college, and was familiar with how you can create new Forth ‘words’ (subroutines) to add functionality to Forth. So I thought it would be interesting to try and mimic a Forth based language as a .Net class and then use extension methods as the mechanism to allow users to add their own Forth words to extend the class.

Well, what I have now goes well beyond that original goal. Here is a high level list of features implemented thus far:

1. A Forth API, exposed as a .Net class library, contains the base Forth keywords as methods on the class.

2. A start to a Forth compiler/interpreter that will provide the familiar iterative Forth programming experience.

    a. The Forth words are converted into IL instructions to call the appropriate method on the Forth API or out to other class libraries containing Forth words. The compiler searches for Forth words in the Forth API class, and if it cannot find it there, will search other .Net dlls residing in the same location. Its searching for any extension method for the Forth API class and then is doing a simple method name comparison.

   b. This application is able to switch between compile mode and execution mode depending on if the source input is creating a new word definition, executing a word or being asked to do both.

   c. When the application is executing words, the compiled IL code is run in a dynamic assembly and then discarded after the program is finished

   d. The states of the Forth stacks are saved after execution of a program and put back in place at the start of the execution of the next program. This provides the developer the ability to execute word by word and examine how those word(s) affect the stack. This is really useful, probably required, with the iterative Forth programming style.

3. New Forth words built in the compiler/interpreter are built into a well known assembly named ForthWords.dll.

   a. The words built in this assembly are constructed as extension methods so they are available when using the Forth API directly.

   b. Because of the iterative Forth programming experience, this assembly stores the source code for its words so the entire ForthWords.dll can be rebuilt whenever a new Forth word is added.

4. Forth words can also be added in C# or Visual Basic .Net by creating class libraries which have extension methods on the Forth API class. From there a developer can use the Forth API methods to write their own Forth words and also to expose Forth to the entire .Net Framework.

At this point the base Forth stack manipulation, variables, constants, math operators, loops, and condition words have been implemented. Even with that, there might be some words I have missed.

To try it out, download the components here. Note: The components require the .Net 3.5 Framework to be installed.

The source code is available here. The project was built using Microsoft C# Visual Studio 2008 Express Edition

To begin a session, start the Forth.exe.

A simple calculation example
Enter


2 2 + . cr

You should see:

4
ok.

An example of the compiler/interpreter switching between new word compilation and then word execution

: hello .”Hello World” cr ; hello

You should see:

Hello World
ok.

This should have added a new .Net assembly named ForthWords.dll in the same directory as Forth.exe

To inspect the stack type .S

To close a session type bye



Check out:
JavaScript Forth Interpreter. Forth in the browser?
Hacking Roomba with Forth

4/22/08 Compiling from source files now supported

   

3 comments:

Anonymous said...

Nice

Eric said...

Very interesting! I also saw a C# Forth on CodeProject some time ago...

Jamey said...

I've only tried your examples and haven't looked at your code yet, but if you really want to understand how computers work, keep studying Forth. I used to belong to a Forth Interest group and met Charles Moore (the inventor of Forth) many times. I even put together one of his "Forth on a chip kits", and ran it as a coprocessor on a TI home computer (yeah, I'm that old!), a NeXT computer (Steve Jobs is still the only real visionary, Big Bill is just a Monopoly player) and a windows PC. The Forth processor did the brunt work for an Expert system. I know of no other language that allows you to develop significant programs with the speed and efficiency of Forth. Although I'm now studying C# and the .NET Framework rather seriously now, it's rather tiring to keep reading all the NEW features Microsoft has discovered. Lambda functions, F#, Generics, LINQ, extension methods, ... Have any of the people at Microsoft Research studied computer science? Even Newton and Einstein (my hero's) "stood on the shoulders of giants". Lisp came before FORTRAN and Forth was used not only to run just about every telescope in the world, but also was used for the first computer experiments in satellites. Please continue your work on Forth! By the way, I use Mathematica to run .NET programs as a true interpretive language. (The people developing F# have long, long way to go if they want a competitive functional language). If you're still a student, you can get the student's version at a really reasonable price.
Jamey Giustino