"Programming Projects In C..." book

It's good to see that some new C programming books still get published. These days they are quite rare, but it's great to see when one does appear. At the moment I am enjoying this one:

Programming Projects In C For Students Of Engineering, Science, and Mathematics.

I bought mine when I found a copy whilst browsing the Cambridge University Press bookshop. As I flipped through the book in the store I stumbled on the chapter about makefiles, and realised that it was going to be good there and then. I liked the iterative way that the makefiles were presented - as a series of layered enhancements.

It even motivated me to go away and improve some of my makefiles, which were already working fine ... but could be done even better. The advantage is that now I have more of a chance to reuse them on other projects.

But now, I've had an opportunity to read more of this book, and have really enjoyed it. There are lots of little gems tucked away which would be worth taking note of. I reckon that most C programmers would find something interesting in this book, not just developers working in the fields of science, mathematics and engineering.

The C programming language has been around for a long time now, I think that even the author admitted to using certain blocks of code for 30 years or so. But for me, that's part of the beauty of it. There are not many languages where you could have written a block of code decades ago and find that it's just as good today.

If you're doing C programming and are curious to see how somebody else uses the language then you'll probably enjoy reading this book.

H2D2: Something to do with Pi

After having success with moving running H2D2 programs between platforms, I wanted to try running H2D2 on the Raspberry Pi. I was pretty confident that it would work, having already compiled H2D2 from source on Debian on another machine. What I wanted to do this time was run some H2D2 code on the Raspberry Pi and then transplant the program to Windows and allow the code to run to completion. So here goes:

...well that seems to work OK. But that was pretty much expected. You can see that I ran the demo code for 500ms this time. But the real trick is to see if the file which has been generated is binary compatible with other platforms. If I move the output file to my Windows machine, will the H2D2 program continue exactly where it left off? Drum roll please.

Yay! After putting all that effort in it's nice to know that H2D2 bytecode is Raspberry Pi compatible, I hope that it goes a long way towards my efforts at crossplatformness. Of course, this also means that I have a means to fork a running program, since I could go back to the file that I copied off the Raspberry Pi and start again from that exact point as many times as I like.

...umm and I also need to write a different example program, all these mandelbrots are starting to get boring again. Trouble is, it is quite a good piece of test code. Anyway, I'm off to implement number arrays in H2D2 now.

Starting to write code in H2D2

So... my latest attempt at writing a programming language, which I'm currently caling H2D2 is taking shape. I can build loops, assign numeric variables and evaluate expressions. It is still only a subset of the syntax from my original DALIS language, but I am able to write some simple programs now. You know what's coming don't you...? Here's a little something that I've been working on, do you know what it is yet?

e=1.1
d=0
h=0
loop
  b=-2
  loop
    n=0
    r=0
    h=127
    loop
      r=r*r-n*n+b
      n=2*d*n+e
      d=r
      h=h-1
    repeat if ((r*r)+(n*n)<4) & h>32
    print h
    b=b+0.04
  repeat if b<1
  print 13
  e=e-0.1
repeat if e>-1.2

Yup, it's the famous ASCII mandelbrot. You'll also notice that I've switched to lower case for the keywords this time round. I got tired of feeling like I'm shouting when writing code. This version also uses '&' to mean 'logical and', instead of the actual word 'and'. This is the syntax tree that's created when I parse that code:

Well that's progress. It's not bad considering my C is still rusty, but it's coming back to me. I haven't timed anything yet, but it certainly feels faster, on my laptop this program draws the mandelbrot set in about one second I reckon. Of course, I can keep the syntax tree as a kind of compiled bytecode, meaning that the program can be run without the need for the parser, which would make it faster still.

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...

A simple DALIS server

A while back I wrote a simple web server and said that I could turn it into a DALIS server that would run outside of IIS. Well that's exactly what I did, take a look at this YouTube clip I've made:

In this clip I'm showing two instances of the same program running (one in IE the other in Chrome). Each program shows a kindof bouncing ball thingy in ASCII Art. This is not the kind of thing that I designed DALIS to do of course, but who cares. In the background you can see the DALIS server runnng as a console app. I've made it so that each thread servicing a web request shows up like a blinking light. This means you can see it is working - and you can tell that I still like blinking LEDs on things. Ahh, do you remember the days when you used to see blinky LEDs on the front of your modem? Good times, eh?

So anyway... whilst I have not said much about the DALIS progaming language recently, development has continued on background threads :-) I have added a few more features to the language. I have also tried to think of a better name for it, but DALIS has kinda stuck now. One day I might have the guts to tell everyone what DALIS stands for, or maybe not.

Modula-2 and the PDP-11

When looking round a second-hand bookshop yesterday, I found this book on Modula-2:

front cover

I seemed to remember that there was some connection between the PDP-11 and the Modula-2 programming language, so I took a chance and bought the book for £1.  I spare no expense on my computer history collection :-) Anyway, now that I've had a chance to check, I found this "The first Modula-2 compiler was completed in 1979 and ran on the DEC PDP-11" (taken from here).  So it does seem like a worthwhile addition to my collection of PDP-11 related objects.  This pleases me.

...and for the sake of completeness, here is the back cover too (even showing the original price tag):

back cover

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
   WRITE RETURN
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: 

DALIS on PSP

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.