--- a/exps/token1.scala Wed Jan 30 12:28:44 2019 +0000
+++ b/exps/token1.scala Thu Jan 31 09:07:50 2019 +0000
@@ -20,25 +20,6 @@
type Bits = List[Bit]
-abstract class Action
-case object ST extends Action
-case object NST extends Action
-case object AL extends Action
-
-abstract class PartialValue
-case object Plhdr extends PartialValue
-case object STS extends PartialValue
-case object ENDSTS extends PartialValue
-case class Ch(c: Char) extends PartialValue
-case object Empt extends PartialValue
-case object Seque extends PartialValue
-case class Posi(i: Int) extends PartialValue
-case class RECRD(x: String) extends PartialValue
-case object ALTSTART extends PartialValue
-case object ALTEND extends PartialValue
-case object RIG extends PartialValue
-case object LEF extends PartialValue
-
abstract class Rexp
case object ZERO extends Rexp
case object ONE extends Rexp
@@ -61,7 +42,7 @@
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp
case class ASTAR(bs: Bits, r: ARexp) extends ARexp
-
+// abbreviations
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
abstract class Val
@@ -72,8 +53,8 @@
case class Right(v: Val) extends Val
case class Stars(vs: List[Val]) extends Val
case class Rec(x: String, v: Val) extends Val
-case class Pos(i: Int, v: Val) extends Val
-case object Prd extends Val
+
+
// some convenience for typing in regular expressions
def charlist2rexp(s : List[Char]): Rexp = s match {
@@ -124,221 +105,43 @@
}
internalise(("a" | "ab") ~ ("b" | ""))
-val action_stack = scala.collection.mutable.ArrayBuffer.empty[Action]
-val next_stack = scala.collection.mutable.ArrayBuffer.empty[Int]
-val regx_stack = scala.collection.mutable.ArrayBuffer.empty[Rexp]
-val pv_stack = scala.collection.mutable.ArrayBuffer.empty[PartialValue]
-var top = 0
-//st is the global var stack, made with a linked list?
-@tailrec
-def decode_stack(sp: Int, bs: Bits): Unit = {
-if(action_stack.isEmpty){
- return
-}
-val action = action_stack.last
-action_stack.trimEnd(1)
-val r = regx_stack.last
-regx_stack.trimEnd(1)
-if(action == ST)//we have the rest of the star to finish(ST -> STAR)
-{
- bs match {
- case Z::bs => {//pv -> partial value Each grid in a stack does not hold a whole value but a partial one.
- pv_stack(sp) = ENDSTS
- if(next_stack.isEmpty)
- return
- val n = next_stack.last
- next_stack.trimEnd(1)
- decode_stack(n, bs)
+
+def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
+ case (ONE, bs) => (Empty, bs)
+ case (PRED(f), C(c)::bs) => (Chr(c), bs)
+ case (ALTS(r::Nil), bs) => decode_aux(r, bs)
+ case (ALTS(rs), bs) => bs match {
+ case Z::bs1 => {
+ val (v, bs2) = decode_aux(rs.head, bs1)
+ (Left(v), bs2)
}
- case S::bs => {
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 1
- }
- next_stack += (sp + 1)
- regx_stack += r
- action_stack += ST
- pv_stack.insert(sp + 1, Plhdr)
- action_stack += NST
- regx_stack += r
- decode_stack(sp, bs)
- }
- case _ => println("Sequence not decodable")
- }
-
-}
-else if(action == NST){
- (r, bs) match{
- case (ONE, bs) => {
- pv_stack(sp) = Empt
- if(next_stack.isEmpty)
- return
- val n = next_stack.last
- next_stack.trimEnd(1)
- decode_stack(n, bs)
- }
- case (PRED(f), C(c)::bs) => {
- pv_stack(sp) = Ch(c)
- if(next_stack.isEmpty)
- return
- val n = next_stack.last
- next_stack.trimEnd(1)
- decode_stack(n, bs)
- }
- case (ALTS(rs), Z::bs1) => {
- pv_stack(sp) = ALTSTART
- pv_stack.insert(sp + 1, LEF)
- pv_stack.insert(sp + 2, Plhdr)
- pv_stack.insert(sp + 3, ALTEND)
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 3
- }
- regx_stack += rs.head
- action_stack += NST
- decode_stack(sp + 2, bs1)
+ case S::bs1 => {
+ val (v, bs2) = decode_aux(ALTS(rs.tail), bs1)
+ (Right(v), bs2)
}
- case (ALTS(rs), S::bs1) => {
- pv_stack(sp) = ALTSTART
- pv_stack.insert(sp + 1, RIG)
- pv_stack.insert(sp + 2, Plhdr)
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 2
- }
- regx_stack += ALTS(rs.tail)
- action_stack += AL
- decode_stack(sp + 2, bs1)
- }
- /*
- val le = rs.length
- val det = bs.take(le - 1)
- val chosen = det.indexWhere(_ == Z)
- action_stack += NST
- pv_stack.insert(sp + 1, Plhdr)
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 1
- }
- if(chosen == -1){
- pv_stack(sp) = Posi(le)
- regx_stack += rs(le - 1)
- decode_stack(sp + 1, bs.drop(le - 1))
- }
- else{
- pv_stack(sp) = Posi(chosen + 1)
- regx_stack += rs(chosen)
- decode_stack(sp + 1, bs.drop(chosen + 1))
- }*/
- case (SEQ(r1, r2), bs) => {
- action_stack += NST
- action_stack += NST
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 2
- }
- next_stack += (sp + 2)
- regx_stack += r2
- regx_stack += r1
- pv_stack.insert(sp + 1, Plhdr)
- pv_stack.insert(sp + 2, Plhdr)
- pv_stack(sp) = Seque
- decode_stack(sp + 1, bs)
- }
- case (STAR(r1), S::bs) => {
- action_stack += ST
- regx_stack += r1
- action_stack += NST
- regx_stack += r1
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 2
- }
- next_stack += sp + 2
- pv_stack(sp) = STS
- pv_stack.insert(sp + 1, Plhdr)
- pv_stack.insert(sp + 1, Plhdr)
- decode_stack(sp + 1, bs)
- }
- case (STAR(_), Z::bs) => {
- pv_stack(sp) = STS
- pv_stack.insert(sp + 1, ENDSTS)
- if(next_stack.isEmpty)
- return
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 1
- }
- val n = next_stack.last
- next_stack.trimEnd(1)
- decode_stack(n, bs)
- }
- case (RECD(x, r1), bs) => {
- pv_stack(sp) = RECRD(x)
- pv_stack.insert(sp + 1, Plhdr)
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 1
- }
- action_stack += NST
- regx_stack += r1
- decode_stack(sp + 1, bs)
- }//shouldn't go beyond this point
- case (_, _) => println("Error with NST")
+ }
+ case (SEQ(r1, r2), bs) => {
+ val (v1, bs1) = decode_aux(r1, bs)
+ val (v2, bs2) = decode_aux(r2, bs1)
+ (Sequ(v1, v2), bs2)
+ }
+ case (STAR(r1), S::bs) => {
+ val (v, bs1) = decode_aux(r1, bs)
+ val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
+ (Stars(v::vs), bs2)
+ }
+ case (STAR(_), Z::bs) => (Stars(Nil), bs)
+ case (RECD(x, r1), bs) => {
+ val (v, bs1) = decode_aux(r1, bs)
+ (Rec(x, v), bs1)
}
}
-else{//action is AL
- r match {
- case (ALTS(r1::Nil)) => {
- pv_stack.insert(sp + 1, ALTEND)
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 1
- }
- action_stack += NST
- regx_stack += r1
- decode_stack(sp, bs)
- }
- case (ALTS(rs)) => {
- bs match {
- case (Z::bs1) => {
- pv_stack(sp) = LEF
- pv_stack.insert(sp + 1, ALTEND)
- pv_stack.insert(sp + 1, Plhdr)
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 2
- }
- regx_stack += rs.head
- action_stack += NST
- decode_stack(sp + 1, bs1)
- }
- case (S::bs2) => {
- pv_stack(sp) = RIG
- pv_stack.insert(sp + 1, Plhdr)
- for(i <- 0 to next_stack.length - 1){
- next_stack(i) = next_stack(i) + 1
- }
- regx_stack += ALTS(rs.tail)
- action_stack += AL
- decode_stack(sp + 1, bs2)
- }
- case _ => println("Not decodable")
- }
- }
- case (rs) => println(r,bs)
- }
-}
-}
-//advantage: may decode chunks of bits
-def decode(r: Rexp, bs: Bits) = {
- action_stack.clear()
- next_stack.clear()
- regx_stack.clear()
- pv_stack.clear()
- action_stack += NST
- regx_stack += r
- pv_stack += Plhdr
-
- decode_stack(0, bs)
-}
-/*
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
case (v, Nil) => v
case _ => throw new Exception("Not decodable")
}
-*/
+
//erase function: extracts the regx from Aregex
def erase(r:ARexp): Rexp = r match{
@@ -405,7 +208,7 @@
case AZERO :: rs1 => flats(rs1)
case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
case r1 :: rs2 => r1 :: flats(rs2)
- }
+}
def simp(r: ARexp): ARexp = r match {
case ASEQ(bs1, r1, r2) => (simp(r1), simp(r2)) match {
@@ -462,19 +265,6 @@
}
-def vsize(v: Val): Int = v match {
- case Empty => 1
- case Chr(c) => 1
- case Sequ(v1, v2) => vsize(v1) + vsize(v2) + 1
- case Left(v1) => vsize(v1) + 1
- case Right(v1) => vsize(v1) + 1
- case Stars(vs) => vs.map(vsize(_)).sum + 1
- case Rec(x, v1) => vsize(v1) + 1
- case Pos(i, v1) => vsize(v1) + 1
- case Prd => 1
-}
-
-
// Lexing Rules for a Small While Language
//symbols
@@ -528,25 +318,16 @@
// Some Tests
//============
-def compute_and_print(r: Rexp, s: String){
- //println(r)
- //println(s)
- lexing_simp(r, s)
- println(pv_stack)
-}
+
println("simple tests:")
-/*
+
println(lexing_simp((SYM.%), "abcd"))
println(lexing_simp(((SYM.%) | NUM), "12345"))
println(lexing_simp((WHILE_REGS), "abcd"))
println(lexing_simp((WHILE_REGS), "12345"))
-println(lexing_simp((WHILE_REGS), "\nwrite \"Fib\";"))
-*/
-compute_and_print((SYM.%), "abcd")
-compute_and_print(((SYM.%) | NUM), "12345")
-compute_and_print((WHILE_REGS), "abcd")
-compute_and_print((WHILE_REGS), "12345")
-compute_and_print((WHILE_REGS), "\nwrite \"Fib\";")
+println(lexing_simp((WHILE_REGS), """write "Fib";"""))
+
+
def time[T](code: => T) = {
val start = System.nanoTime()
@@ -585,14 +366,3 @@
//time(lex_simp(internalise(WHILE_REGS), (prog2 * i).toList))
}
-
-/*
-def recurseTest(i:Int):Unit={
- try{
- recurseTest(i+1)
- } catch { case e:java.lang.StackOverflowError =>
- println("Recursion depth on this system is " + i + ".")
- }
-}
-recurseTest(0)
-*/
\ No newline at end of file