Fahad/Scala/POSIX.sc
author fahadausaf <fahad.ausaf@icloud.com>
Mon, 27 Oct 2014 12:40:19 +0000
changeset 33 8b5ed306e961
parent 31 238c98db6057
child 43 10e7a90d8e7a
permissions -rw-r--r--
new
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
package greeter
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
object POSIX {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
  println("Posix Algorithm")                      //> Posix Algorithm
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
  abstract class Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
  case object NULL extends Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
  case object EMPTY extends Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
  case class CHAR(c: Char) extends Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
  case class ALT(r1: Rexp, r2: Rexp) extends Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
  case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
  case class STAR(r: Rexp) extends Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
  case class RECD(x: String, r: Rexp) extends Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
  abstract class Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
  case object Void extends Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
  case class Chr(c: Char) extends Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
  case class Sequ(v1: Val, v2: Val) extends Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
  case class Left(v: Val) extends Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
  case class Right(v: Val) extends Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
  case class Stars(vs: List[Val]) extends Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
  case class Rec(x: String, v: Val) extends Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
  def charlist2rexp(s: List[Char]): Rexp = s match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
    case Nil => EMPTY
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
    case c :: Nil => CHAR(c)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
    case c :: s => SEQ(CHAR(c), charlist2rexp(s))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
  }                                               //> charlist2rexp: (s: List[Char])greeter.POSIX.Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
  implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
                                                  //> string2rexp: (s: String)greeter.POSIX.Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
  implicit def RexpOps(r: Rexp) = new {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
    def |(s: Rexp) = ALT(r, s)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
    def % = STAR(r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
    def ~(s: Rexp) = SEQ(r, s)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
  }                                               //> RexpOps: (r: greeter.POSIX.Rexp)AnyRef{def |(s: greeter.POSIX.Rexp): greete
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
                                                  //| r.POSIX.ALT; def %: greeter.POSIX.STAR; def ~(s: greeter.POSIX.Rexp): greet
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
                                                  //| er.POSIX.SEQ}
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
  implicit def stringOps(s: String) = new {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
    def |(r: Rexp) = ALT(s, r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
    def |(r: String) = ALT(s, r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
    def % = STAR(s)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
    def ~(r: Rexp) = SEQ(s, r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
    def ~(r: String) = SEQ(s, r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
    def $(r: Rexp) = RECD(s, r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    47
  }                                               //> stringOps: (s: String)AnyRef{def |(r: greeter.POSIX.Rexp): greeter.POSIX.AL
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    48
                                                  //| T; def |(r: String): greeter.POSIX.ALT; def %: greeter.POSIX.STAR; def ~(r:
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    49
                                                  //|  greeter.POSIX.Rexp): greeter.POSIX.SEQ; def ~(r: String): greeter.POSIX.SE
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    50
                                                  //| Q; def $(r: greeter.POSIX.Rexp): greeter.POSIX.RECD}
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    51
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    52
  // size of a regular expressions - for testing purposes
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    53
  def size(r: Rexp): Int = r match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    54
    case NULL => 1
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    55
    case EMPTY => 1
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    56
    case CHAR(_) => 1
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    57
    case ALT(r1, r2) => 1 + size(r1) + size(r2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    58
    case SEQ(r1, r2) => 1 + size(r1) + size(r2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    59
    case STAR(r) => 1 + size(r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    60
    case RECD(_, r) => 1 + size(r)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    61
  }                                               //> size: (r: greeter.POSIX.Rexp)Int
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    62
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    63
  // nullable function: tests whether the regular
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    64
  // expression can recognise the empty string
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    65
  def nullable(r: Rexp): Boolean = r match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    66
    case NULL => false
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    67
    case EMPTY => true
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    68
    case CHAR(_) => false
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    69
    case ALT(r1, r2) => nullable(r1) || nullable(r2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    70
    case SEQ(r1, r2) => nullable(r1) && nullable(r2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    71
    case STAR(_) => true
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    72
    case RECD(_, r1) => nullable(r1)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    73
  }                                               //> nullable: (r: greeter.POSIX.Rexp)Boolean
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    74
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    75
  // derivative of a regular expression w.r.t. a character
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    76
  def der(c: Char, r: Rexp): Rexp = r match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    77
    case NULL => NULL
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    78
    case EMPTY => NULL
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    79
    case CHAR(d) => if (c == d) EMPTY else NULL
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    80
    case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    81
    case SEQ(r1, r2) =>
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    82
      if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    83
      else SEQ(der(c, r1), r2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    84
    case STAR(r) => SEQ(der(c, r), STAR(r))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    85
    case RECD(_, r1) => der(c, r1)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    86
  }                                               //> der: (c: Char, r: greeter.POSIX.Rexp)greeter.POSIX.Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    87
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    88
  // derivative w.r.t. a string (iterates der)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    89
  def ders(s: List[Char], r: Rexp): Rexp = s match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    90
    case Nil => r
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    91
    case c :: s => ders(s, der(c, r))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    92
  }                                               //> ders: (s: List[Char], r: greeter.POSIX.Rexp)greeter.POSIX.Rexp
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    93
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    94
  // extracts a string from value
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    95
  def flatten(v: Val): String = v match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    96
    case Void => ""
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    97
    case Chr(c) => c.toString
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    98
    case Left(v) => flatten(v)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    99
    case Right(v) => flatten(v)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   100
    case Sequ(v1, v2) => flatten(v1) + flatten(v2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   101
    case Stars(vs) => vs.map(flatten).mkString
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   102
    case Rec(_, v) => flatten(v)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   103
  }                                               //> flatten: (v: greeter.POSIX.Val)String
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   104
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   105
  // extracts an environment from a value
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   106
  def env(v: Val): List[(String, String)] = v match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   107
    case Void => Nil
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   108
    case Chr(c) => Nil
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   109
    case Left(v) => env(v)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   110
    case Right(v) => env(v)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   111
    case Sequ(v1, v2) => env(v1) ::: env(v2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   112
    case Stars(vs) => vs.flatMap(env)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   113
    case Rec(x, v) => (x, flatten(v)) :: env(v)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   114
  }                                               //> env: (v: greeter.POSIX.Val)List[(String, String)]
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   115
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   116
  def mkeps(r: Rexp): Val = r match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   117
    case EMPTY => Void
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   118
    case ALT(r1, r2) =>
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   119
      if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   120
    case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   121
    case STAR(r) => Stars(Nil)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   122
    case RECD(x, r) => Rec(x, mkeps(r))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   123
  }                                               //> mkeps: (r: greeter.POSIX.Rexp)greeter.POSIX.Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   124
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   125
  def inj(r: Rexp, c: Char, v: Val): Val = (r, v) match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   126
    case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1) :: vs)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   127
    case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   128
    case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   129
    case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   130
    case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   131
    case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   132
    case (CHAR(d), Void) => Chr(d)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   133
    case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   134
  }                                               //> inj: (r: greeter.POSIX.Rexp, c: Char, v: greeter.POSIX.Val)greeter.POSIX.Va
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   135
                                                  //| l
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   136
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   137
  // main lexing function (produces a value)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   138
  def lex(r: Rexp, s: List[Char]): Val = s match {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   139
    case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   140
    case c :: cs => inj(r, c, lex(der(c, r), cs))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   141
  }                                               //> lex: (r: greeter.POSIX.Rexp, s: List[Char])greeter.POSIX.Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   142
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   143
  def lexing(r: Rexp, s: String): Val = lex(r, s.toList)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   144
                                                  //> lexing: (r: greeter.POSIX.Rexp, s: String)greeter.POSIX.Val
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   145
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   146
  val r = (("1" $ "a") | (("2" $ "b") | ("3" $ "ab"))).%
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   147
                                                  //> r  : greeter.POSIX.STAR = STAR(ALT(RECD(1,CHAR(a)),ALT(RECD(2,CHAR(b)),RECD
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   148
                                                  //| (3,SEQ(CHAR(a),CHAR(b))))))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   149
  env(lexing(r, "ba"))                            //> res0: List[(String, String)] = List((2,b), (1,a))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   150
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   151
  val r1 = "a" | "b"                              //> r1  : greeter.POSIX.ALT = ALT(CHAR(a),CHAR(b))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   152
  lexing(r1, "a")                                 //> res1: greeter.POSIX.Val = Left(Chr(a))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   153
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   154
  // Lexing Rules for a Small While Language
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   155
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   156
  def PLUS(r: Rexp) = r ~ r.%                     //> PLUS: (r: greeter.POSIX.Rexp)greeter.POSIX.SEQ
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   157
  val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   158
                                                  //> SYM  : greeter.POSIX.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   159
                                                  //| ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR(c
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   160
                                                  //| )),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k)),
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   161
                                                  //| CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CHA
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   162
                                                  //| R(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   163
  val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   164
                                                  //> DIGIT  : greeter.POSIX.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),CH
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   165
                                                  //| AR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR(
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   166
                                                  //| 9))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   167
  val ID = SYM ~ (SYM | DIGIT).%                  //> ID  : greeter.POSIX.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(A
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   168
                                                  //| LT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHA
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   169
                                                  //| R(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   170
                                                  //| )),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   171
                                                  //| CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z)),STAR(ALT(ALT
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   172
                                                  //| (ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(AL
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   173
                                                  //| T(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f)),
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   174
                                                  //| CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CHA
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   175
                                                  //| R(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   176
                                                  //| )),CHAR(x)),CHAR(y)),CHAR(z)),ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),C
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   177
                                                  //| HAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   178
                                                  //| (9)))))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   179
  val NUM = PLUS(DIGIT)                           //> NUM  : greeter.POSIX.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   180
                                                  //| CHAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHA
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   181
                                                  //| R(9)),STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),CHAR(1)),CHAR(2)),CH
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   182
                                                  //| AR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR(9))))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   183
  val KEYWORD: Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   184
                                                  //> KEYWORD  : greeter.POSIX.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CHA
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   185
                                                  //| R(s),SEQ(CHAR(k),SEQ(CHAR(i),CHAR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   186
                                                  //| ,SEQ(CHAR(l),CHAR(e)))))),SEQ(CHAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ(C
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   187
                                                  //| HAR(t),SEQ(CHAR(h),SEQ(CHAR(e),CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHAR
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   188
                                                  //| (s),CHAR(e))))),SEQ(CHAR(r),SEQ(CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   189
                                                  //| ,SEQ(CHAR(r),SEQ(CHAR(i),SEQ(CHAR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r),S
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   190
                                                  //| EQ(CHAR(u),CHAR(e))))),SEQ(CHAR(f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHAR
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   191
                                                  //| (e))))))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   192
  val SEMI: Rexp = ";"                            //> SEMI  : greeter.POSIX.Rexp = CHAR(;)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   193
  val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   194
                                                  //> OP  : greeter.POSIX.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   195
                                                  //| CHAR(:),CHAR(=)),SEQ(CHAR(=),CHAR(=))),CHAR(-)),CHAR(+)),CHAR(*)),SEQ(CHAR(
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   196
                                                  //| !),CHAR(=))),CHAR(<)),CHAR(>)),SEQ(CHAR(<),CHAR(=))),SEQ(CHAR(>),CHAR(=))),
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   197
                                                  //| CHAR(%)),CHAR(/))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   198
  val WHITESPACE = PLUS(" " | "\n" | "\t")        //> WHITESPACE  : greeter.POSIX.SEQ = SEQ(ALT(ALT(CHAR( ),CHAR(
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   199
                                                  //| )),CHAR(	)),STAR(ALT(ALT(CHAR( ),CHAR(
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   200
                                                  //| )),CHAR(	))))
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   201
  val RPAREN: Rexp = ")"                          //> RPAREN  : greeter.POSIX.Rexp = CHAR())
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   202
  val LPAREN: Rexp = "("                          //> LPAREN  : greeter.POSIX.Rexp = CHAR(()
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   203
  val BEGIN: Rexp = "{"                           //> BEGIN  : greeter.POSIX.Rexp = CHAR({)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   204
  val END: Rexp = "}"                             //> END  : greeter.POSIX.Rexp = CHAR(})
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   205
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   206
  /*
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   207
 * val WHILE_REGS = (("k" $ KEYWORD) |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   208
                  ("i" $ ID) |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   209
                  ("o" $ OP) |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   210
                  ("n" $ NUM) |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   211
                  ("s" $ SEMI) |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   212
                  ("p" $ (LPAREN | RPAREN)) |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   213
                  ("b" $ (BEGIN | END)) |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   214
                  ("w" $ WHITESPACE)).%
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   215
*/
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   216
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   217
  val WHILE_REGS = (KEYWORD |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   218
    ID |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   219
    OP |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   220
    NUM |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   221
    SEMI |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   222
    LPAREN | RPAREN |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   223
    BEGIN | END |
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   224
    WHITESPACE).%                                 //> WHILE_REGS  : greeter.POSIX.STAR = STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   225
                                                  //| (ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CHAR(s),SEQ(CHAR(k),SEQ(CHAR(i),CH
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   226
                                                  //| AR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i),SEQ(CHAR(l),CHAR(e)))))),SEQ(C
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   227
                                                  //| HAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ(CHAR(t),SEQ(CHAR(h),SEQ(CHAR(e),
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   228
                                                  //| CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e))))),SEQ(CHAR(r),SEQ
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   229
                                                  //| (CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w),SEQ(CHAR(r),SEQ(CHAR(i),SEQ(CH
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   230
                                                  //| AR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r),SEQ(CHAR(u),CHAR(e))))),SEQ(CHAR
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   231
                                                  //| (f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e)))))),SEQ(ALT(ALT(ALT(ALT(AL
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   232
                                                  //| T(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(A
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   233
                                                  //| LT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   234
                                                  //| )),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   235
                                                  //| CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHA
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   236
                                                  //| R(y)),CHAR(z)),STAR(ALT
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   237
                                                  //| Output exceeds cutoff limit.
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   238
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   239
  // Some Tests
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   240
  //============
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   241
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   242
  def time[T](code: => T) = {
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   243
    val start = System.nanoTime()
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   244
    val result = code
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   245
    val end = System.nanoTime()
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   246
    println((end - start) / 1.0e9)
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   247
    result
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   248
  }                                               //> time: [T](code: => T)T
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   249
33
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   250
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   251
  val abc = List('a', 'b', 'c')                   //> abc  : List[Char] = List(a, b, c)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   252
  val nullRexp = null                             //> nullRexp  : Null = null
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   253
  val myRexp = charlist2rexp(abc)                 //> myRexp  : greeter.POSIX.Rexp = SEQ(CHAR(a),SEQ(CHAR(b),CHAR(c)))
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   254
  val myRexp2 = string2rexp("FahadAusaf")         //> myRexp2  : greeter.POSIX.Rexp = SEQ(CHAR(F),SEQ(CHAR(a),SEQ(CHAR(h),SEQ(CHA
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   255
                                                  //| R(a),SEQ(CHAR(d),SEQ(CHAR(A),SEQ(CHAR(u),SEQ(CHAR(s),SEQ(CHAR(a),CHAR(f))))
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   256
                                                  //| ))))))
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   257
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   258
  RexpOps(myRexp2)                                //> res2: AnyRef{def |(s: greeter.POSIX.Rexp): greeter.POSIX.ALT; def %: greete
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   259
                                                  //| r.POSIX.STAR; def ~(s: greeter.POSIX.Rexp): greeter.POSIX.SEQ} = greeter.PO
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   260
                                                  //| SIX$$anonfun$main$1$$anon$1@37ecb28e
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   261
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   262
  stringOps("Fahad")                              //> res3: AnyRef{def |(r: greeter.POSIX.Rexp): greeter.POSIX.ALT; def |(r: Stri
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   263
                                                  //| ng): greeter.POSIX.ALT; def %: greeter.POSIX.STAR; def ~(r: greeter.POSIX.R
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   264
                                                  //| exp): greeter.POSIX.SEQ; def ~(r: String): greeter.POSIX.SEQ; def $(r: gree
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   265
                                                  //| ter.POSIX.Rexp): greeter.POSIX.RECD} = greeter.POSIX$$anonfun$main$1$$anon$
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   266
                                                  //| 2@14bea551
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   267
                                                  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   268
	size(myRexp2)                             //> res4: Int = 19
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   269
	nullable(nullRexp)                        //> scala.MatchError: null
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   270
                                                  //| 	at greeter.POSIX$$anonfun$main$1.nullable$1(greeter.POSIX.scala:59)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   271
                                                  //| 	at greeter.POSIX$$anonfun$main$1.apply$mcV$sp(greeter.POSIX.scala:202)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   272
                                                  //| 	at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   273
                                                  //| cute$1.apply$mcV$sp(WorksheetSupport.scala:76)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   274
                                                  //| 	at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   275
                                                  //| orksheetSupport.scala:65)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   276
                                                  //| 	at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   277
                                                  //| ksheetSupport.scala:75)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   278
                                                  //| 	at greeter.POSIX$.main(greeter.POSIX.scala:3)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   279
                                                  //| 	at greeter.POSIX.main(greeter.POSIX.scala)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   280
	
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   281
	val newRexp = der('a',myRexp)
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   282
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   283
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   284
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   285
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   286
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   287
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   288
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   289
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   290
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   291
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   292
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   293
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   294
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   295
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   296
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   297
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   298
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   299
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   300
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   301
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   302
  
fahadausaf <fahad.ausaf@icloud.com>
parents: 31
diff changeset
   303
  //this is some crap
31
238c98db6057 POSIX Algorithm
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   304
}