Making a VM and Compiler in JavaScript!

This is a minimalistic and very simple implementation of a Virtual Machine and Compiler for … Read more Making a VM and Compiler in JavaScript!


This is a minimalistic and very simple implementation of a Virtual Machine and Compiler for the Brainf*ck language in JavaScript.
The aim of this project is to teach everyone that not all compilers and VM’s have to be complex and huge. After reading this, Hopefully you will have an understanding of how some machines and languages work.

If you learnt anything, or think this helped you in anyway, Don’t forget to leave a star! All PR’s are also welcome.

See tokenizer.js[1])
This is an important step because we can know if any non recognized characters are found in our program, and throw errors early on, before moving forward. It also makes it easier for our Parser to read the program.

Von Neumann architecture.
In Von Neumann’s model, a computer has two central parts: a processing unit, which contains an arithmetic logic unit (ALU) and multiple processor registers, and a control unit with an instruction register and a program counter. Together they’re called the central processing unit, often shortened to CPU. Besides that, the computer also contains memory (RAM), mass storage (think: hard drive), and input/output devices (keyboard and display).
Here is a rough sketch of the Von Neumann architecture:
Von Neumann architecture[2]

When a computer is turned on, the CPU:

  1. Fetches an instruction from memory. The program counter tells the CPU where in memory it can find the next instruction.
  2. Decodes the instruction. To identify which operation should be executed.
  3. Executes the instruction. This can mean either modifying the contents of its registers, or transferring data from the registers to memory, or moving data around in memory, or generating output, or reading input.

These 3 steps are repeated indefinitely. This is known as the fetch-decode-execute cycle. Or the instruction cycle. This is the thing also known as the “Clock” of the computer.

Now, that we know a bit about how a real computer works, We can understand about Virtual Machines.
According to the definition,

A virtual machine is a computer built with software. It’s a software entity that mimics how a computer works.

Just like a real computer, our virtual machine also has a loop of the fetch-decode-execute cycle. Our virtual machine also has a Program counter, it also has a Stack, Memory, Pointers, etc. All made in software.

I won’t go Into much details with the code here, you can look for yourself in the vm.js file[3]. But basically, what our virtual machine is doing, Is taking the Bytecode output from the Compiler, looping through each instruction, Changing the memory location according to the bytecode instructions, and printing the string at the current memory location when it reads the PRINT bytecode instruction.

The code doesnt need any dependencies except node.js to run. Use the following commands to run the code:

> git pull
> node testing.js

You can always contact me on discord via EliteDaMyth#0690. You can also create an issue on this repository if you found something which is not in place. IF you wanna join my discord server, you can find it here:[4]


  1. ^ See tokenizer.js (
  2. ^ Von Neumann architecture (
  3. ^ vm.js file (
  4. ^ (

Source: Echo Js

Categories: Development, Software

Leave a Reply

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