PoC #perl6 #raku BASIC assembler

I’ve recently been getting more interested in ARM assembler, even though my main desktop is an X86. I have also been interested in the work of Val Schorre and his Meta II compiler, and in the grammar capabilities of raku. I was supposed to get an ARM assembler book in time for xmas, but alas, Santa was behind schedule.

Anyway, I managed to get a PoC (Proof of Concept) for raku converting a simple BASIC in ARM assembly. Don’t worry if you’re not on ARM architecture. If you look at the Makefile in the repo I put online, you will see that you can compile using arm-linux-gnueabi-gcc, which I think you can apt install using that package name. What happens is blang.p6 takes as input a file test.bas and outputs test.S. The gcc ARM compiler then compiles it to the file test. You run it as you would a normal executable, even though it is an ARM executable.

It was certainly a fun little project. The raku script is a little over 200 lines long.

BASIC support for the language is, shall we say, rather minimal. It in fact recognises only 3 commands: LET, PRINT and FOR…NEXT.

Here’s what test.bas looks like:

for i = 1+10 to 2+10+10
    let sum = sum + i
    print sum

The actual raku code is a bit to long to say much about, but check it out anyway.

There are other limitations: like PRINT only prints an integer, addition is the only arithmetic operation allowed, and everything is in integers. Now that I’ve figured out FOR statements, other control constructs like WHILE, REPEAT, IF should be relatively straightforward.

The big problems that would need dealing with would be strings and functions, should I decide to do the latter.

I am really undecided as to how one might implement strings. I wonder how something like a ZX81 does it. Should I use malloc/realloc? It seems a bit of a cheat to use too much of the underlying OS. Should a user predeclare the size of a string? That doesn’t seem very BASICy though. Perhaps strings shouldn’t exist at all, and should only be an array of bytes. Hmmm, what to do? Maybe not make it BASIC, but reimplement Wouter van Oortmerssen’s  AmigaE. Or maybe a Pascal compiler.

Should it be a strongly-typed language, or a dynamic one? Hmmm, so difficult to decide.

One idea I like is to have a grammar system in my BASIC, like Perl6 does. I have in mind a kind of “tower of languages”, where each layer builds on the previous one. Perhaps there could be “branches”, where you can pick-and-choose language features and build a compiler accordingly.

Something for me to think about.



About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to PoC #perl6 #raku BASIC assembler

  1. Pingback: 2020.01 20/20 Foresight – Weekly Rakudo News

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s