Tagged: Groovy

New coding project – metaedit – started


Image representing GitHub as depicted in Crunc...

Image via CrunchBase

I have started writing a MetaPost editor – metaeditpostmeta – which is on Github.

I am not sure how much time and effort I will be able to devote to it, but hopefully I will produce something somebody (even if it is just me) can use.

Yesterday I discovered someone had written a DSL with Haskell to generate MetaPost output – and so maybe I could do something similar using Groovy, but to commit myself to that would be a step too far for now.

Still, it is good to be writing some code again.

Missing coding


English: Programmer

English: Programmer (Photo credit: Wikipedia)

Ever been engaged in an intellectual activity where the hours whizz by much faster than you think, as you puzzle over and round the issues while feeling an intense pleasure that makes the rest of the world seem less important?  This what is called “flow” and, generally, it is what I feel when I am coding.

I am not the world’s greatest coder, to be honest I am little better than average (though doing the MSc at Birkbeck made me so much better than I used to be). The pleasure doesn’t come from having a natural skill that means I can write hundreds of lines at a single sitting: like a typical programmer, if I got 20 fully debugged lines out a day, every day, I would count that as decent performance.

But lately I haven’t done any coding at all (apart from a few lines of scripting in the office to ensure SMB mounts are automatic and such like). Instead I have read a lot of computer science papers and spent a lot of time working on a presentation I need to make and a literature review that will come after.

But I miss the coding, and I am missing it more every day.

Now, coding is also very more-ish. If you code to scratch an itch then chances are you make yourself itchy by coding. So earlier this year I wrote a Groovy/Java hex editor – Hexxed – after I wrote a Linux filesystem where I could not find a hex editor that did what I wanted to do, and so on.

So, even as I puzzle about whether I should write some code just for the sake of a mental stretch, I also wonder what I would write.

Getting booted from Wikipedia


A short article on “Binsic Is Not Sinclair Instruction Code” (BINSIC), my BASIC-like interpreter/DSL for Groovy faces getting deleted from Wikipedia on the grounds of lack-of-notability.

It would not be right or proper for me to intervene to stop this, but if you have been a BINSIC user then a proper third-party reference to it (followed by a clearance of the deletion message in the prescribed manner) would be very much appreciated.

The BINSIC article does not generate much traffic here (perhaps a visit a day), so I admit it is not a particularly important project in the world of computer science, but I hope it has been fun for at least a few people and it is worth keeping as a link to a quick and easy way to get BASIC on your computer.

Running BASIC on the Raspberry Pi


Actually, I ran BINSIC, my very own dialect of BASIC on the Raspberry Pi – it is very slow (a bit slower even than a ZX81 back in the day) but it does work.Raspberry Pi running

Haven’t had a chance to investigate what happens if I tweak the settings on the thing – possibly I might be able to speed execution up. Could be that Java and Groovy is just too much bloat, could be that BINSIC just demands a lot of computation (I refuse to consider that it might be poorly designed and executed).

Some fixes for BINSIC


I have made a few small, but important, fixes to BINSIC, the reimplementation of BASIC I have built using Groovy.

You can download the jar file (which can be run in any standard Java environment) from here: 
http://88.198.44.150/binsic.jar

Here’s another BASIC “game” (it’s amazing that this sort of thing used to fascinate those of us with these machines), for you to try – it was fixing this up that helped me find the bugs:

10 REM **DICE GAME**SLR/1983**
20 LET A=0
30 LET B=0
40 PRINT "DICE GAME"
60 PRINT "YOUR THROW="
70 GOSUB 160
80 PRINT "MY THROW="
90 GOSUB 220
100 IF A>B THEN PRINT "YOU WIN"
110 IF A<B THEN PRINT "I WIN"
120 IF A = B THEN PRINT "TIE"
130 LET X$ = INKEY$
132 IF X$ = "" THEN GOTO 130
136 CLS
140 IF X$ = "S" THEN STOP
150 GOTO 10
160 FOR G=1 TO 2
170 LET Z=INT (RND*6+1)
180 LET A=A+Z
190 GOSUB 280
195 PRINT
200 NEXT G
210 RETURN
220 FOR G=1 TO 2
230 LET Z=INT (RND*6+1)
240 LET B=B+Z
250 GOSUB 280
260 NEXT G
270 RETURN
280 REM Draw
282 PAUSE 100
285 IF Z = 1 THEN GOSUB 500
290 IF Z = 2 THEN GOSUB 600
300 IF Z = 3 THEN GOSUB 700
310 IF Z = 4 THEN GOSUB 800
320 IF Z = 5 THEN GOSUB 900
330 IF Z = 6 THEN GOSUB 1000
340 RETURN
400 PRINT "[ ][ ][ ]"
410 RETURN
420 PRINT "[*][ ][*]"
430 RETURN
440 PRINT "[ ][*][ ]"
450 RETURN
460 PRINT "[*][ ][ ]"
470 RETURN
480 PRINT "[ ][ ][*]"
490 RETURN
500 REM 1
510 GOSUB 400
520 GOSUB 440
530 GOSUB 400
540 RETURN
600 REM 2
610 GOSUB 460
620 GOSUB 400
630 GOSUB 480
640 RETURN
700 REM 3
710 GOSUB 460
720 GOSUB 440
730 GOSUB 480
740 RETURN
800 REM 4
810 GOSUB 420
820 GOSUB 400
830 GOSUB 420
840 RETURN
900 REM 5
910 GOSUB 420
920 GOSUB 440
930 GOSUB 420
940 RETURN
1000 REM 6
1010 GOSUB 420
1020 GOSUB 420
1030 GOSUB 420
1040 RETURN

The original game can be found here:
http://zx81.reids4fun.com/zx81/dice/dice_list.html
– I had to change it to cope with the main weakness of BINSIC – that GOTOs to lines inside loops fail.

A problem with Life


Bill Gosper's Glider Gun in action—a variation...

Bill Gosper’s Glider Gun in action—a variation of Conway’s Game of Life. This image was made by using Life32 v2.15 beta, by Johan G. Bontes. (Photo credit: Wikipedia)

I had hoped to “launch” BINSIC – Binsic Is Not Sinclair Instruction Code – my BASIC-as-a-DSL project built using Groovy, this weekend. For the launch I wanted to publish a jar file (so usable by everyone with Java) that ran the version of Conway’s Game of Life (seemed very appropriate for both general – Life being the ultimate hacker meme – and personal – I once wrote a version of Life in Z80 machine code for the ZX80 – reasons) found in Basic Computer Games – but I don’t think I am going to manage it now

The problem is that the code in the book is totally banjaxxed. It uses variables before they are declared and in general looks as though either some lines have been transposed or some code has been omitted altogether. It is certainly plain that the printout of the running program in the book does not reflect the code found on its pages. In any case hacking at this code reveals the full horror of BASIC and how difficult it is to maintain code that is not even in blocks, never mind any other sort of order.

So I have two choices – refactor the BASIC I have in quite a big way to get it to run, or find some new code instead.

But the exercise has not been completely wasted. While hunting down the bugs in David H Ahl’s code I have found more than a few in BINSIC itself.

The expressive power of BASIC


As I have been working on BINSIC – my reimplementation of BASIC as a domain specific language via Groovy- I have been increasingly struct by how unbelievably awful BASIC is (at least in the ZX80/ZX81 dialect that I am basing all this on).

basic coding

basic coding (Photo credit: Terry Freedman)

My memories of it are of a fun language where it was possible to quickly pull together some useful code to do the sorts of things 15, 16 and 17-year-old geeks were interested in.

But I really have to wonder now – it doesn’t even support user-defined functions: looking back I wonder why I wasn’t more enthused by PASCAL when I met it and its procedural programming paradigm at university: it certainly feels that I ought to have seen it as a fantastic improvement (though by then I was more into Z80 machine code than any high-level language).

But BASIC does have its strengths – as I have found out.

This piece of code is a prime example: 100 INPUT V

This means create the numeric variable called V and assign to it the value typed in by the user at the keyboard.

Trying to do this in Groovy/Java requires the creation of a whole new class just to handle the keyboard input, as well as mess about with thread synchronisation to ensure that the process waits for the input … what follows is just a part:

	def waitOnInput()
	{
		def textIn = binsicEngine.binsicWindow.textIn
		clearInputs(textIn)
		def countDown = new CountDownLatch(1)
		textIn.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
			BinsicConstants.INPUT)
		def inputAction = new BinsicInputAction(textIn, binsicEngine.preProc,
			countDown)
		textIn.getActionMap().put(BinsicConstants.INPUT, inputAction)
		countDown.await()
		return inputAction.result

	}

Progress with BINSIC


BINSIC – Binsic Is Not Sinclair Instruction Code – my effort to re-implement Sinclair ZX80/ZX81 BASIC as a domain specific language via Groovy (and eventually a runnable Java JAR file), is making more progress.

ZX80 + bubble TV = modernist bliss (p1120918)

ZX80 + bubble TV = modernist bliss (p1120918) (Photo credit: acb)

Right now it supports:

IF ... THEN ... ELSE
GOTO
GOSUB ... RETURN
LET
FOR ... TO ... STEP ... NEXT
DIM A(x, y, z) (and array derefencing)
CLS
PRINT

Still one or two difficult areas to get through and I have had to make one compromise – unlike on the ZX80 one cannot have a variable and an array with the same letter designation – just too difficult to implement on Java/Groovy.

But, it’s getting there…

Just like 1980


The Sinclair ZX80 (1980). It was the immediate...

The Sinclair ZX80 (1980). It was the immediate predecessor of the ZX81 and shared many of the same design features. (Photo credit: Wikipedia)

There have been (conservatively) 16 “Moore generations” since 1980 – that is to say computing speed should have increased by 2^{16} – approximately 4 million times.

But computation requirements grow to fill the computing power available and BINSIC now runs at about the same speed, when executing the same code, as my ZX80 did when hacking its way through BASIC programs 32 years ago.

It’s a strange sight to see, because it is so evocative of that period.

The code runs slowly largely, I think, because of the way that I have chosen to handle GOTO statements (not that I could think of any other way) – essentially the whole or at least a substantial part, of the program is reparsed every time a GOTO is issued.

Anyway, it adds to the sense of realism!