Skip to content

Commit

Permalink
* Perl: introduce Byte Pair Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Nov 2, 2015
1 parent 6232fd8 commit 4171493
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 38 deletions.
70 changes: 35 additions & 35 deletions QR.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@
ut~L#$T;`nldc~"#{e[%( class~QR{#$L~void~ma!(SJ[]v){SJ~c[]=new~SJ[99999] ,y="",z=y,s=" #{z=t=(0..r=q=126).map{|n|[n,[]]};a="";b=->n{a<<(n%78+ 55)%84+42};
("s=#{E["Z"+Q[E[%( #$D;import~Regex;Void~mX{b="`x60";t="Section`x48ead er+name:=QR; SectionPublic-ma!<-(^n";for~i~![0..#{s=%(@s=global[#{i=(s= %(PR~"`x48
AI^~1.2~PR~"VIS IBLE^~"#{Q["Z"+E["all:`n`t@echo~'#{d["l!el:99999;Z#{E[%( .assembl y~t{}.method~#$L~void~MX{.entrypo!t~ldstr"#{e["m:db`x60#{e[s= "$Z#{E["Z
f#{E["#import" +M["Z_sJ"+E["s=toascii#{E["Z#{E[%(say"#{e["#$D(output);beg!~ H(#{ f((p="eval";%($_="#{s=%(<?php~$f=function($n,$s="Y"){return~str_r epeat($s
,$n);};$z=3+ $w=strlen($s=#{V[Q[E["!t~mX{H#{E["(#{?_*11})dup~=/s(|~~~~~.~~~| )d ef(#{Q["qr:-H('#{Q[e[%(for~c~!"".jo!(["say~'%s'^n"%l~for~l~!#{E[d["eval$s=%q(#$ s)",?']]
}.split("^ n")]):Z('cat("r=fput(char(%d))^n")'%ord(c))`nZ('cat("end^n")'))],?' ]}')." ,B]}){9~7{exch~dup~1~and~79~mul~32~add~exch~2~idiv~3~1~roll~s~e xch~2~!
dex~exch~ put~1~sub~dup~6~eq{1~sub}if}repeat~s~=~pop~pop}forall~=~quit"]+R}}"]], "{$f(",")}"].K(/~{10,}/){"{$f(#{$&.size},'~')}"}})* 3;echo
"^x89PNG ^r^n^x1a^n";$m="";$t="^xc0^0^xff";for($i=-1;$i<128*$z;$m.=$c--?($w-$c||$ i>$z)&&$i/$z<($c<$w?ord($s[(!t)($c/3)]):$c--%3 +2)?$t
[2].$t[ $c%3%2].$t[$c%3]:"^0^0^0":"^0")$c=++$i%$z;foreach(array("I`x48DR".pack("NNC V",$w+2,128,8,2),"IDAT".gzcompress($m),"I END")a
s$d)ech o~pack("NA*N",strlen($d)-4,$d,crc32($d));?>);(s+N*(-s.size%6)).unpack("B*")[ 0].K(/.{6}/){n=$&.to_i~2;((n+14)/26*6 +n+47)
.chr}} ";s|.|$n=ord$&;substr~unpack(B8,chr$n-!t($n/32)*6-41),2|eg;Z~pack"B*",$_).scan( /[~,-:A-z]+|(.)/){p="s++#{$1?"chr~ #{$1.
ord}+e ":$&+?+};"+p};p),1){"'#$s',"}}'')end."]}"`nend)]};quit"]};t=num2cell(b=11-ceil(s /13));for~n=1:9m={};for~i=1:141 f=@(x
,y,n) repmat(['Ook'~x~'~Ook'~y~32],[1~abs(n)]);m(i)=[f(z=46,63,n)~f(q=z-(i<13)*13,q,i-13 )~f(33,z,1)~f(63,z,n)];end; t(x)=
m(dif f([0~s(x=b==n)])+13);end;Zf('%%s',t{:})"]]]}"+N]};"+N]}`x60`nglobal~_start`n_start: mov~edx,#{s.size}`nmov~e cx,m`n
mov~e bx,1`nmov~eax,4`n!t~128`nmov~ebx,0`nmov~eax,1`n!t~128"]}"call~void~[mscorlib]#{C*" : :"}(sJ)ret})]};quit() ;",?$]
.K(?' ){"'^''"}}'"].K(/[:"]/,":^0"),/[~Y`t;"(){}`[`]]/]}"~PR~"`x4bT`x48`x58B`x59E~B`x5 9E )).size+1} x~i8 ]c"#{s.K(/[^"]/){" ^%`x58
"%$&. ord}}^00"declare~i32@puts(i8*)def!e~i32@mX{%1=call~i32@puts(i8*getelementptr([# { i}x~i8]*@s,i3 2~0,i32~0)) ret~i32~0});s.s ize/99}
]{s=su bstr(#{E[s]},i*99,99);g=[Global()];replace("YY","YY",s,g);replace("^"","Y^"", s ,g);t+="^""+s+ "^".Z;^n";}put Str(" k"+b);for~c~ !~array
(t+"); "){d=rep(b+b+"s",8)+"i";for~j~![0..6]d+=b+substr("kki",c>>(6-j)&1,2);putSt r( d);}for~c~!~array("AG-`x48-`x48 Fy.IlD==;=jd lAy=;=jld ltldltl{
lAulAy= jtlldlAyFy=?=jdlAyGFyFyG2AFy>zlAFFBCjldGyGFy>GFy.AGy=G==n`x48==nlldC=j@= jtll dltldlAut11"){for~i~![0..2]putSt r(substr("ski"+b ,c%83- 10>>i*2&3
,1));}}) ]]]};typeof~Z=='function'?Z(s):console.log('%s',s)").bytes{|n|r,z=z[n ]||(b[r/ 78];b[r];q<6083&&z[n]=[q+=1,[]]; t[n])};b[r/78];b[r] }"; !t~i=0,n=0
,q=0;for (;++n<126;)c[n]=""+(char)n;for(;i<#{a.size};){q=q*78+(s.charAt(i)- 13)%84;if( i++%2>0){y=q<n?c[q]:y;c[n++]=z+y.charAt(0);System.ou t .Z(z=c[q]);
q=0;}}}}) ]}"`n!vokevirtual~#$T/Zln(L#{S}J;)V`nreturn`n.end~method)+N]};H ("DO,1<-#"||*s );s?while~t:=ord(move(1))do{i+:=1;u:=-i;every~0to ~7do{u:=u*2+
t%2;t/:=2} ;H("PLEASE")^(i%4/3);H("DO,1SUB#"||i||"<-#"||((c-u)%256));c: =u;};H("PLEASEREAD OUT,1^nPLEASEGIVEUP");end)]};}}"].tr(B,?&),51 ){%(show~"Z'#{
e[$s].K~B+?" ,%("~"^q"~")}'.tr('&','YY');"`n)}]]}^`");fim")})],62){" Zn#$S`n"}]});CloseStr eam(s);QUIT;"]}")]}"~DUP~FOR~S"~&A,&"~C ~NE`x58T~S^"~&A
)^",&"~C~0~DO ~B~."~&char("~COUNT~.~."~),&'"~CR~LOOP~S^"~&^"^""~C ~S"~end~#$D"~C~A~."~STOP "~CR~A~."~END"~CR~B`x59E~;~D~). K(?"){'"34,"'}+?"
]],?%]]}."]}")) ,15){"Pr!t#$S;"}}}}"}]pq"]};}"]}"))],'#{f(',' )}']})"]}"))["~~~~^"~^".""ST OP~RUN."])](Zln(st r"#{g*8}"s)))).reve
rse]}))]};}}"]};} "]}/****//****/";t={};b="";L="";n=i=0;D =->n{L<<(n+62)%92+35;D};s.bytes{|c |n>0?n-=1:(t[c]=(t[c]
||[]).reject{|j|j<i -3560};x=[];t[c].map{|j|k=(0.. 90).f!d{|k|not~s[i+1+k]==s[j+k]}||91;k >4&&x<<[k,j]};x=x.max)?
(n,j=x;x=b.size;(u=[x ,3999].m!;D[u%8 7][u/87];L<<b[0,u];b[0,u]="";x-=u)while~x>0; x=4001+i-j;D[x%87][x/87][n
-5]):b<<c;t[c]+=[i+=1]}; "d=#{Q[E[L]]};s='';while~0<len(d):`n~x~as~!t,y~as~ !t=d;i=3;if(n=(x-5)%92+(y-5)%
92*87)>3999:`n~~for~_~!~ran ge(((d[2]cast~!t-5)%92+6)):s+=s[len(s)+4000-n]`n~else:s+= d[2:i=n+2]`n~d=d[i:]`na=0;for~i~!
~range(len(s)):b~as~!t=s[i];a-=b ;Z(('+'*-a~if~0>a~else'-'*a)+'.');a=b").tr~B,?`x21]};K(/`x21/,"Y" ,s);for(Z"def!e~void~f(n){^"00g,^";fo
r(m=128;m;m/=2){^"00g,4,:^";if(n/m%2<1 )^"4+^";^",^";};^"4,:,^"}^"389**6+44*6+00p29*,^";";++j<=length(s);Z"f("n");")fo r(n=9;substr(s,j,1)`x21=sZf("%c",++n););Z"^"
4,:,@^"^nquit"})]],?']}');"]}";FORiTO`~UPBtDO`~INTn:=ABSt[i];Z(~(50+n%64)+c+~(50+n%8MOD8)+c+~(50+nMOD8)+b+"`x4a"+a)OD]*"REPR"]}")]}");end;)]+"`nsys.exit~0",/[^{}]/]}}",35){y<<",`n"+$S;"%s"}}")+y}]]></#{U}></xsl:
#{I}>"].K~N,'"&~VbLf~&"'}":s="~~~":For~i=0To~7:s~&=Chr(32-(Asc(c)>>7-i~And~1)*23):Next:#$C(s~&n~&Chr(9)&n~&"~~"):Next:#$C(n~&n~&n):End~Sub:End~Module),3){%($H("%s",#$S);)+N}}end~endmodule)],?%]+R}}".reverse],/[`
[`]$]/],"[f~",?]]}"~^x60.&]k^n::=^na)]};Z"0~0~-1";)]]};",?']}';cr"]]]}"|sed~-E~-e~'s/([`x5eY]|Y.){1,120}/Zf("%s","^0");^n/g'],7){%(Zf("%s","#{d[d[$s],?']}")`n)}}quit)]}")),196){%(Z#$S;)}}}").gsub(/[!HJKXYZ^`~]/)
{[B*2,:write,B,:tring,:gsub,"ain()",B*4,:print,g,:in][$&.ord%47%12]})))*"")#_buffer_for_future_bug_fixes_#_buffer_for_future_bug_fixes_#_buffer_for_future_bug_fixes_#_buffer_for_future_bug_fixes_#_buffer_for_fut
f#{E["#import" +M["Z_sJ"+E["s=toascii#{E["Z#{E[%(say"#{e["#$D(output);beg!~ H(#{ f((p="eval";%($_="#{s=%(<?php~$z=3+$w=strlen($s=#{Q[E["!t~mX{H#{E ["(#{?_*
11})dup~=/s( |~~~~~.~~~|)def(#{Q["qr:-H('#{Q[e[%(for~c~!"".jo!(["say~'%s'^n" %l ~for~l~!#{E[d["eval$s=%q(#$s)",?']]}.split("^n")]):Z('cat("r=fput(char(%d))^n") '%ord(c)
)`nZ('cat( "end^n")'))],?']}').",B]}){9~7{exch~dup~1~and~79~mul~32~add~exch~2~ idiv~3 ~1~roll~s~exch~2~!dex~exch~put~1~sub~dup~6~eq{1~sub}if}repeat~s ~=~pop~
pop}foral l~=~quit"]+R}}"]]})*3;echo"^x89PNG^r^n^x1a^n";$m="";$t="^xc0^0^xff";fo r($i=-1;$i<128*$z;$m.=$c--?($w-$c||$i>$z)&&$i/$z<($ c<$w?o
rd($s[(! t)($c/3)]):$c--%3+2)?$t[2].$t[$c%3%2].$t[$c%3]:"^0^0^0":"^0")$c=++$i%$z; foreach(array("I`x48DR".pack("NNCV",$w+2,128,8 ,2),"I
DAT".gz compress($m),"IEND")as$d)echo~pack("NA*N",strlen($d)-4,$d,crc32($d));?>);v= "";127.upto(287){|j|o={};m=n=0;s.size.tim es{|i|
o[f=s[i ,2]]||=0;c=o[f]+=1;m<c&&(m=c;n=f)};v=n+v;s=s.K(n,(j%256).chr)};s="$_='#{Q[s, c=/['Y]/]}';$n=32;$s='#{Q[v,c]}';$s=` x7es{.
.}{$a= $&;$b=chr(--$n&255);`x7es/$b/$a/g;}eg;Z";(s+N*(-s.size%6)).unpack("B*")[0].K(/. {6}/){n=$&.to_i~2;((n+14)/26*6+n+4 7).ch
r}}";s |.|$n=ord$&;substr~unpack(B8,chr$n-!t($n/32)*6-41),2|eg;eval~pack'B*',$_).scan(/ [~,-:A-z]+|(.)/){p="s++#{$1?"ch r~#{$
1.ord }+e":$&+?+};"+p};p),1){"'#$s',"}}'')end."]}"`nend)]};quit"]};t=num2cell(b=11-ceil( s/13));for~n=1:9m={};for~i= 1:141
f=@(x ,y,n)repmat(['Ook'~x~'~Ook'~y~32],[1~abs(n)]);m(i)=[f(z=46,63,n)~f(q=z-(i<13)*13,q, i-13)~f(33,z,1)~f(63,z,n )];end
;t(x) =m(diff([0~s(x=b==n)])+13);end;Zf('%%s',t{:})"]]]}"+N]};"+N]}`x60`nglobal~_start`n _ start:mov~edx,#{s.siz e}`nmo
v~ecx ,m`nmov~ebx,1`nmov~eax,4`n!t~128`nmov~ebx,0`nmov~eax,1`n!t~128"]}"call~void~[msc or lib]#{C*": :"}( sJ)ret})]};quit(); ",?$].
K(?') {"'^''"}}'"].K(/[:"]/,":^0"),/[~Y`t;"(){}`[`]]/]}"~PR~"`x4bT`x48`x58B`x59E~B`x5 9 E)).size+1}x~ i8]c"#{s.K( /[^"]/){"^%`x58 "%$&.or
d}}^00 "declare~i32@puts(i8*)def!e~i32@mX{%1=call~i32@puts(i8*getelementptr([#{i}x~i 8 ]*@s,i32~0,i32 ~0))ret~i32~0} );s.s ize/99}]{s=s ubstr(#
{E[s]} ,i*99,99);g=[Global()];replace("YY","YY",s,g);replace("^"","Y^"",s,g);t+=" ^" "+s+"^".Z;^n";}putStr("k"+b);fo r~c~!~array( t+");"){d =rep(b+b
+"s",8) +"i";for~j~![0..6]d+=b+substr("kki",c>>(6-j)&1,2);putStr(d);}for~c~!~arr ay(" AG-`x48-`x48Fy.IlD==;=jdlAy=;=jl dltldltl{lAulAy= jtlldl AyFy=?=jd
lAyGFyFy G2AFy>zlAFFBCjldGyGFy>GFy.AGy=G==n`x48==nlldC=j@=jtlldltldlAut11"){fo r~i~![0. .2]putStr(substr("ski"+b,c%83-10 >>i*2&3,1));}})]]]} ;ty peof~Z=='f
unction' ?Z(s):console.log('%s',s)").bytes{|n|r,z=z[n]||(b[r/78];b[r];q<608 3&&z[n]=[q +=1,[]];t[n])};b[r/78];b[r]}";!t~i=0,n=0,q=0;for(;++ n <126;)c[n]=
""+(char) n;for(;i<#{a.size};){q=q*78+(s.charAt(i)-13)%84;if(i++%2>0){y=q <n?c[q]:y;c[n+ +]=z+y.charAt(0);System.out.Z(z=c[q]);q=0;}}}})]} "`n!vokevirt
ual~#$T/Zl n(L#{S}J;)V`nreturn`n.end~method)+N]};H("DO,1<-#"||*s);s?whi le~t:=ord(move(1)) do{i+:=1;u:=-i;every~0to~7do{u:=u*2+t%2;t/:=2 };H("PLEASE")^
(i%4/3);H("D O,1SUB#"||i||"<-#"||((c-u)%256));c:=u;};H("PLEASEREADOU T,1^nPLEASEGIVEUP");e nd)]};}}"].tr(B,?&),51){%(show~"Z'#{e[$ s].K~B+?",%("~"
^q"~")}'.tr(' &','YY');"`n)}]]}^`");fim")})],62){"Zn#$S`n"}]});Cl oseStream(s);QUIT;"]}")] }"~DUP~FOR~S"~&A,&"~C~NE`x58T~S ^"~&A)^",&"~C~0~D
O~B~."~&char("~ COUNT~.~."~),&'"~CR~LOOP~S^"~&^"^""~C~S"~end~ #$D"~C~A~."~STOP"~CR~A~."~EN D"~CR~B`x59E~;~D~) .K(?"){'"34,"'}+?"]
],?%]]}."]}")),15 ){"Pr!t#$S;"}}}}"}]pq"]};}"]}"))],'#{f( ',')}']})"]}"))["~~~~^"~^".""STOP~ RUN."])](Zln(str"#{g*
8}"s)))).reverse]}) )]};}}"]};}"]}/****//****/";t= {};b="";L="";n=i=0;D=->n{L<<(n+62)%92+ 35;D};s.bytes{|c|n>0?n-
=1:(t[c]=(t[c]||[]).r eject{|j|j<i-35 60};x=[];t[c].map{|j|k=(0..90).f!d{|k|not~s[ i+1+k]==s[j+k]}||91;k>4&&x
<<[k,j]};x=x.max)?(n,j=x ;x=b.size;(u=[x,3999].m!;D[u%87][u/87];L<<b[0,u];b [0,u]="";x-=u)while~x>0;x=400
1+i-j;D[x%87][x/87][n-5]):b <<c;t[c]+=[i+=1]};"d=#{Q[E[L]]};s='';while~0<len(d):`n~x~ as~!t,y~as~!t=d;i=3;if(n=(x-5)%92
+(y-5)%92*87)>3999:`n~~for~_~!~r ange(((d[2]cast~!t-5)%92+6)):s+=s[len(s)+4000-n]`n~else:s+=d[2:i= n+2]`n~d=d[i:]`na=0;for~i~!~range(len
(s)):b~as~!t=s[i];a-=b;Z(('+'*-a~if~0> a~else'-'*a)+'.');a=b").tr~B,?`x21]};K(/`x21/,"Y",s);for(Z"def!e~void~f(n){^"00 g,^";for(m=128;m;m/=2){^"00g,4,:^";if(n/m%2<
1)^"4+^";^",^";};^"4,:,^"}^"389**6+44*6+00p29*,^";";++j<=length(s);Z"f("n");")for(n=9;substr(s,j,1)`x21=sZf("%c",++n););Z"^"4,:,@^"^nquit"})]],?']}');"]}";FORiTO`~UPBtDO`~INTn:=ABSt[i];Z(~(50+n%64)+c+~(50+n%8MOD
8)+c+~(50+nMOD8)+b+"`x4a"+a)OD]*"REPR"]}")]}");end;)]+"`nsys.exit~0",/[^{}]/]}}",35){y<<",`n"+$S;"%s"}}")+y}]]></#{U}></xsl:#{I}>"].K~N,'"&~VbLf~&"'}":s="~~~":For~i=0To~7:s~&=Chr(32-(Asc(c)>>7-i~And~1)*23):Next:
#$C(s~&n~&Chr(9)&n~&"~~"):Next:#$C(n~&n~&n):End~Sub:End~Module),3){%($H("%s",#$S);)+N}}end~endmodule)],?%]+R}}".reverse],/[`[`]$]/],"[f~",?]]}"~^x60.&]k^n::=^na)]};Z"0~0~-1";)]]};",?']}';cr"]]]}"|sed~-E~-e~'s/([
`x5eY]|Y.){1,120}/Zf("%s","^0");^n/g'],7){%(Zf("%s","#{d[d[$s],?']}")`n)}}quit)]}")),196){%(Z#$S;)}}}").gsub(/[!HJKXYZ^`~]/){[B*2,:write,B,:tring,:gsub,"ain()",B*4,:print,g,:in][$&.ord%47%12]})))*"")#_buffer_for
################################################################## Quine Relay -- Copyright (c) 2013, 2014 Yusuke Endoh (@mametter), @hirekoke #################################################################)
32 changes: 29 additions & 3 deletions src/code-gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ class PHP_Piet < CodeGen
Cmd = ["php QR.php > OUTFILE", "npiet QR.png > OUTFILE"]
Apt = ["php5-cli", nil]
def code
# PHP code contains binary, but the next Perl encodes them as a text.
<<-'END'.lines.map {|l| l.strip }.join
%(
<?php $f=function($n,$s="\\\\"){return str_repeat($s,$n);};
$z=3+$w=strlen($s=#{V[Q[E[PREV]],"{$f(",")}"].gsub(/ {10,}/){"{$f(#{$&.size},' ')}"}})*3;
<?php $z=3+$w=strlen($s=#{Q[E[PREV]]})*3;
echo"\\x89PNG\\r\\n\\x1a\\n";
$m="";
$t="\\xc0\\0\\xff";
Expand All @@ -153,17 +153,41 @@ class Perl < CodeGen
Cmd = "perl QR.pl > OUTFILE"
Apt = "perl"
def code
# BPE: Byte pair encoding
<<-'END'.lines.map {|l| l.strip }.join
(
p="eval";
%(
$_="#{
s=PREV;
v="";
127.upto(287){|j|
o={};
m=n=0;
s.size.times{|i|
o[f=s[i,2]]||=0;
c=o[f]+=1;
m<c&&(m=c;n=f)
};
v=n+v;
s=s.gsub(n,(j%256).chr)
};
s="
$_='#{Q[s,c=/['\\\\]/]}';
$n=32;
$s='#{Q[v,c]}';
$s=~s{..}{
$a=$&;
$b=chr(--$n&255);
~s/$b/$a/g;
}eg;
print
";
(s+N*(-s.size%6)).unpack("B*")[0].
gsub(/.{6}/){n=$&.to_i 2;((n+14)/26*6+n+47).chr}
}";
s|.|$n=ord$&;substr unpack(B8,chr$n-int($n/32)*6-41),2|eg;
print pack"B*",$_
eval pack'B*',$_
).scan(/[ ,-:A-z]+|(.)/){p="s++#{$1?"chr #{$1.ord}+e":$&+?+};"+p};
p
)
Expand Down Expand Up @@ -413,6 +437,7 @@ class Java_ < CodeGen
Cmd = "javac QR.java && java QR > OUTFILE"
Apt = "openjdk-6-jdk"
def code
# LZ78-like compression
<<-'END'.lines.map {|l| l.strip }.join
%(
class QR{
Expand Down Expand Up @@ -792,6 +817,7 @@ class Boo_Brainfuck < CodeGen
Cmd = ["booi QR.boo > OUTFILE", "$(BF) QR.bf > OUTFILE"]
Apt = ["boo", "bf"]
def code
# LZ77-like compression
<<-'END'.lines.map {|l| l.strip.gsub(/^_+/) { " " * $&.size } }.join
(
s=PREV;
Expand Down
Binary file modified thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4171493

Please sign in to comment.