PageRenderTime 16ms CodeModel.GetById 6ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tools/interpreter/liberty_interpreter_object_printer.e

http://github.com/tybor/Liberty
Specman e | 288 lines | 239 code | 34 blank | 15 comment | 6 complexity | c74b5bbffeb2ee463644234c46778a00 MD5 | raw file
  1-- This file is part of Liberty Eiffel.
  2--
  3-- Liberty Eiffel is free software: you can redistribute it and/or modify
  4-- it under the terms of the GNU General Public License as published by
  5-- the Free Software Foundation, version 3 of the License.
  6--
  7-- Liberty Eiffel is distributed in the hope that it will be useful,
  8-- but WITHOUT ANY WARRANTY; without even the implied warranty of
  9-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10-- GNU General Public License for more details.
 11--
 12-- You should have received a copy of the GNU General Public License
 13-- along with Liberty Eiffel.  If not, see <http://www.gnu.org/licenses/>.
 14--
 15class LIBERTY_INTERPRETER_OBJECT_PRINTER
 16
 17inherit
 18   LIBERTY_TYPE_VISITOR
 19
 20creation {LIBERTY_INTERPRETER}
 21   make
 22
 23feature {ANY}
 24   print_object (o: like stream; obj: like object; i: like indent) is
 25      do
 26         if obj = Void then
 27            o.put_line(once "<Void>")
 28         elseif obj.is_void then
 29            o.put_line(once "Void")
 30         else
 31            stream := o
 32            object := obj
 33            indent := i
 34            obj.type.accept(Current)
 35            stream := Void
 36            object := Void
 37         end
 38      end
 39
 40   put_indent (o: OUTPUT_STREAM; tabs: like indent) is
 41         -- `show_stack' helper.
 42      local
 43         i: INTEGER
 44      do
 45         from
 46            i := 1
 47         until
 48            i > tabs
 49         loop
 50            o.put_string(once "   ")
 51            i := i + 1
 52         end
 53      end
 54
 55feature {LIBERTY_UNIVERSE}
 56   visit_type_any (type: LIBERTY_ACTUAL_TYPE) is
 57      do
 58         object.show_stack(stream, indent)
 59      end
 60
 61   visit_type_arguments (type: LIBERTY_ACTUAL_TYPE) is
 62      do
 63         object.show_stack(stream, indent)
 64      end
 65
 66   visit_type_platform (type: LIBERTY_ACTUAL_TYPE) is
 67      do
 68         object.show_stack(stream, indent)
 69      end
 70
 71   visit_type_pointer (type: LIBERTY_ACTUAL_TYPE) is
 72      do
 73         stream.put_character('@')
 74         object.show_stack(stream, indent)
 75      end
 76
 77   visit_type_integer_64 (type: LIBERTY_ACTUAL_TYPE) is
 78      do
 79         object.show_stack(stream, indent)
 80      end
 81
 82   visit_type_integer_32 (type: LIBERTY_ACTUAL_TYPE) is
 83      do
 84         object.show_stack(stream, indent)
 85      end
 86
 87   visit_type_integer_16 (type: LIBERTY_ACTUAL_TYPE) is
 88      do
 89         object.show_stack(stream, indent)
 90      end
 91
 92   visit_type_integer_8 (type: LIBERTY_ACTUAL_TYPE) is
 93      do
 94         object.show_stack(stream, indent)
 95      end
 96
 97   visit_type_real_64 (type: LIBERTY_ACTUAL_TYPE) is
 98      do
 99         object.show_stack(stream, indent)
100      end
101
102   visit_type_real_32 (type: LIBERTY_ACTUAL_TYPE) is
103      do
104         object.show_stack(stream, indent)
105      end
106
107   visit_type_real_80 (type: LIBERTY_ACTUAL_TYPE) is
108      do
109         object.show_stack(stream, indent)
110      end
111
112   visit_type_real_128 (type: LIBERTY_ACTUAL_TYPE) is
113      do
114         object.show_stack(stream, indent)
115      end
116
117   visit_type_character (type: LIBERTY_ACTUAL_TYPE) is
118      local
119         code: INTEGER; char: LIBERTY_INTERPRETER_OBJECT_NATIVE[CHARACTER]
120      do
121         char ::= object
122         inspect
123            char.item
124         when '%N' then
125            stream.put_line(once "'%%N'")
126         when '%T' then
127            stream.put_line(once "'%%T'")
128         when '%R' then
129            stream.put_line(once "'%%R'")
130         when '%%' then
131            stream.put_line(once "'%%%%'")
132         when '%'' then
133            stream.put_line(once "'%%''")
134         else
135            code := char.item.code
136            if code < ' '.code then
137               stream.put_string(once "'%%/")
138               stream.put_integer(code)
139               stream.put_line(once "/'")
140            else
141               stream.put_character('%'')
142               stream.put_character(char.item)
143               stream.put_character('%'')
144               stream.put_new_line
145            end
146         end
147      end
148
149   visit_type_string (type: LIBERTY_ACTUAL_TYPE) is
150      local
151         string: LIBERTY_INTERPRETER_OBJECT_STRUCTURE
152         i, n: INTEGER; storage: LIBERTY_INTERPRETER_NATIVE_ARRAY_TYPED[CHARACTER]
153         count: LIBERTY_INTERPRETER_OBJECT_NATIVE[INTEGER_64]
154         c: CHARACTER; multiline: BOOLEAN
155      do
156         string ::= object
157         if string.has_attribute(storage_attribute) and then string.has_attribute(count_attribute) then
158            storage ::= string.attribute_object(storage_attribute)
159            count ::= string.attribute_object(count_attribute)
160            n := count.item.to_integer_32 - 1
161            multiline := storage.elements.fast_has('%N')
162            if multiline then
163               stream.put_line(once "%"[")
164            else
165               stream.put_character('"')
166            end
167            check
168               storage.elements.lower = 0
169            end
170            from
171               i := 0
172            until
173               i > n
174            loop
175               c := storage.elements.item(i)
176               inspect
177                  c
178               when  '%N' then
179                  stream.put_new_line
180                  put_indent(stream, indent + 1)
181               when '%R' then
182                  stream.put_string(once "%%R")
183               when '%T' then
184                  stream.put_string(once "%%T")
185               when '%%' then
186                  stream.put_string(once "%%%%")
187               when '"' then
188                  stream.put_string(once "%%%"")
189               else
190                  if c.code < ' '.code then
191                     stream.put_character('%%')
192                     stream.put_character('/')
193                     stream.put_integer(c.code)
194                     stream.put_character('/')
195                  else
196                     stream.put_character(c)
197                  end
198               end
199               i := i + 1
200            end
201            if multiline then
202               stream.put_new_line
203               put_indent(stream, indent)
204               stream.put_string(once "%"]")
205            else
206               stream.put_character('"')
207            end
208         end
209         string.show_stack(stream, indent)
210      end
211
212   visit_type_boolean (type: LIBERTY_ACTUAL_TYPE) is
213      do
214         object.show_stack(stream, indent)
215      end
216
217   visit_type_native_array (type: LIBERTY_ACTUAL_TYPE) is
218      do
219         object.show_stack(stream, indent)
220      end
221
222   visit_type_tuple (type: LIBERTY_ACTUAL_TYPE) is
223      do
224         object.show_stack(stream, indent)
225      end
226
227   visit_type_routine (type: LIBERTY_ACTUAL_TYPE) is
228      do
229         object.show_stack(stream, indent)
230      end
231
232   visit_type_procedure (type: LIBERTY_ACTUAL_TYPE) is
233      do
234         object.show_stack(stream, indent)
235      end
236
237   visit_type_function (type: LIBERTY_ACTUAL_TYPE) is
238      do
239         object.show_stack(stream, indent)
240      end
241
242   visit_type_predicate (type: LIBERTY_ACTUAL_TYPE) is
243      do
244         object.show_stack(stream, indent)
245      end
246
247   visit_user_type (type: LIBERTY_ACTUAL_TYPE) is
248      do
249         object.show_stack(stream, indent)
250      end
251
252feature {LIBERTY_VOID_TYPE}
253   visit_void (type: LIBERTY_VOID_TYPE) is
254      do
255         check object.is_void end
256         object.show_stack(stream, indent)
257      end
258
259feature {}
260   make (a_interpreter: like interpreter) is
261      require
262         a_interpreter /= Void
263      do
264         interpreter := a_interpreter
265      ensure
266         interpreter = a_interpreter
267      end
268
269   interpreter: LIBERTY_INTERPRETER
270
271   object: LIBERTY_INTERPRETER_OBJECT
272   indent: INTEGER
273   stream: OUTPUT_STREAM
274
275   storage_attribute: FIXED_STRING is
276      once
277         Result := "storage".intern
278      end
279
280   count_attribute: FIXED_STRING is
281      once
282         Result := "count".intern
283      end
284
285invariant
286   interpreter /= Void
287
288end -- class LIBERTY_INTERPRETER_OBJECT_PRINTER