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") |