Building your own programming language

avatar

I love programming! I mean, I love the backend type of programming. I'm less of a UI guy. There are numerous programming languages and numerous ways to program, but have you ever wondered how a programming language work? Have you ever wanted to create your own?

Well, for me, when I started working on Steem Smart Contracts I knew that the JavaScript Virtual Machine (VM2) I chose wasn't going to be enough to allow arbitrary smart contracts to be deployed, simply because it's just a wrapper around the NodeJS's Virtual Machine which is not that secured. The only way to have full control over a programming language is to actually build your own.

If you ever want to learn how to build your own programming language, I highly recommend you to read Bob Nystrom's website, https://craftinginterpreters.com/, and Thorsten Ball's books available at https://monkeylang.org/.

So, I did use both of these tutorials to start building my own programming language, a Smart Contract language. This language is nothing new, it's just a subset of JavaScript (JS). I love JS but there are tons of things that make it incompatible with a "secured" execution environment.

I worked on that a few months ago and I actually created 3 different Virtual Machines for that language. One in JavaScript, one in TypeScript and one in C#. Building a Virtual Machine on top of JavaScript or C# may not be the best move but I hate working with C++. I actually did implement part of that VM in Rust, that was fun but Rust is a pain to work with, especially the compiler 🙂

My goal was to implement enough of the language so that I can calculate some Fibonacci numbers. I then used that to run a very simple benchmark, calculate Fibonacci 1000.

The script look like that:

function fibonacci(num){
  let a = 1;
  let b = 0; 
  let temp;

  while (num >= 0){
    temp = a;
    a = a + b;
    b = temp;
    num = num - 1;
  }

  return b;
}

let start = clock();
print(fibonacci(1000));
print(clock() - start);

I told you, it's just JavaScript 😄

I ran that code on all 3 VMs:

  • JavaScript: around 40 milliseconds (ms)
  • TypeScript: around 40 ms
  • C#: around 20 ms

Running this script in plain JS would only costs you around 8 ms of your life... so, yes, it is slow, 2 to 5 times slower, BUT, you have full control of what's going on behind the scene. This means that you get to decide how a piece of code gets executed, and by extension, how much it "costs" to execute it.

There's nothing new here, that's how major Smart Contracts platforms out there work, Ethereum, EOS, they have their own VM.

Now that I have some basic features implemented into my VM, I'm going to work on extending it so that it can be a full fledged Smart Contract language.

Posted with STEMGeeks



0
0
0.000
2 comments
avatar

Congratulations @harpagon! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You made more than 300 comments. Your next target is to reach 400 comments.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

Project Activity Update
0
0
0.000