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

YARV INTRODUCTION

YARV INTRODUCTION

yochiyochirb 100th meetup (2015-12-20)

Hirofumi Wakasugi

December 20, 2015
Tweet

More Decks by Hirofumi Wakasugi

Other Decks in Programming

Transcript

  1. :"37 :FU"OPUIFS3VCZ7. w ݱࡏͷ.3* $3VCZ ͷॲཧܥ w 3VCZίʔυΛ࣮ߦ͢ΔͨΊͷԾ૝Ϛγϯ 7. 

    w 3VCZͰ.3*ʹऔΓࠐ·Εͨ w ։ൃऀ͸࡫ాߞҰࢯ YARVINTRODUCTION
  2. 7. 7. 7. ϓϩάϥϛϯάݴޠॲཧܥͱͯ͠ͷ ʮԾ૝Ϛγϯʯ 04 ϓϩάϥϛϯάݴޠ 7. YARVINTRODUCTION 04

    04 04 )8 )8 )8 )8 ͜ͷΑ͏ʹɺϓϩάϥϛϯάݴޠࣗମΛ Ϛϧν؀ڥʹରԠͰ͖ΔΑ͏ઃܭ͢Δͷ ͸ඇৗʹ೉͍͕͠ɺԾ૝ϚγϯΛؒʹڬ Ή͜ͱͰରԠ΍࠷దԽ͕༰қʹͳΔ ݴޠ͸Ծ૝Ϛγϯ͕ղऍͰ͖Δ໋ྩΛు͚Ε͹0, Ծ૝ϚγϯͷϚϧν؀ڥରԠ͸ൺֱత༰қ
  3. ୅දతͳԾ૝Ϛγϯ YARVINTRODUCTION +BWB+7. w ͓ͳ͡Έʮ8SJUF0ODF 3VO"OZXIFSFʯ w +BWB͸΋ͪΖΜɺ4DBMBɺ$MPKVSFͳͲଟ਺͕͜ͷॲཧܥͰಈ࡞͢Δ /&5'SBNFXPSL w

    .4੡͕ͩ044ͱͯ͠ެ։͞Ε͓ͯΓɺຊདྷϓϥοτϑΥʔϜ͸໰Θͳ͍ w 7#/&5ɺ$ɺ'ͳͲͷݴޠ͕͜ͷॲཧܥͰಈ࡞͢Δ
  4. ୅දతͳԾ૝Ϛγϯ YARVINTRODUCTION +BWB+7. w ͓ͳ͡Έʮ8SJUF0ODF 3VO"OZXIFSFʯ w +BWB͸΋ͪΖΜɺ4DBMBɺ$MPKVSFͳͲଟ਺͕͜ͷॲཧܥͰಈ࡞͢Δ /&5'SBNFXPSL w

    .4੡͕ͩ044ͱͯ͠ެ։͞Ε͓ͯΓɺຊདྷϓϥοτϑΥʔϜ͸໰Θͳ͍ w 7#/&5ɺ$ɺ'ͳͲͷݴޠ͕͜ͷॲཧܥͰಈ࡞͢Δ --7.΋--7.*3ͱ͍͏தؒදݱΛར༻͢ ΔͳͲԾ૝Ϛγϯʹ͍ۙͱ͜Ζ͸͋Δ͕ɺ ݫີʹ͸ίϯύΠϥج൫ͳͷͰ ͪΐͬͱδϟϯϧ͕ҧ͏
  5. 0WFSWJFX YARVINTRODUCTION 04 3VCZίʔυ :"37 04 04 04 )8 )8

    )8 )8 :"37 :"37 :"37 $ $ $ $ ᶃ3VCZίʔυ͸:"37໋ྩྻͷόΠτίʔυʹί ϯύΠϧ͞ΕΔ ᶄ֤؀ڥʹରԠͨ͠:"37͕ɺڞ௨ͷ:"37໋ྩ ྻΛղऍ͠$ϓϩάϥϜΛੜ੒࣮ͯ͠ߦ w.3*͸$ݴޠ࣮૷ͳͷͰɺ:"37Λհͯ͠$ͷ ϓϩάϥϜͱ࣮ͯ͠ߦ͞ΕΔ wωΠςΟϒίʔυʹม׵͞ΕΔ+7.ͳͲͱ͸ ͜ͷ఺ͰҟͳΔ͕ɺԾ૝Խͷߏ଄తʹ͸ಉ͡
  6. YARVINTRODUCTION == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>=============== 0000 trace 1 ( 1) 0002

    putspecialobject 1 0004 putspecialobject 2 0006 putobject :hello 0008 putiseq hello 0010 opt_send_without_block <callinfo!mid:core#define_method, argc:3, ARGS_SIMPLE> 0012 pop 0013 trace 1 ( 5) 0015 putself 0016 putself 0017 putstring "Tom" 0019 opt_send_without_block <callinfo!mid:hello, argc:1, FCALL|ARGS_SIMPLE> 0021 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE> 0023 leave == disasm: <RubyVM::InstructionSequence:hello@<compiled>>=============== local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) [ 2] name<Arg> 0000 trace 8 ( 1) 0002 trace 1 ( 2) 0004 putstring "Hello " 0006 getlocal_OP__WC__0 2 0008 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE> 0010 trace 16 ( 3) 0012 leave ( 2) ͜Μͳ:"37໋ྩྻʹͳΔ
  7. == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>=============== 0000 trace 1 ( 1) 0002 putspecialobject

    1 0004 putspecialobject 2 0006 putobject :hello 0008 putiseq hello 0010 opt_send_without_block <callinfo!mid:core#define_method, argc:3, ARGS_SIMPLE> 0012 pop 0013 trace 1 ( 5) 0015 putself 0016 putself 0017 putstring "Tom" 0019 opt_send_without_block <callinfo!mid:hello, argc:1, FCALL|ARGS_SIMPLE> 0021 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE> 0023 leave == disasm: <RubyVM::InstructionSequence:hello@<compiled>>=============== local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) [ 2] name<Arg> 0000 trace 8 ( 1) 0002 trace 1 ( 2) 0004 putstring "Hello " 0006 getlocal_OP__WC__0 2 0008 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE> 0010 trace 16 ( 3) 0012 leave ( 2) ʁ YARVINTRODUCTION
  8. YARVINTRODUCTION ࣈ۟ղੳ 3VCZίʔυΛจࣈͣͭಡΈࠐΈղੳ͢Δ    U J N F

    T E P c O c 10.times do |n| puts n end U*/5&(&3   U*%&/5*'*&3 UJNFT LFZXPSE@EP c U*%&/5*'*&3 O c
  9. YARVINTRODUCTION ࣈ۟ղੳ 3VCZίʔυΛจࣈͣͭಡΈࠐΈղੳ͢Δ    U J N F

    T E P c O c 10.times do |n| puts n end U*/5&(&3   U*%&/5*'*&3 UJNFT LFZXPSE@EP c U*%&/5*'*&3 O c τʔΫϯྻ
  10. YARVINTRODUCTION ߏจղੳ U*/5&(&3   U*%&/5*'*&3 UJNFT LFZXPSE@EP c U*%&/5*'*&3

    O c method_call: ... | primary_value '.' operation2 | ... จ๏نଇʹϚον ύʔα͕จ๏نଇʹج͍ͮͯτʔΫϯྻͷղੳΛߦ͏
  11. লུʜ YARVINTRODUCTION ߏจղੳ QSPHSBN NFUIPE
 BEECMPDL DBMM JOUFHFS 10 QFSJPE

    JEFOUJpFS times EPCMPDL ߏจղੳʹΑͬͯ"45 ந৅ߏจ໦ ͕ੜ੒͞ΕΔ
  12. YARVINTRODUCTION ߏจղੳ 3JQQFSΛ࢖͏ͱ࣮ࡍʹ"45Λ֬ೝͰ͖Δ require 'ripper' require 'pp' code = <<EOS

    10.times do |n| puts n end EOS pp Ripper.sexp(code) [:program, [[:method_add_block, [:call, [:@int, "10", [1, 0]], :".", [:@ident, "times", [1, 3]]], [:do_block, [:block_var, [:params, [[:@ident, "n", [1, 13]]], nil, nil, nil, nil, nil, nil], false], [[:command, [:@ident, "puts", [2, 2]], [:args_add_block, [[:var_ref, [:@ident, "n", [2, 7]]]], false]]]]]]] 4ࣜʂ
  13. YARVINTRODUCTION :"37໋ྩྻ΁ͷίϯύΠϧ ΑΓ୯७ͳ3VCZίʔυΛݩʹઆ໌ puts 2 + 3 3VCZίʔυ /0%&@4$01& /0%&@'$"--

    ̼FUIPE@JEputs /0%&@$"-- NFUIPE@JE+ /0%&@-*5 2 /0%&@-*5 3 Ϩγʔό Ҿ਺ "45ϊʔυ
  14. putself putobject 2 putobject 3 send <callinfo!mid:+, argc:1, … send

    <callinfo!mid:puts, argc:1, ... YARVINTRODUCTION :"37໋ྩྻ΁ͷίϯύΠϧ "45ߏ଄ΛḷΓͳ͕Β :"37໋ྩྻελοΫʹϓογϡ͍ͯ͘͠ /0%&@'$"-- ̼FUIPE@JEputs /0%&@$"-- NFUIPE@JE+ :"37໋ྩྻ /0%&@-*5 2 /0%&@-*5 3
  15. putself putobject 2 putobject 3 opt_plus opt_send_simple <callinfo!mid:puts, argc:1, ...

    YARVINTRODUCTION :"37໋ྩྻ΁ͷίϯύΠϧ :"37໋ྩྻ͸࠷దԽ͞ΕΔ /0%&@'$"-- ̼FUIPE@JEputs /0%&@$"-- NFUIPE@JE+ :"37໋ྩྻ /0%&@-*5 2 /0%&@-*5 3 ߋʹ࠷దԽΛߦ͍ɺ ࠷దԽ໋ͨ͠ྩʹஔ͖׵͑Δ
  16. YARVINTRODUCTION puts 2 + 3 ࣈ۟ղੳ ߏจղੳ 5IF-POHBOE8JOEJOH3PBEUP:"37*OTUSVDUJPOT 3VCZίʔυ ίϯύΠϧ

    :"37໋ྩྻ ίʔυΛτʔΫϯྻʹ෼ղ τʔΫϯྻ͔Β"45Λੜ੒ "45͔Β࠷దԽ͞Εͨ :"37໋ྩྻ όΠτίʔυ Λੜ੒
  17. YARVINTRODUCTION :"37໋͕ྩʹै͍$ϓϩάϥϜΛ࣮ߦ ͜ͷΑ͏ʹͯ͠3VCZϓϩάϥϜ͸࣮ߦ͞ΕΔ $ cat example.rb puts 2 + 3

    $ ruby example.rb 5 ࣈ۟ղੳɺ ߏจղੳɺ :"37໋ྩྻ΁ͷίϯύΠϧɺ $ϓϩάϥϜͷ࣮ߦ
  18. YARVINTRODUCTION ࢀߟ ˒ :"37.BOJBDT w IUUQNBHB[JOFSVCZJTUOFU :BSW.BOJBDT ˒ 3VCZͷ͘͠Έ3VCZ6OEFSB.JDSPTDPQF w

    IUUQTFTUPSFPINTIBDPKQUJUMFT1 ҎԼͷهࣄ΍ॻ੶Λࢥ͍͖ͬΓࢀߟʹ͠·ͨ͠