File: e_when.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 (769 lines) | stat: -rw-r--r-- 21,974 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
-- 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_WHEN
   --
   -- To store a when clause of an inspect instruction.
   --

inherit GLOBALS

creation {EIFFEL_PARSER} make

creation {E_WHEN, WHEN_LIST} from_e_when

feature

   start_position: POSITION
         -- Of the first character of keyword "when".

   header_comment: COMMENT
         -- Of the when clause.

   compound: COMPOUND
         -- Of the when clause if any.

   when_list: WHEN_LIST
         -- Corresponding container when checked.

   use_current: BOOLEAN is
      do
         if compound /= Void then
            Result := compound.use_current
         end
      end

   stupid_switch(run_time_set: RUN_TIME_SET): BOOLEAN is
      do
         Result := (compound = Void 
		    or else 
		    compound.stupid_switch(run_time_set))
      end

   e_inspect: E_INSPECT is
      do
         Result := when_list.e_inspect
      end

feature {WHEN_LIST}

   simplify_2(cc: CHARACTER_CONSTANT; ic: INTEGER_CONSTANT) is
      local
         i: INTEGER
      do
         from
            i := list.lower
         until
            i > list.upper
         loop
            list.item(i).simplify_2(i, cc, ic)
            i := i + 1
         end
	 if compound /= Void then
	    compound := compound.simplify_2
	 end
      end
   
   verify_scoop(allowed: FORMAL_ARG_LIST) is
      local
         i: INTEGER
      do
         if compound /= Void then
            compound.verify_scoop(allowed)
         end
         from
            i := list.upper
         until
            i < list.lower
         loop
            list.item(i).verify_scoop(allowed)
            i := i - 1
         end
      end

feature {E_WHEN_VISITOR}

   accept(visitor: E_WHEN_VISITOR) is
      do
         visitor.visit_e_when(Current)
      end

feature {EIFFEL_PARSER}

   make(sp: like start_position; hc: like header_comment) is
      require
         not sp.is_unknown
      do
         start_position := sp
         header_comment := hc
      ensure
         start_position = sp
      end

feature {WHEN_LIST}

   afd_check is
      do
         if compound /= Void then
            compound.afd_check
         end
      end

   safety_check is
      do
         if compound /= Void then
            compound.safety_check
         end
      end

feature {WHEN_LIST}

   compile_to_jvm(else_position: POSITION; remainder: INTEGER) is
         -- Where `remainder' is the number of E_WHEN after Current.
      local
         point3, point4, bi: INTEGER; must_test: BOOLEAN
	 ca: like code_attribute
      do
         ca := code_attribute
         if remainder > 0 then
            must_test := true
         elseif ace.no_check then
            must_test := true
         else -- boost :
            must_test := not else_position.is_unknown
         end
         points1.clear
         if must_test then
            from
               bi := values.lower
            until
               bi > values.upper
            loop
               if values.item(bi) = values.item(bi+1) then
                  ca.opcode_dup
                  ca.opcode_push_integer(values.item(bi))
                  points1.add_last(ca.opcode_if_icmpeq)
               else
                  ca.opcode_dup
                  ca.opcode_push_integer(values.item(bi))
                  point3 := ca.opcode_if_icmplt
                  ca.opcode_dup
                  ca.opcode_push_integer(values.item(bi+1))
                  points1.add_last(ca.opcode_if_icmple)
                  ca.resolve_u2_branch(point3)
               end
               bi := bi + 2
            end
            point4 := ca.opcode_goto
         end
         ca.resolve_with(points1)
         if compound /= Void then
            compound.compile_to_jvm
         end
         point2 := ca.opcode_goto
         if must_test then
            ca.resolve_u2_branch(point4)
         end
      end

   compile_to_jvm_resolve_branch is
      do
         code_attribute.resolve_u2_branch(point2)
      end

   compile_to_c is
      local
         bi: INTEGER
      do
         cpp.put_string(once "%Nif(")
         from
            bi := values.lower
         until
            bi > values.upper
         loop
            cpp.put_character('(')
            if values.item(bi) = values.item(bi+1) then
               cpp.put_integer(values.item(bi))
               cpp.put_string(once "==")
               cpp.put_inspect
            else
               cpp.put_character('(')
               cpp.put_integer(values.item(bi))
               cpp.put_string(once "<=")
               cpp.put_inspect
               cpp.put_string(fz_39)
               cpp.put_inspect
               cpp.put_string(once "<=")
               cpp.put_integer(values.item(bi+1))
               cpp.put_character(')')
            end
            cpp.put_character(')')
            --
            bi := bi + 2
            if bi < values.upper then
               cpp.put_string(once "||")
            end
         end
         cpp.put_string(once "){%N")
         if compound /= Void then
            compound.compile_to_c
         end
         cpp.put_string(fz_12)
      end

   compile_to_c_switch is
      local
         bi, v: INTEGER
      do
         from
            bi := values.lower
         until
            bi > values.upper
         loop
            from
               v := values.item(bi)
            until
               v > values.item(bi+1)
            loop
               cpp.put_string(once "case ")
               cpp.put_integer(v)
               cpp.put_string(once ":%N")
               v := v + 1
            end
            bi := bi + 2
         end
         if compound /= Void then
            compound.compile_to_c
         end
         cpp.put_string(once "break;%N")
      end

   pretty_print is
      local
         i: INTEGER
      do
         pretty_printer.keyword(fz_when)
         pretty_printer.indent_level_increment
         if header_comment /= Void then
            header_comment.pretty_print
         end
         if list /= Void then
            from
               i := list.lower
            until
               i > list.upper
            loop
               list.item(i).pretty_print
               i := i + 1
               if i <= list.upper then
                  pretty_printer.put_character(',')
		  if not pretty_printer.zen_mode then
		     pretty_printer.put_character(' ')
		  end
               end
            end
         end
         pretty_printer.indent_level_decrement
         pretty_printer.keyword(fz_then)
         if compound /= Void then
            compound.pretty_print
         end
      end

   includes_integer(v: INTEGER): BOOLEAN is
      local
         i: INTEGER
      do
         if
            values /= Void
               and then
            v >= values.item(values.lower)
               and then
            v <= values.item(values.upper)
         then
            from
               i := values.lower
            until
               Result or else i > values.upper
            loop
               Result := values.item(i) <= v and v <= values.item(i+1)
               i := i + 2
            end
         end
      end

   includes_integer_between(l,u: INTEGER): BOOLEAN is
      require
         invalid_range: l < u
      local
         i: INTEGER
      do
         if
            values /= Void
               and then
            u >= values.item(values.lower)
               and then
            l <= values.item(values.upper)
         then
            from
               i := values.lower
            until
               Result or else i > values.upper
            loop
               Result := values.item(i) <= u and l <= values.item(i+1)
               i := i + 2
            end
         end
      end

   largest_range_of_values: INTEGER is
         -- The largest number of consecutive values - returns 0 if none
      local
         i, range: INTEGER
      do
         if values /= Void then
            from
               i := values.lower
            until
               i > values.upper
            loop
               range := values.item(i+1) - values.item(i) + 1
               if range > Result then
                  Result := range
               end
               i := i + 2
            end
         end
      end

feature {WHEN_LIST, E_WHEN}

   to_runnable_integer(wl: like when_list): like Current is
      require
         wl /= Void
      local
         ct: E_TYPE; ne, i: INTEGER; when_item: WHEN_ITEM
      do
         if when_list = Void then
            ne := nb_errors
            when_list := wl
            if list = Void then
               error_handler.add_position(e_inspect.start_position)
               error(start_position,em2)
            else
               from
                  i := list.lower
               until
                  i > list.upper or else nb_errors - ne > 0
               loop
                  when_item := list.item(i).to_runnable_integer(Current)
                  if when_item = Void then
                     error(start_position,em1)
                  else
                     list.put(when_item,i)
                  end
                  i := i + 1
               end
            end
            if compound /= Void then
               ct := smart_eiffel.top_rf.current_type
               compound := compound.to_runnable(ct)
               if compound = Void then
                  error(start_position,em1)
               end
            end
            Result := Current
         else
            !!Result.from_e_when(Current)
            Result := Result.to_runnable_integer(wl)
         end
      ensure
         Result.when_list = wl
      end

   to_runnable_character(wl: like when_list): like Current is
      require
         wl /= Void
      local
         ct: E_TYPE; ne, i: INTEGER; when_item: WHEN_ITEM
      do
         if when_list = Void then
            ne := nb_errors
            when_list := wl
            if list = Void then
               error_handler.add_position(e_inspect.start_position)
               error(start_position,em2)
            else
               from
                  i := list.lower
               until
                  i > list.upper or else nb_errors - ne > 0
               loop
                  when_item := list.item(i).to_runnable_character(Current)
                  if when_item = Void then
                     error(start_position,em1)
                  else
                     list.put(when_item,i)
                  end
                  i := i + 1
               end
            end
            if compound /= Void then
               ct := smart_eiffel.top_rf.current_type
               compound := compound.to_runnable(ct)
               if compound = Void then
                  error(start_position,em1)
               end
            end
            Result := Current
         else
            !!Result.from_e_when(Current)
            Result := Result.to_runnable_character(wl)
         end
      ensure
         Result.when_list = wl
      end

   to_runnable_string(wl: like when_list): like Current is
      require
         wl /= Void;
      local
         ct: E_TYPE;
         ne, i: INTEGER
         when_item: WHEN_ITEM;
      do
         if when_list = Void then
            ne := nb_errors;
            when_list := wl;
            if list = Void then
               error_handler.add_position(e_inspect.start_position);
               error(start_position,em2);
            else
               from
                  i := list.lower;
               until
                  i > list.upper or else nb_errors - ne > 0
               loop
                  when_item := list.item(i).to_runnable_string(Current);
                  if when_item = Void then
                     error(start_position,em1);
                  else
                     list.put(when_item,i);
                  end;
                  i := i + 1;
               end;
            end;
            if compound /= Void then
               ct := smart_eiffel.top_rf.current_type;
               compound := compound.to_runnable(ct);
               if compound = Void then
                  error(start_position,em1);
               end;
            end;
            Result := Current;
         else
            !!Result.from_e_when(Current);
            Result := Result.to_runnable_string(wl);
         end;
      ensure
         Result.when_list = wl
      end;

feature {WHEN_ITEM_1, WHEN_ITEM_2}

   always_selected(index: INTEGER) is
      do
	 if compound /= Void then
	    compound := compound.simplify_2
	 end
	 when_list.always_selected(compound)
      end

feature {WHEN_ITEM_1}

   add_when_item_1(wi1: WHEN_ITEM_1) is
      require
         wi1 /= Void
      local
         i, v: INTEGER
      do
         v := wi1.expression_value
         if e_inspect.includes(v) then
            err_occ(v,wi1.start_position)
         elseif values = Void then
            !!values.make(501,502)
            values.put(v,values.lower)
            values.put(v,values.upper)
         else
            i := locate_in_values(v)
            if i = values.lower then  -- v is lower than lowest value
               if v = values.item(i) - 1 then  -- just change lower
                  values.put(v,i)
               else
                  values.resize(values.lower-2, values.upper)
                  values.put(v,values.lower)
                  values.put(v,values.lower+1)
               end
            elseif i > values.upper then  -- v is higher than highest value
               if v = values.item(i-1) + 1 then  -- just change upper
                  values.put(v,i-1)
               else
                  values.resize(values.lower, values.upper+2)
                  values.put(v,values.upper-1)
                  values.put(v,values.upper)
               end
            else
               if v = values.item(i-1) + 1 and v = values.item(i) - 1 then
                  values.put(values.item(i+1),i-1)
                  from until i > values.upper-2 loop
                     values.put(values.item(i+2),i)
                     values.put(values.item(i+3),i+1)
                     i := i + 2
                  end
                  values.resize(values.lower, values.upper-2)
               elseif v = values.item(i-1) + 1 then  -- just change upper
                  values.put(v,i-1)
               elseif v = values.item(i) - 1 then  -- just change lower
                  values.put(v,i)
               else
                  values.resize(values.lower, values.upper+2)
                  from
                     i := values.upper - 1
                  until
                     v > values.item(i-1)
                  loop
                     values.put(values.item(i-2),i)
                     values.put(values.item(i-1),i+1)
                     i := i - 2
                  end
                  values.put(v,i)
                  values.put(v,i+1)
               end
            end
         end
      ensure
         e_inspect.includes(wi1.expression_value)
      end

feature {WHEN_ITEM_2}

   add_when_item_2(wi2: WHEN_ITEM_2) is
      require
         wi2 /= Void
      local
         l, u, i: INTEGER
      do
         l := wi2.lower_value
         u := wi2.upper_value
         if l >= u then
            error(wi2.start_position,"Not a good slice.")
         elseif e_inspect.includes_between(l,u) then
            from
               i := l
            until
               e_inspect.includes(i)  -- try to locate the exact problem
            loop
                if i = l then
                   l := l + 1
                   i := u
                else
                   u := u - 1
                   i := l
                end
            end
            err_occ(i,wi2.start_position)
         elseif values = Void then
            !!values.make(501,502)
            values.put(l,values.lower)
            values.put(u,values.upper)
         else
            i := locate_in_values(l)
            if i = values.lower then  -- l and u are lower than lowest value
               if u = values.item(i) - 1 then  -- just change lower
                  values.put(l,i)
               else
                  values.resize(values.lower-2, values.upper)
                  values.put(l,values.lower)
                  values.put(u,values.lower+1)
               end
            elseif i > values.upper then  -- l and u are higher than highest
               if l = values.item(i-1) + 1 then  -- just change upper
                  values.put(u,i-1)
               else
                  values.resize(values.lower, values.upper+2)
                  values.put(l,values.upper-1)
                  values.put(u,values.upper)
               end
            else
               if l = values.item(i-1) + 1 and u = values.item(i) - 1 then
                  values.put(values.item(i+1),i-1)
                  from until i > values.upper-2 loop
                     values.put(values.item(i+2),i)
                     values.put(values.item(i+3),i+1)
                     i := i + 2
                  end
                  values.resize(values.lower, values.upper-2)
               elseif l = values.item(i-1) + 1 then  -- just change upper
                  values.put(u,i-1)
               elseif u = values.item(i) - 1 then  -- just change lower
                  values.put(l,i)
               else
                  values.resize(values.lower, values.upper+2)
                  from
                     i := values.upper - 1
                  until
                     l > values.item(i-1)
                  loop
                     values.put(values.item(i-2),i)
                     values.put(values.item(i-1),i+1)
                     i := i - 2
                  end
                  values.put(l,i)
                  values.put(u,i+1)
               end
            end
         end
      end

feature {EIFFEL_PARSER}

   add_value(v: EXPRESSION) is
      require
         v /= Void
      local
         element: WHEN_ITEM
      do
         !WHEN_ITEM_1!element.make(v)
         if list = Void then
            !!list.with_capacity(4,1)
	 end
	 list.add_last(element)
      end

   add_slice(min, max: EXPRESSION) is
      require
         min /= Void
         max /= Void
      local
         element: WHEN_ITEM
      do
         if min.is_manifest_string then
            error_handler.add_position(min.start_position)
            fatal_error(once "Cannot use '..' with manifest strings")
         elseif max.is_manifest_string then
            error_handler.add_position(max.start_position)
            fatal_error(once "Cannot use '..' with manifest strings")
         else
            !WHEN_ITEM_2!element.make(min,max)
            if list = Void then
               !!list.with_capacity(4,1)
            end
            list.add_last(element)
         end
      end

   set_compound(c: like compound) is
      do
         compound := c
      ensure
         compound = c
      end

feature {E_WHEN, MANIFEST_STRING_INSPECTOR}

   list: ARRAY[WHEN_ITEM]

feature {NONE}

   values: ARRAY[INTEGER]
	 -- To store pairs of range values.

   points1: FIXED_ARRAY[INTEGER] is
         -- To reach the `compound'.
      once
         !!Result.with_capacity(12)
      end

   point2: INTEGER
         -- To go outside the E_INSPECT.

   from_e_when(other: like Current) is
      local
         i: INTEGER
         when_item: WHEN_ITEM
      do
         start_position := other.start_position
         list := other.list.twin
         from
            i := list.lower
         until
            i > list.upper
         loop
            when_item := list.item(i).twin
            when_item.clear_e_when
            list.put(when_item,i)
            i := i + 1
         end
         header_comment := other.header_comment
         compound := other.compound
      end

   err_occ(v: INTEGER; p: POSITION) is
      do
         error_handler.add_position(e_inspect.start_position)
         error_handler.append("Second occurrence for this value (")
         error_handler.append(v.to_string)
         error(p,") in the same inspect.")
      end

   locate_in_values(v: INTEGER): INTEGER is
        -- returns index in values table where v would be inserted
      require
         values /= Void
      do
         from
            Result := values.lower
         until
            Result > values.upper
               or else
            v < values.item(Result)
         loop
            Result := Result + 2
         end
      ensure
         (Result - values.lower) \\ 2 = 0
         Result < values.upper implies v < values.item(Result)
         Result > values.upper implies v > values.item(Result-1)
      end

   em1: STRING is "Bad when clause."
   em2: STRING is "Empty when clause in inspect."

end -- E_WHEN