|
21 | 21 | # - support 'include'-ing a module and bringing in variables into namespace |
22 | 22 | # - cosmetic issues in the OPT frontend for Ruby: |
23 | 23 | # - relabel "Global frame" as "Globals" or something |
24 | | -# - display symbol type properly, and maybe range type too? |
25 | 24 | # - display None values as 'nil' |
| 25 | +# - display booleans as 'true' and 'false' |
| 26 | +# - rename 'list' as 'array' |
| 27 | +# - rename 'dict' as 'hash' |
26 | 28 | # |
27 | 29 | # Useful notes from http://phrogz.net/programmingruby/frameset.html: |
28 | 30 | # - "First, every object has a unique object identifier (abbreviated as |
|
41 | 43 |
|
42 | 44 |
|
43 | 45 | require 'json' |
| 46 | +require 'set' |
44 | 47 | require 'stringio' |
45 | 48 |
|
46 | 49 | require 'debug_inspector' # gem install debug_inspector, use on Ruby 2.X |
@@ -110,7 +113,40 @@ def encode(dat) |
110 | 113 | # (this way, 3.0 prints as '3.0' and not as 3, which looks like an int) |
111 | 114 | return dat |
112 | 115 | else |
113 | | - return dat.inspect # TODO: make into a rich object and fill up the heap |
| 116 | + my_id = dat.object_id |
| 117 | + |
| 118 | + my_small_id = @id_to_small_IDs[my_id] |
| 119 | + if !my_small_id |
| 120 | + my_small_id = @cur_small_ID |
| 121 | + @id_to_small_IDs[my_id] = @cur_small_ID |
| 122 | + @cur_small_ID += 1 |
| 123 | + end |
| 124 | + |
| 125 | + ret = ['REF', my_small_id] |
| 126 | + |
| 127 | + # punt early if you've already encoded this object |
| 128 | + return ret if @encoded_heap_objects.include? my_small_id |
| 129 | + |
| 130 | + new_obj = [] |
| 131 | + @encoded_heap_objects[my_small_id] = new_obj |
| 132 | + |
| 133 | + # otherwise encode it in the heap and return ret |
| 134 | + if dat.class == Array |
| 135 | + new_obj << 'LIST' |
| 136 | + dat.each { |e| new_obj << encode(e) } |
| 137 | + elsif dat.class == Hash |
| 138 | + new_obj << 'DICT' |
| 139 | + dat.each { |k, v| new_obj << [encode(k), encode(v)] } |
| 140 | + elsif dat.class == Set |
| 141 | + new_obj << 'SET' |
| 142 | + dat.each { |e| new_obj << encode(e) } |
| 143 | + else |
| 144 | + # catch-all punting case! includes Range, Symbol, Regexp |
| 145 | + new_obj << dat.class.to_s |
| 146 | + new_obj << dat.inspect |
| 147 | + end |
| 148 | + |
| 149 | + return ret |
114 | 150 | end |
115 | 151 | end |
116 | 152 | end |
|
0 commit comments