ざっくり学ぶ言語のしくみ

A26e80e38252b6b47ee5130db90c2cc9?s=47 itkrt2y
December 09, 2017
3.5k

 ざっくり学ぶ言語のしくみ

A26e80e38252b6b47ee5130db90c2cc9?s=128

itkrt2y

December 09, 2017
Tweet

Transcript

  1. 7.
  2. 15.

    ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  3. 16.

    ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  4. 17.

    ίʔυΛτʔΫϯʢίʔυͷ࠷খ୯Ґʣʹ෼ׂ͢Δ puts ( 2 ࣝผࢠ + ਺஋ ࠨׅހ puts (10

    + 2*3)/4 10 ਺஋ + * ۭന ਺஋ * 3 ) ӈׅހ ۭന / / ਺஋ 4 ۭന
  5. 18.

    # Structͷ഑ྻ΍࿈૝഑ྻͷ഑ྻͳͲɺ΍Γํ͸৭ʑ [ [:identifier, "puts"], [:space, " "], [:lparen, "("],

    [:int, "10"], [:space, " "], [:plus, "+"], [:space, " "], [:int, "2"], [:asterisk, "*"], [:int, "3"], [:rparen, ")"], [:slash, "/"], [:int, "4"] ] ίʔυ্Ͱ͸͜Μͳײ͡
  6. 19.

    ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  7. 20.

    τʔΫϯྻ͔Βந৅ߏจ໦Λ࡞Δ ident:puts lparen:( int:10 plus:+ space: space: space: int:2 asterisk:*

    int:3 rparen:) slash:/ int:4 int:10 plus:+ int:2 asterisk:* int:3 ident:puts slash:/ int:4 <token>:<literal>
  8. 21.

    ந৅ߏจ໦ͱ͸ʁ • ໦ߏ଄ͷσʔλߏ଄ • AST (Abstract Syntax Tree) • ந৅

    == ຊ࣭Λൈ͖ग़ͨ͠΋ͷ • ίϝϯτ΍ۭനͳͲɺ࣮ߦ্ෆཁͳ৘ใΛഉআ͠ඞཁ ͳ৘ใͷΈͰߏ੒͞ΕΔ
  9. 22.

    ident:puts lparen:( int:10 plus:+ space: space: space: int:2 asterisk:* int:3

    rparen:) slash:/ int:4 int:10 plus:+ int:2 asterisk:* int:3 ident:puts slash:/ int:4 ࠓճͷྫͰ͸੺࿮ͷ΋ͷ͕ASTʹ͸ͳ͍
  10. 23.

    int:10 plus:+ int:2 asterisk:* int:3 ident:puts slash:/ int:4 ͪͳΈʹׅހ͕ͳ͔ͬͨΒ puts

    10 + 2 * 3 / 4 ߏจ໦ͷߏ଄Ͱ࣮ߦॱং͕ දݱ͞Ε͍ͯΔ
  11. 24.

    ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  12. 27.

    ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  13. 43.

    ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex Ҏ্ɺͬ͘͟Γͨ͠ݴޠͷ͘͠ΈͰͨ͠ ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  14. 44.

    ͪΌΜͱษڧ͍ͨ͠ਓ͸ Writing An Interpreter In Go͕Φεεϝ • ӳޠͷຊͰ͕͢ɺςετίʔυ͕͔ͬ͠Γͯ͠ΔͷͰίʔ υΛಡΊ͹ͳΜͱ͔ͳΓ·͢ •

    A Tour of GoΛҰ௨Γཧղ͍ͯ͠Ε͹େৎ෉ • amazon.co.jpͰkindle൛ΛߪೖՄೳ • 2351ԁʢ2017/12/09࣌఺ʣ
  15. 47.
  16. 50.

    ίʔυ ධՁ ࣈ۟ղੳ + ߏจղੳ ந৅ߏจ໦ ݁Ռ Ruby 3? :"37

    ໋ྩྻ ίϯύΠϧ ωΠςΟϒ ίʔυ ίϯύΠϧ ࣮ߦ
  17. 56.

    parse.c parse.y bison def/keywords gperf ίʔυ ந৅ߏจ໦ ΑΓਖ਼֬ʹݴ͏ͱ parse.o y.tab.c

    mv lex.c cc Rubyιʔείʔυ׬શղઆ ୈ10ষ ύʔαΑΓ cpp
  18. 60.

    bison (yacc) • ύʔαδΣωϨʔλ • yaccͷ্Ґޓ׵ • yacc == yet

    another compiler compiler • compiler compiler == compiler generator • Ͱ΋yacc͸parser generator • Backus-Naur FormʢBNF: όοΧεφ΢Ξه๏ʣ • ͜͏͍͏ͷ !
  19. 64.

    Ripper • Ruby૊ΈࠐΈͷύʔαϥΠϒϥϦ require 'ripper' code = 'puts "Hello World!"'

    # ࣈ۟ղੳ Ripper.lex(code) # ߏจղੳ Ripper.sexp(code) # sexp == symbolic expression (Sࣜ)
  20. 68.

    puts (10 + 2*3)/4 == disasm: #<ISeq:<main>@sample.rb>===================================== 0000 trace 1

    ( 1) 0002 putself 0003 putobject 10 0005 putobject 2 0007 putobject 3 0009 opt_mult <callinfo!mid:*, argc:1, ARGS_SIMPLE>, <callcache> 0012 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache> 0015 putobject 4 0017 opt_div <callinfo!mid:/, argc:1, ARGS_SIMPLE>, <callcache> 0020 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, <callcache> 0023 leave
  21. 72.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  22. 73.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  23. 74.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  24. 75.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave  TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  25. 76.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave   TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  26. 77.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave    TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  27. 78.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave   TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  28. 79.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave  TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  29. 80.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave   TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  30. 81.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave  TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  31. 82.

    0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave OJM YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  32. 86.