Fuel Cost Calculator
A fuel cost calculator and driving directions map I developed that estimates trip distance, total gallons used, total fuel cost and fuel stops complete with links to area GasBuddy.com maps.

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 for your own Forth words, IL instructions will call out to the methods defined in your own .Net class libraries. The words you create will need to be implemented as extension methods for the Forth API class.

   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 Hacking Roomba with Forth

4/22/08 Compiling from source files now supported

   

2 comments:

Anonymous said...

Nice

Eric said...

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