cw2_marking/c4.sc
changeset 978 8778d23fef92
equal deleted inserted replaced
977:1e6eca42d90b 978:8778d23fef92
       
     1 // for testing tokenisation
       
     2 
       
     3 import scala.util.{Try, Success, Failure}
       
     4 import $file.cw024_add
       
     5 import cw024_add._
       
     6 
       
     7 def listcmp[A](xs: List[A], ys: List[A]): List[A] = (xs, ys) match {
       
     8   case (Nil, Nil) => Nil
       
     9   case (x::xs, y::ys) => if (x == y) listcmp(xs, ys) else (x::xs)
       
    10   case _ => xs
       
    11 } 
       
    12 
       
    13 
       
    14 // some students eliminated "..." therefore a specific comparison function
       
    15 def lsteq(xs: List[Token], ys: List[Token]): Boolean = (xs, ys) match {
       
    16   case (Nil, Nil) => true
       
    17   case (T_STRING(s1)::xs, T_STRING(s2)::ys) => 
       
    18     (s1 == s2 || s2.drop(1).dropRight(1) == s1) && lsteq(xs, ys)
       
    19   case (x::xs, y::ys) => x == y && lsteq(xs, ys)
       
    20   case _ => false
       
    21 } 
       
    22 
       
    23 // some students kept string T_Num
       
    24 import scala.language.implicitConversions
       
    25 given Conversion[Int, String] = (n => n.toString)
       
    26 given Conversion[String,Char] = (s => s.head)
       
    27 
       
    28 // programs to test (and number of toplevel definitions)
       
    29 //val uregs = List((RECD("x", PLUS(CHAR('a'))), "aaa")) 
       
    30 
       
    31 val tks = 
       
    32   List(T_ID("bnd"), 
       
    33        T_OP(":="), 
       
    34        T_NUM(1), 
       
    35        T_SEMI, 
       
    36        T_KEYWORD("while"), 
       
    37        T_ID("bnd"), 
       
    38        T_OP("<"), 
       
    39        T_NUM(101), 
       
    40        T_KEYWORD("do"), 
       
    41        T_PAREN("{"), 
       
    42        T_KEYWORD("write"), 
       
    43        T_ID("bnd"), 
       
    44        T_SEMI, 
       
    45        T_KEYWORD("write"), 
       
    46        T_STRING("\": \""), 
       
    47        T_SEMI, 
       
    48        T_ID("n"), 
       
    49        T_OP(":="), 
       
    50        T_ID("bnd"), 
       
    51        T_SEMI, 
       
    52        T_ID("cnt"), 
       
    53        T_OP(":="), 
       
    54        T_NUM(0), 
       
    55        T_SEMI, 
       
    56        T_KEYWORD("while"), 
       
    57        T_ID("n"), 
       
    58        T_OP(">"), 
       
    59        T_NUM(1), 
       
    60        T_KEYWORD("do"), 
       
    61        T_PAREN("{"), 
       
    62        T_KEYWORD("write"), 
       
    63        T_ID("n"), 
       
    64        T_SEMI, 
       
    65        T_KEYWORD("write"), 
       
    66        T_STRING("\",\""), 
       
    67        T_SEMI, 
       
    68        T_KEYWORD("if"), 
       
    69        T_ID("n"), 
       
    70        T_OP("%"), 
       
    71        T_NUM(2), 
       
    72        T_OP("=="), 
       
    73        T_NUM(0), 
       
    74        T_KEYWORD("then"), 
       
    75        T_ID("n"), 
       
    76        T_OP(":="), 
       
    77        T_ID("n"), 
       
    78        T_OP("/"), 
       
    79        T_NUM(2), 
       
    80        T_KEYWORD("else"), 
       
    81        T_ID("n"), 
       
    82        T_OP(":="), 
       
    83        T_NUM(3), 
       
    84        T_OP("*"), 
       
    85        T_ID("n"), 
       
    86        T_OP("+"), 
       
    87        T_NUM(1), 
       
    88        T_SEMI, 
       
    89        T_ID("cnt"), 
       
    90        T_OP(":="), 
       
    91        T_ID("cnt"), 
       
    92        T_OP("+"), 
       
    93        T_NUM(1), 
       
    94        T_PAREN("}"), 
       
    95        T_SEMI, 
       
    96        T_KEYWORD("write"), 
       
    97        T_STRING("\" => \""), 
       
    98        T_SEMI, 
       
    99        T_KEYWORD("write"), 
       
   100        T_ID("cnt"), 
       
   101        T_SEMI, 
       
   102        T_KEYWORD("write"), 
       
   103        T_STRING("\"\\n\""), 
       
   104        T_SEMI, 
       
   105        T_ID("bnd"), 
       
   106        T_OP(":="), 
       
   107        T_ID("bnd"), 
       
   108        T_OP("+"), 
       
   109        T_NUM(1), 
       
   110        T_PAREN("}"))
       
   111 
       
   112 
       
   113 
       
   114   //print(s"  Testing lexing of ${file.padTo(16, ' ')} ")
       
   115 val str = os.read(os.pwd / "collatz2.while")
       
   116   
       
   117 Try(test_string(str)) match {
       
   118   case Success(v) if lsteq(v, tks) 
       
   119                   => println(s"   Generated the correct token sequence.")
       
   120   case Success(v) => println(s"   Generated $v\n instead of\n $tks\n\n${listcmp(v, tks)}).") ; throw new Exception("Different")
       
   121   case Failure(e) => println(s"   Exception raised.") ; throw(e)
       
   122 }
       
   123