exps/lex_blex_Frankensteined.scala
author Chengsong
Mon, 04 Feb 2019 13:10:26 +0000
changeset 304 82a99eec5b73
permissions -rw-r--r--
3 files to be compiled together and then run scala Spiral a b where a, b are integers to see the time distribution
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
304
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     1
package RexpRelated
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     2
import scala.language.implicitConversions    
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     3
import scala.language.reflectiveCalls
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     4
import scala.annotation.tailrec   
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     5
import scala.util.Try
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     6
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     7
abstract class Bit
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     8
case object Z extends Bit
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     9
case object S extends Bit
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    10
case class C(c: Char) extends Bit
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    11
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    12
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    13
abstract class Rexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    14
case object ZERO extends Rexp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    15
case object ONE extends Rexp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    16
case class PRED(f: Char => Boolean) extends Rexp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    17
case class ALTS(rs: List[Rexp]) extends Rexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    18
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    19
case class STAR(r: Rexp) extends Rexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    20
case class RECD(x: String, r: Rexp) extends Rexp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    21
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    22
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    23
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    24
object Rexp{
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    25
  type Bits = List[Bit]
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    26
  // abbreviations
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    27
  def CHAR(c: Char) = PRED(_ == c)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    28
  def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    29
  def PLUS(r: Rexp) = SEQ(r, STAR(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    30
  def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    31
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    32
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    33
  def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    34
    case Nil => Nil
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    35
    case (x::xs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    36
      val res = f(x)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    37
      if (acc.contains(res)) distinctBy(xs, f, acc)  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    38
      else x::distinctBy(xs, f, res::acc)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    39
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    40
  } 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    41
  // some convenience for typing in regular expressions
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    42
  def charlist2rexp(s : List[Char]): Rexp = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    43
    case Nil => ONE
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    44
    case c::Nil => CHAR(c)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    45
    case c::s => SEQ(CHAR(c), charlist2rexp(s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    46
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    47
  implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    48
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    49
  implicit def RexpOps(r: Rexp) = new {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    50
    def | (s: Rexp) = ALT(r, s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    51
    def % = STAR(r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    52
    def ~ (s: Rexp) = SEQ(r, s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    53
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    54
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    55
  implicit def stringOps(s: String) = new {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    56
    def | (r: Rexp) = ALT(s, r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    57
    def | (r: String) = ALT(s, r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    58
    def % = STAR(s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    59
    def ~ (r: Rexp) = SEQ(s, r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    60
    def ~ (r: String) = SEQ(s, r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    61
    def $ (r: Rexp) = RECD(s, r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    62
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    63
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    64
  // translation into ARexps
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    65
  def fuse(bs: Bits, r: ARexp) : ARexp = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    66
    case AZERO => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    67
    case AONE(cs) => AONE(bs ++ cs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    68
    case APRED(cs, f) => APRED(bs ++ cs, f)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    69
    case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    70
    case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    71
    case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    72
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    73
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    74
  def internalise(r: Rexp) : ARexp = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    75
    case ZERO => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    76
    case ONE => AONE(Nil)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    77
    case PRED(f) => APRED(Nil, f)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    78
    case ALTS(List(r1, r2)) => 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    79
      AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    80
    case ALTS(r1::rs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    81
      val AALTS(Nil, rs2) = internalise(ALTS(rs))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    82
      AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    83
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    84
    case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    85
    case STAR(r) => ASTAR(Nil, internalise(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    86
    case RECD(x, r) => internalise(r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    87
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    88
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    89
  internalise(("a" | "ab") ~ ("b" | ""))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    90
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    91
  def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    92
    case (ONE, bs) => (Empty, bs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    93
    case (PRED(f), C(c)::bs) => (Chr(c), bs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    94
    case (ALTS(r::Nil), bs) => decode_aux(r, bs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    95
    case (ALTS(rs), bs) => bs match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    96
      case Z::bs1 => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    97
        val (v, bs2) = decode_aux(rs.head, bs1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    98
        (Left(v), bs2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    99
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   100
      case S::bs1 => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   101
        val (v, bs2) = decode_aux(ALTS(rs.tail), bs1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   102
        (Right(v), bs2)			
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   103
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   104
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   105
    case (SEQ(r1, r2), bs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   106
      val (v1, bs1) = decode_aux(r1, bs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   107
      val (v2, bs2) = decode_aux(r2, bs1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   108
      (Sequ(v1, v2), bs2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   109
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   110
    case (STAR(r1), S::bs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   111
      val (v, bs1) = decode_aux(r1, bs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   112
      //println(v)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   113
      val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   114
      (Stars(v::vs), bs2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   115
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   116
    case (STAR(_), Z::bs) => (Stars(Nil), bs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   117
    case (RECD(x, r1), bs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   118
      val (v, bs1) = decode_aux(r1, bs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   119
      (Rec(x, v), bs1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   120
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   121
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   122
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   123
  def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   124
    case (v, Nil) => v
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   125
    case _ => throw new Exception("Not decodable")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   126
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   127
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   128
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   129
  //erase function: extracts the regx from Aregex
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   130
  def erase(r:ARexp): Rexp = r match{
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   131
    case AZERO => ZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   132
    case AONE(_) => ONE
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   133
    case APRED(bs, f) => PRED(f)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   134
    case AALTS(bs, rs) => ALTS(rs.map(erase(_)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   135
    case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   136
    case ASTAR(cs, r)=> STAR(erase(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   137
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   138
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   139
  //--------------------------------------------------------------------------------------------------------START OF NON-BITCODE PART
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   140
  // nullable function: tests whether the regular 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   141
  // expression can recognise the empty string
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   142
  def nullable (r: Rexp) : Boolean = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   143
    case ZERO => false
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   144
    case ONE => true
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   145
    case PRED(_) => false
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   146
    case ALTS(rs) => rs.exists(nullable)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   147
    case SEQ(r1, r2) => nullable(r1) && nullable(r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   148
    case STAR(_) => true
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   149
    case RECD(_, r) => nullable(r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   150
    //case PLUS(r) => nullable(r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   151
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   152
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   153
  // derivative of a regular expression w.r.t. a character
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   154
  def der (c: Char, r: Rexp) : Rexp = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   155
    case ZERO => ZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   156
    case ONE => ZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   157
    case PRED(f) => if (f(c)) ONE else ZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   158
    case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   159
    case SEQ(r1, r2) => 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   160
      if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   161
      else SEQ(der(c, r1), r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   162
    case STAR(r) => SEQ(der(c, r), STAR(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   163
    case RECD(_, r1) => der(c, r1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   164
    //case PLUS(r) => SEQ(der(c, r), STAR(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   165
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   166
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   167
  def flatten(v: Val) : String = v match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   168
    case Empty => ""
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   169
    case Chr(c) => c.toString
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   170
    case Left(v) => flatten(v)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   171
    case Right(v) => flatten(v)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   172
    case Sequ(v1, v2) => flatten(v1) + flatten(v2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   173
    case Stars(vs) => vs.map(flatten).mkString
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   174
    case Rec(_, v) => flatten(v)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   175
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   176
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   177
  // extracts an environment from a value
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   178
  def env(v: Val) : List[(String, String)] = v match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   179
    case Empty => Nil
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   180
    case Chr(c) => Nil
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   181
    case Left(v) => env(v)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   182
    case Right(v) => env(v)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   183
    case Sequ(v1, v2) => env(v1) ::: env(v2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   184
    case Stars(vs) => vs.flatMap(env)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   185
    case Rec(x, v) => (x, flatten(v))::env(v)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   186
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   187
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   188
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   189
  // injection part
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   190
  def mkeps(r: Rexp) : Val = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   191
    case ONE => Empty
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   192
    case ALTS(List(r1, r2)) => 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   193
      if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   194
    case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   195
    case STAR(r) => Stars(Nil)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   196
    case RECD(x, r) => Rec(x, mkeps(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   197
    //case PLUS(r) => Stars(List(mkeps(r)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   198
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   199
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   200
  def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   201
    case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   202
    case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   203
    case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   204
    case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   205
    case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   206
    case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   207
    case (PRED(_), Empty) => Chr(c) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   208
    case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   209
    //case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   210
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   211
  def lex(r: Rexp, s: List[Char]) : Val = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   212
    case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   213
    case c::cs => inj(r, c, lex(der(c, r), cs))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   214
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   215
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   216
  def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   217
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   218
  // some "rectification" functions for simplification
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   219
  def F_ID(v: Val): Val = v
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   220
  def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   221
  def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   222
  def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   223
    case Right(v) => Right(f2(v))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   224
    case Left(v) => Left(f1(v))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   225
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   226
  def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   227
    case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   228
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   229
  def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   230
    (v:Val) => Sequ(f1(Empty), f2(v))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   231
  def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   232
    (v:Val) => Sequ(f1(v), f2(Empty))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   233
  def F_RECD(f: Val => Val) = (v:Val) => v match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   234
    case Rec(x, v) => Rec(x, f(v))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   235
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   236
  def F_ERROR(v: Val): Val = throw new Exception("error")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   237
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   238
  // simplification of regular expressions returning also an
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   239
  // rectification function; no simplification under STAR 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   240
  def simp(r: Rexp): (Rexp, Val => Val) = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   241
    case ALTS(List(r1, r2)) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   242
      val (r1s, f1s) = simp(r1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   243
      val (r2s, f2s) = simp(r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   244
      (r1s, r2s) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   245
        case (ZERO, _) => (r2s, F_RIGHT(f2s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   246
        case (_, ZERO) => (r1s, F_LEFT(f1s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   247
        case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   248
                  else (ALTS(List(r1s, r2s)), F_ALT(f1s, f2s)) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   249
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   250
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   251
    case SEQ(r1, r2) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   252
      val (r1s, f1s) = simp(r1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   253
      val (r2s, f2s) = simp(r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   254
      (r1s, r2s) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   255
        case (ZERO, _) => (ZERO, F_ERROR)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   256
        case (_, ZERO) => (ZERO, F_ERROR)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   257
        case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   258
        case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   259
        case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   260
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   261
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   262
    case RECD(x, r1) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   263
      val (r1s, f1s) = simp(r1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   264
      (RECD(x, r1s), F_RECD(f1s))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   265
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   266
    case r => (r, F_ID)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   267
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   268
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   269
  val each_simp_time = scala.collection.mutable.ArrayBuffer.empty[Long]
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   270
  val each_simp_timeb = scala.collection.mutable.ArrayBuffer.empty[Long]
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   271
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   272
  def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   273
    case Nil => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   274
      if (nullable(r)) {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   275
        mkeps(r) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   276
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   277
      else throw new Exception("Not matched")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   278
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   279
    case c::cs => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   280
      val (r_simp, f_simp) = simp(der(c, r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   281
      inj(r, c, f_simp(lex_simp(r_simp, cs)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   282
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   283
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   284
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   285
  def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   286
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   287
  //println(lexing_simp(("a" | "ab") ~ ("b" | ""), "ab"))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   288
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   289
  // filters out all white spaces
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   290
  def tokenise(r: Rexp, s: String) = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   291
    env(lexing_simp(r, s)).filterNot { _._1 == "w"}
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   292
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   293
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   294
  //reads the string from a file 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   295
  def fromFile(name: String) : String = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   296
    io.Source.fromFile(name).mkString
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   297
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   298
  def tokenise_file(r: Rexp, name: String) = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   299
    tokenise(r, fromFile(name))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   300
  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   301
  //   Testing
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   302
  //============
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   303
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   304
  def time[T](code: => T) = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   305
    val start = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   306
    val result = code
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   307
    val end = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   308
    println((end - start)/1.0e9)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   309
    result
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   310
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   311
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   312
  //--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   313
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   314
  // bnullable function: tests whether the aregular 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   315
  // expression can recognise the empty string
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   316
  def bnullable (r: ARexp) : Boolean = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   317
    case AZERO => false
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   318
    case AONE(_) => true
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   319
    case APRED(_,_) => false
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   320
    case AALTS(_, rs) => rs.exists(bnullable)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   321
    case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   322
    case ASTAR(_, _) => true
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   323
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   324
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   325
  def mkepsBC(r: ARexp) : Bits = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   326
    case AONE(bs) => bs
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   327
    case AALTS(bs, rs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   328
      val n = rs.indexWhere(bnullable)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   329
      bs ++ mkepsBC(rs(n))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   330
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   331
    case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   332
    case ASTAR(bs, r) => bs ++ List(Z)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   333
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   334
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   335
  // derivative of a regular expression w.r.t. a character
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   336
  def bder(c: Char, r: ARexp) : ARexp = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   337
    case AZERO => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   338
    case AONE(_) => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   339
    case APRED(bs, f) => if (f(c)) AONE(bs:::List(C(c))) else AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   340
    case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   341
    case ASEQ(bs, r1, r2) => 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   342
      if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(mkepsBC(r1), bder(c, r2)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   343
      else ASEQ(bs, bder(c, r1), r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   344
    case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   345
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   346
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   347
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   348
  def ders (s: List[Char], r: Rexp) : Rexp = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   349
    case Nil => r
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   350
    case c::s => ders(s, der(c, r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   351
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   352
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   353
  // derivative w.r.t. a string (iterates bder)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   354
  @tailrec
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   355
  def bders (s: List[Char], r: ARexp) : ARexp = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   356
    case Nil => r
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   357
    case c::s => bders(s, bder(c, r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   358
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   359
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   360
  def flats(rs: List[ARexp]): List[ARexp] = rs match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   361
      case Nil => Nil
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   362
      case AZERO :: rs1 => flats(rs1)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   363
      case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   364
      case r1 :: rs2 => r1 :: flats(rs2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   365
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   366
  //val flats_time = scala.collection.mutable.ArrayBuffer.empty[Long]
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   367
  //val dist_time = scala.collection.mutable.ArrayBuffer.empty[Long]
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   368
  var flats_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   369
  var dist_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   370
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   371
  def bsimp(r: ARexp, depth: Int): ARexp = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   372
  {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   373
    r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   374
      case ASEQ(bs1, r1, r2) => (bsimp(r1, depth), bsimp(r2, depth)) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   375
          case (AZERO, _) => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   376
          case (_, AZERO) => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   377
          case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   378
          case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   379
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   380
      case AALTS(bs1, rs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   381
        depth match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   382
          case 0 => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   383
            flats(distinctBy(rs, erase)) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   384
              case Nil => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   385
              case s :: Nil => fuse(bs1, s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   386
              case rs => AALTS(bs1, rs) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   387
            } 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   388
          }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   389
          case n => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   390
            val rs_simp = rs.map(bsimp(_, n - 1))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   391
            val time2 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   392
            val flat_res = flats(rs_simp)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   393
            val time3 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   394
            val dist_res = distinctBy(flat_res, erase)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   395
            val time4 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   396
            flats_time = flats_time + time3 - time2
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   397
            dist_time = dist_time + time4 - time3
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   398
            //flats_time += time3 - time2
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   399
            //dist_time += time4 - time3
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   400
            //distinctBy(flats(rs.map(bsimp)), erase) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   401
            dist_res match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   402
              case Nil => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   403
              case s :: Nil => fuse(bs1, s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   404
              case rs => AALTS(bs1, rs)  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   405
            }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   406
          }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   407
        }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   408
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   409
      case r => r
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   410
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   411
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   412
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   413
  //----------------------------------------------------------------------------This bsimp is the original slow one
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   414
  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   415
  def bsimp(r: ARexp): ARexp = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   416
    case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   417
        case (AZERO, _) => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   418
        case (_, AZERO) => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   419
        case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   420
        case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   421
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   422
    case AALTS(bs1, rs) => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   423
      val rs_simp = rs.map(bsimp)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   424
      val time2 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   425
      val flat_res = flats(rs_simp)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   426
      val time3 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   427
      val dist_res = distinctBy(flat_res, erase)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   428
      val time4 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   429
      flats_time = flats_time + time3 - time2
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   430
      dist_time = dist_time + time4 - time3
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   431
      dist_res match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   432
        case Nil => AZERO
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   433
        case s :: Nil => fuse(bs1, s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   434
        case rs => AALTS(bs1, rs)  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   435
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   436
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   437
    case r => r
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   438
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   439
  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   440
  //----------------------------------------------------------------------------experiment bsimp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   441
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   442
  def bders_simp (s: List[Char], r: ARexp) : ARexp = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   443
    case Nil => r
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   444
    case c::s => bders_simp(s, bsimp(bder(c, r)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   445
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   446
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   447
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   448
  def time[T](code: => T) = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   449
    val start = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   450
    val result = code
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   451
    val end = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   452
    println((end - start)/1.0e9)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   453
    result
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   454
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   455
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   456
  // main unsimplified lexing function (produces a value)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   457
  def blex(r: ARexp, s: List[Char]) : Bits = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   458
    case Nil => if (bnullable(r)) mkepsBC(r) else throw new Exception("Not matched")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   459
    case c::cs => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   460
      val der_res = bder(c,r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   461
      blex(der_res, cs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   462
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   463
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   464
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   465
  def bpre_lexing(r: Rexp, s: String) = blex(internalise(r), s.toList)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   466
  //def blexing(r: Rexp, s: String) : Val = decode(r, blex(internalise(r), s.toList))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   467
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   468
  var bder_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   469
  var bsimp_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   470
  var mkepsBC_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   471
  var small_de = 2
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   472
  var big_de = 5
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   473
  var usual_de = 3
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   474
  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   475
  def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   476
    case Nil => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   477
      if (bnullable(r)) {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   478
        //println(asize(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   479
        val time4 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   480
        val bits = mkepsBC(r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   481
        val time5 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   482
        mkepsBC_time = time5 - time4
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   483
        bits
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   484
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   485
    else throw new Exception("Not matched")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   486
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   487
    case c::cs => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   488
      val time1 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   489
      val der_res = bder(c,r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   490
      val time2 = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   491
      val simp_res = bsimp(der_res)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   492
      val time3 = System.nanoTime()  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   493
      bder_time = bder_time + time2 - time1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   494
      bsimp_time = bsimp_time + time3 - time2
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   495
      blex_simp(simp_res, cs)      
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   496
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   497
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   498
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   499
  //-------------------------------------------------------------------------------------tests whether simp(simp(r)) == simp(r) holds true
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   500
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   501
  def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   502
    case Nil => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   503
      if (bnullable(r)) {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   504
        //println(asize(r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   505
        mkepsBC(r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   506
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   507
    else throw new Exception("Not matched")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   508
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   509
    case c::cs => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   510
      val der_res = bder(c,r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   511
      val simp_res = bsimp(der_res)  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   512
      //val simp_res2 = bsimp(simp_res)  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   513
      //println("Size reduction from "+asize(der_res)+ " to " +asize(simp_res)+" to " + asize(simp_res2)) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   514
      blex_simp(simp_res, cs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   515
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   516
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   517
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   518
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   519
  def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   520
    case Nil => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   521
      if (nullable(r)) {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   522
        mkeps(r) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   523
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   524
      else throw new Exception("Not matched")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   525
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   526
    case c::cs => {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   527
      val start = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   528
      val (r_simp, f_simp) = simp(der(c, r))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   529
      val end = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   530
      println((end - start)/1.0e9)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   531
      inj(r, c, f_simp(lex_simp(r_simp, cs)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   532
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   533
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   534
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   535
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   536
  //size: of a Aregx for testing purposes 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   537
  def size(r: Rexp) : Int = r match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   538
    case ZERO => 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   539
    case ONE => 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   540
    case PRED(_) => 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   541
    case SEQ(r1, r2) => 1 + size(r1) + size(r2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   542
    case ALTS(rs) => 1 + rs.map(size).sum
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   543
    case STAR(r) => 1 + size(r)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   544
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   545
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   546
  def asize(a: ARexp) = size(erase(a))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   547
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   548
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   549
  // decoding does not work yet
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   550
  def blexing_simp(r: Rexp, s: String) = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   551
    //flats_time.clear()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   552
    //dist_time.clear()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   553
    flats_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   554
    dist_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   555
    bder_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   556
    bsimp_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   557
    mkepsBC_time = 0L
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   558
    val start = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   559
    val bit_code = blex_simp(internalise(r), s.toList)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   560
    val end = System.nanoTime()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   561
    println("total time: "+ (end - start)/1.0e9)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   562
    println("spent on flats: " + (flats_time/(1.0e9)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   563
    println("spent on distinctBy: " + (dist_time/(1.0e9)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   564
    println("spent on bder: "+ bder_time/1.0e9)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   565
    println("spent on bsimp: " + bsimp_time/1.0e9)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   566
    println("spent on mkepsBC: " + mkepsBC_time/1.0e9)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   567
    //println(s"The length of the string ${s.length}; length of bit sequence:")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   568
    //println((bit_code.length))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   569
    //println(final_derivative)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   570
    //bit_code
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   571
    //decode(r, bit_code) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   572
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   573
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   574
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   575
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   576
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   577
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   578
  // Lexing Rules for a Small While Language
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   579
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   580
  //symbols
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   581
  val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   582
  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   583
  //digits
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   584
  val DIGIT = PRED("0123456789".contains(_))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   585
  //identifiers
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   586
  val ID = SYM ~ (SYM | DIGIT).% 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   587
  //numbers
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   588
  val NUM = STAR(DIGIT)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   589
  //keywords
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   590
  val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   591
  val AKEYWORD: Rexp = ALTS(List("skip" , "while" , "do" , "if" , "then" , "else" , "read" , "write" , "true" , "false"))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   592
  //semicolons
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   593
  val SEMI: Rexp = ";"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   594
  //operators
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   595
  val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   596
  val AOP: Rexp = ALTS(List(":=" , "==" , "-" , "+" , "*" , "!=" , "<" , ">" , "<=" , ">=" , "%" , "/"))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   597
  //whitespaces
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   598
  val WHITESPACE = PLUS(" " | "\n" | "\t")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   599
  //parentheses
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   600
  val RPAREN: Rexp = ")"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   601
  val LPAREN: Rexp = "("
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   602
  val BEGIN: Rexp = "{"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   603
  val END: Rexp = "}"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   604
  //strings...but probably needs not
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   605
  val STRING: Rexp = "\"" ~ SYM.% ~ "\""
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   606
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   607
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   608
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   609
  val WHILE_REGS = (("k" $ KEYWORD) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   610
                    ("i" $ ID) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   611
                    ("o" $ OP) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   612
                    ("n" $ NUM) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   613
                    ("s" $ SEMI) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   614
                    ("str" $ STRING) |
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   615
                    ("p" $ (LPAREN | RPAREN)) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   616
                    ("b" $ (BEGIN | END)) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   617
                    ("w" $ WHITESPACE)).%
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   618
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   619
  val AWHILE_REGS = (
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   620
    ALTS(
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   621
      List(
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   622
        ("k" $ AKEYWORD), 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   623
                    ("i" $ ID),
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   624
                    ("o" $ AOP) , 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   625
                    ("n" $ NUM) ,
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   626
                    ("s" $ SEMI) ,
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   627
                    ("str" $ STRING), 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   628
                    ("p" $ (LPAREN | RPAREN)), 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   629
                    ("b" $ (BEGIN | END)), 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   630
                    ("w" $ WHITESPACE)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   631
      )
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   632
    )
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   633
  ).%
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   634
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   635
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   636
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   637
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   638
  //--------------------------------------------------------------------------------------------------------START OF NON-BITCODE PART (TESTING)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   639
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   640
  // Two Simple While programs
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   641
  //========================
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   642
  println("prog0 test")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   643
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   644
  val prog0 = """read n"""
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   645
  println(env(lexing_simp(WHILE_REGS, prog0)))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   646
  println(tokenise(WHILE_REGS, prog0))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   647
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   648
  println("prog1 test")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   649
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   650
  val prog1 = """read  n; write (n)"""
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   651
  println(tokenise(WHILE_REGS, prog1))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   652
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   653
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   654
  // Bigger Tests
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   655
  //==============
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   656
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   657
  def escape(raw: String): String = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   658
    import scala.reflect.runtime.universe._
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   659
    Literal(Constant(raw)).toString
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   660
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   661
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   662
  val prog2 = """
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   663
  write "Fib";
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   664
  read n;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   665
  minus1 := 0;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   666
  minus2 := 1;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   667
  while n > 0 do {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   668
    temp := minus2;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   669
    minus2 := minus1 + minus2;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   670
    minus1 := temp;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   671
    n := n - 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   672
  };
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   673
  write "Result";
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   674
  write minus2
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   675
  """
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   676
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   677
  val prog3 = """
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   678
  start := 1000;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   679
  x := start;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   680
  y := start;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   681
  z := start;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   682
  while 0 < x do {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   683
  while 0 < y do {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   684
    while 0 < z do {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   685
      z := z - 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   686
    };
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   687
    z := start;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   688
    y := y - 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   689
  };     
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   690
  y := start;
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   691
  x := x - 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   692
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   693
  """
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   694
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   695
  for(i <- 400 to 400 by 1){
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   696
    println(i+":")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   697
    blexing_simp(WHILE_REGS, prog2 * i)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   698
  } */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   699
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   700
    /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   701
    for (i <- 2 to 5){
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   702
      for(j <- 1 to 3){
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   703
        println(i,j)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   704
        small_de = i
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   705
        usual_de = i + j
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   706
        big_de = i + 2*j 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   707
        blexing_simp(AWHILE_REGS, prog2 * 100)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   708
      }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   709
    }*/
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   710
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   711
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   712
  println("Tokens of prog2")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   713
  println(tokenise(WHILE_REGS, prog2).mkString("\n"))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   714
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   715
  val fib_tokens = tokenise(WHILE_REGS, prog2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   716
  fib_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   717
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   718
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   719
  val test_tokens = tokenise(WHILE_REGS, prog3)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   720
  test_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   721
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   722
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   723
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   724
  println("time test for blexing_simp")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   725
  for (i <- 1 to 1 by 1) {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   726
    lexing_simp(WHILE_REGS, prog2 * i)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   727
    blexing_simp(WHILE_REGS, prog2 * i)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   728
    for( j <- 0 to each_simp_timeb.length - 1){
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   729
      if( each_simp_timeb(j)/each_simp_time(j) >= 10.0 )
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   730
        println(j, each_simp_timeb(j), each_simp_time(j))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   731
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   732
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   733
  */
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   734
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   735
  // Tiger Language
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   736
  //================
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   737
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   738
  //symbols
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   739
  val TSYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   740
  //digits
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   741
  val TDIGIT = PRED("0123456789".contains(_))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   742
  //identifiers
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   743
  val TID = TSYM ~ (TSYM | TDIGIT | "_").% 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   744
  //numbers
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   745
  val TNUM = PLUS(TDIGIT)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   746
  //keywords
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   747
  val TKEYWORD : Rexp = { "array" | "break" | "do" | "else" | "end" | "for" | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   748
                          "function" | "if" | "in" | "let" | "nil" | "of" | "then" | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   749
                          "to" | "type" | "var" | "while" }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   750
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   751
  //operators
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   752
  val TOP: Rexp = { "(" | ")" | "[" | "]" | "{" | "}" | ":" | ":=" | "." | "," | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   753
        ";" | "/" | "+" | "-" | "=" | "<>" | ">" | "<" | ">=" | "<=" | "&" | "|" }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   754
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   755
  //whitespaces
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   756
  val TSPECIAL : Rexp = PRED((""".:=()\;-""" ++ "\"").contains(_))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   757
  val TWS : Rexp = " " | "\n" | "\t"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   758
  //comments...but probably needs not
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   759
  val TCOMMENT: Rexp = """/*""" ~ (TSYM | TWS | TSPECIAL | TDIGIT).% ~ """*/"""
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   760
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   761
  val TWHITESPACE : Rexp = PLUS(TWS) | TCOMMENT
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   762
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   763
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   764
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   765
  //strings...but probably needs not
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   766
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   767
  val TSTRING: Rexp = "\"" ~ (TSYM | " " | TSPECIAL | TDIGIT).% ~ "\""
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   768
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   769
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   770
  // for indicating lexing errors
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   771
  val ERROR = PRED((_) => true)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   772
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   773
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   774
  val TIGER_REGS = (("k" $ TKEYWORD) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   775
                    ("i" $ TID) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   776
                    ("o" $ TOP) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   777
                    ("n" $ TNUM) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   778
                    ("str" $ TSTRING) |
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   779
                    ("w" $ TWHITESPACE) | 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   780
                    ("err" $ ERROR)).%
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   781
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   782
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   783
  //println(tokenise_file(TIGER_REGS, "test.tig").mkString("\n"))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   784
  //println(tokenise_file(TIGER_REGS, "queens.tig").mkString("\n"))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   785
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   786
  //tokenise(TCOMMENT,"""/**/""")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   787
  //tokenise(TCOMMENT,"""/*a a a */""")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   788
  //tokenise(TIGER_REGS,"""/*a a a */""")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   789
  //tokenise(TCOMMENT,"""/* A program to solve the 8-queens problem */""")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   790
  //tokenise(TIGER_REGS,"""/* A program to solve the 8-queens problem */""")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   791
  //tokenise(TCOMMENT,"""/*  for i:= 0 to c do print("."); print("\n"); flush();*/""")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   792
  //tokenise(TIGER_REGS,"""/*  for i:= 0 to c do print("."); print("\n"); flush();*/""")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   793
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   794
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   795
  //--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART (TESTING)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   796
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   797
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   798
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   799
  def clear() = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   800
    print("")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   801
    //print("\33[H\33[2J")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   802
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   803
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   804
  //testing the two lexings produce the same value
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   805
  //enumerates strings of length n over alphabet cs
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   806
  def strs(n: Int, cs: String) : Set[String] = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   807
    if (n == 0) Set("")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   808
    else {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   809
      val ss = strs(n - 1, cs)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   810
      ss ++
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   811
      (for (s <- ss; c <- cs.toList) yield c + s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   812
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   813
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   814
  def enum(n: Int, s: String) : Stream[Rexp] = n match {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   815
    case 0 => ZERO #:: ONE #:: s.toStream.map(CHAR)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   816
    case n => {  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   817
      val rs = enum(n - 1, s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   818
      rs #:::
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   819
      (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   820
      (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   821
      (for (r1 <- rs) yield STAR(r1))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   822
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   823
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   824
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   825
  //tests blexing and lexing
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   826
  def tests_blexer_simp(ss: Set[String])(r: Rexp) = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   827
    clear()
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   828
    //println(s"Testing ${r}")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   829
    for (s <- ss.par) yield {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   830
      val res1 = Try(Some(lexing_simp(r, s))).getOrElse(None)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   831
      val res2 = Try(Some(blexing_simp(r, s))).getOrElse(None)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   832
      if (res1 != res2) println(s"Disagree on ${r} and ${s}")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   833
      if (res1 != res2) println(s"   ${res1} !=  ${res2}")
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   834
      if (res1 != res2) Some((r, s)) else None
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   835
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   836
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   837
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   838
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   839
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   840
  //enum(3, "abc").map(tests_blexer_simp(strs(3, "abc"))).toSet
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   841
  /*
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   842
  def single_expression_explorer(ar: ARexp, ss: Set[String]): Unit = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   843
    for (s <- ss){
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   844
      
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   845
      val der_res = bder(c, ar) 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   846
      val simp_res = bsimp(der_res)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   847
      println(asize(der_res))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   848
      println(asize(simp_res))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   849
      single_expression_explorer(simp_res, (sc - c))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   850
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   851
  }*/
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   852
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   853
  //single_expression_explorer(internalise(("c"~("a"+"b"))%) , Set('a','b','c'))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   854
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   855
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   856
}
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   857
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   858
import Rexp.Bits
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   859
abstract class ARexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   860
case object AZERO extends ARexp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   861
case class AONE(bs: Bits) extends ARexp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   862
case class APRED(bs: Bits, f: Char => Boolean) extends ARexp
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   863
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   864
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   865
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   866
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   867
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   868
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   869
abstract class Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   870
case object Empty extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   871
case class Chr(c: Char) extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   872
case class Sequ(v1: Val, v2: Val) extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   873
case class Left(v: Val) extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   874
case class Right(v: Val) extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   875
case class Stars(vs: List[Val]) extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   876
case class Rec(x: String, v: Val) extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   877
//case class Pos(i: Int, v: Val) extends Val
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
   878
case object Prd extends Val