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 |