ces

BGPM implemented in Odin and in C

I'm continuing my obsession with GPM and decided to implement it yet again. Although the work was completed a couple of months ago, I pushed it to Github only yesterday, so I thought I might as well write up a blog entry about it today. The code is available from: https://github.com/romforth/bgpm.mr

Whereas last time around I started with an implementation in Rust, this time I was using a BCPL implementation of BGPM which is available from Github as the base. I assume BGPM stands for BCPL GPM and it was Martin Richards' reimplementation of GPM in BCPL (which for those of you who are unaware of history, was the grandparent of C via B).

Part of the reason to create yet another implementation was motivated by the fact that Martin Richards had found and fixed a subtle bug in Strachey's version (see the file bcplprogs/bgpm/bug.bgpm in his repo) which turns out to be hard to fix with the original single stack implementation. Martin Richards had used a two stack implementation and I liked the symmetry between that and the data+return stack architecture of Forth.

After staring at the BCPL code for a while, and making some initial attempts to see what the translated C code might look like, I decided on a lark to try something different. CPL (and its descendant, BCPL) were some of the earliest languages to support multi-assignment statements and the only C-like language that appears to have support for it today is Odin. The creator of Odin, Ginger Bill has a blog that does a survey of the available languages that support multi-assignment but doesn't list either CPL or BCPL. No matter, I decided that I'd give Odin a try.

Just like in the Rust to C conversion that I used in the earlier project, I used a helper Perl script called convert to help with making the changes from BCPL to Odin until the errors reported by the Odin compiler dropped below ~100 lines. For the rest of the changes I resorted to manually fixing up the code which could not be handled by easy regexp replacements. The major places where I had to manually intervene was in the handling of loops since BCPL has REPEAT/REPEATUNTIL/REPEATWHILE/UNTIL in addition to the C like FOR/WHILE whereas Odin has just one loop construct: 'for' and nothing else. I liked that bit of minimalism but I didn't like the fact that Odin also doesn't have gotos so I had to do a bit of code shuffling and had to contort the code a bit to get things working without gotos.

On the more positive side, one of the nicer features of Odin was that it was able to match the sigsegv back to the line of code that caused it without having to use -g and getting GDB involved in the picture.

Another thing I liked was the fact that definitions in Odin could be anywhere - unlike C, which needs forward declarations - an issue that I kept running into in the previous GPM porting project. Yet another difference, which might get counted as a negative coming from C, was that parameters are not writable so I needed to throw in a few additional variables for a bunch of routines to shadow the inputs.

Once the Odin port was completed and seen to be working well, I decided to port that to C since it seemed to be a fairly trivial/easy exercise and I threw it into the repo, just in case I might need a C version in the future.

Although BCPL is history, I did get the feeling that if it had structs and pointers (instead of just the store/! operator, which Forth appears to have inherited), it might have been the reigning language champion and C may never have seen the light of day.

Anyway, that's my speculation into alternative computing history for today and a little note to myself about Odin - I see that it natively supports hashes and Perl has been my goto language when I need to reach for a hash so I may try more of its features in the future.

← Previous
○ Solarpunk Webring
Next →