lex_blex_Frankensteined.scala
changeset 15 cd0ceaf89c1d
parent 14 610f14009c0b
child 16 c51178fa85fe
--- a/lex_blex_Frankensteined.scala	Sat Apr 13 16:18:23 2019 +0100
+++ b/lex_blex_Frankensteined.scala	Sun May 05 22:02:29 2019 +0100
@@ -383,7 +383,42 @@
       case AZERO :: rs1 => flats(rs1)
       case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
       case r1 :: rs2 => r1 :: flats(rs2)
+  }
+  def remove(v: Val): Val = v match{
+    case Right(v1) => v1
+    case Left(v1) => v1
+    case _ => throw new Exception("Not removable")
+  }
+  def augment(v: Val, i: Int): Val = if(i > 1) augment(Right(v), i - 1) else Right(v)
+//an overly complex version
+/*
+    if(rel_dist >0){//the regex we are dealing with is not what v points at
+      rs match{
+        case Nil => throw new Exception("Trying to simplify a non-existent value")
+        case AZERO :: rs1 => flats_vsimp(rs1, rel_dist - 1, remove(v))
+        case AALTS(bs, rs1) :: rs2 => flats_vsimp(rs2, rel_dist - 1, augment(v, rs1.length - 1))//rs1 is guaranteed to have a len geq 2
+        case r1 :: rs2 => flats_vsimp(rs2, rel_dist - 1, v)
+      }
     }
+    else if(rel_dist == 0){//we are dealing with regex v is pointing to -- "v->r itself"
+      rs match{//r1 cannot be zero
+        AALTS(bs, rs1) :: rs2 => flats_vsimp(  )
+        AZERO::rs2 => throw new Exception("Value corresponds to zero")
+        r1::rs2 => flats_vsimp(rs2, rel_dist - 1, v)
+      }
+
+    }
+    else{
+
+    }
+    */
+  def flats_vsimp(rs: List[ARexp], position: Int): Int = (rs, position) match {
+    case (_, 0) => 0
+    case (Nil, _) => 0
+    case (ZERO :: rs1, _) => flats_vsimp(rs1, position - 1) - 1
+    case (AALTS(bs, rs1) :: rs2, _) => rs1.length - 1 + flats_vsimp(rs2, position - 1)
+    case (r1 :: rs2, _) => flats_vsimp(rs2, position - 1)
+  }
   def rflats(rs: List[Rexp]): List[Rexp] = rs match {
     case Nil => Nil
     case ZERO :: rs1 => rflats(rs1)
@@ -413,6 +448,69 @@
     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
     case r => r
   }
+  def find_pos(v: Val, rs: List[Rexp]): Int = (rs, v) match{
+    case (Right(v), r::Nil) => 1
+    case (Left(v), r::rs) => 0
+    case (Right(v), r::rs) => find_pos(v, rs) + 1
+    case (v, _) => 0
+  }
+  def remove(v: Val, rs: List[Rexp]) : Val = (rs,v) match {//remove the outmost layer of ALTS's Left and Right
+    case (Right(v), r::Nil) => v 
+    case (Left(v), r::rs) => v 
+    case (Right(v), r::rs) => remove(v, rs)
+  }
+  def simple_end(v: Value): Boolean = v match {
+    case Left(v) => return false
+    case Right(v) => return simple_end(v)
+    case v => return true
+  }
+  def isend(v: Val, rs: List[Rexp], position: Int): Boolean = {
+    val rsbh = rs.slice(position, rs.length)
+    val out_end = if(flats(rsbh) == Nil) true else false
+    val inner_end = simple_end(v)
+    inner_end && out_end
+  }
+  def get_coat(v: Val, rs: List[Rexp], vs: Val): Val = (rs, v) match{//the dual operation of remove(so-called by myself)
+    case (Right(v), r::Nil) => Right(vs)
+    case (Left(v), r::rs) => Left(vs) 
+    case (Right(v), r::rs) => Right(get_coat(v, rs, vs))
+  }
+  def coat(v: Val, i: Int) : Val = i match {
+    case 0 => v
+    case i => coat(Right(v), i - 1)
+  }
+  def bsimp2(r: ARexp, v: Val): (ARexp, Val => Val) = (r,v) match{
+    case (ASEQ(bs1, r1, r2), v) => (bsimp2(r1), bsimp2(r2)) match {
+        case ((AZERO, _), (_, _) )=> (AZERO, undefined)
+        case ((_, _), (AZERO, _)) => (AZERO, undefined)
+        case ((AONE(bs2), f1) , (r2s, f2)) => (fuse(bs1 ++ bs2, r2s), lambda v => v match { case Sequ(_, v) => f2(v) } )
+        case ((r1s, f1), (r2s, f2)) => (ASEQ(bs1, r1s, r2s), lambda v => v match {case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))}
+    }
+    case AALTS(bs1, rs) => {
+      val init_ind = find_pos(v, rs)
+      val vs = bsimp2(rs[init_ind], remove(v, rs))//remove all the outer layers of left and right in v to  match the regx rs[i]
+      val rs_simp = rs.map(bsimp)
+      val vs_kernel = rs_simp[init_ind] match {
+        case AALTS(bs2, rs2) => remove(vs, rs_simp[init_ind])//remove the secondary layer of left and right
+        case r => vs
+      }
+      val vs_for_coating = if(isend(vs, rs_simp, init_ind)) vs_kernel else Left(vs_kernel)
+
+      val r_s = rs_simp[init_ind]
+      val shift = flats_vsimp(vs, rs_simp, init_ind) + find_pos(vs, rs_simp[init_ind])
+      val vf = coat(vs_for_coating, shift + init_ind)
+
+      val flat_res = flats(rs_simp)//flats2 returns a list of regex and a single v
+      val dist_res = distinctBy(flat_res, erase)
+      dist_res match {
+        case Nil => AZERO
+        case s :: Nil => fuse(bs1, s)
+        case rs => AALTS(bs1, rs)  
+      }
+    }
+    //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
+    case r => r  
+  }
   def super_bsimp(r: ARexp): ARexp = r match {
     case ASEQ(bs1, r1, r2) => (super_bsimp(r1), super_bsimp(r2)) match {
         case (AZERO, _) => AZERO