Lizzie, a Scripting Language for .NET
We take a look at an open source language that one developer has created, and how he hopes it can reduce compile times and learning curves for developers.
Join the DZone community and get the full member experience.
Join For FreeLizzie is a dynamically compiled scripting language for .NET, allowing you to incorporate dynamically loaded pieces of code into your C# and F# projects. One of its defining traits, is that, first of all, out of the box, it is literally theoretically impossible to execute malicious code, simply since out of the box, it doesn't contain a single piece of functionality that changes the state of your computer in any way. If you need such functions, which I assume most would, then creating such functions is as easy as marking your method with an attribute, and making sure it has the correct signature. Below is an example.
using System;
using lizzie;
class MainClass
{
[Bind(Name = "write")]
object Write(Binder<MainClass> binder, Arguments arguments)
{
Console.WriteLine(arguments.Get(0));
return null;
}
public static void Main(string[] args)
{
// Some inline Lizzie code
var code = @"
var(@foo, function({
+(bar, ""world"")
}, @bar))
write(foo(""Hello ""))
";
// Creating a Lizzie lambda object from the above code, and evaluating it
var lambda = LambdaCompiler.Compile<MainClass>(new MainClass(), code);
lambda();
// Waiting for user input
Console.Read();
}
}
Notice how we "bind" a C# method in the above code to our Lizzie lambda object. This makes the C# method available as a "function" internally within our Lizzie code. This trait allows you to easily extend the language, with whatever domain specific extensions you need to solve your particular problem. This makes the language particularly well suited for "Domain Specific Languages."
In a way, Lizzie is Lisp, with JavaScript's syntax. This makes it blisterinly fast to compile a Lizzie snippet. In fact, there is an example over at its GitHub project page, where I compile a snippet of Lizzie code 10,000 times, and the entire process only takes about 2,200 milliseconds on my machine. So compiling Lizzie code is probably some 5-10 orders of magnitudes faster than compiling the equivalent C#, F#, or Boo code.
The language is extremely dynamic, has no type safety, and also executes fairly fast, since the end result is basically just a bunch of statically compiled delegates, chained together to create a lambda Func<object>, where each of its "branches" are simply CLR delegates. Since it's also extremely secure, unless you mindlessly extend it with suicidal methods, it's also a perfect fit for passing code over the network, to evaluate the code somewhere else than where it was created. In such a way, it's arguably to code what JSON is to data.
Due to the fact that its syntax resembles JavaScript, you should be able to use JavaScript syntax highlighters to edit code snippets, but (of course) without AutoCompletion, since its has different ways of declaring functions and variables.
You can find it in NuGet as "lizzie", or download its source code on GitHub, here. The entire code for Lizzie is roughly 2,000 lines, where probably 50% of the code is comments. The entire documentation for it is only 12 pages of print, allowing you to literally learn the language in 20 minutes. You can see a video demonstration of the language here. The language is open source and licensed under the terms of the MIT license.
Opinions expressed by DZone contributors are their own.
Comments