Show HN: A toy compiler I built in high school (runs in browser)

vire-lang.web.app

3 points by xeouz 8 hours ago

Hey HN,

Indian high schooler here, currently prepping for JEE, thought itd be nice to share here.

Three years ago in 9th/10th grade I got a knack for coding, I taught myself and made a custom compiler with LLVM to try to learn C++. So I spent a lot of time learning LLVM from the docs and also C++. It's not some marvelous piece of engineering,

I designed the syntax to be a mix of C and what I wished C looked like back in 9th grade.

It has:

  - Basic types like bool, int, double, float, char etc. with type casting
  - Variables, Arrays, Assign operators & Shorthands
  - Conditionals (if/else-if/else), Operators (and/or), arithmetics (parenthesis etc)
  - Arrays and indexing stuff
  - C style Loops (for/while) and break/continue
  - Structs and dot accessing
  - extern C interop with the "extern" keyword
Some challenges I faced:

  - Emscripten and WASM, as I also had to make it run on my demo website
  - Learning typescript and all for the website (lol)
  - Custom parser with basic error reporting and Semantic analysis was a PITA for my undeveloped brain (I was 15)
  - Learning LLVM from the docs
Important Learnings:

  - Testing is a very important aspect of making software, I skipped it - big regret
  - Learning how computers interpret text
  - Programming in general was a new tour for me
  - I appreciate unique_ptrs and ownership
Github: https://github.com/xeouz/virec

Its on my github and there's a link to my web demo (https://vire-lang.web.app/), it might take some time to load the binary from firebase.

Very monolithic, ~7500 lines of code, I’d really appreciate any feedback, criticism, or pointers on how I could’ve done this better.

xeouz 8 hours ago

OP here! If anyone is curious about the syntax without opening the demo, here is an implementation of insertion sort:

extern puti(n: int);

func sort(arr: int[5]) {

    let i=0;
    let key=0;
    let j=0;

    for(i=0; i<5; ++i)
    {
        key = arr[i];
        j = i-1;

        while(j>=0)
        {
            if(arr[j] < key)
            {
                break;
            }

            arr[j + 1] = arr[j];
            j-=1;
        }

        arr[j + 1] = key;
    }
}

let arr=[61,86,53,19,61];

sort(arr);

for(let i=0; i<5; ++i) {

    puti(arr[i]);

}