Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby2.7の新機能で簡易版Rubyインタプリタを実装してみる

 Ruby2.7の新機能で簡易版Rubyインタプリタを実装してみる

Hello hey for Engineers vol.3(day2)の発表資料です。
Ruby2.7の新機能パターンマッチを使って簡易版Rubyインタプリタを実装してみるという話です。

fuqda

May 09, 2019
Tweet

More Decks by fuqda

Other Decks in Programming

Transcript

  1. Ruby2.7ͷ৽ػೳͰ

    ؆қ൛RubyΠϯλϓϦλΛ

    ࣮૷ͯ͠ΈΔ

    2019/05/09
    Hello hey for Engineers vol.3ɹɹ


    fuqda(fuqda90)

    View full-size slide

  2. fuqdaͱਃ͠·͢
    ʲܦྺʳ

    4*FS ೥਺ϲ݄
    ͔Β

    ແڅΠϯλʔϯϲ݄Λܦͯ

    4503&4KQ΁ྲྀΕண͖೥໨


    ʲͦͷଞʳ

    ීஈ͸5BNBSCͱ͍͏

    ଟຎ஍Ҭ ٢঵ࣉʙീԦࢠ·Ͱ
    ͷ

    ஍Ҭ3VCZίϛϡχςΟΛɹ

    ओ࠵ͯ͠·͢

    View full-size slide

  3. ࠂ஌ ݸਓత

    View full-size slide

  4. ݄ʹ5BNBSCͰ
    ΧϯϑΝϨϯε
    ΍Γ·͢✨

    View full-size slide

  5. ςʔϚ͸ʮRubyistͱͯ͠ͷ੒௕ʯ

    View full-size slide

  6. Tama Rubyձٞ01

    d

    ೚ҙࢀՃͷ࠙਌ձd·Ͱ

    !ौ୩ηϧϦΞϯλϫʔ' (.0:PVST

    جௐߨԋɿҏ౻३Ұ͞Μ !KODIJUP

    جௐߨԋɿޒेཛྷ๜໌͞Μ !JHBJHB

    ͦͷଞ$'1ʴ5BNBSCϝϯόʔ-5

    View full-size slide

  7. CFPઈࢍड෇தͰ͢

    (ҰൠࢀՃืू͸6݄։࢝༧ఆ)
    https://tama-rb.github.io/tamarubykaigi01/

    View full-size slide

  8. ࠓ೔ͷ͓࿩

    View full-size slide

  9. Ruby2.7ͷύλʔϯϚονΛ
    ࢼͯ͠ΈΔͱ͍͏࿩
    3VCZͷτϥϯΫ NBTUFS
    ʹ

    1BUUFSO.BUDIJOHͱ͍͏৽ػೳ͕

    ௥Ճ͞Ε͍ͯΔͱͷ͜ͱ

    ͱ͍͏Θ͚Ͱʂຊ೔͸

    ͦΕΛࢼͯ͠Έ·͢

    View full-size slide

  10. ิ଍ɿRubyKaigi2019Ͱͷ

    ൃද͸ͪ͜Β
    https://speakerdeck.com/k_tsj/pattern-matching-new-feature-in-ruby-2-dot-7

    View full-size slide

  11. ࠓճ

    ύλʔϯϚονϯάΛ
    ࢼ͢୊ࡐ

    View full-size slide

  12. 3VCZͰ࡞Δ

    ؆қ൛
    3VCZΠϯλϓϦλ

    View full-size slide

  13. ʲલఏʳ
    ͜͜Ͱ࡞ΔΠϯλϓϦλʹ͍ͭͯ͸ɺ

    िץΞεΩʔͰ࿈ࡌ͞Ε͍ͯͨ
    (2016.09~2017.01)

    RubyͰֶͿRubyͷίʔυΛҰ෦ྲྀ༻͠ɺ

    ͦ͜ʹखΛೖΕ͍ͯ͘ܗͰ

    ਐΊ͍͖ͯ·͢
    refs :ʮୈ5ճ RubyͰม਺෇͖ి୎Λ࡞ͬͯΈΔʯ
    https://ascii.jp/elem/000/001/230/1230449/

    View full-size slide

  14. ͦ΋ͦ΋ΠϯλϓϦλͬͯ…
    JUFSQSFUFSˠҙຯɿʮ௨༁ʯ


    ਓ͕ؒॻ͍ͨιʔείʔυΛ

    ಡΈࠐΜͰϓϩάϥϜͱͯ͠

    ࣮ߦ͢ΔϓϩάϥϜͷ͜ͱ

    View full-size slide

  15. ྫ͑͹͜ΜͳࣜΛಡΈࠐΜͰ

    ΠϯλʔϓϦλ͕ղऍ͢Δ৔߹

    View full-size slide

  16. ΠϯλϓϦλ͕

    ΍Δ͜ͱͬͯʁ

    View full-size slide

  17. ࣈ۟ղੳ
    ߏจղੳ
    ࣮ߦ

    View full-size slide

  18. 1. ࣈ۟ղੳ



    จࣈྻΛͭͭ෦඼ͷ഑ྻʹม׵͢Δ



    View full-size slide

  19. 2. ߏจղੳ



    ϓϩάϥϜͱ࣮ͯ͠ߦՄೳͳߏจ໦ʹม׵͢Δ



    View full-size slide

  20. 3. ࣮ߦ
    ߏจ໦Λೖྗͱͯ͠ϓϩάϥϜΛ࣮ߦ͢Δ

    View full-size slide

  21. ࣈ۟ղੳ
    ߏจղੳ
    ࣮ߦ
    ࣌ؒͷ౎߹ʹΑΓ ͷ
    ෦෼͸ग़དྷ߹͍ͷϥΠϒϥϦΛ
    ྲྀ༻͠·͢

    View full-size slide

  22. ࠓճͷࡐྉᶃ

    ߏจղੳ༻ͷgem
    NJOSVCZ
    https://github.com/mame/minruby

    View full-size slide

  23. ࠓճͷࡐྉᶄ

    ߏจ໦ΛRubyͱ࣮ͯ͠ߦ͢ΔΫϥε
    ࣮ߦ͢ΔϑΝΠϧ
    JOUFSQSFUFSSC

    View full-size slide

  24. ࠓճͷࡐྉᶅ

    ߏจ໦Λ࡞ΔͨΊͷܭࢉࣜΛهࡌͨ͠Ϋϥε
    ίϚϯυ࣮ߦ͢ΔϑΝΠϧͷҾ਺ͱͳΔϑΝΠϧ
    ʮʯ͕هࡌ͞Ε͍ͯΔ͚ͩ

    GPSNVMBSC

    View full-size slide

  25. ͡Ό࣮͋ࡍɺ
    3VCZͷύλʔϯϚονϯάΛ
    ΠϯλϓϦλͷॲཧͰ࢖͏ͱͯ͠ɺ
    ͲΜͳ෦෼Ͱ࢖͑ΔͩΖ͏ʁ

    View full-size slide

  26. case~whenΛεοΩϦॻ͘

    ͨΊʹ࢖͏ͷ͸Ͳ͏ͩΖ͏ʁ
    ɹɹɹɹ ߏจղੳޙͷ഑ྻ
    Λ
    ࠶ؼతʹݕࡧͯ͠Լهͷϝιουͷ
    3VCZͱͯ͠

    ࣮ߦ͢Δ෦෼ʹ

    खΛೖΕͯΈΔ

    View full-size slide

  27. ࢼͯ͠ΈΑ͏

    View full-size slide

  28. ࠓճ࢖͏

    ύλʔϯϚονϯάͷ
    छྨ
    ɹɹɹɹ˞ύλʔϯϚονϯάͷه๏ʹ͸

    ɹɹɹɹ͍͔ͭ͘ͷόϦΤʔγϣϯ͕͋Γ·͢

    View full-size slide

  29. As Pattern
    DBTFʹ౉ͬͨ஋͕
    ύλʔϯʹҰக͢Δͱ
    Ұகͨ͠஋ͱ

    Ҏ߱ͷม਺Λ
    ඥ͚ͮΔ͜ͱ͕ग़དྷΔ

    View full-size slide

  30. ͦͷଞͷύλʔϯϚονϯάʹ͍ͭͯ
    7BMVF1BUUFSO

    7BSJBCMF1FUUFSO

    "MUFSOBUJWF1BUUFSO

    "SSBZ1BUUFSO

    )BTI1BUUFSO
    ͪ͜Βͷهࣄ͕෼͔Γ΍͔ͬͨ͢ͷͰؾʹͳΔํ͸ͦͪΒ΁

    !NBLJDBNFMʮ3VCZͷ৽ػೳ1BUUFSO.BUDIJOH͕࠷ߴͰͨ͠ʯ

    IUUQTRJJUBDPNNBLJDBNFMJUFNTDEFGEFEC

    View full-size slide

  31. As PatternΛ࢖͏લޙͷมߋࠩ෼
    ࠓճͷέʔεͰ͸࠶ؼॲཧΛॻ͔ͳͯ͘΋Α͘ͳΓ·ͨ͠

    ଟগ͸γϡοͱॻ͚͔ͨ΋͠Ε·ͤΜɻ
    ΋ͬͱ៉ྷʹॻ͚ΔͩΖ͏͚Ͳࠓճ͸ΰϝϯͳ͍͞ʜ

    View full-size slide

  32. खݩͰಈ͔͢৔߹
    ΋͠खݩͰࢼͯ͠Έ͍ͨ
    ͱ͍͏ํ͸HJTUʹιʔεΛ
    ஔ͍͓͖ͯ·ͨ͠ͷͰɺ
    ͦͪΒͰ΋͓ܰ͘ࢼ͠௖͚·͢
    ஫) Rubyͷόʔδϣϯ͸2.7.0devͰ͢
    https://gist.github.com/Shigeyuki-fukuda/37c2e9da1ac3744ea24920dacd25580f

    View full-size slide

  33. ࣮ࡍʹಈ͔ͨ݁͠Ռ

    SBJMTSVOOFSίϚϯυʹҾ਺Λ౉ͯ͠

    ࣮ߦͯ͠Έͨͱ͜ΖΩϟϓνϟͷ௨Γ

    ૝ఆ௨Γͷܭࢉ͕͞Ε·ͨ͠
    ܭࢉ݁Ռ͕Ͱ͋Δ͜ͱΛ֬ೝ

    View full-size slide

  34. ࠓճ΍ͬͯΈͯ…
    ࠓճ͸ɺͷΑ͏ͳԋࢉࢠͱ਺஋ͭͷ୯७ͳܭࢉࣜͰ͕ͨ͠ɺ
    ΍ԼهͷΑ͏ͳෳ਺ߦͷܭࢉʹରԠ͠Α͏ͱ͢Δͱ
    ΋͏গ͠࢖͍ํΛ޻෉͢Δඞཁ͕͋Γͦ͏Ͱ͢
    ΋͏গ͠޻෉ͯ͠࡞Γ͔ͨͬͨͰ͕͢ɺ·ͨ࣍ͷػձʹ

    ɹɹɹɹɹɹɹɹɹɹ
    ɹɹɹɹɹɹɹɹɹɹ
    FYෳ਺ߦͷܭࢉࣜΛಡΈࠐΉࡍͷྫ

    View full-size slide

  35. RubyͬͯΠΠͰ͢ΑͶ✨
    ࠓճ͸3VCZͷτϥϯΫʹ௥Ճ͞Εͨ
    ϕʔλ൛ͷύλʔϯϚονϯάΛ3VCZͷΠϯλϓϦλͷ࣮૷
    ͱ͍͏έʔεΛྫʹ͍ͬͯ͡Έ·ͨ͠
    ΍ͬͺΓ3VCZͰίʔυΛॻ͘ͷָ͍ͬͯ͠Ͱ͢ΑͶ✨☺
    3VCZͷདྷ೥ϦϦʔε͕ࠓ͔Βָ͠ΈͰ͢
    SCFOW͔ΒखܰʹམͱͤΔͷͰ

    ·ͩͷํ͸2.7Λࢼͯ͠Έͯ͸͍͔͕Ͱ͠ΐ͏͔

    View full-size slide

  36. ͱ͍͏Θ͚Ͱ

    View full-size slide

  37. 8FBSF)JSJOH
    4503&4KQ͸

    Ұॹʹಇ͍ͯ͘ΕΔ஥ؒΛืू͍ͯ͠·͢

    View full-size slide