progs/lexer/lexer.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 09 Nov 2024 06:31:45 +0000
changeset 973 f25d338d16c9
parent 971 51e00f223792
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
     1
// A simple lexer inspired by work of Sulzmann $ Lu
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//==================================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// Call the test cases with 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
//   amm lexer.sc small
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//   amm lexer.sc fib
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//   amm lexer.sc loops
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
     9
//   amm lexer.sc email
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
//   amm lexer.sc all
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
// regular expressions including records
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
abstract class Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case object ZERO extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case object ONE extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case class CHAR(c: Char) extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case class STAR(r: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class RECD(x: String, r: Rexp) extends Rexp  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
                // records for extracting strings or tokens
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
// values  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
abstract class Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case object Empty extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case class Chr(c: Char) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
case class Sequ(v1: Val, v2: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
case class Left(v: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
case class Right(v: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
case class Stars(vs: List[Val]) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
case class Rec(x: String, v: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
   
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
// some convenience for typing in regular expressions
969
0dfa2923a7c6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 960
diff changeset
    36
import scala.language.implicitConversions
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
def charlist2rexp(s : List[Char]): Rexp = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  case Nil => ONE
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  case c::Nil => CHAR(c)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
}
947
dc31a099dc98 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 945
diff changeset
    43
969
0dfa2923a7c6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 960
diff changeset
    44
given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
    45
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    46
//extension (s: String) {
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    47
//  def $ (r: Rexp) = RECD(s, r)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    48
//}
936
0b5f06539a84 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 846
diff changeset
    49
extension (s: String) {
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
    50
  def $ (r: Rexp) = RECD(s, r)
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    51
  def | (r: Rexp) = ALT(s, r)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    52
  def | (r: String) = ALT(s, r)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    53
  def % = STAR(s)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    54
  def ~ (r: Rexp) = SEQ(s, r)
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    55
  def ~ (r: String) = SEQ(s, r)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
}
971
51e00f223792 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 970
diff changeset
    57
 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    59
extension (r: Rexp) {
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    60
  def | (s: Rexp) = ALT(r, s)
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    61
  def % = STAR(r)
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    62
  def ~ (s: Rexp) = SEQ(r, s)
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    63
}
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    64
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
def nullable(r: Rexp) : Boolean = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case ZERO => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  case ONE => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case CHAR(_) => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 955
diff changeset
    70
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  case STAR(_) => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  case RECD(_, r1) => nullable(r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
def der(c: Char, r: Rexp) : Rexp = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
  case ZERO => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  case ONE => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  case CHAR(d) => if (c == d) ONE else ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  case SEQ(r1, r2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
    else SEQ(der(c, r1), r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  case STAR(r) => SEQ(der(c, r), STAR(r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  case RECD(_, r1) => der(c, r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    87
// extracts a string from a value
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
def flatten(v: Val) : String = v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
  case Empty => ""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
  case Chr(c) => c.toString
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
  case Left(v) => flatten(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  case Right(v) => flatten(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
  case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
  case Stars(vs) => vs.map(flatten).mkString
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  case Rec(_, v) => flatten(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
// extracts an environment from a value;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
// used for tokenising a string
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
def env(v: Val) : List[(String, String)] = v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
  case Empty => Nil
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  case Chr(c) => Nil
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  case Left(v) => env(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  case Right(v) => env(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  case Sequ(v1, v2) => env(v1) ::: env(v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  case Stars(vs) => vs.flatMap(env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case Rec(x, v) => (x, flatten(v))::env(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
// The injection and mkeps part of the lexer
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
//===========================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
def mkeps(r: Rexp) : Val = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
  case ONE => Empty
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  case ALT(r1, r2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  case STAR(r) => Stars(Nil)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  case RECD(x, r) => Rec(x, mkeps(r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case (CHAR(d), Empty) => Chr(c) 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   135
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   136
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
// some "rectification" functions for simplification
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
def F_ID(v: Val): Val = v
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
  case Right(v) => Right(f2(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  case Left(v) => Left(f1(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  (v:Val) => Sequ(f1(Empty), f2(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
  (v:Val) => Sequ(f1(v), f2(Empty))
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 800
diff changeset
   152
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
def F_ERROR(v: Val): Val = throw new Exception("error")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
// simplification
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
def simp(r: Rexp): (Rexp, Val => Val) = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  case ALT(r1, r2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
    val (r1s, f1s) = simp(r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
    val (r2s, f2s) = simp(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
    (r1s, r2s) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
      case (_, ZERO) => (r1s, F_LEFT(f1s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
    }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
  case SEQ(r1, r2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    val (r1s, f1s) = simp(r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
    val (r2s, f2s) = simp(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
    (r1s, r2s) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
      case (ZERO, _) => (ZERO, F_ERROR)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
      case (_, ZERO) => (ZERO, F_ERROR)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
    }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
  case r => (r, F_ID)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
// lexing functions including simplification
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
  case Nil => if (nullable(r)) mkeps(r) else 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
    { throw new Exception("lexing error") } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
  case c::cs => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
    val (r_simp, f_simp) = simp(der(c, r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
def lexing_simp(r: Rexp, s: String) = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
  env(lex_simp(r, s.toList))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
// The Lexing Rules for the WHILE Language
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
def PLUS(r: Rexp) = r ~ r.%
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
def Range(s : List[Char]) : Rexp = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
  case Nil => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
  case c::Nil => CHAR(c)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
  case c::s => ALT(CHAR(c), Range(s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
def RANGE(s: String) = Range(s.toList)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
800
9eea6a801e10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 793
diff changeset
   206
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
val DIGIT = RANGE("0123456789")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
val ID = SYM ~ (SYM | DIGIT).% 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
val NUM = PLUS(DIGIT)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
val SEMI: Rexp = ";"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
846
3a535de22816 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 827
diff changeset
   213
val WHITESPACE = PLUS(" " | "\n" | "\t" | "\r")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
val RPAREN: Rexp = "{"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
val LPAREN: Rexp = "}"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   219
val WHILE_REGS = (("k" $ KEYWORD) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   220
                  ("i" $ ID) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   221
                  ("o" $ OP) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   222
                  ("n" $ NUM) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   223
                  ("s" $ SEMI) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   224
                  ("str" $ STRING) |
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   225
                  ("p" $ (LPAREN | RPAREN)) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   226
                  ("w" $ WHITESPACE)).%
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
// Two Simple While Tests
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
//========================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   232
@arg(doc = "small tests")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
def small() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
  val prog0 = """read n"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
  println(s"test: $prog0")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
  println(lexing_simp(WHILE_REGS, prog0))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
  val prog1 = """read  n; write n"""  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
  println(s"test: $prog1")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
  println(lexing_simp(WHILE_REGS, prog1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
// Bigger Tests
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
//==============
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
// escapes strings and prints them out as "", "\n" and so on
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
def esc(raw: String): String = {
970
1d4659dd83fe updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 969
diff changeset
   250
  "\"" + raw.replaceAll("\\n", "\\\\n").replaceAll ("\\t", "\\\\t") + "\""
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
def escape(tks: List[(String, String)]) =
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
  tks.map{ case (s1, s2) => (s1, esc(s2))}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
val prog2 = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
write "Fib";
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
read n;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
minus1 := 0;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
minus2 := 1;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
while n > 0 do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
  temp := minus2;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
  minus2 := minus1 + minus2;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
  minus1 := temp;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
  n := n - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
};
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
write "Result";
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
write minus2
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   272
@arg(doc = "Fibonacci test")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
def fib() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
  println("lexing fib program")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
  println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n"))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
val prog3 = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   281
start := 1000;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
x := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   283
y := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   284
z := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   285
while 0 < x do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   286
 while 0 < y do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   287
  while 0 < z do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   288
    z := z - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   289
  };
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   290
  z := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   291
  y := y - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   292
 };     
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   293
 y := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   294
 x := x - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   295
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   296
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   297
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   298
@arg(doc = "Loops test")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   299
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   300
def loops() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   301
  println("lexing Loops")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   302
  println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n"))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   303
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   304
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   305
@arg(doc = "Email Test")
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   306
@main
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   307
def email() = {
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   308
  val lower = "abcdefghijklmnopqrstuvwxyz"
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   309
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   310
  val NAME = RECD("name", PLUS(RANGE(lower ++ "_.-")))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   311
  val DOMAIN = RECD("domain", PLUS(RANGE(lower ++ "-")))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   312
  val RE = RANGE(lower ++ ".")
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   313
  val TOPLEVEL = RECD("top", (RE ~ RE) |
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   314
                             (RE ~ RE ~ RE) | 
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   315
                             (RE ~ RE ~ RE ~ RE) | 
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   316
                             (RE ~ RE ~ RE ~ RE ~ RE) |
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   317
                             (RE ~ RE ~ RE ~ RE ~ RE ~ RE))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   318
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   319
  val EMAIL = NAME ~ "@" ~ DOMAIN ~ "." ~ TOPLEVEL
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   320
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   321
  println(lexing_simp(EMAIL, "christian.urban@kcl.ac.uk"))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   322
}
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   323
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   324
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   325
@arg(doc = "All tests.")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   326
@main
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   327
def all() = { small(); fib() ; loops() ; email() } 
792
34132a854d03 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 757
diff changeset
   328
34132a854d03 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 757
diff changeset
   329
34132a854d03 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 757
diff changeset
   330
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   331
/*
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   332
// Ammonite still provides 
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   333
//
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   334
//    scala.reflect.runtime.universe._
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   335
//
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   336
// which has been removed in Scala 3.
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   337
//
945
5070392a1bd7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 944
diff changeset
   338
// for escaping strings in Scala 3 use this equivalent code
5070392a1bd7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 944
diff changeset
   339
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   340
import scala.quoted._
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   341
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   342
def escapeImpl(raw: Expr[String])(using Quotes): Expr[String] = {
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   343
  import quotes.reflect.*
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   344
  Literal(StringConstant(raw.show)).asExprOf[String]
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   345
}
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   346
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   347
inline def esc(inline raw: String): String = ${escapeImpl('{raw})}
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   348
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   349
val s = "abc\ndef"       // not escaped
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   350
val e = esc("abc\ndef")  // escaped
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   351
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   352
println(s)
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   353
println(e)
969
0dfa2923a7c6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 960
diff changeset
   354
*/