lex_blex_Frankensteined.scala
changeset 151 73f990bc6843
parent 150 b51d34113d47
equal deleted inserted replaced
150:b51d34113d47 151:73f990bc6843
     1 package RexpRelated
     1 
       
     2 import Element.elem
     2 import scala.language.implicitConversions    
     3 import scala.language.implicitConversions    
     3 import scala.language.reflectiveCalls
     4 import scala.language.reflectiveCalls
     4 import scala.annotation.tailrec   
     5 import scala.annotation.tailrec   
     5 import scala.util.Try
     6 import scala.util.Try
     6 
     7 
       
     8 
       
     9 package RexpRelated
       
    10 {
     7 abstract class Bit
    11 abstract class Bit
     8 case object Z extends Bit
    12 case object Z extends Bit
     9 case object S extends Bit
    13 case object S extends Bit
    10 //case class C(c: Char) extends Bit
    14 //case class C(c: Char) extends Bit
    11 
    15 
    27   type Mon = (Char, Rexp)
    31   type Mon = (Char, Rexp)
    28   type Lin = Set[Mon]
    32   type Lin = Set[Mon]
    29   def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
    33   def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
    30   def PLUS(r: Rexp) = SEQ(r, STAR(r))
    34   def PLUS(r: Rexp) = SEQ(r, STAR(r))
    31   def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
    35   def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
       
    36 
       
    37 
       
    38 
       
    39 
    32 
    40 
    33 
    41 
    34   def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
    42   def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
    35     case Nil => Nil
    43     case Nil => Nil
    36     case (x::xs) => {
    44     case (x::xs) => {
   659         case ((AONE(bs2), v1s) , (r2s, v2s)) => (vtails => Sequ(v1, vunsimp(r2, v2)(vtails)))  //(fuse(bs1 ++ bs2, r2s), v2s )//v2 tells how to retrieve bits in r2s, which is enough as the bits of the first part of the sequence has already been integrated to the top level of the second regx.
   667         case ((AONE(bs2), v1s) , (r2s, v2s)) => (vtails => Sequ(v1, vunsimp(r2, v2)(vtails)))  //(fuse(bs1 ++ bs2, r2s), v2s )//v2 tells how to retrieve bits in r2s, which is enough as the bits of the first part of the sequence has already been integrated to the top level of the second regx.
   660         case ((r1s, v1s), (r2s, v2s)) => (vsmall => vsmall match {
   668         case ((r1s, v1s), (r2s, v2s)) => (vsmall => vsmall match {
   661           case Sequ(v1small, v2small) => Sequ(vunsimp(r1, v1)(v1small), vunsimp(r2, v2)(v2small))
   669           case Sequ(v1small, v2small) => Sequ(vunsimp(r1, v1)(v1small), vunsimp(r2, v2)(v2small))
   662           case _ => {
   670           case _ => {
   663             println(vsmall) ;
   671             println(vsmall) ;
       
   672             println(aregex_simple_print(r1))
       
   673             println(v1)
       
   674             println(aregex_simple_print( r2))
       
   675             println(v2)
   664             throw new Exception("bad arguments sequ")
   676             throw new Exception("bad arguments sequ")
   665           }
   677           }
   666           //(ASEQ(bs1, r1s, r2s),  Sequ(v1s, v2s))
   678           //(ASEQ(bs1, r1s, r2s),  Sequ(v1s, v2s))
   667         })
   679         })
   668     }
   680     }
   826   def unify(rs: List[ARexp], v: Val): Val = {
   838   def unify(rs: List[ARexp], v: Val): Val = {
   827     Position( pos_i(rs, v), pos_v(rs, v) )
   839     Position( pos_i(rs, v), pos_v(rs, v) )
   828   }
   840   }
   829   def deunify(rs_length: Int, v: Val): Val = v match{
   841   def deunify(rs_length: Int, v: Val): Val = v match{
   830     case Position(i, v0) => {
   842     case Position(i, v0) => {
   831       if(i <0) {println(rs_length, v); throw new Exception("deunify minus")} 
   843       if(i <0) {throw new Exception("deunify minus")} 
   832       else if(rs_length == 1) {println(v); v}
   844       else if(rs_length == 1) { v0}
   833       else if(rs_length - 1 == i) coat(v0, i) 
   845       else if(rs_length - 1 == i) {coat(v0, i)} 
   834       else coat(Left(v0), i)
   846       else {coat(Left(v0), i)}
   835       }
   847     }
   836     case _ => throw new Exception("deunify error")
   848     case _ => throw new Exception("deunify error")
   837   }
   849   }
   838   def flats2(front: List[ARexp], i: Int, rs: List[ARexp], v: Val): (List[ARexp], Val) = v match {
   850   def flats2(front: List[ARexp], i: Int, rs: List[ARexp], v: Val): (List[ARexp], Val) = v match {
   839     case Position(j, v0) => {
   851     case Position(j, v0) => {
   840       if (i < 0) (front ::: flats(rs), Position(j, v0) ) 
   852       if (i < 0) (front ::: flats(rs), Position(j, v0) ) 
   854         }
   866         }
   855       }
   867       }
   856     }
   868     }
   857     case _ => throw new Exception("flats2 error")
   869     case _ => throw new Exception("flats2 error")
   858   }
   870   }
   859   def distinctBy2[B, C](v: Val, xs: List[B], f: B => C, acc: List[C] = Nil, res: List[B] = Nil): (List[B], Val) = xs match {
   871   def distinctBy2[B, C](j: Int, v: Val, xs: List[B], f: B => C, acc: List[C] = Nil, res: List[B] = Nil): (List[B], Val) = xs match {
   860     case Nil => (res, v)
   872     case Nil => (res, v)
   861     case (x::xs) => {
   873     case (x::xs) => {
   862       val re = f(x)
   874       val re = f(x)
   863       if (acc.contains(re)) v match { 
   875       if (acc.contains(re)) v match { 
   864         case Position(i, v0) => distinctBy2(Position(i - 1, v0), xs, f, acc, res)  
   876         case Position(i, v0) => if(j > 0) distinctBy2(j - 1, Position(i - 1, v0), xs, f, acc, res)  
       
   877         else if(j < 0)distinctBy2(j - 1, Position(i, v0), xs, f, acc, res) else throw new Exception("Value not POSIX")
   865         case _ => throw new Exception("dB2")
   878         case _ => throw new Exception("dB2")
   866       }
   879       }
   867       else distinctBy2(v, xs, f, re::acc, x::res)
   880       else distinctBy2(j - 1, v, xs, f, re::acc, x::res)
   868     }
   881     }
   869   }
   882   }
   870    def bsimp2(r: ARexp, v: Val): (ARexp, Val) = (r,v) match{
   883    def bsimp2(r: ARexp, v: Val): (ARexp, Val) = (r,v) match{
   871     case (ASEQ(bs1, r1, r2), Sequ(v1, v2)) => (bsimp2(r1, v1), bsimp2(r2, v2)) match {
   884     case (ASEQ(bs1, r1, r2), Sequ(v1, v2)) => (bsimp2(r1, v1), bsimp2(r2, v2)) match {
   872         case ((AZERO, _), (_, _) )=> (AZERO, undefined)
   885         case ((AZERO, _), (_, _) )=> (AZERO, undefined)
   873         case ((_, _), (AZERO, _)) => (AZERO, undefined)
   886         case ((_, _), (AZERO, _)) => (AZERO, undefined)
   874         case ((AONE(bs2), v1s) , (r2s, v2s)) => (fuse(bs1 ++ bs2, r2s), v2s )//v2 tells how to retrieve bits in r2s, which is enough as the bits of the first part of the sequence has already been integrated to the top level of the second regx.
   887         case ((AONE(bs2), v1s) , (r2s, v2s)) => (fuse(bs1 ++ bs2, r2s), v2s )//v2 tells how to retrieve bits in r2s, which is enough as the bits of the first part of the sequence has already been integrated to the top level of the second regx.
   875         case ((r1s, v1s), (r2s, v2s)) => (ASEQ(bs1, r1s, r2s),  Sequ(v1s, v2s))
   888         case ((r1s, v1s), (r2s, v2s)) => (ASEQ(bs1, r1s, r2s),  Sequ(v1s, v2s))
   876     }
   889     }
   877     case (AALTS(bs1, rs), v) => {
   890     case (AALTS(bs1, rs), v) => {
       
   891       println("Entry into AALTS")
       
   892       rs.map(println(aregex_simple_print()))
   878       val vlist = unify(rs, v)
   893       val vlist = unify(rs, v)
   879       vlist match {
   894       vlist match {
   880         case Position(i, v0) => {
   895         case Position(i, v0) => {
   881           val v_simp = bsimp2(rs(i), v0)._2
   896           val v_simp = bsimp2(rs(i), v0)._2
       
   897           println("map on bsimp")
   882           val rs_simp = rs.map(bsimp)
   898           val rs_simp = rs.map(bsimp)
   883           val flat_res = flats2( Nil, i, rs_simp, Position(i, v_simp) )
   899           val flat_res = flats2( Nil, i, rs_simp, Position(i, v_simp) )
   884           val dist_res = distinctBy2(flat_res._2, flat_res._1, erase)
   900           println("list after flats2")
       
   901           flat_res._1.map(println(aregex_simple_print(_)))
       
   902           val dist_res = distinctBy2(i, flat_res._2, flat_res._1, erase)
       
   903           println("list after distinctBy2")
   885           val rs_new = dist_res._1
   904           val rs_new = dist_res._1
       
   905           rs_new.map(println(aregex_simple_print(_)))
   886           val v_new = deunify(rs_new.length, dist_res._2)
   906           val v_new = deunify(rs_new.length, dist_res._2)
   887           rs_new match {
   907           rs_new match {
   888             case Nil => (AZERO, undefined)
   908             case Nil => (AZERO, undefined)
   889             case s :: Nil => (fuse(bs1, s), v_new)
   909             case s :: Nil => (fuse(bs1, s), v_new)
   890             case rs => (AALTS(bs1, rs), v_new)
   910             case rs => (AALTS(bs1, rs), v_new)
  1281   }
  1301   }
  1282   */
  1302   */
  1283 
  1303 
  1284 
  1304 
  1285   //--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART (TESTING)
  1305   //--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART (TESTING)
  1286 
  1306   def bstostick(bs: List[Bit]): Element = bs match {
       
  1307     //case b::Nil => elem(b.toString)
       
  1308     case b::bs1 => elem(b.toString) beside bstostick(bs1)
       
  1309     case Nil => elem(' ', 1, 1)
       
  1310   }
       
  1311 
       
  1312   def aregex_simple_print(r: ARexp): Element = r match {
       
  1313     case AALTS(bs,rs) => {
       
  1314       val bitstick = bstostick(bs)
       
  1315       rs match {
       
  1316         case r::rs1 =>  
       
  1317         rs1.foldLeft( 
       
  1318         ((elem("(") left_align bitstick) beside 
       
  1319         aregex_simple_print(r))  )( (acc, r2) => acc beside ((elem(" + ") above elem("   ")) beside aregex_simple_print(r2) )) beside
       
  1320         elem(")")
       
  1321         case Nil => elem(' ', 1, 1)
       
  1322       }
       
  1323     }
       
  1324     case ASEQ(bs, r1, r2) => {
       
  1325       ((elem("[") left_align bstostick(bs)) beside  aregex_simple_print(r1) beside elem("~") beside aregex_simple_print(r2) beside (elem("]") above elem(" "))) 
       
  1326     }
       
  1327     case ASTAR(bs, r) => {
       
  1328       r match {
       
  1329         case AONE(_) | AZERO | ACHAR(_, _) => {
       
  1330           (elem("{") left_align bstostick(bs)) beside (aregex_simple_print(r) beside elem("}*")) 
       
  1331         }
       
  1332         case _ => {
       
  1333           (elem("{") left_align bstostick(bs)) beside (aregex_simple_print(r) beside elem("}*")) 
       
  1334         }
       
  1335       }
       
  1336     }
       
  1337     case AONE(bs) => {
       
  1338       elem("1") left_align bstostick(bs)
       
  1339     }
       
  1340     case ACHAR(bs, c) => {
       
  1341       elem(c, 1, 1) left_align bstostick(bs)
       
  1342     }
       
  1343     case AZERO =>
       
  1344     {
       
  1345       elem ("0") above elem(" ")
       
  1346     }
       
  1347   }
  1287 
  1348 
  1288 
  1349 
  1289   def clear() = {
  1350   def clear() = {
  1290     print("")
  1351     print("")
  1291     //print("\33[H\33[2J")
  1352     //print("\33[H\33[2J")
  1342 
  1403 
  1343   //single_expression_explorer(internalise(("c"~("a"+"b"))%) , Set('a','b','c'))
  1404   //single_expression_explorer(internalise(("c"~("a"+"b"))%) , Set('a','b','c'))
  1344 
  1405 
  1345 
  1406 
  1346 }
  1407 }
       
  1408 
       
  1409 
  1347 
  1410 
  1348 import Rexp.Bits
  1411 import Rexp.Bits
  1349 abstract class ARexp 
  1412 abstract class ARexp 
  1350 case object AZERO extends ARexp
  1413 case object AZERO extends ARexp
  1351 case class AONE(bs: Bits) extends ARexp
  1414 case class AONE(bs: Bits) extends ARexp
  1366 case class Rec(x: String, v: Val) extends Val
  1429 case class Rec(x: String, v: Val) extends Val
  1367 case class Position(i: Int, v: Val) extends Val
  1430 case class Position(i: Int, v: Val) extends Val
  1368 case object undefined extends Val
  1431 case object undefined extends Val
  1369 //case class Pos(i: Int, v: Val) extends Val
  1432 //case class Pos(i: Int, v: Val) extends Val
  1370 case object Prd extends Val
  1433 case object Prd extends Val
       
  1434 }