token1.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 30 Jan 2019 12:13:41 +0000
changeset 296 9aebc106549b
permissions -rw-r--r--
added Chengsong's experiment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
296
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.language.implicitConversions    
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import scala.language.reflectiveCalls
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import scala.annotation.tailrec   
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
  case Nil => Nil
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  case (x::xs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
    val res = f(x)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    if (acc.contains(res)) distinctBy(xs, f, acc)  
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
    else x::distinctBy(xs, f, res::acc)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
} 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
abstract class Bit
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case object Z extends Bit
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
case object S extends Bit
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case class C(c: Char) extends Bit
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
type Bits = List[Bit]
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
abstract class Action
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object ST extends Action
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case object NST extends Action
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case object AL extends Action
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
abstract class PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
case object Plhdr extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case object STS extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
case object ENDSTS extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case class Ch(c: Char) extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
case object Empt extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
case object Seque extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
case class Posi(i: Int) extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
case class RECRD(x: String) extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
case object ALTSTART extends  PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
case object ALTEND extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
case object RIG extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
case object LEF extends PartialValue
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
abstract class Rexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
case object ZERO extends Rexp
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
case object ONE extends Rexp
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
case class PRED(f: Char => Boolean) extends Rexp
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
case class ALTS(rs: List[Rexp]) extends Rexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
case class STAR(r: Rexp) extends Rexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
case class RECD(x: String, r: Rexp) extends Rexp
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
// abbreviations
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
def CHAR(c: Char) = PRED(_ == c)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
def PLUS(r: Rexp) = SEQ(r, STAR(r))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
abstract class ARexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
case object AZERO extends ARexp
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
case class AONE(bs: Bits) extends ARexp
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
case class APRED(bs: Bits, f: Char => Boolean) extends ARexp
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
abstract class Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
case object Empty extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
case class Chr(c: Char) extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
case class Sequ(v1: Val, v2: Val) extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
case class Left(v: Val) extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
case class Right(v: Val) extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
case class Stars(vs: List[Val]) extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
case class Rec(x: String, v: Val) extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
case class Pos(i: Int, v: Val) extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
case object Prd extends Val
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
   
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
// some convenience for typing in regular expressions
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
def charlist2rexp(s : List[Char]): Rexp = s match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  case Nil => ONE
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  case c::Nil => CHAR(c)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
implicit def RexpOps(r: Rexp) = new {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  def | (s: Rexp) = ALT(r, s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  def % = STAR(r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  def ~ (s: Rexp) = SEQ(r, s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
implicit def stringOps(s: String) = new {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  def | (r: Rexp) = ALT(s, r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  def | (r: String) = ALT(s, r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  def % = STAR(s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
  def ~ (r: Rexp) = SEQ(s, r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  def ~ (r: String) = SEQ(s, r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  def $ (r: Rexp) = RECD(s, r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
// translation into ARexps
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  case AZERO => AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  case AONE(cs) => AONE(bs ++ cs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  case APRED(cs, f) => APRED(bs ++ cs, f)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
def internalise(r: Rexp) : ARexp = r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  case ZERO => AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  case ONE => AONE(Nil)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  case PRED(f) => APRED(Nil, f)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  case ALTS(List(r1, r2)) => 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
    AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  case ALTS(r1::rs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
     val AALTS(Nil, rs2) = internalise(ALTS(rs))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
     AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _)))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  case STAR(r) => ASTAR(Nil, internalise(r))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  case RECD(x, r) => internalise(r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
internalise(("a" | "ab") ~ ("b" | ""))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
val action_stack = scala.collection.mutable.ArrayBuffer.empty[Action]
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
val next_stack = scala.collection.mutable.ArrayBuffer.empty[Int]
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
val regx_stack = scala.collection.mutable.ArrayBuffer.empty[Rexp]
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
val pv_stack = scala.collection.mutable.ArrayBuffer.empty[PartialValue]
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
var top = 0
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
//st is the global var stack, made with a linked list?
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
@tailrec
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
def decode_stack(sp: Int, bs: Bits): Unit = {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
if(action_stack.isEmpty){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  return 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
val action = action_stack.last
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
action_stack.trimEnd(1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
val r = regx_stack.last
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
regx_stack.trimEnd(1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
if(action == ST)//we have the rest of the star to finish(ST -> STAR)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
{
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  bs match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
    case Z::bs => {//pv -> partial value  Each grid in a stack does not hold a whole value but a partial one.
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
      pv_stack(sp) = ENDSTS
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
      if(next_stack.isEmpty)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
        return 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
      val n = next_stack.last
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
      next_stack.trimEnd(1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
      decode_stack(n, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
    case S::bs => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
        next_stack(i) = next_stack(i) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
      next_stack += (sp + 1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
      regx_stack += r  
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
      action_stack += ST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
      pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
      regx_stack += r 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
      decode_stack(sp, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
    case _ => println("Sequence not decodable")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
else if(action == NST){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
  (r, bs) match{
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
    case (ONE, bs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
      pv_stack(sp) = Empt
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
      if(next_stack.isEmpty)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
        return 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
      val n = next_stack.last
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
      next_stack.trimEnd(1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
      decode_stack(n, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
    case (PRED(f), C(c)::bs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
      pv_stack(sp) = Ch(c)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
      if(next_stack.isEmpty)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
        return 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
      val n = next_stack.last
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
      next_stack.trimEnd(1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
      decode_stack(n, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
    case (ALTS(rs), Z::bs1) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
      pv_stack(sp) = ALTSTART
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
      pv_stack.insert(sp + 1, LEF)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
      pv_stack.insert(sp + 2, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
      pv_stack.insert(sp + 3, ALTEND)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
        next_stack(i) = next_stack(i) + 3
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
      regx_stack += rs.head
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
      decode_stack(sp + 2, bs1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
    case (ALTS(rs), S::bs1) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
      pv_stack(sp) = ALTSTART
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
      pv_stack.insert(sp + 1, RIG)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
      pv_stack.insert(sp + 2, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
        next_stack(i) = next_stack(i) + 2
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
      regx_stack += ALTS(rs.tail)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
      action_stack += AL
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
      decode_stack(sp + 2, bs1)		
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
      /*
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
      val le = rs.length
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
      val det = bs.take(le - 1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
      val chosen =  det.indexWhere(_ == Z)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
      pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
        next_stack(i) = next_stack(i) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
      if(chosen ==  -1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
        pv_stack(sp) = Posi(le)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
        regx_stack += rs(le - 1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
        decode_stack(sp + 1, bs.drop(le - 1))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
      else{
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
        pv_stack(sp) = Posi(chosen + 1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
        regx_stack += rs(chosen)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
        decode_stack(sp + 1, bs.drop(chosen + 1))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
      }*/
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
    case (SEQ(r1, r2), bs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
        next_stack(i) = next_stack(i) + 2
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
      next_stack += (sp + 2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
      regx_stack += r2
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
      regx_stack += r1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
      pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
      pv_stack.insert(sp + 2, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
      pv_stack(sp) = Seque
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
      decode_stack(sp + 1, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
    case (STAR(r1), S::bs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
      action_stack += ST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
      regx_stack += r1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
      regx_stack += r1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
        next_stack(i) = next_stack(i) + 2
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
      next_stack += sp + 2
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
      pv_stack(sp) = STS
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
      pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
      pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
      decode_stack(sp + 1, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
    case (STAR(_), Z::bs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
      pv_stack(sp) = STS 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
      pv_stack.insert(sp + 1, ENDSTS)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
      if(next_stack.isEmpty)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
        return 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
        next_stack(i) = next_stack(i) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
      val n = next_stack.last
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
      next_stack.trimEnd(1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
      decode_stack(n, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
    case (RECD(x, r1), bs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
      pv_stack(sp) = RECRD(x)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
      pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
        next_stack(i) = next_stack(i) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
      regx_stack += r1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
      decode_stack(sp + 1, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
    }//shouldn't go beyond this point
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
    case (_, _) => println("Error with NST")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
else{//action is AL
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
    case (ALTS(r1::Nil)) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
      pv_stack.insert(sp + 1, ALTEND)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
      for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
        next_stack(i) = next_stack(i) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
      action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
      regx_stack += r1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
      decode_stack(sp, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
    case (ALTS(rs)) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
      bs match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
        case (Z::bs1) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
          pv_stack(sp) = LEF
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
          pv_stack.insert(sp + 1, ALTEND)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
          pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
          for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
            next_stack(i) = next_stack(i) + 2
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
          }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
          regx_stack += rs.head
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
          action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
          decode_stack(sp + 1, bs1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
        }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
        case (S::bs2) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
          pv_stack(sp) = RIG
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
          pv_stack.insert(sp + 1, Plhdr)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
          for(i <- 0 to next_stack.length - 1){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
            next_stack(i) = next_stack(i) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
          }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
          regx_stack += ALTS(rs.tail)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
          action_stack += AL
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
          decode_stack(sp + 1, bs2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
        }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
        case _ => println("Not decodable")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
      }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
    case (rs) => println(r,bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
//advantage: may decode chunks of bits
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
def decode(r: Rexp, bs: Bits) = {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
  action_stack.clear()
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
  next_stack.clear()
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
  regx_stack.clear()
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
  pv_stack.clear()
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
  action_stack += NST
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  regx_stack += r
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
  pv_stack += Plhdr
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
  decode_stack(0, bs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
/*
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
  case (v, Nil) => v
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
  case _ => throw new Exception("Not decodable")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
*/
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
//erase function: extracts the regx from Aregex
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
def erase(r:ARexp): Rexp = r match{
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
  case AZERO => ZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
  case AONE(_) => ONE
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
  case APRED(bs, f) => PRED(f)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
  case AALTS(bs, rs) => ALTS(rs.map(erase(_)))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
  case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
  case ASTAR(cs, r)=> STAR(erase(r))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
// nullable function: tests whether the aregular 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
// expression can recognise the empty string
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
def nullable (r: ARexp) : Boolean = r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
  case AZERO => false
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
  case AONE(_) => true
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
  case APRED(_,_) => false
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
  case AALTS(_, rs) => rs.exists(nullable)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
  case ASEQ(_, r1, r2) => nullable(r1) && nullable(r2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  case ASTAR(_, _) => true
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
def mkepsBC(r: ARexp) : Bits = r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  case AONE(bs) => bs
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  case AALTS(bs, rs) => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
    val n = rs.indexWhere(nullable)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
    bs ++ mkepsBC(rs(n))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
  case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
  case ASTAR(bs, r) => bs ++ List(Z)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
// derivative of a regular expression w.r.t. a character
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
def der(c: Char, r: ARexp) : ARexp = r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
  case AZERO => AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
  case AONE(_) => AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
  case APRED(bs, f) => if (f(c)) AONE(bs:::List(C(c))) else AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
  case AALTS(bs, rs) => AALTS(bs, rs.map(der(c, _)))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
  case ASEQ(bs, r1, r2) => 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
    if (nullable(r1)) AALT(bs, ASEQ(Nil, der(c, r1), r2), fuse(mkepsBC(r1), der(c, r2)))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
    else ASEQ(bs, der(c, r1), r2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
  case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), der(c, r)), ASTAR(Nil, r))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
// derivative w.r.t. a string (iterates der)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
@tailrec
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
def ders (s: List[Char], r: ARexp) : ARexp = s match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
  case Nil => r
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
  case c::s => ders(s, der(c, r))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
// main unsimplified lexing function (produces a value)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
def lex(r: ARexp, s: List[Char]) : Bits = s match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
  case Nil => if (nullable(r)) mkepsBC(r) else throw new Exception("Not matched")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
  case c::cs => lex(der(c, r), cs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
def pre_lexing(r: Rexp, s: String) = lex(internalise(r), s.toList)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
//def lexing(r: Rexp, s: String) : Val = decode(r, lex(internalise(r), s.toList))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
def flats(rs: List[ARexp]): List[ARexp] = rs match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
    case Nil => Nil
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
    case AZERO :: rs1 => flats(rs1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
    case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
    case r1 :: rs2 => r1 :: flats(rs2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
def simp(r: ARexp): ARexp = r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
  case ASEQ(bs1, r1, r2) => (simp(r1), simp(r2)) match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
      case (AZERO, _) => AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
      case (_, AZERO) => AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
  case AALTS(bs1, rs) => distinctBy(flats(rs.map(simp)), erase) match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
    case Nil => AZERO
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
    case s :: Nil => fuse(bs1, s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
    case rs => AALTS(bs1, rs)  
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
  case r => r
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
def ders_simp (s: List[Char], r: ARexp) : ARexp = s match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
  case Nil => r
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
  case c::s => ders_simp(s, simp(der(c, r)))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
def lex_simp(r: ARexp, s: List[Char]) : Bits = s match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
  case Nil => {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
    if (nullable(r)) {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
      //println(asize(r))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
      mkepsBC(r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
    }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
   else throw new Exception("Not matched")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
  }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
  case c::cs => lex_simp(simp(der(c, r)), cs)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
//size: of a Aregx for testing purposes 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
def size(r: Rexp) : Int = r match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
  case ZERO => 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
  case ONE => 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
  case PRED(_) => 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
  case ALTS(rs) => 1 + rs.map(size).sum
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
  case STAR(r) => 1 + size(r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
def asize(a: ARexp) = size(erase(a))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
// decoding does not work yet
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
def lexing_simp(r: Rexp, s: String) = {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
  val final_derivative = lex_simp(internalise(r), s.toList)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
  println("The length of bit sequence:")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
  println((final_derivative.length))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
  //println(final_derivative)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
  decode(r, final_derivative) 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
  //println(vsize(value))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
def vsize(v: Val): Int = v match {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
  case Empty => 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
  case Chr(c) => 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
  case Sequ(v1, v2) => vsize(v1) + vsize(v2) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
  case Left(v1) => vsize(v1) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
  case Right(v1) => vsize(v1) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
  case Stars(vs) => vs.map(vsize(_)).sum + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
  case Rec(x, v1) => vsize(v1) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
  case Pos(i, v1) => vsize(v1) + 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
  case Prd => 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
// Lexing Rules for a Small While Language
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
//symbols
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
//digits
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
val DIGIT = PRED("0123456789".contains(_))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
//identifiers
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
val ID = SYM ~ (SYM | DIGIT).% 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
//numbers
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
val NUM = PLUS(DIGIT)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
//keywords
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
//semicolons
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
val SEMI: Rexp = ";"
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
//operators
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
//whitespaces
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
val WHITESPACE = PLUS(" " | "\n" | "\t")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
//parentheses
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
val RPAREN: Rexp = ")"
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
val LPAREN: Rexp = "("
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
val BEGIN: Rexp = "{"
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
val END: Rexp = "}"
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
//strings...but probably needs not
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
val WHILE_REGS = (("k" $ KEYWORD) | 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
                  ("i" $ ID) | 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
                  ("o" $ OP) | 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
                  ("n" $ NUM) | 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
                  ("s" $ SEMI) | 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
                  ("str" $ STRING) |
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
                  ("p" $ (LPAREN | RPAREN)) | 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
                  ("b" $ (BEGIN | END)) | 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
                  ("w" $ WHITESPACE)).%
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
// filters out all white spaces
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
//def tokenise(r: Rexp, s: String) = 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
//  env(lexing_simp(r, s)).filterNot { _._1 == "w"}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
//reads the string from a file 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
//def fromFile(name: String) : String = 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
//  io.Source.fromFile(name).mkString
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
//def tokenise_file(r: Rexp, name: String) = 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
//  tokenise(r, fromFile(name))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
// Some Tests
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
//============
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
def compute_and_print(r: Rexp, s: String){
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
  //println(r)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
  //println(s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
  lexing_simp(r, s)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
  println(pv_stack)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
println("simple tests:")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
/*
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
println(lexing_simp((SYM.%), "abcd"))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
println(lexing_simp(((SYM.%) | NUM), "12345"))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
println(lexing_simp((WHILE_REGS), "abcd"))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
println(lexing_simp((WHILE_REGS), "12345"))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
println(lexing_simp((WHILE_REGS), "\nwrite \"Fib\";"))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
*/
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
compute_and_print((SYM.%), "abcd")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
compute_and_print(((SYM.%) | NUM), "12345")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
compute_and_print((WHILE_REGS), "abcd")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
compute_and_print((WHILE_REGS), "12345")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
compute_and_print((WHILE_REGS), "\nwrite \"Fib\";")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
def time[T](code: => T) = {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
  val start = System.nanoTime()
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
  val result = code
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  val end = System.nanoTime()
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
  println((end - start)/1.0e9)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
  result
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
val prog2 = """
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
write "Fib";
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
read n;
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
minus1 := 0;
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
minus2 := 1;
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
while n > 0 do {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
  temp := minus2;
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
  minus2 := minus1 + minus2;
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
  minus1 := temp;
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
  n := n -x 1
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
};
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
write "Result";
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
write minus2
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
"""
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
/*
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
val prog2 = """
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
write "Fib";
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
"""
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
*/
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
println("Iteration test with fib")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
for (i <- 900 to 1000 by 50) {
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
  print(i.toString + ":  ")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
  time(lexing_simp((WHILE_REGS), (prog2 * i)))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
  //time(lex_simp(internalise(WHILE_REGS), (prog2 * i).toList))
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
/*
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
def recurseTest(i:Int):Unit={
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
   try{
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
      recurseTest(i+1)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
   } catch { case e:java.lang.StackOverflowError => 
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
      println("Recursion depth on this system is " + i + ".")
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
   }
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
}
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
recurseTest(0)
9aebc106549b added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
*/