views:

1225

answers:

6

Hello,

I’m starting a project where I need to implement a light-weight interpreter. The interpreter is used to execute simple scientific algorithms. The programming language that this interpreter will use should be simple, since it is targeting non- software developers (for example, mathematicians.)

The interpreter should support basic programming languages features:

  • Real numbers, variables, multi-dimensional arrays
  • Binary (+, -, *, /, %) and Boolean (==, !=, <, >, <=, >=) operations
  • Loops (for, while), Conditional expressions (if)
  • Functions

MathWorks MatLab is a good example of where I’m heading, just much simpler. The interpreter will be used as an environment to demonstrate algorithms; simple algorithms such as finding the average of a dataset/array, or slightly more complicated algorithms such as Gaussian elimination or RSA.

Best/Most practical resource I found on the subject is Ron Ayoub’s entry on Code Project (Parsing Algebraic Expressions Using the Interpreter Pattern) - a perfect example of a minified version of my problem.

The Purple Dragon Book seems to be too much, anything more practical?

The interpreter will be implemented as a .NET library, using C#. However, resources for any platform are welcome, since the design-architecture part of this problem is the most challenging.

Any practical resources?

(please avoid “this is not trivial” or “why re-invent the wheel” responses)

+7  A: 

I would write it in ANTLR. Write the grammar, let ANTLR generate a C# parser. You can ANTLR ask for a parse tree, and possibly the interpreter can already operate on the parse tree. Perhaps you'll have to convert the parse tree to some more abstract internal representation (although ANTLR already allows to leave out irrelevant punctuation when generating the tree).

Martin v. Löwis
+2  A: 

It might sound odd, but Game Scripting Mastery is a great resource for learning about parsing, compiling and interpreting code.

You should really check it out:

http://www.amazon.com/Scripting-Mastery-Premier-Press-Development/dp/1931841578

nlaq
Outstanding book! The scripting language described in the book is implemented in C# at http://www.codeproject.com/KB/cs/Conscript.aspx
Dour High Arch
The funny thing is that Alex Varanese and I go way way back, we used to talk on irc in our young teens. Interesting to see that he is an author now. He was a master of x86 assembly.
FlySwat
+2  A: 

One way to do it is to examine the source code for an existing interpreter. I've written a javascript interpreter in the D programming language, you can download the source code from http://ftp.digitalmars.com/dmdscript.zip

Walter Bright, Digital Mars

Walter Bright
+2  A: 

I'd recommend leveraging the DLR to do this, as this is exactly what it is designed for.

Create Your Own Language ontop of the DLR

FlySwat
A: 

Have you considered using IronPython? It's easy to use from .NET and it seems to meet all your requirements. I understand that python is fairly popular for scientific programming, so it's possible your users will already be familiar with it.

Daniel Plaisted
A: 

Lua was designed as an extensible interpreter for use by non-programmers. (The first users were Brazilian petroleum geologists although the user base has broadened considerably since then.) You can take Lua and easily add your scientific algorithms, visualizations, what have you. It's superbly well engineered and you can get on with the task at hand.

Of course, if what you really want is the fun of building your own, then the other advice is reasonable.

Norman Ramsey