Permalink
Browse files

Implement the bytecode generator.

  • Loading branch information...
1 parent 8e5b949 commit f6d630bd97b1a4504c4a00eef0b7b00d8256b789 @macournoyer macournoyer committed Mar 28, 2012
Showing with 31 additions and 8 deletions.
  1. +31 −8 bytecode_generator.rb
View
@@ -23,43 +23,66 @@ def initialize
end
def compile_all(nodes)
-
+ nodes.each do |node|
+ node.compile(self)
+ end
end
def number_literal(value)
-
+ emit PUSH_NUMBER, literal_index(value)
end
def string_literal(value)
-
+ emit PUSH_STRING, literal_index(value)
end
def true_literal
-
+ emit PUSH_BOOL, 1
end
def false_literal
-
+ emit PUSH_BOOL, 0
end
def nil_literal
-
+ emit PUSH_NIL
end
def set_local(name, value)
-
+ value.compile(self)
+ emit SET_LOCAL, local_index(name)
end
def get_local(name)
end
def call(receiver, method, arguments)
+ if receiver
+ receiver.compile(self)
+ else
+ emit PUSH_SELF
+ end
+
+ arguments.each do |argument|
+ argument.compile(self)
+ end
+ # Sample optimization
+ if method == "+"
+ emit ADD
+ return
+ end
+
+ emit CALL, literal_index(method), arguments.size
end
def if(condition, body)
-
+ condition.compile(self)
+ emit JUMP_UNLESS, 0
+ offset_index = @instructions.size - 1
+ body.compile(self)
+ @instructions[offset_index] = @instructions.size - 1 - offset_index
end
# true if the local variable as been defined

0 comments on commit f6d630b

Please sign in to comment.