Mike Parsons

@mikeptweet

Making a .Net REPL

My first experience with computers happened at university while I was interning with a local government agency. It was on an old Compaq 386 and the “programming environment” was dBase III. The minute I typed in …

use database
browse

I was completely hooked. I was blown away by how easy and versatile it was to create, browse and manipulate data with just a handful of commands in a completely interactive environment. I like to think that I built a successful career on the foundations of dBase, and later FoxPro.

It’s been a long time since I used FoxPro but I never lost my love for REPl’s (read-eval-print-loop). Being able to drop into an interactive environment like SQL, the Linux command prompt or the browser JavaScript console makes me feel like I have computing super powers. For me, I’ve have yet to find a programming tool or development environment that allows me to express or demonstrate ideas in code like an interactive REPL.

With all the recent interest and advances in bots and “conversational computing”, I think we are about to enter a new golden age of REPL’s. With that said, I am going to write a series of articles that demonstrate how easy it is to create a useful REPL from scratch. This first example focuses on the Microsoft .Net programming environment. Before I start, I know that many readers will point out a ton of very fine and useful REPLs for .Net. I am aware of most of them and this example is not meant to suggest that any of these would not be better suited for your own tasks. The purpose of this article and subsequent ones is to take the “minimalist” approach so that you create something functional without relying on a bunch of external dependencies.

So lets get started … here’s the entire code:

//let's add a few useful namespaces
import System;
import System.IO;
import System.Net;
import System.Web;
import System.Data;
import System.Xml;
import System.Windows.Forms;
//create a global object/propertybag
var $={};
while(true) {
Console.Write('.Net>');
try {
Console.WriteLine(eval(Console.ReadLine()));
}
catch(e) {
Console.WriteLine(e);
}
}

Save this code in a text file … dotnetrepl.js, for example

To compile the code, we are going to take advantage of a little know feature of the .Net framework — the javascript compiler. This compiler (jsc.exe) resides in the .Net Framework directory. On my machine, this is

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\jsc.exe

To compile your REPL, open a command prompt to the location of your dotnetrepl.js and type:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\jsc.exe dotnetrepl.js

This will create a dotnetrepl.exe in your directory and you are good to go with your new REPL.

The REPL uses Javascript syntax but you can access any .Net Framework Class. Here are a few examples:

I have prepared a short video that illustrates a few other examples:

Hopefully you found this quick introduction useful. In subsequent articles, I will build a series of simple, yet useful REPL’s that you may use as starting point for your own computing discoveries.

More by Mike Parsons

Topics of interest

More Related Stories