How it started

On a Sunday I go for a walk with an old dev buddy of mine. Maths super genius, and we always chat about how we are bored in life when it comes to what we do.

We were just shooting the sh*t and I got on about how I'd like to make a proper compiler in C for kytschBASIC. Something you can build proper desktop apps and the likes. I'd been moaning about my utter dislike for a certain PHP framework and what I call hipster C, sorry Rust guys :-P, and he goes why don't we write our own language!

I've been itching to write something stupidly hard and get back into another lang for ages, this fit the bill perfectly.

So we chatted for a few more hours and that was it, I was hooked like a mutha fu... I couldn't sleep that night for dreaming of sytaxes, commands, loops and naked woma...errr I mean compiler designs.

I spent the next few days trying to focus on other stuff I needed to get done but this was burning a hole in my head. So come the Thursday of that week I'd decided to jump into it blind and full of spunk like I usually do with crack cocaine projects like this.

So I decide to go with C and laid out a doc with the start of the syntax of the language.

Me and my mate have a running joke where you say something and it's final, no discussions whats so ever, for example:

The earth is flat! And that's that!


I just really love it and I've been using it all the time for everything! HAHAHAHA! So That's That has been picked for the name of the language...

What is this?

It's THAT'S THAT of course!

My first version of the syntax for it was like this:-

// Always make classes first letter uppercase.
class Mike extends Welsh"
{
    PRINT "// Define public variable and type cast as String"
    pub var name:String = "Mike";
    var type:String = "user";

    // _kickstart is the constructor
    fn _kickstart()
    {
        // Perhaps "_" means system functions
        // was going to call it _main
        // but how about a throwback to the Amiga :-p
    }

    // Built in class function for cloning
    lock fn _clone()
    {

    }

    // Built in class function for destroying
    lock fn _destroy()
    {

    }

    // public function
    pub fn im_public()
    {
        // Clone an object
        var cloned:Mike = me._clone();
    }

    // protected function
    lock fn im_protected()
    {
        
    }

    // No pub means private function
    fn im_private()
    {
        
    }
}

// Create a variable holding the Mike class.
var mike:Mike = new Mike();

// Write to screen from the Mike class.
echo mike.name;"

And I set out using that and started to write the parser in C...boy did I underestimate my C skills lol!

Segmentation faults anyone?

This fast become the pain in my arse that I almost binned the whole thing there and then!

Rust looking pretty good about now, eh?
Shut it hipster!

After many hours of crying into a tub of ice cream, I got the books out and started reading.

I'm a collector of old computing books from 80s mainly and I had a number of them on C.

Yes, Yes, I see it now, allocate the pissing memory first!

After around 12hrs of my hair getting ever thinner I did it, I got the parser to run and parse the various bits into their proper parts.

The crack cocaine did it and I was on top of the world!

So what did I learn up till this point? I'm not as smart as I pretend to be, I have the reading ability of a 3 year old and this isn't like kytschBASIC at all.

kytschBASIC was a breeze to write cause first off it's not a compiler just an interpreter and the re-write I did was in Zephir-lang which is just soooo gorgeous of lang it's not even funny. Coupled with it being a high level lang that holds your hand like that sweaty man in the park that one day back in '88, but I digress...

This is a monster of a calibre that I'd not fully realised but boy oh boy is this insane amounts of FUN!

Any one out there with the time (and the hair) looking for a challenge to really learn the ins and outs of true dev and computer architecture I can't stress enough what a project like this does for you.

And the refactoring begins...

Now I was again getting to know the C lang and how to build up things I started to refactor the code. Splitting bits up, making (or should I say trying to make) reusable pieces of code and just get the parser to build the Abstract Syntax Tree better known as the AST.

Check this geek out with the terminology!

If your looking for a guide on how to build a compiler you ain't going to find it here...sorry. I suck at writing detailed howto's and you just get some other dev contacting you,

Dear Mike, errr it's actually called a LEXER not a parser.

My brain just doesn't retain words like other's do so explaining things in proper technical terms just isn't what you'd get from me. If it's a howto install or use something I've written no probs, but getting something this detailed on paper, my noodle just laughs and leaves for a cuppa.

2+2=5

So to sum up the first few days of the dev for anyone looking to tackle this themselves.

Get reading!

Get yourself some books to really help you wrap your head around how compilers work.

Look up Principles of Compiler Design by Alfred V.Aho and Jeffery D.Ullman nicknamed the Dragon Book.

I'd recommend Modern Compiler Design by Dick Grune, Kees van Reeuwijk, Henri E. Bal, Ceriel J.H. Jacobs, Koen Langendoen.

If your not interested in actually compiling but are just after some interpreted lang, start there. It's a great way of learning how to write a parser, LEXER!, and get you producing something fast.

Don't try and write the slickest most optimised code in the world, JUST WRITE!

You can always come back and refactor later on. Just get something up and running.

There is nothing more off putting than writing and writing and never seeing your fruits output on screen somewhere. For me I'm decades into this dev life so I more patient and have taken those ball kicks for long enough to throw myself into something bigger.

Talk to other developers, see what they've done and their pitfalls. Don't be put off by the neg-heads online who say why bother. We bother cause we can, and THAT'S THAT! You don't learn anything by staying in your comfort zone, challenge yourself and dream big!

Pick a language for your compiler you are comfortable with. I'm using C purely cause I wanted to get up to speed with it again and I'm a pure masochist. But it doesn't have to be something super low level. Sure it'll help later down the line but there are plenty of ways of getting your AST compiled into something. Just scan around the web for something that'll do it in your fav lang, and get some books!

Checkout The LLVM Compiler Infrastructure as they have a ton of docs and tools to help you to compile.

Reach out to me and I can try and help.

Images