Skip to content

Instantly share code, notes, and snippets.

@darkf

darkf/vm_draft.txt

Created Jan 31, 2012
Embed
What would you like to do?
Draft of a VM for a game modeling language
This is the virtual machine for a pure OO language based on game creation.
It is, at essence, a stack machine. There are no registers.
Instructions only have one argument, which can be an integer or a string.
Function arguments are pushed in reverse order, C-style. (e.g. x(1,2,3) is 3 2 1 x on the stack)
Methods always have at least one argument -- "self", which is just the current object.
Instructions:
sys_init ; initialize system stuff
; x <- Object()
load_global "Object"
construct_object ; basically new()
dup
load_attr "__init__" ; load up the constructor
call_function 1 ; call the constructor with self
store_local "x" ; store it in x
; x is Circle(5)
load_global "Circle"
load_attr "__init__"
load_const 5
load_local "x"
call_function 2
pop ; unused return value
; on sys:init do
; sys.print "hi"
; end
load_global "sys"
dup
load_event "init"
register_event 0 ; no args (pushes the event back onto the stack)
register_event_ptr 0xdeadbabe (ptr to the function body)
at PC 0xdeadbabe:
enter_block
; pop args (here we have self, which is sys, but we're too lazy for that)
pop
load_const "hi"
load_global "sys"
dup
load_attr "print"
call_function 1
leave_block
push_nil ; return value
return ; return from function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.