PageRenderTime 17ms CodeModel.GetById 7ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tools/interpreter/liberty_interpreter_native_array_typed.e

http://github.com/tybor/Liberty
Specman e | 364 lines | 205 code | 23 blank | 136 comment | 11 complexity | 6c2e54e617304553572e052528bdc18a 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_NATIVE_ARRAY_TYPED[E_]
 16
 17inherit
 18   LIBERTY_INTERPRETER_NATIVE_ARRAY
 19
 20creation {LIBERTY_INTERPRETER_NATIVE_ARRAY_CREATOR, LIBERTY_INTERPRETER_NATIVE_ARRAY_TYPED, LIBERTY_INTERPRETER}
 21   make, with_storage
 22
 23feature {ANY}
 24   copy (other: like Current) is
 25      do
 26         interpreter := other.interpreter
 27         accessor := other.accessor
 28         type := other.type
 29         item_type := other.item_type
 30         elements := other.elements
 31      end
 32
 33   is_equal (other: LIBERTY_INTERPRETER_OBJECT): BOOLEAN is
 34      local
 35         o: like Current
 36      do
 37         if other.is_void then
 38            interpreter.fatal_error("Unexpected Void parameter", other.position)
 39         else
 40            o ::= other
 41            Result := elements = o.elements
 42         end
 43      end
 44
 45   hash_code: INTEGER is
 46      do
 47         Result := elements.to_pointer.hash_code
 48      end
 49
 50   item (index: INTEGER): LIBERTY_INTERPRETER_OBJECT is
 51      do
 52         Result := accessor.retrieve(elements.item(index))
 53      end
 54
 55   put (o: LIBERTY_INTERPRETER_OBJECT; index: INTEGER) is
 56      do
 57         elements.put(accessor.store(o), index)
 58      end
 59
 60   first: LIBERTY_INTERPRETER_OBJECT is
 61      do
 62         Result := accessor.retrieve(elements.first)
 63      end
 64
 65   last: LIBERTY_INTERPRETER_OBJECT is
 66      do
 67         Result := accessor.retrieve(elements.last)
 68      end
 69
 70   lower: INTEGER is
 71      do
 72         Result := elements.lower
 73      end
 74
 75   upper: INTEGER is
 76      do
 77         Result := elements.upper
 78      end
 79
 80   count: INTEGER is
 81      do
 82         Result := elements.count
 83      end
 84
 85   is_empty: BOOLEAN is
 86      do
 87         Result := elements.is_empty
 88      end
 89
 90   out_in_tagged_out_memory is
 91      local
 92         i: INTEGER
 93      do
 94         tagged_out_memory.extend('{')
 95         tagged_out_memory.append(type.full_name)
 96         tagged_out_memory.extend('[')
 97         from
 98            i := elements.lower
 99         until
100            i > elements.upper
101         loop
102            if i > elements.lower then
103               tagged_out_memory.append(once ", ")
104            end
105            elements.item(i).out_in_tagged_out_memory
106            i := i + 1
107         end
108         tagged_out_memory.append(once "]}")
109      end
110
111feature {LIBERTY_INTERPRETER_EXTERNAL_TYPE_ANY_BUILTINS} -- Standard builtings
112   builtin_is_equal (other: LIBERTY_INTERPRETER_OBJECT; a_position: LIBERTY_POSITION): BOOLEAN is
113      local
114         i: INTEGER
115         o: like Current
116      do
117         if type = other.type then
118            o ::= other
119            from
120               Result := count = o.count
121               i := lower
122            until
123               i > upper or else not Result
124            loop
125               Result := elements.item(i).is_equal(o.elements.item(i))
126               i := i + 1
127            end
128         else
129            interpreter.fatal_error("Type mismatch: expected " + type.full_name + ", but got " + other.type.full_name, a_position)
130         end
131      end
132
133   builtin_standard_is_equal (other: LIBERTY_INTERPRETER_OBJECT; a_position: LIBERTY_POSITION): BOOLEAN is
134      do
135         Result := builtin_is_equal(other, a_position)
136      end
137
138   builtin_copy (other: LIBERTY_INTERPRETER_OBJECT; a_position: LIBERTY_POSITION) is
139      local
140         o: like Current
141      do
142         if type = other.type then
143            o ::= other
144            elements := o.elements
145         else
146            interpreter.fatal_error("Type mismatch: expected " + type.full_name + ", but got " + other.type.full_name, a_position)
147         end
148      end
149
150   builtin_twin (a_position: LIBERTY_POSITION): like Current is
151      do
152         create Result.with_storage(interpreter, type, item_type, elements, elements.capacity, a_position)
153      end
154
155   builtin_standard_copy (other: LIBERTY_INTERPRETER_OBJECT; a_position: LIBERTY_POSITION) is
156      do
157         builtin_copy(other, a_position)
158      end
159
160   builtin_standard_twin (a_position: LIBERTY_POSITION): like Current is
161      do
162         create Result.with_storage(interpreter, type, item_type, elements, elements.capacity, a_position)
163      end
164
165feature {LIBERTY_INTERPRETER_OBJECT}
166   do_deep_twin (deep_twin_memory: DICTIONARY[LIBERTY_INTERPRETER_OBJECT, LIBERTY_INTERPRETER_OBJECT]; a_position: LIBERTY_POSITION): LIBERTY_INTERPRETER_OBJECT is
167      local
168         i: INTEGER; elements_twin: like elements
169      do
170         Result := deep_twin_memory.reference_at(Current)
171         if Result = Void then
172            create elements_twin.make(elements.capacity)
173            from
174               i := elements.lower
175            until
176               i > elements.upper
177            loop
178               put(item(i).do_deep_twin(deep_twin_memory, a_position), i)
179               i := i + 1
180            end
181            create {LIBERTY_INTERPRETER_NATIVE_ARRAY_TYPED[E_]} Result.with_storage(interpreter, type, item_type, elements_twin, elements.capacity, a_position)
182            deep_twin_memory.put(Result, Current)
183         end
184      end
185
186   do_deep_equal (object: LIBERTY_INTERPRETER_OBJECT; deep_equal_memory: SET[LIBERTY_INTERPRETER_OBJECT]; a_position: LIBERTY_POSITION): BOOLEAN is
187      local
188         i: INTEGER
189         na: LIBERTY_INTERPRETER_NATIVE_ARRAY
190      do
191         if deep_equal_memory.fast_has(Current) then
192            Result := True
193         elseif object.type /= type then
194            interpreter.fatal_error("Type mismatch: expected " + type.full_name + ", but got " + object.type.full_name, a_position)
195         elseif na ?:= object then -- may be Void!
196            na ::= object
197            if na.item_type /= item_type then
198               interpreter.fatal_error("Type mismatch: expected " + type.full_name + ", but got " + object.type.full_name, a_position)
199            else
200               Result := count = na.count
201               from
202                  i := lower
203               until
204                  not Result or else i > upper
205               loop
206                  Result := item(i).do_deep_equal(na.item(i), deep_equal_memory, a_position)
207                  i := i + 1
208               end
209            end
210         end
211         deep_equal_memory.fast_add(Current)
212      end
213
214feature {LIBERTY_INTERPRETER_OBJECT_PRINTER, LIBERTY_INTERPRETER_FEATURE_CALL}
215   show_stack (o: OUTPUT_STREAM; indent: INTEGER) is
216      local
217         e: E_; i: INTEGER
218      do
219         o.put_character('{')
220         o.put_string(type.full_name)
221         o.put_character('<')
222         o.put_character('<')
223         o.put_new_line
224         from
225            i := lower
226         until
227            i > 16 or else i > upper
228         loop
229            e := elements.item(i)
230            interpreter.object_printer.put_indent(o, indent + 1)
231            interpreter.object_printer.print_object(o, accessor.retrieve(e), indent + 1)
232            i := i + 1
233         end
234         if upper > 16 then
235            interpreter.object_printer.put_indent(o, indent + 1)
236            o.put_string(once "... (capacity=")
237            o.put_integer(elements.capacity)
238            o.put_character(')')
239            o.put_new_line
240         end
241         interpreter.object_printer.put_indent(o, indent)
242         o.put_character('>')
243         o.put_character('>')
244         o.put_character('}')
245         o.put_new_line
246      end
247
248feature {LIBERTY_INTERPRETER_EXTERNAL_TYPE_NATIVE_ARRAY_BUILTINS}
249   builtin_element_sizeof: INTEGER is
250      do
251         not_yet_implemented
252      end
253
254   builtin_calloc (capacity: INTEGER; a_position: LIBERTY_POSITION): like Current is
255      do
256         create Result.make(interpreter, type, item_type, default_pointer, capacity, a_position)
257      end
258
259   builtin_slice_copy (at: INTEGER; src: like Current; src_min, src_max: INTEGER) is
260      local
261         i, j: INTEGER
262      do
263         from
264            i := src_min
265            j := at
266         until
267            i > src_max
268         loop
269            put(src.item(i), j)
270            i := i + 1
271            j := j + 1
272         end
273      end
274
275feature {}
276   make (a_interpreter: like interpreter; a_type: like type; a_item_type: like item_type; a_storage: POINTER; a_capacity: INTEGER; a_position: like position) is
277      require
278         a_interpreter /= Void
279         a_type /= Void
280         a_item_type /= Void
281         a_capacity >= 0
282         a_position /= Void
283         a_storage.is_not_null implies a_capacity > 0
284      do
285         interpreter := a_interpreter
286         type := a_type
287         item_type := a_item_type
288         position := a_position
289         if a_storage.is_null then
290            create elements.make(a_capacity)
291         else
292            create elements.from_external(a_storage, a_capacity)
293         end
294         accessor ::= accessor_factory.accessor(a_interpreter, a_item_type, a_position)
295      ensure
296         interpreter = a_interpreter
297         type = a_type
298         item_type = a_item_type
299         position = a_position
300      end
301
302   with_storage (a_interpreter: like interpreter; a_type: like type; a_item_type: like item_type; a_elements: TRAVERSABLE[E_]; a_capacity: INTEGER; a_position: like position) is
303      require
304         a_interpreter /= Void
305         a_elements /= Void
306         a_position /= Void
307         a_capacity >= a_elements.count
308      local
309         i: INTEGER
310      do
311         interpreter := a_interpreter
312         type := a_type
313         item_type := a_item_type
314         position := a_position
315         create elements.make(a_capacity)
316         from
317            i := a_elements.lower
318         until
319            i > a_elements.upper
320         loop
321            elements.put(a_elements.item(i), i + elements.lower - a_elements.lower)
322            i := i + 1
323         end
324         accessor ::= accessor_factory.accessor(a_interpreter, item_type, a_position)
325      ensure
326         interpreter = a_interpreter
327         type = a_type
328         item_type = a_item_type
329         position = a_position
330      end
331
332   expanded_twin: like Current is
333      do
334         Result := twin
335      end
336
337   accessor_factory: LIBERTY_INTERPRETER_NATIVE_ARRAY_ACCESSOR_FACTORY is
338      once
339         create Result.make
340      end
341
342feature {LIBERTY_INTERPRETER_NATIVE_ARRAY_TYPED}
343   accessor: LIBERTY_INTERPRETER_NATIVE_ARRAY_ACCESSOR_TYPED[E_]
344
345feature {LIBERTY_INTERPRETER_NATIVE_ARRAY_TYPED, LIBERTY_INTERPRETER_OBJECT_PRINTER}
346   elements: FAST_ARRAY[E_]
347
348feature {LIBERTY_INTERPRETER_TO_EXTERNAL}
349   to_external: POINTER is
350      do
351         Result := elements.to_external
352      end
353
354feature {LIBERTY_INTERPRETER_NATIVE_ARRAY_CREATOR}
355   from_external (a_external: POINTER; a_capacity: INTEGER) is
356      do
357         elements.from_external(a_external, a_capacity)
358      end
359
360invariant
361   elements /= Void
362   accessor /= Void
363
364end -- class LIBERTY_INTERPRETER_NATIVE_ARRAY_TYPED