Page 42 - MSDN Magazine, November 2018
P. 42

Figure 4 Using Some Default Keywords
The first argument to the “if ” keyword is a condition. The sec- ond argument is a lambda block that’s evaluated if the condition yields non-null (true). The third argument is an optional lambda block that’s evaluated if the condition yields null (false). So the “if ” keyword is in fact a function to which you can supply a lambda argument to, using the “{ ... code ... }” syntax to declare your lambda. This might feel slightly weird in the beginning, because everything happens in between the opening and closing paren- theses of your keywords, unlike other programming languages that use a more traditional syntax. However, in order to create a programming language compiler in 300 lines of code, some bold decisions simply had to be made. And Lizzie is all about simplicity.
Lizzie’s functions are surprisingly similar to the structure of an s-expression from Lisp, though without the weird Polish notation. Because an s-expression can describe anything, and Lizzie’s func- tions are simply s-expressions with the symbol (first argument) outside of its parentheses, you can describe anything with Lizzie. This arguably turns Lizzie into a dynamic implementation of Lisp for the Common Language Runtime (CLR), with a more intuitive syntax for C#/JavaScript developers. It allows you to add dynamic code on top of your statically compiled C#, without having to read thousands of pages of documentation to learn a new programming language. In fact, the entire documentation for Lizzie is only 12 pages of text, which means a software developer can literally learn Lizzie in about 20 minutes.
Lizzie—JSON for Code
One of my favorite features of Lizzie is its lack of features. Let me illustrate this with a partial list of what Lizzie can’t do. Lizzie can’t:
• read or write from your file system
• execute SQL queries
• ask you for your password
• change the state of your computer at all
In fact, a piece of Lizzie code out of the box can’t be malicious, not even in theory! This lack of features gives Lizzie some unique abilities that Roslyn and C# scripting don’t provide.
In its original state, Lizzie is completely safe, allowing you to securely transmit code over a network, from one computer to another computer, the same way you’d use JSON to transmit data. Then at your endpoint that accepts Lizzie code, you’d have to explicitly implement support for whatever functions you need your Lizzie code to have access to, in order to make it work for your use case. This might include a C# method that reads data from a SQL database or the ability to update data in a SQL database or to read or write files. However, all of these function invocations can be delayed until you’ve made sure that the code trying to do whatever it’s trying to do is actually allowed to do it. Hence, you can easily implement authentication and authorization before you allow it to, for example “insert sql,” “read file” or anything else.
This property of Lizzie allows you to create a generic HTTP REST endpoint where the client layer sends Lizzie code to your server and where it’s then evaluated. You can then have your server create a JSON response that it sends back to your client. And more interest- ingly, you can implement this securely. You can implement a single HTTP REST endpoint that accepts only POST requests containing
using System; using lizzie;
class Demo1 {
[Bind(Name = "write")]
object write(Binder<Demo1> binder, Arguments arguments) {
Console.WriteLine(arguments.Get(0));
return null; }
}
class MainClass {
public static void Main(string[] args) {
var code = @"
// Creating a function var(@my-function, function({
+('The answer is ', +(input1, input2)) }, @input1, @input2))
// Evaluating the function var(@result, my-function(21,2))
// Writing out the result on the console write(result)
";
var lambda = LambdaCompiler.Compile(new Demo1(), code); lambda();
} }
dynamic scripting language, from within my C# code, while adding a new keyword to the scripting language I’m using. It took just 33 lines of code in total, including comments. These 33 lines of code allow you to claim you’ve created your own programming language. Anders Hejlsberg, move over rover, and let little Jimmy take over ...
Is Lizzie a “Real” Programming Language?
To answer that question, you need to think about what you consider a real programming language to be. Lizzie is Turing-complete and, at least in theory, allows you to solve every computing problem you might imagine. So, according to the formal definition of what constitutes a “real” programming language, it’s certainly as real as any other programming language. On the other hand, it’s neither interpreted nor compiled, because every function invocation is simply a dictionary lookup. Moreover, it features only a handful of constructs, and everything is centered around the “function- (arguments)” syntax. In fact, even if statements follow the function syntax previously defined in the generic delegate:
// Creates a function taking one argument var(@foo, function({
// Checking the value of the argument if(eq(input, 'Thomas'), {
write('Welcome home boss!') }, {
write('Welcome stranger!') }) // End of if
}, @input)) // End of function
// Invoking the function foo('John Doe')
The syntax of if is as follows:
if(condition, {lambda-true}, [optional] {lambda-else})
36 msdn magazine
C#


































































































   40   41   42   43   44