Coder Social home page Coder Social logo

djhenderson / python-simple-vm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cslarsen/python-simple-vm

0.0 2.0 0.0 148 KB

A simple virtual machine w/constant folding implemented in Python

Home Page: https://csl.name/post/vm/

License: Other

Python 100.00%

python-simple-vm's Introduction

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

python-simple-vm's People

Contributors

cslarsen avatar

Watchers

James Cloos avatar Doug Henderson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.