summaryrefslogtreecommitdiff
path: root/test/files/run/regularpatmat.scala
blob: cbcc16295240701e2be88936e01ff934fae1c0ec (plain) (blame)
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
// Burak's test suite for regular pattern matching

// contains 17 visitors

// testW? are for recognition only ( no variables )
// testB? are for variables binding

object values  { // test values reused in nearly all test cases

    val s0:List[Char] = Nil ;
    val s1:List[Char] = 'a'::'b'::'c'::Nil ;
    val s2:List[Char] = 'a'::'b'::'c'::s1 ;
    val s3:List[Char] = 'a'::'a'::'a'::Nil ;
    val s4:List[Char] = 'b'::'c'::Nil ;
    val s5:List[Char] = 'b'::Nil ;
    val s6:List[Char] = 'b'::'a'::'a'::'a'::Nil ;

    val s7:List[Int]  = 1::2::7::Nil ;
    val s8:List[Int]  = 1::0::1::0::Nil;
    val s9:List[Int]  = Nil ;

    val s10:List[Char] = '7'::'0'::'1'::'.'::'2'::'4'::Nil ;


}

// not tests matching without binding

// 2do            case [ 'a'; x; y ]   => 100
//                case [ z @ ('a'; x; y) ]   => 100
// 2do             case [ 'a'*; x @ ( _;('a'; 'b')* ); y @ 'b'* ]   => 100
//                case _               => 321 // 20022 // this never happens

object testBK  {

        import values._ ;

        import scala.testing.UnitTest._ ;

    def doit1(e: List[Char]):Int = e.match {

                case List( 'a'*, x @ ( 'a',('a', 'b')* ), y @ ('b'*) )   => { 100 }

                case List( _ * )         => 321
        };

    def test1:Unit = {
                System.out.println("testBK");
                test[List[Char],Int]( doit1, s0, 321);
                test[List[Char],Int]( doit1, s1, 321);
                test[List[Char],Int]( doit1, s2, 321);
                test[List[Char],Int]( doit1, s3, 100);
                test[List[Char],Int]( doit1, s4, 321);
                test[List[Char],Int]( doit1, s5, 321);
                test[List[Char],Int]( doit1, s6, 321)
    };

    def main( args:Array[ String ] ) = {
         test1;
    }

}
object testBL {

  import scala.testing.UnitTest._ ;

  def preTest(a:String,b:String):boolean = (a==b);

  def doit( x:List[String] ):String = x.match {

    case List( z @ "John" ) => z

  }

        // BEWARE: main type should be specified...
        // often, last thing is not () then you have a problem

  def main(args:Array[String]):Unit = {

       val a = "John";
       val b = "John";

        test2[String,String,boolean](preTest,a,b,true);
        test[List[String],String]( doit, List( b ), "John" )


  }
}
object testBM  {

    import scala.testing.UnitTest._ ;
    import values._ ;

    def doit1(e: List[Char]):List[Char] = e.match {

                case List( 'a'*, x @ ( 'a',('a', 'b')* ), y @ ('b'*) )

                        => { x }

                case List( 'a'*, x @ ('a', 'b')* , y @ (('a','b','c') *) )

                        => { y }

                case List( _ * )

                        => Nil
        };

    def test1:Unit = {
                System.out.println("testBM");
                test[List[Char],List[Char]]( doit1, s0, Nil);
                test[List[Char],List[Char]]( doit1, s1, s1);
                test[List[Char],List[Char]]( doit1, s2, s2);

                test[List[Char],List[Char]]( doit1, s3, List('a'));
                test[List[Char],List[Char]]( doit1, s4, Nil);
                test[List[Char],List[Char]]( doit1, s5, Nil);
                test[List[Char],List[Char]]( doit1, s6, Nil);

                val t7:List[Char] = 'a'::'a'::'a'::'b'::'b'::'b'::Nil;
                val t7ex:List[Char] = 'a'::'a'::'b'::Nil;

                test[List[Char],List[Char]]( doit1, t7, t7ex );
                ()
    };

    def main( args:Array[ String ] ) = {
         test1;
    }

}
object testBN {

    import scala.testing.UnitTest._ ;
    import values._ ;

        class testClass;

        case class testA( arg:List[Char] ) extends testClass;

    def doit1(e: testClass):List[Char] = e.match {
                case testA(List( 'a', x, y ))   => x::y::Nil
                case _                      => Nil
        };

    def test1:Unit = {
                System.out.print("BN preTest: ");
                System.out.println( Nil == Nil );
                System.out.println("testBN");

                test[testClass,List[Char]]
                        ( doit1, testA(s0), Nil);

                test[testClass,List[Char]]
                        ( doit1, testA(s1), 'b'::'c'::Nil);

                test[testClass,List[Char]]
                        ( doit1, testA(s2), Nil);

                test[testClass,List[Char]]
                        ( doit1, testA(s3), 'a'::'a'::Nil);

                test[testClass,List[Char]]
                        ( doit1, testA(s4), Nil);

                test[testClass,List[Char]]
                        ( doit1, testA(s5), Nil);

                test[testClass,List[Char]]
                        ( doit1, testA(s6), Nil);

    };


    def main( args:Array[String] ) = {

                test1

    }

}

object testBO  {

        // example suggested by Matthias
        import scala.testing.UnitTest._ ;


        case class Person( firstname:String, lastname:String );

        def onlyJohn( db:List[ Person ] ):List[ String ] = {

           db.match {

                case List( Person( "John", lastname  ) )

                        => lastname::Nil

                case _
                        => Nil

           }

        }

        /** first longest match policy -> the star is greedy/hungry/...
         */

        def searchLastJohn( db:List[ Person ] ):String = {

           db.match {

                case List( _ *, Person( "John", lastname  ), _ * )
                        => lastname

                case _
                        => "<not found>"

           }

        }

        /** first longest match policy -> star is a greedy/hungry
         */

        def searchJohns( db:List[Person]):List[String] = {

           db.match {

                case List( before @ (_ *), Person( "John", lastname  ), _ * )
                        => { //System.out.print("before is : "+before );
                             lastname::searchJohns( before )
                                }

                case _
                        => Nil

           }

        }

        def main( args:Array[String] ) = {

                val p1 = Person("Albert",  "Camus");
                val p2 = Person("Henry",   "Miller");
                val p3 = Person("John",    "Le Carre");
                val p4 = Person("Herbert", "Franke");
                val p5 = Person("John",    "Smith");
                val p6 = Person("William", "Gibson");

                val db:List[Person] = p1::p2::p3::p4::p5::p6::Nil;

                val db2:List[Person] = p3::Nil;

                System.out.println("testBO");

                test[ List[Person], List[ String ]]
                        ( onlyJohn, db, Nil );

                test[ List[Person], List[ String ]]
                        ( onlyJohn, db2, "Le Carre"::Nil );

                test[ List[Person], String ]
                        ( searchLastJohn, db, "Smith" );

                test[ List[Person], String ]
                        ( searchLastJohn, db2, "Le Carre" );

                test[ List[Person], List[ String ]]
                        ( searchJohns, db, "Smith"::"Le Carre"::Nil );

                test[ List[Person], List[ String ]]
                        ( searchJohns, db2, "Le Carre"::Nil );

        }

}
object testWR  {

        import values._ ;

        import scala.testing.UnitTest._ ;

    def doit1(e: List[Char]):Int = e.match {
                case List( 'a', 'b', 'c' ) => 100
                case _                     => 321
        };

    def test1:Unit = {
                System.out.println("testWR_1");
                test[List[Char],Int]( doit1, s0, 321);
                test[List[Char],Int]( doit1, s1, 100);
                test[List[Char],Int]( doit1, s2, 321);
                test[List[Char],Int]( doit1, s3, 321);
                test[List[Char],Int]( doit1, s4, 321);
                test[List[Char],Int]( doit1, s5, 321);
                test[List[Char],Int]( doit1, s6, 321)
    };

    def doit2(e: List[Char]):Int = e.match {
                case List( ('a', 'b','c')? ) => 1000
                case _                       => 321
	}

    def test2:Unit = {
                System.out.println("testWR_2");
                test[List[Char],Int]( doit2, s0, 1000);
                test[List[Char],Int]( doit2, s1, 1000);
                test[List[Char],Int]( doit2, s2, 321);
                test[List[Char],Int]( doit2, s3, 321);
                test[List[Char],Int]( doit2, s4, 321);
                test[List[Char],Int]( doit2, s5, 321);
                test[List[Char],Int]( doit2, s6, 321);
    }


    def doit3(e: List[Char]):String = e.match {
                case List( ('a', 'a','a')? ) => "ok"
                case _                   => "fail"
	}

    def test3:Unit = {
                System.out.println("testWR_3");
                test[List[Char],String]( doit3, s0, "ok");
                test[List[Char],String]( doit3, s1, "fail");
                test[List[Char],String]( doit3, s2, "fail");
                test[List[Char],String]( doit3, s3, "ok");
                test[List[Char],String]( doit3, s4, "fail");
                test[List[Char],String]( doit3, s5, "fail");
                test[List[Char],String]( doit3, s6, "fail");
    }

    def doit4(e: List[Char]):String = e.match {
                case List( ('a'|'b')*,('a'|'b'|'c')+ ) => "ga!!!!"
                case _                             => "gu"
	}

    def test4:Unit = {
                System.out.println("testWR_4");
                test[List[Char],String]( doit4, s0, "gu");
                test[List[Char],String]( doit4, s1, "ga!!!!");
                test[List[Char],String]( doit4, s2, "ga!!!!");
                test[List[Char],String]( doit4, s3, "ga!!!!");
                test[List[Char],String]( doit4, s4, "ga!!!!");
                test[List[Char],String]( doit4, s5, "ga!!!!");
                test[List[Char],String]( doit4, s6, "ga!!!!");
    }

    def doit5(e: List[Int]):String = e.match {
                case List( (0|1)+ )               => "binary"
                case _                        => "not binary"
	}

    def test5:Unit = {
                System.out.println("testWR_5");
                test[List[Int],String]( doit5, s7, "not binary");
                test[List[Int],String]( doit5, s8, "binary");
                test[List[Int],String]( doit5, s9, "not binary");
    }

    //  { ('0'..'9')*;'.';('0'..'9');('0'..'9')* ]
    def doit6(e: List[Char]):String = e.match {
                case List( ('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9')*,
                       '.',
                       ('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'),
                       ('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9')* )

                                              => "decimal number"
                case _                        => "not decimal"
	}

    def test6:Unit = {
                System.out.println("testWR_6");
                test[List[Char],String]( doit6, s3, "not decimal");
                test[List[Char],String]( doit6, s10, "decimal number");
    }

    def main( args:Array[String] ) = {
        test1;
        test2;
        test3;
        test4;
        test5;
        test6
    }



}
object testWS {

        import values._ ;

        import scala.testing.UnitTest._ ;

        /* strings:

            "blabla" == [ "bla";"bla" ] == [ 'b';'l';'a';'b';'l';'a' ]

            [ "blabla";'x';'y'? ] == [ ('b';'l';'a';'b';'l';'a'); 'x'; 'y'? ]

        */

        /*
            def isIdentifierStart(c:char) ;

            case [ ... ; _isIdentifierStart_ ; ... ]

            calls method is..., needs to have type (elementType)Boolean

            translated to pattern

                 [ ... ; Apply(is..., Tree.Empty) ; ... ]

         */

         /* for tree automata:

        [ t0; t1; ...; tn ]  with ti = labeli ( argsi )

        gets translated to

        [ _isTree$0_ ; _isTree$1_ ; ... ; _isTree$n_ ]

        where isTree$i( t ) = t.is[ labeli ] ... (algebraic matcher)

        special case: sequences

        [ ...; seq ; ... ] where seq = [ ... ]

        gets translated to

        [ ...; _seq$0_ ; ...] with seq$0( s ) = t.is[ Sequence ] and
                                                seq$0match( s.newIterator )

        subroutines return
          1) d'abord true or false,
          2) later ( true|false, environment )
                       assume order on variables, enviroment is a tuple/sequence
        */

    def doit1(e: List[Char]):Int = e.match {
                case List( 'a', 'b', 'c' )        => 100
                case List( ('a', 'b','c')? )      => 1004
                case List( ('a', 'a','a')? )      => 50
                case List( ('a'|'b')*,('a'|'b') ) => 700
                case _                        => 321
        };

    def test1:Unit = {
                System.out.println("testWS");
                test[List[Char],Int]( doit1, s0, 1004);
                test[List[Char],Int]( doit1, s1, 100);
                test[List[Char],Int]( doit1, s2, 321);
                test[List[Char],Int]( doit1, s3, 50);
                test[List[Char],Int]( doit1, s4, 321);
                test[List[Char],Int]( doit1, s5, 700);
                test[List[Char],Int]( doit1, s6, 700);
    };

    def main( args:Array[String] ) = {
        test1;
    }



}
object testWT  {

        import values._ ;

        import scala.testing.UnitTest._ ;

    def doit1(e: List[Char]):Int = e.match {
                case List( 'a', _, _ )   => 100
                case List( _ * )         => 321
                case _               => 20022 // this never happens
        };

    def test1:Unit = {
                System.out.println("testWT");
                test[List[Char],Int]( doit1, s0, 321);
                test[List[Char],Int]( doit1, s1, 100);
                test[List[Char],Int]( doit1, s2, 321);
                test[List[Char],Int]( doit1, s3, 100);
                test[List[Char],Int]( doit1, s4, 321);
                test[List[Char],Int]( doit1, s5, 321);
                test[List[Char],Int]( doit1, s6, 321)
    };

    def main( args:Array[ String ] ) = {
         test1;
    }

}
object testWV {

        import values._ ;

        import scala.testing.UnitTest._ ;

        class testClass;

        case class testA( arg:List[Char] ) extends testClass;

    def doit1( e: testClass ):Int = e.match {

                case testA( List( 'a', 'b', 'c' ))        => 100
                case testA( List( ('a', 'b','c')? ))      => 1004
                case testA( List( ('a', 'a','a')? ))      => 50
                case testA( List( ('a'|'b')*,('a'|'b') )) => 700
                case testA( _ )                        => 321

        };

    def test1:Unit = {
                System.out.println("testWV");
                test[testClass,Int]( doit1, testA(s0), 1004);
                test[testClass,Int]( doit1, testA(s1), 100);
                test[testClass,Int]( doit1, testA(s2), 321);
                test[testClass,Int]( doit1, testA(s3), 50);
                test[testClass,Int]( doit1, testA(s4), 321);
                test[testClass,Int]( doit1, testA(s5), 700);
                test[testClass,Int]( doit1, testA(s6), 700);
    };


    def main( args:Array[String] ) = {

                test1

    }

}

object testWW {

        import values._ ;

        import scala.testing.UnitTest._ ;

        class testClass;
        case class testA( arg:List[Char] ) extends testClass;

        def doit1(e: List[testClass]):Int = e.match {

                case List( testA(List()), testA( List( 'a', 'b' )) )        => 100
                case _                                                      => 321

        };

        def test1:Unit = {
                val x1 = List( testA(s0) );

                System.out.println("testWW");

                test[List[testClass],Int]( doit1, x1, 321 );

                val x2 = List( testA(Nil), testA('a'::'b'::Nil) );

                test[List[testClass],Int]( doit1, x2, 100 );

        }

        def main( args:Array[String] ) = {
                test1;
        }

}

object Test {
  def main(args: Array[String]): Unit = {
    testWR.main( args );
    testWS.main( args );
    testWT.main( args );
    testWV.main( args );
    testWW.main( args );
    testBK.main( args );
    testBL.main( args );
    testBM.main( args );
    testBN.main( args );
    testBO.main( args );
    ()
  }
}