🌳

TRICK 2022 を syntax_tree で解析してみる

2022/09/10に公開

まずは syntax_tree をインストール。かんたんです。

~ took 1m53s
👺 gem install syntax_tree
Fetching prettier_print-0.1.0.gem
Fetching syntax_tree-3.5.0.gem
Successfully installed prettier_print-0.1.0
Successfully installed syntax_tree-3.5.0
Building YARD (yri) index for prettier_print-0.1.0...
Building YARD (yri) index for syntax_tree-3.5.0...
Done installing documentation for prettier_print, syntax_tree after 4 seconds
2 gems installed

~ took 7s
👺 stree version
3.5.0

TRICK 2022 のコードを持ってきます。

~/tmp/rubykaigi2022
👺 git clone git@github.com:tric/trick2022.git
Cloning into 'trick2022'...
Authenticated to github.com ([20.27.177.113]:22).
remote: Enumerating objects: 73, done.
remote: Counting objects: 100% (73/73), done.
remote: Compressing objects: 100% (62/62), done.
remote: Total 73 (delta 9), reused 73 (delta 9), pack-reused 0
Transferred: sent 3256, received 181464 bytes, in 2.2 seconds
Bytes per second: sent 1463.5, received 81563.5
Receiving objects: 100% (73/73), 172.90 KiB | 188.00 KiB/s, done.
Resolving deltas: 100% (9/9), done.

~/tmp/rubykaigi2022
👺 cat trick2022/01-tompng/entry.rb
                               eval((s=%~c=(0..35
                         ).map{s[2*_1+1]}*'';class$Inte
                     ger;def$quXinclude(Math ;spXo(a)=self*
                 a.pow(87X=h=32.chr;g=PI/480;ls=(sp*31X,89)%89;
               def$abX+'eval((s=%'+(n=? .next)+s*88.chr+[nXs()=[a
            =self%X+'.split(',sp*25+'?'+88.chr+');(0..36).mapX89,89-
          a].miX{s[2*_1].split}',sp*31+".join.tr('$',$/)))"]*$/)Xn;end
         ;reqX.split$/;trap(:INT){puts;exit};q=->t,i{a,y=((t+i*99)Xuire
       'matrX%960). ivmod(80);[(a*(7+i)+i*23)%79+(y+a)/(5+i%4)%2,39Xix';1
      5.tiX-y/2]};p=->t,u{a=->b,c{(0..5).sum{(u%2-1)*E**(t*(b+c*_1)*gXmes{
     |i,*X.i+ i*u+=5+sin(u*u))}};x,z=a[5,3].           5,3].rect;x+=y.Xv|z=
    *?!Xi a[19,4];z+=w;r=(4+(x.abs+z.i).ab                };t=(0..959).fX..?
   W,?Xind{|t|(0..29).all?{x,y=q[t,_1];(x                   2||h=ls[y][x]X[,*
  ?]..X[/[^!-}]/]}};h=($**h+h).chr;eval(                     []}   ->(x,yX?};a
  =(0X,a,b){x=x*36+39.5;y=19.5-y*18;b*=1                            |i|((yX..1
 34)X-b).ceil..y+b).map{|j|((x-i)/a+(y j)/                          .times{X.ma
 p{zXx,z=p[t,_1];l=u```=0;while``````(l<1)``;          u+```=0      ;d=x-y;X.in
dex(Xl+=(d.abs+(z-w``)``.i).ab``s*1.``1 ;x``,z=y``,w;o[v``=``x.r    d.imag/Xc[i+
15*Xd.abs*l*sin(2*``l-t``*g*80``-_1)         l*(``1-l)/``6,a``=l*( -l)**2*0.X_1]
)};X7,a*2]&&o[v,z,``0.0``3,l**``                ``times``{|i``|(8+i).times{|Xw=*
MatXj|o[sin(i)/2+````` ```sin(                   `/2.0`````````)*j/200,j*0.0Xrix
[*(X5-1,0.02,0.1]``}}   ``.                       q[t,``_1];m``[y][x]= };i=-X0..
44).X1;$><<(['%%','[H                            .map{|j|(0..79).map{|k|x=(Xmap{
 |i,X -39.5)/35.8;y=(                            i+=1;m[j][k]?h:c[i]):ls[j]X*b|
 v<<X[k];}*''}*$/<<0)      1)%9                     te"`")#iJw6$Dcc1]r*G"eDXa.z
  ip(X%P<M^Az!7?f*yxM   `d"qi77UD*T!: .^B][T          ?%:"w82/s3A[M]V %KbeX0..
  ).suX5,G_=6D65#,zMaW   $lsR=!Uvhio  zb   :          dE:50obh|RgCTFfy*+)Xm{-(
   p=(XH0},JA>;rxRht+    ADjV;e&'/h   Qg              cO?,C%J a0mE+Qy2a9'X(i+
    _2)X^):t|am}b`k>e     zxd[HJ9    ony               JC gucaew/gcoWRy_X**9
     .1).X*]u+DL(&hH  wc  8531x'b    bd=           }  < z  Say vJ@{?+$Xto_i
      %88+XV-GE'5^Va  <9   *`?9b1   >_ }     8g   3   4 l  zH_/o@j*2vX1)*(
       _1||(Xp]h"u=4-  U.   0nz2-  E9U      #"pl=^   e    E(=+x6s/pXb<<p;
         0))}X3t_@K?C   C&  ^6(BE  > K  2DA B8jDlS  b*    WpM(|t(2X;b}]
          .lup.X5R(B71  la  u?5V?  M(2  dP  <S<,fo  Qv   >Fj4<?KXsolve
            (v);13X"$;  wL  b,l^f$  f,5  z  R,PhV#  pM   w^7UX5.time
               s{c[i+X  (8  NfbKS)  2*`  I RW7zgh!P j6   tX15*_1]
                 =z[a[_1]X enuc,T=  Oi.  > 5D=#(;(%  ?X||w.shif
                     t]}};eval(X9P  lRQ  S dq4M(Xc)~.split(
                         ?X);(0..36).map{s[2*_1].split}
                               .join.tr('$',$/)))

解析結果は下記の通り

~/tmp/rubykaigi2022
👺 stree ast trick2022/01-tompng/entry.rb
(program
  (statements
    ((fcall
        (ident "eval")
        (arg_paren
          (args
            ((paren
                (statements
                  ((assign
                      (var_field (ident "s"))
                      (call
                        (string_literal
                          ((tstring_content
                              "c=(0..35\n" +
                              "                         ).map{s[2*_1+1]}*'';class$Inte\n" +
                              "                     ger;def$quXinclude(Math ;spXo(a)=self*\n" +
                              "                 a.pow(87X=h=32.chr;g=PI/480;ls=(sp*31X,89)%89;\n" +
                              "               def$abX+'eval((s=%'+(n=? .next)+s*88.chr+[nXs()=[a\n" +
                              "            =self%X+'.split(',sp*25+'?'+88.chr+');(0..36).mapX89,89-\n" +
                              "          a].miX{s[2*_1].split}',sp*31+\".join.tr('$',$/)))\"]*$/)Xn;end\n" +
                              "         ;reqX.split$/;trap(:INT){puts;exit};q=->t,i{a,y=((t+i*99)Xuire\n" +
                              "       'matrX%960). ivmod(80);[(a*(7+i)+i*23)%79+(y+a)/(5+i%4)%2,39Xix';1\n" +
                              "      5.tiX-y/2]};p=->t,u{a=->b,c{(0..5).sum{(u%2-1)*E**(t*(b+c*_1)*gXmes{\n" +
                              "     |i,*X.i+ i*u+=5+sin(u*u))}};x,z=a[5,3].           5,3].rect;x+=y.Xv|z=\n" +
                              "    *?!Xi a[19,4];z+=w;r=(4+(x.abs+z.i).ab                };t=(0..959).fX..?\n" +
                              "   W,?Xind{|t|(0..29).all?{x,y=q[t,_1];(x                   2||h=ls[y][x]X[,*\n" +
                              "  ?]..X[/[^!-}]/]}};h=($**h+h).chr;eval(                     []}   ->(x,yX?};a\n" +
                              "  =(0X,a,b){x=x*36+39.5;y=19.5-y*18;b*=1                            |i|((yX..1\n" +
                              " 34)X-b).ceil..y+b).map{|j|((x-i)/a+(y j)/                          .times{X.ma\n" +
                              " p{zXx,z=p[t,_1];l=u```=0;while``````(l<1)``;          u+```=0      ;d=x-y;X.in\n" +
                              "dex(Xl+=(d.abs+(z-w``)``.i).ab``s*1.``1 ;x``,z=y``,w;o[v``=``x.r    d.imag/Xc[i+\n" +
                              "15*Xd.abs*l*sin(2*``l-t``*g*80``-_1)         l*(``1-l)/``6,a``=l*( -l)**2*0.X_1]\n" +
                              ")};X7,a*2]&&o[v,z,``0.0``3,l**``                ``times``{|i``|(8+i).times{|Xw=*\n" +
                              "MatXj|o[sin(i)/2+````` ```sin(                   `/2.0`````````)*j/200,j*0.0Xrix\n" +
                              "[*(X5-1,0.02,0.1]``}}   ``.                       q[t,``_1];m``[y][x]= };i=-X0..\n" +
                              "44).X1;$><<(['%%','[H                            .map{|j|(0..79).map{|k|x=(Xmap{\n" +
                              " |i,X -39.5)/35.8;y=(                            i+=1;m[j][k]?h:c[i]):ls[j]X*b|\n" +
                              " v<<X[k];}*''}*$/<<0)      1)%9                     te\"`\")#iJw6$Dcc1]r*G\"eDXa.z\n" +
                              "  ip(X%P<M^Az!7?f*yxM   `d\"qi77UD*T!: .^B][T          ?%:\"w82/s3A[M]V %KbeX0..\n" +
                              "  ).suX5,G_=6D65#,zMaW   $lsR=!Uvhio  zb   :          dE:50obh|RgCTFfy*+)Xm{-(\n" +
                              "   p=(XH0},JA>;rxRht+    ADjV;e&'/h   Qg              cO?,C%J a0mE+Qy2a9'X(i+\n" +
                              "    _2)X^):t|am}b`k>e     zxd[HJ9    ony               JC gucaew/gcoWRy_X**9\n" +
                              "     .1).X*]u+DL(&hH  wc  8531x'b    bd=           }  < z  Say vJ@{?+$Xto_i\n" +
                              "      %88+XV-GE'5^Va  <9   *`?9b1   >_ }     8g   3   4 l  zH_/o@j*2vX1)*(\n" +
                              "       _1||(Xp]h\"u=4-  U.   0nz2-  E9U      #\"pl=^   e    E(=+x6s/pXb<<p;\n" +
                              "         0))}X3t_@K?C   C&  ^6(BE  > K  2DA B8jDlS  b*    WpM(|t(2X;b}]\n" +
                              "          .lup.X5R(B71  la  u?5V?  M(2  dP  <S<,fo  Qv   >Fj4<?KXsolve\n" +
                              "            (v);13X\"$;  wL  b,l^f$  f,5  z  R,PhV#  pM   w^7UX5.time\n" +
                              "               s{c[i+X  (8  NfbKS)  2*`  I RW7zgh!P j6   tX15*_1]\n" +
                              "                 =z[a[_1]X enuc,T=  Oi.  > 5D=#(;(%  ?X||w.shif\n" +
                              "                     t]}};eval(X9P  lRQ  S dq4M(Xc)")))
                        (period ".")
                        (ident "split")
                        (arg_paren (args ((CHAR "?X")))))),
                    (call
                      (call
                        (method_add_block
                          (call (paren (statements ((dot2 (int "0") (int "36"))))) (period ".") (ident "map"))
                          (brace_block (statements ((call (aref (var_ref (ident "s")) (args ((binary (int "2") * (var_ref (ident "_1")))))) (period ".") (ident "split"))))))
                        (period ".")
                        (ident "join"))
                      (period ".")
                      (ident "tr")
                      (arg_paren (args ((string_literal ((tstring_content "$"))), (var_ref (gvar "$/"))))))))))))))))

ちなみに、shinh award: "Most orderly code" についてはあまりにも想定外だったのかエラーになります。

~/tmp/rubykaigi2022
❯ cat trick2022/04-tompng/entry.rb
rule = $*[00]&.to_i || 30
h  ||= $*[01]&.to_i || 32
[ % if % TRICK-2022 %% %]
# ] if a /%{(?<a>)/ =~ %} if + %} - %[
# } if b /%<(?<b>)/ =~ %> if - %> + %{
# > if c /%+(?<c>)/ =~ %+ if + %+ - %<
# + if d /%-(?<d>)/ =~ %- if - %- + %+
# - if e f, g, h, / =~ %/ if def e(f, g, h, *) =
# + if d /%-(?<d>)/ =~ %- if g.map! do rule.[] _1.join.to_i 2 end.!
# > if c /%+(?<c>)/ =~ %+ if !(g = *[g[-1], *g, g[0]].each_cons(3))
# } if b /%<(?<b>)/ =~ %> if puts(g.join.tr '01', ' #') || h > 0 &&
# ] if a /%{(?<a>)/ =~ %} if !g ||= [*g = [0] * h, 1] + g if h -= 1
eval(File.read f ||= __FILE__)

~/tmp/rubykaigi2022
👺 stree ast trick2022/04-tompng/entry.rb
Error: Cannot find expected TStringEnd
  10 | # > if c /%+(?<c>)/ =~ %+ if !(g = *[g[-1], *g, g[0]].each_cons(3))
  11 | # } if b /%<(?<b>)/ =~ %> if puts(g.join.tr '01', ' #') || h > 0 &&
> 12 | # ] if a /%{(?<a>)/ =~ %} if !g ||= [*g = [0] * h, 1] + g if h -= 1
     |                        ^
  13 | eval(File.read f ||= __FILE__)

今年もRubyKaigiの開催を感謝します。楽しい時間をありがとうございます。

Discussion

ログインするとコメントできます