Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* lib/unholy/pyasm.rb: re-merge the line numbers fix.

  • Loading branch information...
commit eab2a68059233e78ff25e8271e85988e6015fa61 1 parent 3b65c66
_why authored
Showing with 32 additions and 9 deletions.
  1. +2 −1  README
  2. +27 −8 lib/unholy/pyasm.rb
  3. +3 −0  python/Kernel.py
View
3  README
@@ -26,7 +26,8 @@
To compile Ruby to a .pyc:
> bin/unholy test.rb
- > python test.rb.pyc
+ > PYTHONPATH=python \
+ python test.rb.pyc
---
View
35 lib/unholy/pyasm.rb
@@ -4,13 +4,13 @@ class Pyasm
}
attr_accessor :argc, :nlocals, :stacksize, :flags, :consts, :bytecode,
- :filename, :lineno, :name, :symbols, :stacknow, :varsyms, :jumps, :labels
- def initialize(fname, name = "<module>")
+ :filename, :lineno, :name, :symbols, :stacknow, :varsyms, :jumps, :labels, :lines
+ def initialize(fname, name = "<module>", lineno = 0)
@argc, @nlocals, @stacksize, @flags, @filename, @lineno, @name, @stack, @nopop =
- 0, 0, 1, 0x40, fname, 1, name, [], 0
+ 0, 0, 1, 0x40, fname, lineno, name, [], 0
@consts = [-1, nil]
@symbols = [:Kernel]
- @bytecode, @varsyms, @labels, @jumps = [], [], {}, {}
+ @bytecode, @varsyms, @labels, @lines, @jumps = [], [], {}, [], {}
end
def add_const(obj)
@@ -43,6 +43,11 @@ def mark_jump n, bc
end
def pop_top; bc 0x01; dump_stack end
+ def binary_add
+ add = bc 0x17
+ @stack.pop
+ add
+ end
def ret_val; bc 0x53 end
def build_class; bc 0x59 end
def store_name(name)
@@ -86,6 +91,7 @@ def load_fast(n)
stack_push Object.new, bc(0x7c, n, 0x0)
end
def store_fast(n)
+ dump_stack
bc 0x7d, n, 0x0
end
def call_func(arity)
@@ -95,6 +101,9 @@ def call_func(arity)
def make_func(arity)
bc 0x84, arity, 0x0
end
+ def line n
+ @lines << [n, @bytecode.flatten.length]
+ end
def prep_send
@nopop -= 1 if @nopop > 0
end
@@ -156,6 +165,9 @@ def leave
def newarray size
build_list size
end
+ def opt_plus
+ binary_add
+ end
def putnil
load_const(nil)
end
@@ -227,7 +239,7 @@ def define type, id, iseq, is_singleton
bytes = @bytecode.slice! idx..-1
bytes.shift unless receiver
- asm = Pyasm.new(@filename, id.to_s)
+ asm = Pyasm.new(@filename, id.to_s, @lines.last[0])
asm.load_iseq iseq
if type == :class
load_const(id.to_s)
@@ -268,9 +280,9 @@ def load_iseq iseq
iseq.last.each do |inst|
case inst
when Integer # line no
- nil
+ line inst
when Symbol
- self.label inst
+ label inst
when Array
# p inst
inst[0] = :message if inst[0] == :send
@@ -336,7 +348,14 @@ def to_pickle
f << @filename.to_pickle
f << @name.to_pickle
f << 1.to_plong
- f << "".to_pickle
+
+ lnotab = ""
+ lastn, lastpos = @lines[0]
+ @lines[1..-1].each do |n, pos|
+ lnotab << [pos - lastpos, n - lastn].pack("cc")
+ lastn, lastpos = n, pos
+ end
+ f << lnotab.to_pickle
f
end
View
3  python/Kernel.py
@@ -1,3 +1,6 @@
def puts(*args):
for x in args: print x
if not args: print
+
+class BasicObject:
+ pass
Please sign in to comment.
Something went wrong with that request. Please try again.