create account

Building your own programming language by harpagon

View this thread on: hive.blogpeakd.comecency.com
· @harpagon ·
$6.22
Building your own programming language
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](https://github.com/harpagon210/steemsmartcontracts) I knew that the JavaScript Virtual Machine ([VM2](https://github.com/patriksimek/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/](https://craftinginterpreters.com/), and Thorsten Ball's books available at [https://monkeylang.org/](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:

```javascript
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](https://stemgeeks.net)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 124 others
properties (23)
authorharpagon
permlinkbuilding-your-own-programming-language
categoryhive-163521
json_metadata{"tags":["smart-contracts","programming","diy","stem"],"links":["https://github.com/harpagon210/steemsmartcontracts","https://github.com/patriksimek/vm2","https://craftinginterpreters.com/","https://monkeylang.org/"],"app":"stemgeeks/0.1","format":"markdown","canonical_url":"https://stemgeeks.net/@harpagon/building-your-own-programming-language"}
created2020-11-05 21:05:12
last_update2020-11-05 21:05:12
depth0
children2
last_payout2020-11-12 21:05:12
cashout_time1969-12-31 23:59:59
total_payout_value3.464 HBD
curator_payout_value2.758 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,955
author_reputation20,287,642,226,146
root_title"Building your own programming language"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,408,205
net_rshares31,699,014,256,528
author_curate_reward""
vote details (188)
@enforcer48 ·
What would you name your language?
properties (22)
authorenforcer48
permlinkre-harpagon-qjgbzu
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2020.10.9"}
created2020-11-08 00:28:45
last_update2020-11-08 00:28:45
depth1
children0
last_payout2020-11-15 00:28:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length34
author_reputation426,238,777,098,689
root_title"Building your own programming language"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,435,917
net_rshares0
@hivebuzz ·
Congratulations @harpagon! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@harpagon/comments.png?202011151829"></td><td>You made more than 300 comments. Your next target is to reach 400 comments.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@harpagon) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @hivebuzz:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/update-202011"><img src="https://images.hive.blog/64x128/https://i.imgur.com/C5NcoUe.png"></a></td><td><a href="/hivebuzz/@hivebuzz/update-202011">Project Activity Update</a></td></tr></table>
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-harpagon-20201115t184147000z
categoryhive-163521
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2020-11-15 18:41:45
last_update2020-11-15 18:41:45
depth1
children0
last_payout2020-11-22 18:41:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length907
author_reputation369,388,541,731,718
root_title"Building your own programming language"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,543,376
net_rshares0