public this repo is viewable by everyone
Description: a ruby-to-pyc compiler
Clone URL: git://github.com/why/unholy.git
 * lib/unholy/pyasm.rb: re-merge the line numbers fix.
why (author)
9 days ago
commit  eab2a68059233e78ff25e8271e85988e6015fa61
tree    5235bdc07e381c9797ee35ac724d27ee8b50f62d
parent  3b65c66935a74db4c6580b425afe0518aa59699b
...
26
27
28
29
 
 
30
31
32
...
26
27
28
 
29
30
31
32
33
0
@@ -26,7 +26,8 @@
0
   To compile Ruby to a .pyc:
0
 
0
   > bin/unholy test.rb
0
- > python test.rb.pyc
0
+ > PYTHONPATH=python \
0
+ python test.rb.pyc
0
   
0
             ---
0
 
...
4
5
6
7
8
 
 
9
10
 
11
12
13
 
14
15
16
...
43
44
45
 
 
 
 
 
46
47
48
...
86
87
88
 
89
90
91
...
95
96
97
 
 
 
98
99
100
...
156
157
158
 
 
 
159
160
161
...
227
228
229
230
 
231
232
233
...
268
269
270
271
 
272
273
 
274
275
276
...
336
337
338
339
 
 
 
 
 
 
 
 
340
341
342
...
4
5
6
 
 
7
8
9
 
10
11
12
 
13
14
15
16
...
43
44
45
46
47
48
49
50
51
52
53
...
91
92
93
94
95
96
97
...
101
102
103
104
105
106
107
108
109
...
165
166
167
168
169
170
171
172
173
...
239
240
241
 
242
243
244
245
...
280
281
282
 
283
284
 
285
286
287
288
...
348
349
350
 
351
352
353
354
355
356
357
358
359
360
361
0
@@ -4,13 +4,13 @@ class Pyasm
0
   }
0
 
0
   attr_accessor :argc, :nlocals, :stacksize, :flags, :consts, :bytecode,
0
- :filename, :lineno, :name, :symbols, :stacknow, :varsyms, :jumps, :labels
0
- def initialize(fname, name = "<module>")
0
+ :filename, :lineno, :name, :symbols, :stacknow, :varsyms, :jumps, :labels, :lines
0
+ def initialize(fname, name = "<module>", lineno = 0)
0
     @argc, @nlocals, @stacksize, @flags, @filename, @lineno, @name, @stack, @nopop =
0
- 0, 0, 1, 0x40, fname, 1, name, [], 0
0
+ 0, 0, 1, 0x40, fname, lineno, name, [], 0
0
     @consts = [-1, nil]
0
     @symbols = [:Kernel]
0
- @bytecode, @varsyms, @labels, @jumps = [], [], {}, {}
0
+ @bytecode, @varsyms, @labels, @lines, @jumps = [], [], {}, [], {}
0
   end
0
 
0
   def add_const(obj)
0
@@ -43,6 +43,11 @@ class Pyasm
0
   end
0
 
0
   def pop_top; bc 0x01; dump_stack end
0
+ def binary_add
0
+ add = bc 0x17
0
+ @stack.pop
0
+ add
0
+ end
0
   def ret_val; bc 0x53 end
0
   def build_class; bc 0x59 end
0
   def store_name(name)
0
@@ -86,6 +91,7 @@ class Pyasm
0
     stack_push Object.new, bc(0x7c, n, 0x0)
0
   end
0
   def store_fast(n)
0
+ dump_stack
0
     bc 0x7d, n, 0x0
0
   end
0
   def call_func(arity)
0
@@ -95,6 +101,9 @@ class Pyasm
0
   def make_func(arity)
0
     bc 0x84, arity, 0x0
0
   end
0
+ def line n
0
+ @lines << [n, @bytecode.flatten.length]
0
+ end
0
   def prep_send
0
     @nopop -= 1 if @nopop > 0
0
   end
0
@@ -156,6 +165,9 @@ class Pyasm
0
   def newarray size
0
     build_list size
0
   end
0
+ def opt_plus
0
+ binary_add
0
+ end
0
   def putnil
0
     load_const(nil)
0
   end
0
@@ -227,7 +239,7 @@ class Pyasm
0
     bytes = @bytecode.slice! idx..-1
0
     bytes.shift unless receiver
0
 
0
- asm = Pyasm.new(@filename, id.to_s)
0
+ asm = Pyasm.new(@filename, id.to_s, @lines.last[0])
0
     asm.load_iseq iseq
0
     if type == :class
0
       load_const(id.to_s)
0
@@ -268,9 +280,9 @@ class Pyasm
0
     iseq.last.each do |inst|
0
       case inst
0
       when Integer # line no
0
- nil
0
+ line inst
0
       when Symbol
0
- self.label inst
0
+ label inst
0
       when Array
0
         # p inst
0
         inst[0] = :message if inst[0] == :send
0
@@ -336,7 +348,14 @@ class Pyasm
0
     f << @filename.to_pickle
0
     f << @name.to_pickle
0
     f << 1.to_plong
0
- f << "".to_pickle
0
+
0
+ lnotab = ""
0
+ lastn, lastpos = @lines[0]
0
+ @lines[1..-1].each do |n, pos|
0
+ lnotab << [pos - lastpos, n - lastn].pack("cc")
0
+ lastn, lastpos = n, pos
0
+ end
0
+ f << lnotab.to_pickle
0
     f
0
   end
0
 
...
1
2
3
 
 
 
...
1
2
3
4
5
6
0
@@ -1,3 +1,6 @@
0
 def puts(*args):
0
   for x in args: print x
0
   if not args: print
0
+
0
+class BasicObject:
0
+ pass

Comments

    No one has commented yet.