koi-vm-ruby

Koi-vm-ruby is a simple proof-of-concept Virtual Machine for the Koi programming language written in Ruby. It was created with learning in mind and is written so that it is easy to understand and play with. This virtual machine implements an instruction set specifically designed for running the Koi programming language. A compiler for producing compatible bytecode can be found at the koi-reference-compiler.

Examples

The standard ‘Hello world!’, KoiVM style:

require 'koi'
include KoiVMRuby

VM.new.run [
  PUSH_STRING, "Hello World!\n",
  PRINT
]

#=> Hello world!

And an old-school ‘Blast off’ example:

require 'koi'
include KoiVMRuby

VM.new.run [
  PUSH_INT, 11,
  PUSH_INT, 1,
  SUBTRACT,
  DUP,
  TO_STRING,
  PRINT,
  PUSH_STRING, ", ",
  PRINT,
  DUP,
  PUSH_INT, 0,
  EQUAL,
  JUMP_UNLESS, -13,
  PUSH_STRING, "Blast Off!\n",
  PRINT
]

#=> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, Blast off!

Installation

This VM is normally installed as part of Koi’s default toolchain. However if you would like to install it on it’s own you can do so by installing the gem like so:

gem install koi-vm-ruby

Usage

require 'rubygems'
require 'koi-vm-ruby'

include KoiVMRuby

vm = VM.new

vm.run( bytecode )

Author & Credits

Author

Aaron Gough

Copyright © 2010 Aaron Gough (thingsaaronmade.com), released under the MIT license