File: e_loop.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (386 lines) | stat: -rw-r--r-- 11,740 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
-- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries
--
-- SmartEiffel is  free software;  you can redistribute it and/or  modify it
-- under  the terms of the  GNU General Public License, as published by  the
-- Free Software Foundation; either version 2, or (at your option) any later
-- version.
-- SmartEiffel is distributed in the hope that it will be useful but WITHOUT 
-- ANY WARRANTY;  without  even the implied warranty  of MERCHANTABILITY  or
-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
-- more details.  You should have received a copy of  the GNU General Public
-- License along with SmartEiffel;  see the file COPYING.  If not,  write to
-- the Free Software Foundation,  Inc., 59 Temple Place - Suite 330,  Boston, 
-- MA 02111-1307, USA.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
--			   - University of Nancy 1 - FRANCE
-- Copyright(C) 2003:      INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
--			   - University of Nancy 2 - FRANCE
--
--		 Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
--			   Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
class E_LOOP
   --
   -- The Eiffel instruction : "from ... until ... loop ... end".
   --

inherit INSTRUCTION

creation make

feature

   start_position: POSITION
         -- Of letter 'f' of "from".

   initialize: COMPOUND

   invariant_clause: LOOP_INVARIANT

   variant_clause: LOOP_VARIANT

   until_expression: EXPRESSION

   loop_body: COMPOUND

   side_effect_free: BOOLEAN is False
      
   once_pre_computable: BOOLEAN is False

   end_mark_comment: BOOLEAN is True

   afd_check is
      do
         if loop_check then
            if variant_clause /= Void then
               variant_clause.afd_check
            end
            if invariant_clause /= Void then
               invariant_clause.afd_check
            end
         end
         if initialize /= Void then
            initialize.afd_check
         end
         until_expression.afd_check
         if loop_body /= Void then
            loop_body.afd_check
         end
      end

   safety_check is
      do
         if initialize /= Void then
            initialize.safety_check
         end
         until_expression.safety_check
         if loop_body /= Void then
            loop_body.safety_check
         end
      end

   simplify_2(container: COMPOUND; index: INTEGER) is
      local
	 bc: BOOLEAN_CONSTANT
      do
         if initialize /= Void then
            initialize := initialize.simplify_2
         end
         until_expression := until_expression.simplify_2
	 bc ?= until_expression
	 if loop_body /= Void then
            loop_body := loop_body.simplify_2
         end
	 if bc /= Void and then bc.value and then initialize = Void then
	    container.remove(index)
	 end
      end

   collect_c_tmp is
      do
      end

   compile_to_c is
      local
         need_se_tmp, variant_flag, invariant_flag: BOOLEAN
      do
         if loop_check and then variant_clause /= Void then
            cpp.put_string(once "{int c=0;int v=0;%N")
            variant_flag := True
         end
         if initialize /= Void then
            initialize.compile_to_c
         end
         if loop_check and then invariant_clause /= Void then
            invariant_clause.compile_to_c
            invariant_flag := True
         end
         until_expression.collect_c_tmp
         need_se_tmp := cpp.se_tmp_open_declaration
         cpp.put_string(once "while (!(")
         if ace.no_check then
            cpp.trace_boolean_expression(until_expression)
         else
            until_expression.compile_to_c
         end
         cpp.put_string(once "))%N{%N")
         if variant_flag then
            cpp.variant_check(variant_clause.expression)
         end
         if loop_body /= Void then
            loop_body.compile_to_c
         end
         if invariant_flag then
            invariant_clause.compile_to_c
         end
         cpp.put_string(once "}%N")
         if need_se_tmp then
            cpp.se_tmp_close_declaration
         end
         if variant_flag then
            cpp.put_string(fz_12)
         end
      end

   compile_to_jvm is
      local
         until_point, exit_point: INTEGER
         ca: like code_attribute
         counter, previous: INTEGER
      do
         ca := code_attribute
         if loop_check then
            counter := ca.extra_local_size1
            ca.opcode_iconst_0
            ca.opcode_istore(counter)
            previous := ca.extra_local_size1
            ca.opcode_iconst_0
            ca.opcode_istore(previous)
         end
         if initialize /= Void then
            initialize.compile_to_jvm
         end
         if loop_check then
            if invariant_clause /= Void then
               invariant_clause.compile_to_jvm(True)
               ca.opcode_pop
            end
         end
         until_point := ca.program_counter
         exit_point := until_expression.jvm_branch_if_true
         if loop_check then
            if variant_clause /= Void then
               ca.opcode_iload(counter)
               ca.opcode_iload(previous)
               ca.runtime_check_loop_variant(variant_clause.expression)
               ca.opcode_istore(previous)
            end
         end
         if loop_body /= Void then
            loop_body.compile_to_jvm
         end
         if loop_check then
            if invariant_clause /= Void then
               invariant_clause.compile_to_jvm(True)
               ca.opcode_pop
            end
         end
         if loop_check then
            ca.opcode_iinc(counter,1)
         end
         ca.opcode_goto_backward(until_point)
         ca.resolve_u2_branch(exit_point)
      end

   use_current: BOOLEAN is
      do
         if loop_check and then variant_clause /= Void then
            Result := Result or else variant_clause.use_current
         end
         if initialize /= Void then
            Result := Result or else initialize.use_current
         end
         Result := Result or else until_expression.use_current
         if loop_check and then invariant_clause /= Void then
            Result := Result or else invariant_clause.use_current
         end
         if loop_body /= Void then
            Result := Result or else loop_body.use_current
         end
      end

   stupid_switch(run_time_set: RUN_TIME_SET): BOOLEAN is
      do
         Result := 
	    (initialize = Void or else initialize.stupid_switch(run_time_set))
	    and then
	    until_expression.stupid_switch(run_time_set)
	    and then
	    (loop_body = Void or else loop_body.stupid_switch(run_time_set))
      end

   to_runnable(ct: E_TYPE): like Current is
      local
         ue: like until_expression
      do
         if current_type = Void then
            current_type := ct
	    loop_check := ct.base_class.loop_check
            if initialize /= Void then
               initialize := initialize.to_runnable(ct)
               if initialize = Void then
                  error(start_position,"Bad initialisation part.")
               end
            end
            if loop_check and then invariant_clause /= Void then
               invariant_clause := invariant_clause.to_runnable(ct)
               if invariant_clause = Void then
                  error(start_position,"Bad invariant.")
               end
            end
            if loop_check and then variant_clause /= Void then
               variant_clause := variant_clause.to_runnable(ct)
               if variant_clause = Void then
                  error(start_position,"Bad variant for this loop.")
               end
            end
            ue := until_expression.to_runnable(ct)
            if ue /= Void then
               if not ue.result_type.is_boolean then
                  error(ue.start_position,
                        "Expression of until must be BOOLEAN.")
                  error_handler.add_type(ue.result_type,fz_is_not_boolean)
                  error_handler.print_as_error
               end
               until_expression := ue
            else
               error(start_position, "This loop has an invalid expression.")
            end
            if loop_body /= Void then
               loop_body := loop_body.to_runnable(ct)
               if loop_body = Void then
                  error(start_position,"Invalid loop body.")
               end
            end
            if nb_errors = 0 then
               Result := Current
            end
         else
            !!Result.make(start_position,initialize,invariant_clause,
                          variant_clause,until_expression,loop_body)
            Result := Result.to_runnable(ct)
         end
      end

   pretty_print is
      local
         semi_colon_flag: BOOLEAN
      do
         pretty_printer.indent
         pretty_printer.keyword(fz_from)
            if initialize /= Void then
               initialize.pretty_print
            end
         if invariant_clause /= Void then
            invariant_clause.pretty_print
         end
         if variant_clause /= Void then
            pretty_printer.indent
            pretty_printer.keyword(fz_variant)
            pretty_printer.indent_level_increment
            semi_colon_flag := pretty_printer.semi_colon_flag
            pretty_printer.set_semi_colon_flag(False)
            variant_clause.pretty_print
            pretty_printer.set_semi_colon_flag(semi_colon_flag)
            pretty_printer.indent_level_decrement
	 end
         pretty_printer.indent
         pretty_printer.keyword(fz_until)
         pretty_printer.indent_level_increment
         pretty_printer.indent
         pretty_printer.set_semi_colon_flag(False)
         until_expression.pretty_print
         pretty_printer.indent_level_decrement
         pretty_printer.indent
         pretty_printer.keyword(fz_loop)
         pretty_printer.indent
         if loop_body /= Void then
            loop_body.pretty_print
         end
         pretty_printer.indent
         pretty_printer.keyword(fz_end)
         if pretty_printer.print_end_of_statement then
            pretty_printer.put_end_of(fz_loop)
         end
      end

feature {COMPOUND,INSTRUCTION_WITH_COMMENT}

   verify_scoop(allowed: FORMAL_ARG_LIST) is
      local
         dummy: BOOLEAN
      do
         if initialize /= Void then
            initialize.verify_scoop(allowed)
         end
         if invariant_clause /= Void then
            invariant_clause.verify_scoop(allowed)
         end
         if variant_clause /= Void then
            variant_clause.verify_scoop(allowed)
         end
         dummy := until_expression.verify_scoop(allowed)
         if loop_body /= Void then
            loop_body.verify_scoop(allowed)
         end
      end

feature {E_LOOP_VISITOR}

   accept(visitor: E_LOOP_VISITOR) is
      do
         visitor.visit_e_loop(Current)
      end

feature {NONE}

   current_type: E_TYPE

   loop_check: BOOLEAN

   make(sp: like start_position
        i: like initialize
        ic : like invariant_clause
        vc: like variant_clause
        ue : like until_expression
        lb : like loop_body) is
      require
         not sp.is_unknown
         ue /= Void
      do
         start_position := sp
         initialize := i
         invariant_clause := ic
         variant_clause := vc
         until_expression := ue
         loop_body := lb
      ensure
         start_position = sp
         initialize = i
         invariant_clause = ic
         variant_clause = vc
         until_expression = ue
         loop_body = lb
      end

invariant

   not start_position.is_unknown

   until_expression /= Void

end -- E_LOOP