Project DescriptionThis is an embeddable LISP interpreter, allowing for dynamic evaluation of strings containing LISP expressions. We have used this as a mechanism to provide in-the-field-modifiable business logic for certain applications.
It's based on a state-machine-based parser which converts strings into LISP expressions, and evaluates a value.
There is an example project and a test project provided in the source code, showing how to use the evaluator libraries.
ExampleHere is an implementation of the Newton's iterative method for computing square roots
http://mitpress.mit.edu/sicp/chapter1/node9.html
public static void TestSqrt()
{
var lispProgram = new [ ]
{
"(setq epsilon 1e-14)",
"(defun sqrt-iter (guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))",
"(defun improve (guess x) (average guess (/ x guess)))",
"(defun average (x y) (/ (+ x y) 2))",
"(defun abs (x) (if (< x 0) (- x) x))",
"(defun square (x) (* x x))",
"(defun good-enough? (guess x) (< (abs (- (square guess) x)) epsilon))",
"(defun sqrt (x) (sqrt-iter 1.0 x))",
};
var interpreter = new Interpreter().Initialize(lispProgram);
for (var i = 0; i <= 25; i++)
{
var root = (double) interpreter.Execute("(sqrt {0})", i);
var square = (double) interpreter.Execute("(square {0})", root);
Console.WriteLine("sqrt({0}) = {1}, square(sqrt({0})) = {2}", i, root, square);
}
}
The C# code calls the LISP code to do the evaluation, and then prints out the results.
Try it and see it work! :)
Comments, contributions and critique welcome! But don't ask why I
need this! If you do, you don't understand LISP at all :)
NotesI wrote this in 2008 when I was putting Visual Studio 2008 and .NET 3.5 through its paces. I've just converted the project to Visual Studio 2010 and run a quick pass of Resharper on it to fix formatting.
There are a few performance issues arising from the approach of using Exceptions as a control-flow mechanism, which I'll get around to fixing when I have some time.
P.S.http://xkcd.com/297/