exps/both.scala
changeset 311 8b8db9558ecf
parent 307 ee1caac29bb2
child 312 8b0b414e71b0
--- a/exps/both.scala	Mon Feb 11 23:18:05 2019 +0000
+++ b/exps/both.scala	Sun Feb 17 22:15:06 2019 +0000
@@ -444,9 +444,6 @@
   case r => r
 }
 
-
-
-
 def bders_simp (s: List[Char], r: ARexp) : ARexp = s match {
   case Nil => r
   case c::s => bders_simp(s, bsimp(bder(c, r)))
@@ -463,7 +460,8 @@
  decode(r, blex_simp(internalise(r), s.toList))
 
 
-def btokenise_simp(r: Rexp, s: String) = env(blexing_simp(r, s)).map(esc2)
+def btokenise_simp(r: Rexp, s: String) = 
+  env(blexing_simp(r, s)).map(esc2)
 
 
 
@@ -503,6 +501,33 @@
 
 def btokenise_simp_full(r: Rexp, s: String) = env(blexing_simp_full(r, s)).map(esc2)
 
+// bders2 for strings in the ALTS case
+
+def bders2_simp(s: List[Char], a: ARexp) : ARexp = {
+  //println(s"s = ${s.length}   a = ${asize(a)}")
+  //Console.readLine
+  (s, a) match {
+  case (Nil, r) => r
+  case (s, AZERO) => AZERO
+  case (s, AONE(_)) => AZERO
+  case (s, APRED(bs, f, _)) => 
+    if (f(s.head) && s.tail == Nil) AONE(bs:::List(C(s.head))) else AZERO
+  case (s, AALTS(bs, rs)) => bsimp(AALTS(bs, rs.map(bders2_simp(s, _)))) 
+  case (c::s, r) => bders2_simp(s, bsimp(bder(c, r)))
+}}
+
+
+
+def blexing2_simp(r: Rexp, s: String) : Val = {
+  val bder = bders2_simp(s.toList, internalise(r))
+  if (bnullable(bder)) decode(r, bmkeps(bder)) else 
+  throw new Exception("Not matched")
+
+}
+
+def btokenise2_simp(r: Rexp, s: String) = 
+  env(blexing2_simp(r, s)).map(esc2)
+
 
 
 //   Testing
@@ -594,14 +619,22 @@
 println(astring(bders_simp("aaaaaabaaaabbbbbaaaaaaaaaaaaaaa".toList, internalise(re1))))
 
 
+for (i <- 0 to 100 by 5) {
+  //print("Old: " + time(tokenise_simp(re1, "a" * i)))
+  print(" Bit: " + time(btokenise_simp(re1, "a" * i)))
+  print(" Bit full simp: " + time(btokenise_simp_full(re1, "a" * i)))
+  println(" Bit2: " + time(btokenise2_simp(re1, "a" * i)))
+}
 
-
+Console.readLine
 
 
 // Bigger Tests
 //==============
 
 
+println("Big tests")
+
 val fib_prog = """
 write "Fib";
 read n;
@@ -627,12 +660,14 @@
   print("Old: " + time(tokenise_simp(WHILE_REGS, fib_prog * i)))
   print(" Bit: " + time(btokenise_simp(WHILE_REGS, fib_prog * i)))
   println(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i)))
+  //println(" Bit2: " + time(btokenise2_simp(WHILE_REGS, fib_prog * i)))
 }
 
 
 println("Original " + size(WHILE_REGS))
 println("Size Bit  " + asize(bders_simp((fib_prog * 1).toList, internalise(WHILE_REGS))))
 println("Size Bitf " + asize(bders_simp_full((fib_prog * 1).toList, internalise(WHILE_REGS))))
+println("Size Bit2 " + asize(bders2_simp((fib_prog * 1).toList, internalise(WHILE_REGS))))
 println("Size Old  " + size(ders_simp((fib_prog * 1).toList, WHILE_REGS)))
 println("Size Pder " + psize(pders_simp((fib_prog * 1).toList, WHILE_REGS)))