DALIS on the command line

I originally wrote DALIS as a cloud based programming language, but since I had abstracted out all the IO, it has been really easy to make a command line version as well.  So I have thrown together a few simple scripts to play around with. Consider this, for example:

   min = NUMBER(arg1)
   max = NUMBER(arg2)
   gap = (max-min) / 10
   WRITE "deg F deg C"+RETURN
   LOOP f=min
      WRITE TEXT(f,1)+" "+TEXT(c,1)+RETURN
   REPEAT f=f+gap IF f<(max+1)
   WRITE "You must pass two arguments, min and max."+RETURN

As you can see, DALIS programs can now accept parameters (the web based version allows you to pass the parameters in the query string of the url). In this example you specify a minimum and maximum temperature in degrees farenheight and the program makes a simple table converting them to celcius.  Here is a screenshot:

DALIS on the command line

You never know, this might even make for a useful little scripting language.

DALIS User Input

As I have mentioned before, I have been adding User Input to my new programming language, codenamed DALIS.  Because the language is designed to work in a request / response environment I wanted the language to reflect that without getting over complicated.  So this is what I have got at the moment:

WRITE "Welcome to another DALIS program."+RETURN
ASK "Type your name and press ENTER: "
name = ANSWER
IF name="Dave"
   WRITE RETURN+"Good morning Dave."
   WRITE RETURN+"Hello "+name+"."

The ASK ... ANSWER syntax looks pretty straightforward to me.  If I run this program, this is what happens:

Starting the program

Then I put in a name:

Entering a name

Finally, this is the end result:

The result

It seems to work fine.  Simple code which even runs on my phone.  I also have a plan to enable forms, so that you can submit several values at once.  That is for another day though...

Dr Retro, I presume?

OK, so you're probably getting bored of mandelbrots by now. Sorry. But there have been more improvements to DALIS, mainly in the area of string handling; namely there is some. I can now assign string variables and do string comparisons and concatenations. This means that I can output some pretty html with colour and everything :-) Just take a look that this bad boy:

DALIS can do colour

How very retro. But it is like DALIS has jumped from the world of the ASCII character terminals of the 70s to the microcomputers of the 80s. OK, in case you're interested I'm mapping a 9 bit colour palette to the equivalent html colour values. But I'll try not to post any more mandelbrot images for a while, honest.

Improvements to DALIS

After working on some improvements to my DALIS programming language, I decided to celebrate by actually taking the time to understand the algorithm that draws the mandelbrot set better. After some reading on wikipedia, I was able to refactor it somewhat. So, this is my current implementation:

LOOP d=0, h=0, e=1.1
   LOOP b=-2
      LOOP n=0, h=127, r=0
         r = r*r-n*n+b
         n = 2*d*n+e
      REPEAT d=r, h=h-1 IF ((r*r)+(n*n) < 4) AND (h > 32)
      WRITE (h)
   REPEAT b=b+0.04 IF b < 1
REPEAT e=e-0.1 IF e > -1.2

I think it's much better. As you can see, it's now possible for several variables to be assigned on the same line and there is a RETURN keyword to write line breaks easily. Drawing the mandelbrot set in ten lines of code is pretty good, surely?

At the moment I'm working on a means of getting user input, which is going well, and in the background I'm thinking of ways to build simple webservices with DALIS... At this rate I'll have to start writing a book entitled "The DALIS programming language" :-)

DALIS - experiments on other platforms

I decided to give my new (experimental) programming language, codenamed DALIS a try on some other platforms.  This one is obvious I guess - the good old iphone:

DALIS on iphone

...but I was pleasantly surprised to see it working OK on my PSP as well: 


The fonts on the PSP don't come out the same, but it seems to run OK, which is the main thing.  I would be fun to try a Kindle, but I don't have one (yet).  The advantage of a cloud-based language like this is that you only need a browser, so it should work on all kinds of things.

DALIS - proof of concept

For the last week I've been writing an experimental cloud-based programming language in the evenings (as you do).  The language has just reached the point where it can actually do something... So I’ve ported my ASCII Mandelbrot drawing program from C, take look at these screenshots:

DALIS code view

That's the source code, and ...

The output of the program

... that's the output it gives. 

Not bad, I think.  The language syntax needs lots more work, as does the parser, but it’s good enough to prove *something*.  I’ve set it to do timeslices; the code runs for so many milliseconds (whilst rendering the page on the webserver) and then returns the results, with the program state hidden inside the html.  The next page request picks up where it left off, so you can actually watch the Mandelbrot as it is being drawn.  It’s not fast, but it does actually work.  I’m sure I’ll optimise it.  Well it’s an idea ... I'm still not sure if it’s a good idea though!  But it has been fun, so I'll spend a bit more time on it at least.

PostWizard - my first NuGet Package

For a while I have been fiddling around with session-less MVC3, the type of thing that can be easily load balanced. When you do this it would still be nice to be able to remember values between pages, and you'd think that some hidden html form fields would do trick nicely, but how could this be integrated with MVC? That is what I have been messing about with.  Adding a bit of JSON magic should make it even better.

So, I decided to release the result of my efforts somehow. A good chance to try out this NuGet thing :-) I guess.

I've called it 'PostWizard' and you can find it on NuGet here. I hope that it proves useful to somebody.

Creating MVC3 Razor views from a T4 Template

I blogged here about using T4 templates to inspect class properties. I also mentioned in passing that I had used it to create some MVC3 Razor views, so here is an example of that.

The process is essentially the same, except you need to change the output type from your template, like this:

<#@ output extension=".cshtml" #>

To get it to work on a basic model class you'll need to add some extra references, to allow the compiler to do its thing. I've allowed for that by passing in an array of assembly file names. So you can do something like this:

Parser p = new Parser(file, "T4RazorView.Models.HomeModel");
p.AddReferences = getReferences();

...and implement a method which returns them, something like this:

string[] getReferences()
   return new[] {
      "C:\\Program Files (x86)\\Microsoft ASP.NET
      \\ASP.NET MVC 3\\Assemblies\\System.Web.Mvc.dll",

Which is all there is to it. If you add extra properties to the Model, the Razor View will automatically update. So here's an example MVC3 project in Visual Studio 2010 if you're interested.

Inspecting Class Properties from a T4 Template

I wanted to write a T4 template to find all the properties inside one of my C# classes, which would allow me to auto-generate some code. After that, if I added another property the code should automatically update - and I would have one less thing to remember.

The solution I came up with involved using the C# compiler from inside the T4 template. The template actually builds an in-memory Assembly and then reflects over the compiled class to find it's properties. Then you can do whatever you like with them in the output. So here is an example project using that approach.

This means that you only need to do something like this:

string path = this.Host.ResolvePath("");
string file1 = Path.Combine(path, "MyClass.cs");
string file2 = Path.Combine(path, "MyClass2.cs");
Parser p = new Parser(new string[] { file1, file2 }, "T4ClassProperties.MyClass");
p.WriteProperties(WriteLine, formatter);

...and provide an extra function to format the output how you want it, like this:

string formatter(string s)
   return(string.Format("Console.WriteLine(\"- {0}\");",s));

The example I've given makes a C# method that outputs the names of all the properties in a specified class to the console. But in reality I have used it to do things like build MVC3 razor views automatically.  To do that you'd just need to tell the template to output a .cshtml file instead.

The limitations are that the class file you are inspecting needs to compile stand-alone, or you need to be able to pass an array of source file names that will enable the class to build. You also have to specify any special assembly references, which I have not shown in this example, but is supported by my template. Also, to make it automatic, you need to get the T4 template to run as a part of the build process. So I've hacked the project file to run a "BeforeBuild" target in this example.

As you can see, all the magic happens inside ClassParse.tt, which you simply reference in your own template with something like:

<#@ include file="ClassParse.tt" #>

I thought that it might be worth blogging this, since it might come in handy again one day. I'm only looking at *properties* ... but it could be adapted to look at methods or anything else you can access via reflection...

[NOTE: if you'd like to know more about using T4 with MVC3, then you might to take a look here.]