• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    Python
  • License
    Other
  • Created over 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple virtual machine w/constant folding implemented in Python

This is the code from the blog post "Making a simple VM interpreter in Python", which you can find at https://csl.name/post/vm/

Made by Christian Stigen Larsen, with some improvements from the people at r/Python. Put in the public domain.

To run:

Hit CTRL+D or type "exit" to quit.
> 2 3 + 5 * println
Constant-folded (2 + 3) to 5
Constant-folded (5 * 5) to 25
25
> ^D

To test:

$ python vm.py test
Code before optimization: [2, 3, '+', 5, '*', 'println']
Constant-folded (2 + 3) to 5
Constant-folded (5 * 5) to 25
Code after optimization: [25, 'println']
Stack after running original program:
25
Data stack (top first):
Stack after running optimized program:
25
Data stack (top first):
Result: OK
** Program 1: Runs the code for `print((2+3)*4)`
20

** Program 2: Ask for numbers, computes sum and product.
Enter a number: 12
Enter another number: 13
Their sum is: 25
Their product is: 156

** Program 3: Shows branching and looping (use CTRL+D to exit).
Enter a number: 1
The number 1 is odd.
Enter a number: 2
The number 2 is even.
Enter a number: 3
The number 3 is odd.
Enter a number: ^D

More Repositories

1

jp2a

Converts jpg images to ASCII
HTML
608
star
2

mandelbrot-js

Fast rendering of the Mandelbrot set in HTML5 canvas using JavaScript
JavaScript
344
star
3

wpm

Typeracer-like console app for measuring your WPM
Python
318
star
4

minijit

A basic x86-64 JIT compiler written from scratch in stock Python
Python
210
star
5

stack-machine

A simple stack-based virtual machine in C++ with a Forth like programming language
C++
162
star
6

arv

A fast 23andMe DNA parser and inferrer for Python
C++
114
star
7

mersenne-twister

This Mersenne Twister is a fast pseudo-random number generator (PRNG) in C++
C++
83
star
8

dna-traits

A fast 23andMe genome text file parser, now superseded by arv
Python
65
star
9

mickey-scheme

Mickey Scheme is an interpreter for R7RS Scheme written in pure C++
C++
62
star
10

crianza

A stack machine VM, interpreter and genetic programming library
Python
47
star
11

miller-rabin

The Miller-Rabin probabilistic primality test in C++ w/GMP
C++
30
star
12

libunwind-examples

A few libunwind examples
C++
27
star
13

lua-cpp

Tutorial code for Lua and C++ integration
C++
20
star
14

luajit-cpp

Example C++ shared library loaded in LuaJIT through FFI
C++
19
star
15

brainfuck-jit

Brainfuck JIT VMs
Python
17
star
16

c64-examples

Simple C64 programs compiled from the command line
Assembly
15
star
17

lyn

Python bindings for GNU Lightning
Python
9
star
18

vimp

Command line plugin manager for vim
Python
8
star
19

busy-beaver

Calculates the uncomputable Busy Beaver Ξ£-function
Python
7
star
20

gameboy

A Gameboy emulator in Python
Python
7
star
21

eulers-totient-function

A fast implementation of Euler's totient function phi(n) in C++
C++
7
star
22

skall

SKALL is a minimal, experimental UNIX shell
C
6
star
23

cellular-automaton

Cellular automaton using the HTML5 canvas.
6
star
24

nash-cipher

John Nash's encryption scheme from his 1955 letter to the NSA in C++.
C++
5
star
25

chicken-play

Chicken Scheme HTML5 rendering library
Scheme
3
star
26

poseur

Simple presentation tool
Python
3
star
27

vev

Simple HTTP server request routing in Python
Python
3
star
28

mickey-scheme-historic

Mickey Scheme is an interpreter for R7RS Scheme written in C++
C++
3
star
29

rosalind

Solutions to the Rosalind.info puzzles
C++
2
star
30

impute-me

This is the code behind the www.impute.me site. It contains algorithms for imputing personal genomes, as well as a range of custom-made analysis for genetics-based disease and health.
R
2
star
31

elv

Parses bank CSV files
Python
1
star
32

q

Prints C/C++ definitions from current directory
Python
1
star
33

latex-template

A small LaTeX template to get you started
TeX
1
star
34

presentation-vm

The presentation "How to make a simple virtual machine"
Brainfuck
1
star
35

project-euler

My Project Euler solutions
C++
1
star
36

dotfiles

Collection of my personal dot-files
Shell
1
star
37

armstrong-notes

Notes for M.A. Armstrong's Groups and Symmetry
TeX
1
star
38

py-html-generator

A quick-and-dirty HTML document generator and renderer
Python
1
star
39

callcc-c

Experimental undelimited continuations in C via x86-64 assembly
C
1
star