More work on H2D2

Since I've been writing the parser for H2D2 - the continuation of my work on DALIS, I thought that it might be useful to see the syntax trees that are being generated. So I found a nifty tool called phpSyntaxTree which is a website that will draw a graphical version of a tree from bracketed notation.

So I added some code to H2D2 which will output the syntax tree created by the parser in bracketed notation, and now I can paste the resulting text into phpSyntaxTree and see what the results look like.

Whilst the H2D2 language doesn't have much of a syntax yet, I have been able to parse things like this example code:

an = 99
PRINT an
mynum = 88
PRINT mynum
PRINT 1 + 2*3
IF 1 PRINT 1 PRINT 0
PRINT (1+2)*3

That program tests the majority of things that I have written so far, simple things like branching, expression parsing and assigning numeric variables. This is how that program comes out:

You can see the difference between the two expressions, one with brackets and the other without, and when I run the program, the first result is 7 and the second result is 9, so it seems to be working fine. When H2D2 programs are executed I'm using a non-recursive algorithm, which means that the code can be frozen at any point and then resumed - even if the code was part way through evaluating an expression there's no problem in carrying on exactly where the code was paused. So far I've only tested all this in Pelles C on Windows, but I will put a quick makefile together soon so that I can compile it with GCC (on linux), I want to keep the C code for H2D2 as portable as I can. The next thing that I'm going to try and build is a loop, which is kinda important.

Reading GPIO values on the Raspberry Pi in C#

So, after some distraction due to fiddling with H2D2 and reading my new book on programming TCP/IP sockets :-) I've finally gotten round to testing the getting of GPIO values in C# on my Raspberry Pi with the help of the libpigpio shared C library which I slapped together. Also, in the meantime I've tested out libpigpio on Debian Wheezy which seems fine.

I'm happy to report that it seems to work great, here is the C# test program I used:

using System;
using System.Threading;
using LibPiGpio;
 
namespace RPiBlinky
{
    class Program
    {
        static void Main(string[] args)
        {
            RpiGpio.SetOutputPins(new[] { 7 });
            RpiGpio.SetInputPins(new[] { 17 });
 
            while (!Console.KeyAvailable)
            {
                RpiGpio.Pins[7] = RpiGpio.Pins[17];
                Thread.Sleep(10);
            }
            Console.ReadKey();
        }
    }
}

....which sets the value of one GPIO based on the value of another. I hooked up a button and an LED like I did before, so pressing the button makes the LED come on. That code makes it look pretty easy doesn't it?

Anyway, the next thing is to expose this stuff to a browser, so that GPIO values can be read/written over the internet. I'm working on that... I just need to tear myself away from TCP/IP sockets and programming language design long enough.

DALIS Reloaded: H2D2

I've recently been thinking that my DALIS programming language may have gone as far as I'm prepared to take it in its current incarnation. It was only supposed to be a proof of concept, so I probably went way past that stage some time ago. But some of the most recent features have been hard to implement because of decisions (and shortcuts) which I made at the start. Also, I've learned more about programming language implementation now, so I'd like to go back and change some things.

And since I've had the Raspberry Pi I've been reminded how much I really like programming in C, so I've decided that this time round I'll do the language in C and try to make it portable. I suppose there's an outside chance it may fit on a small microprocessor like one of the AVR ATMegas that I have lying around.

So... I've started again from scratch, bringing with me the things I learned from the DALIS proof of concept. This time round I'm not sure if I'll stick to the same syntax as the original DALIS or not. In my head the codename for this new language is H2D2.

Because the re-write is in C I'm hopeful that it will be easier to port and that it will be somewhat faster - even if speed isn't one of my main aims. At some point you *know* I'm going to render the Mandelbrot set as a benchmark don't you... be warned :-)

But many of the original ideas from DALIS are coming with me, programs should be able to be paused and resumed, and a paused program should be capable of being serialised before being restarted. This should allow me to continue running H2D2 programs on the cloud.

This time I'm going to generate syntax trees - my current thinking is that the source code can generate a syntax tree, and what I'm really building is a Virtual Machine which is capable of executing the syntax tree directly. The virtual machine can save the running state (the stack and the value of all variables) after any instruction which could become a resume point. The original DALIS didn't use syntax trees, it interpreted one line of source code at a time and used recursive functions to parse expressions. My new approach means that running a program won't need to keep parsing the source code, which will hopefully be more efficient.

So I've started all this from an empty project in the Pelles C IDE. I've not used any non-standard libraries, and I've written my own implementations of things like the stack and the tree traversal algorithm. So far I've implemented instructions like IF, ELSE, ADD, SUBTRACT, MULTIPLY, as well as something that will print an integer on the screen (which will eventually become PRINT or WRITE or whatever I call it). I can't assign variables yet though (but constant number values can be coded in the H2D2 source).

So currently I'm building the parser to see if I can construct the syntax trees from little bits of H2D2 source code. It's looking positive, I have some simple programs running, evaluating expressions and printing results, that type of thing. I've even been able to save a running program to disk and then resume the same program afterwards, which is cool.

The next big step has to be assigning variables because then I'll be able to try some more interesting stuff...

The arrival of my very own Raspberry Pi

A few weeks back my own Raspberry Pi arrived, so I thought that I should post a couple of photos to mark the occasion:

But I've been so busy recently that I haven't gotten round to posting them. So ... the Raspberry Pi that I borrowed has now returned to its rightful owner, and now I can continue experimenting on my own machine.

Another computer

Ages ago, I decided to build a new computer from an Intel D410PT motherboard that I bought. I was (and still am) going to install it as a PDP-11 emulator. Anyway, I built the machine but never got round to installing an OS on it. Actually, I did try to see if it would run Windows 7 ... but Windows was not happy with being installed on a USB flash drive as a boot disk.

But, since recently I've gotten used to Debian, I thought that I'd try that. This is an OS that is very happy to boot from a USB flash drive. This is cool, since I can swap hard disks for the machine without even mucking about with screwdrivers. Here is the machine running Debian 6 off a small 4GB flash drive I bought in Tesco:

So... here are the things that I actually bought to build it:

  • Intel D410PT motherboard (with integrated Atom processor)
  • 1GB Kingston Technology 667MHz DDR2 memory
  • LinITX mini-ITX case inc wall mount and power supply CFI-ACD29CC

...all that stuff cost me £103.00 including the shipping costs. I only had to add the USB flash drive for storage (but I already had that lying around). It's not as pretty as a mac mini, but it's still quite a small little machine. All the software I've used is free, so I have Debian 6 (squeeze) coupled with Google Chrome and it quite happily streams TV channels through BBC iPlayer and other services like TV Catchup. So at the moment it's working as a nice cheap little media centre. The next step is to install SIMH and start emulating some old hardware...

Retro clock LED driver plugin

I thought that I'd lost this code, but now I've found it in a dusty corner of my hard disk. It works with the USB LED Driver that I wrote some time ago. It's a different clock plugin, where each digit scrolls into view as if the clock had four rotating wheels for displaying the numbers.

If you're using my driver, then you might like to give this a try, I certainly like it.

All you need to do is extract the .cs file from the zip here and put it in the folder where the driver is installed (usually C:\Program Files\Hobbsindustries\LED USB Matrix Driver). The next time you start the driver you should see the extra option as an available plugin. Enjoy.

TCP/IP Sockets in C

I've recently bought TCP/IP Sockets in C: Practical Guide for Programmers and it's one of the best programming books I've bought in ages. I don't think that I've enjoyed thumbing through a programming book this much since the 1980s. Seriously. I saw it over a year ago in Cambridge Central Library and immediately liked it, but at the time I was not doing any work on *nix type OSes, so decided that it would be a waste to get my own copy. Now I have the Raspberry Pi of course, so that's different.

So I've already written a very trivial web server in C, which runs as a daemon and is multi-threaded. That took me about two hours to do. When I've tried it on the Raspberry Pi (I hope to do that soon) then I'll post it in case anybody else want their RasPi to present an interface that's accessible from a browser.