By Rob Blackwell
Introduction
L Sharp is a powerful lisp-based scripting language for .NET available from www.lsharp.org. L Sharp is free software, and it's easy to embed into your own applications. This article shows you how.
Background
Once an application becomes popular, it's often useful to provide a scripting facility to allow users to add new features, provide customisations and automate difficult or repetitive tasks. Microsoft provides VBA to script Office applications, but the scripting idea isn't new; Emacs is a popular and long-standing Unix-based editor that can be customised using Lisp.
If you've ever thought about adding scripting to your own application, it's tempting to roll-your-own language, start with a few commands and go from there. The trouble is that as you add new language constructs, your interpreter just gets more and more complicated. Using L Sharp, you can leverage a lot of functionality with just a few lines of code; you get a powerful scripting language with access to all your existing classes and objects.
Getting Started
Download the L Sharp binary distribution from www.lsharp.org/download.html
Unzip it and put the files into a directory such as C:\LSharp.
You can start the L Sharp interpreter interactively from a command prompt to familiarise yourself with the language (it can also be used in a Windows Forms application, more on that later):
C:\LSharp>lsharp
Welcome to L Sharp .NET, a powerful lisp-based scripting language for .NET.
Copyright (C) 2005 Rob Blackwell & Active Web Solutions.
This program is free software and is distributed under the terms of
the GNU General Public License.
For more information, see www.lsharp.org
Build 1.2.2189.27737
Microsoft Windows NT 5.1.2600 Service Pack 2
CLR 2.0.50727.42
> (writeline console "Hello World")
Hello World
null
>
Take a look at the L Sharp web site for documentation and sample scripts to get you started and give you a feel for the power of Lisp.
Adding L Sharp to Your Own Application
The LSharp.dll library is distributed under the terms of the GNU Lesser General Public Library, allowing it to be integrated into other applications even if they are commercial offerings.
Lets assume that you have already developed a Windows Forms application in C Sharp (it could equally be VB.NET). Add a reference to the LSharp.dll, then add a using statement to the top of your C Sharp source code file
using LSharp;
Now, whenever you want to run some script, its just a case of calling the LSharp.Runtime object, like this.
String script = "(+ 5 5)";
object result = Runtime.EvalString(script);
As you might expect, result is now set to 10.
Let's just think about what's going on here. The C Sharp program is compiled, but the L Sharp script can be any string. It could have been loaded from a text file or entered into a text box. There is no compilation to worry about, the answer is just interpreted and evaluated at runtime. The script could contain plug-ins or features which you, the programmer, haven't even thought of yet. The key thing is that scripts can be added without you having to recompile your application.
Of course, more complex scripts are more useful;
String script = "(do (reference \"System.Windows.Forms\") (using \"System.Windows.Forms\") (Show MessageBox \"Hello World\" \"L Sharp\"))";
object result = Runtime.EvalString(script);
This script shows a "Hello World" message box.
Scripting your objects
The simplest way to give L Sharp access to your own application objects is to set a variable in the L Sharp environment, before you call EvalString.
Add a button to your form and in the click event handler, do the following:
LSharp.Environment environment = new LSharp.Environment();
environment.Assign(Symbol.FromName("myform"), this);
object result = Runtime.EvalString(script, environment);
Now within your L Sharp script, the variable myform is bound to the calling form, so you can set the window title like this:
(set_text myform "I Changed the Title")
In fact, L Sharp uses runtime reflection to allow you to call any method in the .NET framework or in your application.
Security
One downside to allowing users to script an application is that they can change the way that the application behaves, and that means they could add mailcious code. Remember that scripts can delete files and do all kinds of nasty things; you must be careful.
The .NET framework provides Code Access Security. This can be used to assign permissions to code and restrict the power of scripts, but that's beyond the scope of this article.
More Information
For more information, including a full language reference, see www.lsharp.org. L Sharp news also appears on my blog at http://www.robblackwell.org.uk/?cat=3.
Rob Blackwell, Jan 2006.