On the dangers of an unfocused mind

In the last post I noted that I have an interpreter for SoapyBASIC, but wanted to rework it as a compiler.

The work to-date has been very much side-hustle, personal interest, hasn’t had a lot of serious time devoted to it, and in doing so, spans about FOUR years!

In that time I’ve explored different ideas of what I wanted, what it should look and feel like, what the milestones might be, while all the time riding the wave of Swift evolving into a credible language.

As with all old codebases there are going to be things you want to do differently, and this is definitely the case here.

In particular, the original goal was to interpret SoapyBASIC and include an interactive environment, like 1980s microcomputers.

This has left me with a single, ever-mutating AST (abstract syntax tree) that evolves through parsing, multiple phases of semantic analysis and finally interpretation.

Swift doesn’t really like mutable structures and since Swift 6 language model with strict data race safety, it’s become a bit of a witch-hunt.

Almost there, so let’s move goalposts!

So I chose to implement the pipeline using immutable ASTs for each stage.

  • Lexer to produce a token stream (always was immutable)
  • Parser to produce a parse only AST
  • Semantic Analyser to produce a semantic AST
  • IR generator to generate some intermediate representation, importantly being a flat structure, no longer a tree.
  • An optimiser, probably
  • Code generator to produce… what? Oh right–
  • A virtual machine with a specific ISA (instruction set architecture) to run the final code.

This was starting to look a lot like a full blown compiler pipeline!

But there are still clear goals, right?

I have written a compiler before, but that was 30 odd years ago, for something C-like and didn’t have IR or any kind of optimiser. LLVM didn’t exist.

So I got distracted by looking at what my VM would look like. I don’t like where x86_64 went. ARM64 is looking very complicated, RISC-V is promising, and of course I looked at simple stack-based VMs like Java.

I looked at how LLVM/clang works today.

I pondered how I might have a VM where I had syscalls that would implement code loading, even compilation or an IDE/REPL of some kind?

I looked at how a compiler might hamper the ability to have a full REPL, although other languages manage this, it’s not a primary interface for them (Swift, Java, Python, etc).

All this meant three or four weeks of doing very little except making my goals look less well defined and further away.

I did however keep to the immutable plan, so to date, I have achieved:

  • ✅ Lexer to produce a token stream (always was immutable)
  • ✅ Parser to produce a parse only AST
  • ✅Semantic Analyser to produce a semantic AST
  • ✅ Lots of bug fixes in the original; more testing.
  • ✅ Migrated to Swift Testing
  • Swift 6 language model clean

Current goals, then.

  • Keep on the compiler path
  • Define an IR (intermediate representation) language to translate the semantic AST into.
  • Define a target ISA (instruction set architecture) for my VM– knowing that it will always be a VM and so there’s really no point in having a nod toward parallelism/pipelining/caching etc. Maybe it’ll be stack based like Java?
  • Embrace SSA (static single assignment) in the IR.
  • Build an independent assembler for the target ISA.

I’d like to see the above done by end 25Q1 (it’s January 1st 2025 now).

Final thoughts

I like being influenced and informed by the likes of LLVM, the retro virtual machine scene, cutting edge RISC ISA designs, and whatever fancy thing catches my eye, but I also want 2025 to be a year of measurable achievements.

Not just in this, but all areas of retired life.


Comments

One response to “On the dangers of an unfocused mind”

  1. […] on the last post, I’m keeping focus on the plan, which is to move SoapyBASIC from an interpreter written in […]

Leave a Reply

Your email address will not be published. Required fields are marked *