lex_blex_Frankensteined.scala
changeset 150 b51d34113d47
parent 148 c8ef391dd6f7
child 151 73f990bc6843
equal deleted inserted replaced
149:6c5920fd02a7 150:b51d34113d47
   639   }
   639   }
   640   //coat does the job of "coating" a value
   640   //coat does the job of "coating" a value
   641   //given the number of right outside it
   641   //given the number of right outside it
   642   def coat(v: Val, i: Int) : Val = i match {
   642   def coat(v: Val, i: Int) : Val = i match {
   643     case 0 => v
   643     case 0 => v
   644     case i => coat(Right(v), i - 1)
   644     case i => if(i > 0) coat(Right(v), i - 1) else {println(v,i); throw new Exception("coat minus")}
   645   }
   645   }
   646   def decoat(v:Val, i: Int) : Val = i match {
   646   def decoat(v:Val, i: Int) : Val = i match {
   647     case 0 => v
   647     case 0 => v
   648     case i => v match {
   648     case i => v match {
   649       case Right(v0) => decoat(v0, i - 1)
   649       case Right(v0) => decoat(v0, i - 1)
   750     }
   750     }
   751     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
   751     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
   752     case (r, v) => (v => v)
   752     case (r, v) => (v => v)
   753   }
   753   }
   754   //This version takes a regex and a value, return a simplified regex and its corresponding simplified value 
   754   //This version takes a regex and a value, return a simplified regex and its corresponding simplified value 
   755   def bsimp2(r: ARexp, v: Val): (ARexp, Val) = (r,v) match{
   755   //def flats2(r: Arexp, v: val): (ARexp, Val) = (r, v) match{
       
   756   //  case (AALTS(bs, rs), Right(v0)) => rs match {
       
   757   //    case Nil => (AALTS(bs, Nil), v)
       
   758   //    case AZERO::rs0 =>  flats2(AALTS(bs, rs0), v0) 
       
   759   //    case 
       
   760   //  case (AALTS(bs, rs), Left(v0)) => 
       
   761   //}
       
   762   //liangren qixinxieli zuo yixie shiqing 
       
   763   //shi xinzhaobuxuan de moqi
       
   764   //ni yao fanzhangben 
       
   765   //na wo jiu zhineng peini fanzhangben 
       
   766   //ni xianshuo wo weini zuole haoduohaoduo
       
   767   //na wo jiu haohao gen ni fenxifenxi
       
   768   //ni daodi zuole shenme 
       
   769   //ni meizuo shenme 
       
   770   //zhishi yinwei ni lei 
       
   771   //suoyi ni juede ni zuole haoduo 
       
   772   //zuofanyeshi 
       
   773   //shi weile ni shiwu cai zuo name jingxi
       
   774   //shiwo de hua meitian sancan huadeshijian 
       
   775   //buchaoguo liangxiaoshi
       
   776   //ni zuode henduoshihou doushi weile ziji
       
   777   //ni zuoshiqing de shihou ye zongshi bawo jiaolai bangmang
       
   778   //ni de xiaolv hai hendi 
       
   779   //zonghe kanlai
       
   780   //ni buhuizhaoguziji meiyoudandang nenglibuzu haizisi
       
   781   //suoyi wo zui taoyan gennishuozhexie 
       
   782   //shuoli bianbuguolai 
       
   783   //jiu ba ren xiaoshihoude shiqing nachulaishuo, lai exin ren
       
   784   //nibangwo daoyibeishui, wo buleyujieshou, wo bushuoxiexie 
       
   785   //nandao woshuo sheiyaonidaole, zoukai? buyaoni geiwo dao? wo ziji dao?
       
   786   //nishuo qiantou doushi ni zaizuo 
       
   787   //zheliwoyao buchong
       
   788   //quancheng ni yigerenzuo yidunfan de cishu
       
   789   //dagai you duoshaoci?
       
   790   //kending you jici, dan bushi duoshu
       
   791   //duoshu shi wo huilai yihou 
       
   792   //ni rangwo lai chaocai huozhe lai jieguan zuowan shengyude gongzuo 
       
   793   //ni zuo de zhexieshihou 
       
   794   //yiban jiushi lianggecai 
       
   795   //huozhe yicai yitang
       
   796   //wo hui renwei 
       
   797   //zhe shi ni shenti keyi shiying de gongzuoliang
       
   798   //moren ni ziji hui zhaoguhao ziji 
       
   799   //ruguo ni leile, yehui lai zhaowo bangmang
       
   800   //suoyi wo moren ni zai zhe qijian shi meiyou
       
   801   //guolao, ruguo you, wo renwei 
       
   802   //1 ni meiyou zhaoguhao niziji
       
   803   //2 nimeiyou jishi xiangwo xunqiu bangzhu
       
   804   //3 ni jibensangshi laodongnengli le,
       
   805   //yihou huolu doushi wo lai bao le jiu wanle
       
   806   //niziji meinengli jiu buyao dui zuoshi de ren 
       
   807   //tiaosanjiansi
       
   808   //ni shibushi taomi xigecai jiu hui lei
       
   809   //na shuoming
       
   810   //yaome ni tili taicha--jibensangshi laodongli
       
   811   //yaome ni fangfabuheshi(xiaolvdi, zishiyouwenti daozhi shentilaosun)--ni yinggai tiaozheng ziji zuoshide fangfa, women yiqilai zhaozhao yuanyin
       
   812   //cuowu de silushi: nikanwo dou zhemelei le,
       
   813   //shuoming wo zuole henduo, ni bu tiliangwo
       
   814   //ni zuo zhexie doushi yinggaide
       
   815   //yinwei wo shi nimama erqie wo leile
       
   816   def pos_i(rs: List[ARexp], v: Val): Int = (rs, v) match {
       
   817     case (r::Nil,         v1) => 0
       
   818     case ( r::rs1, Right(v) ) => pos_i(rs1, v) + 1
       
   819     case ( r::rs1, Left(v)  ) => 0
       
   820   }
       
   821   def pos_v(rs: List[ARexp], v: Val): Val = (rs, v) match {
       
   822     case (r::Nil,         v1) => v1
       
   823     case (r::rs1, Right(v)  ) => pos_v(rs1, v)
       
   824     case (r::rs1, Left(v)   ) => v
       
   825   }
       
   826   def unify(rs: List[ARexp], v: Val): Val = {
       
   827     Position( pos_i(rs, v), pos_v(rs, v) )
       
   828   }
       
   829   def deunify(rs_length: Int, v: Val): Val = v match{
       
   830     case Position(i, v0) => {
       
   831       if(i <0) {println(rs_length, v); throw new Exception("deunify minus")} 
       
   832       else if(rs_length == 1) {println(v); v}
       
   833       else if(rs_length - 1 == i) coat(v0, i) 
       
   834       else coat(Left(v0), i)
       
   835       }
       
   836     case _ => throw new Exception("deunify error")
       
   837   }
       
   838   def flats2(front: List[ARexp], i: Int, rs: List[ARexp], v: Val): (List[ARexp], Val) = v match {
       
   839     case Position(j, v0) => {
       
   840       if (i < 0) (front ::: flats(rs), Position(j, v0) ) 
       
   841       else if(i == 0){
       
   842         rs match {
       
   843           case AALTS(bs, rs1) :: rs2 => ( (front ::: rs1.map(fuse(bs, _))):::flats(rs2), Position(j + rs1.length - 1, pos_v(rs1, v0)))
       
   844           case r::rs2 => (front ::: List(r) ::: flats(rs2), Position(j, v0))
       
   845           case _ => throw new Exception("flats2 i = 0")
       
   846         }
       
   847       }
       
   848       else{
       
   849         rs match {
       
   850           case AZERO::rs1 => flats2(front, i - 1, rs1, Position(j - 1, v0))
       
   851           case AALTS(bs, rs1) ::rs2 => flats2(front:::rs1.map(fuse(bs, _)), i - 1, rs2, Position(j + rs1.length - 1, v0))
       
   852           case r::rs1 => flats2(front:::List(r), i - 1, rs1, Position(j, v0)) 
       
   853           case _ => throw new Exception("flats2 i>0")
       
   854         }
       
   855       }
       
   856     }
       
   857     case _ => throw new Exception("flats2 error")
       
   858   }
       
   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 {
       
   860     case Nil => (res, v)
       
   861     case (x::xs) => {
       
   862       val re = f(x)
       
   863       if (acc.contains(re)) v match { 
       
   864         case Position(i, v0) => distinctBy2(Position(i - 1, v0), xs, f, acc, res)  
       
   865         case _ => throw new Exception("dB2")
       
   866       }
       
   867       else distinctBy2(v, xs, f, re::acc, x::res)
       
   868     }
       
   869   }
       
   870    def bsimp2(r: ARexp, v: Val): (ARexp, Val) = (r,v) match{
   756     case (ASEQ(bs1, r1, r2), Sequ(v1, v2)) => (bsimp2(r1, v1), bsimp2(r2, v2)) match {
   871     case (ASEQ(bs1, r1, r2), Sequ(v1, v2)) => (bsimp2(r1, v1), bsimp2(r2, v2)) match {
   757         case ((AZERO, _), (_, _) )=> (AZERO, undefined)
   872         case ((AZERO, _), (_, _) )=> (AZERO, undefined)
   758         case ((_, _), (AZERO, _)) => (AZERO, undefined)
   873         case ((_, _), (AZERO, _)) => (AZERO, undefined)
   759         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.
   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.
   760         case ((r1s, v1s), (r2s, v2s)) => (ASEQ(bs1, r1s, r2s),  Sequ(v1s, v2s))
   875         case ((r1s, v1s), (r2s, v2s)) => (ASEQ(bs1, r1s, r2s),  Sequ(v1s, v2s))
   761     }
   876     }
   762     case (AALTS(bs1, rs), v) => {
   877     case (AALTS(bs1, rs), v) => {
       
   878       val vlist = unify(rs, v)
       
   879       vlist match {
       
   880         case Position(i, v0) => {
       
   881           val v_simp = bsimp2(rs(i), v0)._2
       
   882           val rs_simp = rs.map(bsimp)
       
   883           val flat_res = flats2( Nil, i, rs_simp, Position(i, v_simp) )
       
   884           val dist_res = distinctBy2(flat_res._2, flat_res._1, erase)
       
   885           val rs_new = dist_res._1
       
   886           val v_new = deunify(rs_new.length, dist_res._2)
       
   887           rs_new match {
       
   888             case Nil => (AZERO, undefined)
       
   889             case s :: Nil => (fuse(bs1, s), v_new)
       
   890             case rs => (AALTS(bs1, rs), v_new)
       
   891           }
       
   892         }
       
   893         case _ => throw new Exception("Funny vlist bsimp2")
       
   894       }
       
   895     }
       
   896     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
       
   897     case (r, v) => (r, v)  
       
   898   }
       
   899   /*def bsimp2(r: ARexp, v: Val): (ARexp, Val) = (r,v) match{
       
   900     case (ASEQ(bs1, r1, r2), Sequ(v1, v2)) => (bsimp2(r1, v1), bsimp2(r2, v2)) match {
       
   901         case ((AZERO, _), (_, _) )=> (AZERO, undefined)
       
   902         case ((_, _), (AZERO, _)) => (AZERO, undefined)
       
   903         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.
       
   904         case ((r1s, v1s), (r2s, v2s)) => (ASEQ(bs1, r1s, r2s),  Sequ(v1s, v2s))
       
   905     }
       
   906     case (AALTS(bs1, rs), v) => {
   763       val init_ind = find_pos(v, rs)
   907       val init_ind = find_pos(v, rs)
   764       val vpointr = bsimp2(rs(init_ind), remove(v, rs))//remove all the outer layers of left and right in v to  match the regx rs[i]
   908       val vpointr = bsimp2(rs(init_ind), remove(v, rs))//remove all the outer layers of left and right in v to  match the regx rs[i]
   765       val target_sv = vpointr._2
   909       val target_sv = vpointr._2
   766       val target_sr = vpointr._1
   910       val target_sr = vpointr._1
   767 
   911 
   792         case rs => (AALTS(bs1, rs), vdb)
   936         case rs => (AALTS(bs1, rs), vdb)
   793       }
   937       }
   794     }
   938     }
   795     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
   939     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
   796     case (r, v) => (r, v)  
   940     case (r, v) => (r, v)  
   797   }
   941   }*/
   798   //the below are all residuals from the bsimp2 function 
   942   //the below are all residuals from the bsimp2 function 
   799         //val vs_for_coating = if(isend(vs._2, rs_simp, init_ind)||flat_res.length == 1) vs_kernel else Left(vs_kernel)
   943         //val vs_for_coating = if(isend(vs._2, rs_simp, init_ind)||flat_res.length == 1) vs_kernel else Left(vs_kernel)
   800         //val vf = coat(vs_for_coating, new_ind)
   944         //val vf = coat(vs_for_coating, new_ind)
   801       //flats2 returns a list of regex and a single v
   945       //flats2 returns a list of regex and a single v
   802       //now |- vf: ALTS(bs1, flat_res)
   946       //now |- vf: ALTS(bs1, flat_res)
   855       }
   999       }
   856     }
  1000     }
   857     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
  1001     //case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
   858     case r => r
  1002     case r => r
   859   }
  1003   }
   860 
  1004 //nice website http://www.nhc.gov.cn/xcs/kpzs/202002/d228d85eaa19412eaaa4eee740f03101.shtml
       
  1005 //orientalism
   861 
  1006 
   862   def simp_weakened(r: Rexp): Rexp = r match {
  1007   def simp_weakened(r: Rexp): Rexp = r match {
   863     case SEQ(r1, r2) => (simp_weakened(r1), r2) match {
  1008     case SEQ(r1, r2) => (simp_weakened(r1), r2) match {
   864         case (ZERO, _) => ZERO
  1009         case (ZERO, _) => ZERO
   865         case (_, ZERO) => ZERO
  1010         case (_, ZERO) => ZERO
  1217 case class Sequ(v1: Val, v2: Val) extends Val
  1362 case class Sequ(v1: Val, v2: Val) extends Val
  1218 case class Left(v: Val) extends Val
  1363 case class Left(v: Val) extends Val
  1219 case class Right(v: Val) extends Val
  1364 case class Right(v: Val) extends Val
  1220 case class Stars(vs: List[Val]) extends Val
  1365 case class Stars(vs: List[Val]) extends Val
  1221 case class Rec(x: String, v: Val) extends Val
  1366 case class Rec(x: String, v: Val) extends Val
       
  1367 case class Position(i: Int, v: Val) extends Val
  1222 case object undefined extends Val
  1368 case object undefined extends Val
  1223 //case class Pos(i: Int, v: Val) extends Val
  1369 //case class Pos(i: Int, v: Val) extends Val
  1224 case object Prd extends Val
  1370 case object Prd extends Val