Cookpad 2016 Summer Intern: Programming Paradigm

Cookpad 2016 Summer Intern: Programming Paradigm

クックパッド2016夏インターン講義5日目: プログラミングパラダイム

0d9e60b12d0a40841b1a746b112ad36b?s=128

Minero Aoki

August 19, 2016
Tweet

Transcript

  1. 11.

    ࠓ೔࣮૷͢Δػೳ w ੔਺Ϧςϥϧʢ   ʣ w Ճࢉʢ ʣ w

    ৚݅ԋࢉࢠʢD UFʣͱJGจ w ؔ਺ݺͼग़͠ͱҾ਺ͷࢀরɺSFUVSOจ w ؆୯ͳ࠷దԽ w BOENPSF
  2. 18.

    Φϓγϣϯ͸IFMQʹฉ͚ % ./bin/jetspider --help jetspider [options] {JS_FILE | -e SOURCE}

    --dump-tokens Dumps lexical tokens and quit. --dump-ast Dumps AST and quit. --dump-semantic Dumps semantic tree and quit. --dump-object Dumps object file and quit. -e, --source=TEXT Give source code from option. --help Prints this message and quit. % ./bin/jsvm --help Usage: ./bin/jsvm [options] SOURCE Options: -p, --print Executes source and print the last result. -t, --trace Executes source with tracing. -d, --disassemble Disassembles source.
  3. 21.

    +FU4QJEFSͷιʔεπϦʔ w CJO w KFUTQJEFSίϯύΠϥʔ w KTWN7. w MJCKFUTQJEFS w

    DPNQJMFSSCίϯύΠϥυϥΠόʔ w QBSTFSSCύʔαʔ w SFTPMWFSSCҙຯղੳث w DPEF@HFOFSBUPSSCίʔυδΣωϨʔλʔ w BTTFNCMFSSCόΠτίʔυ%4- w PCKFDU@pMFSC+4$ϑΝΠϧ ͜͜Λॻ͘
  4. 23.

    ϑΣΠζߏจղੳ w ιʔείʔυʢςΩετʣ ͷߏจΛνΣοΫ͢Δ ͱಉ࣌ʹɺѻ͍΍͍͢ Α͏πϦʔߏ଄ʢ"45  "CTUSBDU4ZOUBY5SFFʣ ΁ม׵͢Δ JG

    J \ QSJOU J  ^FMTF\ J ^ JG DBMM QSJOU J  J  UIFO FMTF MIT SIT GVODUJPO BSHT  J  MFGU SJHIU DPOE
  5. 24.

    ࣈ۟ղੳ w ߏจղੳͰ͸·ͣιʔε ίʔυʢจࣈͷྻʣΛ τʔΫϯʢ୯ޠʣͷྻ ʹ෼ׂ͢Δ w ίϝϯτ΍ۭന͸͜ͷ ஈ֊Ͱফͯ͠͠·͏
 ʢ͜ͱ͕ଟ͍ɻ+4΍3VCZʹ͸҉໧ͷจ

    ऴ୺͕͋ΔͨΊվߦͷ৘ใ͸࢒͢ඞཁ ͕͋Δɻ+BWB%PD΍42-ώϯτͳͲί ϝϯτ͕ඞཁͳ৔߹΋͋Δʣ lJG J \aOQSJOU J aO^FMTF \aOJaO^z JG lz J   lz \ laOz QSJOU J  laOz ^ lz FMTF lz \ laOl J ll ʜʜ
  6. 25.

    <PQ>τʔΫϯྻΛݟΔ % ./bin/jetspider js/arith.js --dump-tokens FUNCTION function S " "

    IDENT f ( ( IDENT x ) ) S " " { { S "\n " RETURN return S " " NUMBER 1 S " " + + S " " IDENT x S " " * * S " " NUMBER 3 ; ; S "\n" } } S "\n" KFUTQJEFSEVNQUPLFOT'*-&KT GVODUJPOG Y \ SFUVSO Y  ^ ˝ιʔείʔυʢKTBSJUIKTʣ
  7. 27.

    <PQ>"45ΛݟΔ % ./bin/jetspider --dump-ast js/arith.js type: SourceElementsNode value: - type:

    FunctionDeclNode value: f arguments: - type: ParameterNode value: x function_body: type: FunctionBodyNode value: type: SourceElementsNode value: - type: ReturnNode value: type: AddNode left: type: NumberNode value: 1 value: type: MultiplyNode left: type: ResolveNode value: x value: type: NumberNode value: 3 KFUTQJEFSEVNQBTU'*-&KT GVODUJPOG Y \ SFUVSO Y  ^ ˝ιʔείʔυʢKTBSJUIKTʣ WBMVF "EE/PEF /VNCFS/PEF .VMUJQMZ/PEF 3FTPMWF/PEF /VNCFS/PEF 3FUVSO/PEF WBMVF MFGU WBMVF MFGU WBMVF WBMVFY WBMVF
  8. 29.

    <PQ>தؒίʔυΛݟΔ % ./bin/jetspider --dump-sem js/arith.js - type: FunctionDeclNode value: f

    arguments: - type: ParameterNode value: x function_body: type: FunctionBodyNode value: type: SourceElementsNode value: - type: ReturnNode value: type: AddNode left: type: NumberNode value: 1 value: type: MultiplyNode left: type: ResolveNode value: x ref: "[function f]:param:x" value: type: NumberNode value: 3 - type: SourceElementsNode value: - KFUTQJEFSEVNQTFNBOUJD'*-&KT "EE/PEF /VNCFS/PEF .VMUJQMZ/PEF 3FTPMWF/PEF /VNCFS/PEF 3FUVSO/PEF WBMVF MFGU WBMVF MFGU WBMVF WBMVF WBMVFY WBMVF WBMVFY QBSBNY 1BSBNFUFS/PEF 'VODUJPO%FDM/PEF BSHVNFOUT 7BSJBCMFΦϒδΣΫτ
  9. 31.

    ϑΣΠζίʔυੜ੒ w ҙຯղੳࡁΈͷσʔλ Λݩʹɺػցޠ΍7.ίʔ υΛੜ੒͢Δ w ࣮ࡍ͸ίʔυ͚ͩͰ͸ ͳ͘ɺγϯϘϧςʔϒϧ ʢม਺දʣ΍ϦϯΫ৘ ใ΋ؚΉΦϒδΣΫτϑΝ

    ΠϧΛੜ੒͢Δ "EE/PEF /VNCFS/PEF .VMUJQMZ/PEF 3FTPMWF/PEF /VNCFS/PEF 3FUVSO/PEF WBMVF MFGU WBMVF MFGU WBMVF WBMVF WBMVFY YQBSBN WBMVF dd 01 int8 1 54 00 00 getarg 0 dd 03 int8 3 1d mul 1b add 05 return
  10. 32.

    <PQ>ίϯύΠϧ͢Δ KFUTQJEFS'*-&KT % ./bin/jetspider js/arith.js % ls -l js/arith.jsc -rw-r--r--

    1 minero-aoki staff 228 Aug 25 10:59 js/arith.jsc w KTϑΝΠϧΛࢦఆ͢Δ͚ͩ Ͱ0, w ֦ுࢠΛ KTDʹม͑ͨϑΝ Πϧʢ+4$ϑΝΠϧʣ͕ੜ੒ ͞ΕΔ
  11. 33.

    <PQ>+4$ϑΝΠϧΛݟΔ KFUTQJEFSEVNQPCKFDU'*-&KT % ./bin/jetspider --dump-obj js/arith.js - n_vars: 0 n_args:

    1 vars: - x prolog_length: 0 js_version: 185 n_fixed: 0 script_bits: 32 code: - dd 01 int8 1 - 54 00 00 getarg 0 - dd 03 int8 3 - 1d mul - 1b add - 05 return - c5 stop srcnotes: '' filename: js/arith.js lineno: 1 n_slots: 3 static_level: 1 atoms: [] blocks: [] upvars: [] regexps: [] closed_args: [] closed_vars: [] trynotes: [] consts: [] - n_vars: 0 n_args: 0 ҎԼུ w όΠτίʔυ͚ͩͰ͸ͳ͘ɺ +4$ϑΝΠϧͷ߲໨͕͢΂ͯ දࣔ͞ΕΔ w ౰વ͚ͩͲίʔυδΣωϨʔ λʔ͕׬੒͍ͯ͠ͳ͍ͱΤ ϥʔʹͳͬͯ͠·͏ w ࣮૷ͨ݁͠ՌΛ֬ೝ͢Δͨ Ίʹ࢖༻
  12. 34.

    +4$ϑΝΠϧͷߏ଄ w 4QJEFS.POLFZ಺Ͱ͸ؔ਺ ఆٛͭʹରͯ͠TUSVDU +4'VODUJPO͕ͭੜ੒͞Ε Δ w +4$ϑΝΠϧʹ͸ιʔεϑΝ ΠϧͰఆٛ͞Εͨ͢΂ͯͷؔ ਺ʹରԠ͢ΔTUSVDU

    +4'VODUJPO͕֨ೲ͞ΕΔ w ࠷ޙͷͭ͸ৗʹTUSVDU +44DSJQUͰϝϯόʔ͕গͳ͍ +4$ϑΝΠϧ TUSVDU+4'VODUJPO TUSVDU+4'VODUJPO TUSVDU+44DSJQU UPQMFWFM NBHJD Y%&"%# ؔ਺ͷ਺ ʢ ͸τοϓϨϕϧʣ
  13. 37.

    ՝୊ͷ࣮૷εςοϓ  ࣮૷͢΂͖+BWB4DSJQUͷίʔυΛ֬ೝ͢Δ
 ʢͰ͖Δ͚ͩγϯϓϧͳ+4ίʔυΛ༻ҙ͠Α͏ʂʣ  KFUTQJEFSEVNQBTU TFNͰ"45ΛݟΔ  KTWNE EJTBTTFNCMF

    Ͱʮ౴͑ʯͷόΠτίʔυΛݟΔ  $PEF(FOFSBUPSͷϝιουΛ࣮૷͢Δ  KFUTQJEFSEVNQPCKͰग़ྗ݁ՌΛ֬ೝ͢Δ  ίϯύΠϧ͠ɺKTWNQSJOUͰ࣮ߦ͢Δ
  14. 38.

    "45ΛݟΔ % ./bin/jetspider --dump-ast js/number.js type: SourceElementsNode value: - type:

    ExpressionStatementNode value: type: NumberNode value: 77 ʮจʯͷϦετΛද͢ϊʔυ ͭͷ͔ࣜΒͳΔ ʮจʯͷϊʔυ ੔਺Ϧςϥϧ
 ͷϊʔυ
  15. 40.

    OVNCFSKTͷٯΞηϯϒϧ % ./bin/jsvm -d js/number.js main: 00000: dd 4d int8

    77 00002: 02 popv 00003: c5 stop ‣ Θ͔Βͳ໋͍ྩ͕͋ͬͨΒ(3061"%ͷόΠτ ίʔυϦϑΝϨϯεͰ֬ೝ /VNCFS/PEFͰग़ྗ͢΂͖෦෼ &YQSFTTJPO4UBUFNFOU/PEF͕ग़ྗ τοϓϨϕϧͱؔ਺ͷ຤ඌͰࣗಈग़ྗ
  16. 41.

    "45ͷτϥόʔε w USBWFSTFπϦʔͷϊʔ υΛॱ൪ʹͨͲ͍ͬͯ ͘ૢ࡞ w खଓ͖ܕݴޠͳΒ࠶ؼ ݺͼग़͠ɺ001Ͱ͸ 7JTJUPSύλʔϯΛ࢖͏ ͷ͕ఆੴ

    τϥόʔεʢUSBWFSTFʣ "EE/PEF /VNCFS/PEF .VMUJQMZ/PEF 3FTPMWF/PEF /VNCFS/PEF 3FUVSO/PEF ̍ ̎ ̏ ̐ ̑ ̒
  17. 42.

    7JTJUPSύλʔϯͰ
 "45Λτϥόʔε͢Δ DMBTT"457JTJUPS EFGWJTJU@3FUVSO/PEF OPEF  WJTJUOPEFWBMVFˡSFUVSOจͷࣜΛτϥόʔε FOE EFGWJTJU@"EE/PEF OPEF

     WJTJUOPEFMFGUˡl zͷࠨଆͷࢬΛτϥόʔε WJTJUOPEFWBMVFˡl zͷӈଆͷࢬΛτϥόʔε FOE EFGWJTJU@/VNCFS/PEF OPEF  ੔਺Ϧςϥϧʹ͸ͨͲΔࢬ͕ͳ͍ʢMFBGʣ FOE FOE "EE/PEF /VNCFS/PEF /VNCFS/PEF 3FUVSO/PEF
  18. 46.

    ࿅श໰୊ w l lԋࢉࢠʢ"EE/PEFʣΛ࣮૷ͤΑ w KTBEEKT KTBEEKTΛಈ࡞ͤ͞Α͏ w lzԋࢉࢠʢ4VCUSBDU/PEFʣͷ࣮૷͕ࢀߟʹͳΔ w

    BEEKT BEEKTͰొ৔͢Δάϩʔόϧม਺POF UXP UISFFʹ͸ͦΕͧΕ ੔਺  ͕ࣄલʹ୅ೖͯ͋͠ΔʢKTWNͷಠࣗ࢓༷ʣ w  ͷΑ͏ʹϦςϥϧ͚ͩͷՃࢉͩͱɺ࠷దԽ͞ΕͯՃࢉࣗମ͕ফ໓ ͯ͠͠·͏ͷͰɺม਺Λ࢖ͬͯ࠷దԽΛճආ͍ͯ͠Δʜʜ
  19. 48.

    <PQ>7.ͷಈ࡞ΛݟΔ w KTWNʹUSBDFΦϓγϣ ϯΛ෇͚࣮ͯߦ͢ΔͱҎ Լͷ಺༰΋ग़ྗͰ͖Δ w ࣮ߦ͠Α͏ͱ͍ͯ͠Δ όΠτίʔυ w QVTIͱQPQ

    w มԽޙͷ7.ελοΫ KTWNUSBDF'*-&KTD % ./bin/jsvm --trace js/lvar.js 1: 00000: 00 nop stack: 6: 00001: f1 00 00 callglobal "f" stack: function f() {\n var a = 1, b;\n p(a);\n} undefined 6: 00004: 3a 00 00 call 0 inputs: f, f @ 2 output: (void 0) @ 0 stack: 2: 00000: 3f one output: 1 @ 1 stack: 1 2: 00001: 57 00 00 setlocal 0 inputs: 1 @ 1 output: (void 0) @ 0 stack: 2: 00005: 56 00 01 getlocal 1 output: b @ 1 stack: undefined 2: 00008: 51 pop inputs: b @ 1 stack: 3: 00009: d9 00 00 callgname "p" ....
  20. 52.

    4QJEFS.POLFZ7.
 ࣮ߦͷ࢓૊Έʢʣ1$ w ໋ྩ͸όΠτҎ্ͰɺՄม௕ɻ ྫ͑͹͔͚ࢉΛ͢ΔNVM໋ྩ͸ Y%ͷόΠτ w 1$ 1SPHSBN$PVOUFS Ϩδε

    λʔ͸ݱࡏ࣮ߦதͷʢ͜Ε͔Β ࣮ߦ͢ΔʣόΠτίʔυҐஔΛ ࢦ͢ w 1$͕ࢦ໋͢ྩΛ࣮ߦͯ͠1$Λ ࣍ʹਐΊΔɺ͕7.ͷجຊͷಈ͖ dd 01 int8 1 54 00 00 getarg 0 dd 03 int8 3 1d mul 1b add 05 return 1$ dd 01 int8 1 54 00 00 getarg 0 dd 03 int8 3 1d mul 1b add 05 return 1$ ஋ ஋ HFUBSHΛ࣮ߦ
  21. 53.

    4QJEFS.POLFZ7. ࣮ߦͷ࢓૊Έʢʣແ৚݅δϟϯϓ w ແ৚݅δϟϯϓ໋ྩ HPUP͸1$Λมߋ͢Δ w ʮHPUPʯ͸
 ʮ1$Λ ʯ͢ΔޮՌ ͕͋Δ

    .... 07 00 0f ifeq 15 d9 00 00 callgname 0 dd 07 int8 7 3a 00 01 call 1 51 pop 06 00 0c goto 12 d9 00 00 callgname 0 dd 09 int8 9 3a 00 01 call 1 51 pop c5 stop 1$ ஋ .... 07 00 0f ifeq 15 d9 00 00 callgname 0 dd 07 int8 7 3a 00 01 call 1 51 pop 06 00 0c goto 12 d9 00 00 callgname 0 dd 09 int8 9 3a 00 01 call 1 51 pop c5 stop 1$ ஋
  22. 54.

    4QJEFS.POLFZ7. ࣮ߦͷ࢓૊Έʢʣ৚݅෇͖δϟϯϓ w ʮJGFRʯ͸ʮελο Ϋઌ಄͕GBMTFͳΒ1$ Λ ʯ
 ͨͿΜɺʮUSVFͳΒͦͷ··࣍ͷ໋ྩʹߦ͖ɺGBMTFͳ Βඈ΂ʯͱ͍͏ҙਤͩͱࢥ͏͚Ͳʜʜɻ
 ͪͳΈʹJGFRͱ͍͏໊લͷͤ͘ʹ౳Ձൺֱ΋͠ͳ͍ɻ

    .... 07 00 0f ifeq 15 d9 00 00 callgname 0 dd 07 int8 7 3a 00 01 call 1 51 pop 06 00 0c goto 12 d9 00 00 callgname 0 dd 09 int8 9 3a 00 01 call 1 51 pop c5 stop 1$ ஋ .... 07 00 0f ifeq 15 d9 00 00 callgname 0 dd 07 int8 7 3a 00 01 call 1 51 pop 06 00 0c goto 12 d9 00 00 callgname 0 dd 09 int8 9 3a 00 01 call 1 51 pop c5 stop 1$ ஋
  23. 58.

    ՝୊ w ؔ਺ݺͼग़͠ʢ'VODUJPO$BMM/PEFʣΛ࣮૷ͤΑɻ ·ͣ͸άϩʔόϧؔ਺ͷΈαϙʔτ͠Α͏ɻ w KTGVODBMMKTΛಈ͔ͦ͏ɻ w Q͸ࣄલʹఆٛ͞Εͨؔ਺Ͱɺ3VCZͷQͱಉ͘͡ΦϒδΣΫτΛ͍͍ײ͡ʹ දࣔ͢Δಇ͖͕͋Δɻ w

    ώϯτ໋ྩͷγʔέϯε͸࣍ͷͱ͓Γɻ  DBMMHOBNFlؔ਺໊zˡؔ਺Λऔಘͯ͠ελοΫʹϓογϡ  ୈҾ਺ΛελοΫʹϓογϡ  ୈҾ਺ΛελοΫʹϓογϡ  ʜʜ  DBMMҾ਺ͷ਺
  24. 61.

    ՝୊ w ੔਺Ϧςϥϧ͕zzͷͱ͖͸JOU໋ྩͰ͸ͳ͘POF ໋ྩΛ࢖͏Α͏࠷దԽͤΑ w KTPQUPOFKT͕࠷దԽ͞ΕΔΑ͏ʹ͠Α͏ w l zԋࢉͷఆ਺৞ΈࠐΈΛ࣮૷ͤΑ w

    "45ϨϕϧͰॲཧ͢Δํ๏ͱɺόΠτίʔυϨϕϧͰॲཧ͢Δํ๏ͷͭ ͕ߟ͑ΒΕΔɻ"45ϨϕϧͰॲཧͯ͠͠·͏ͷָ͕ɻ w KTPQUBEEKTͰBEE໋ྩ͕ൃߦ͞Εͳ͍Α͏ʹ͠Α͏
  25. 62.

    ୅දతͳ࣮ߦ଎౓࠷దԽ w ໋ྩڧ౓ͷ௿ݮ w JOU EE ˠPOF G  w

    Y ˠY w ఆ਺ͷίϯύΠϧ࣌ܭࢉʢఆ਺৞ΈࠐΈʣ w QSJOU J    ˠQSJOU J   w ॲཧͷϧʔϓ֎΁ͷҠಈ w GPS WBSJJJ \QSJOU Y  J ^
 ˣ
 WBSOY 
 GPS WBSJJJ \QSJOU O J ^
  26. 63.

    ϓϩάϥϜͷҙຯͱ͸ w ϓϩάϥϜͷҙຯ͸มΘ͍ͬͯͳ͍Α͏ʹࢥ͑Δ w มΘ͍ͬͯͳ͍΋ͷʢ㲈ҙຯʣ w ࣜͷ஋ʢʣ w ϓϩάϥϜ֎ʹݟ͑ΔมԽʢʮʯͱදࣔ͞ΕΔʣ w

    มΘͬͨ΋ͷʢ㱠ҙຯʣ w ܭࢉաఔ w ϝϞϦ্ͷ஋ͷมߋύλʔϯ QSJOU    QSJOU   ࠷దԽͷͨΊ࣍ͷΑ͏ͳఆ਺৞ΈࠐΈΛͨ͠ͱ͢Δ ˞ͨͩ͠ݴޠʹΑΓʮҙຯʯ͸ҟͳΔ
  27. 71.

    ՝୊ w ҎԼͷΦϒδΣΫτࢦ޲ػೳΛ࣮૷ͤΑ w OFXࣜ w ϓϩύςΟͷಡΈॻ͖ w PCKQSPQܗ͚ࣜͩͰΑ͍ɻPCK<bQSPQ`>͸ෆཁʢจࣈྻϦςϥϧΛ࣮૷ ͍ͯ͠ͳ͍ͱҙຯͳ͍ͨΊʣ

    w ϝιουݺͼग़͠ w ϝιουఆٛ͸͢Ͱʹఆٛ͞Ε͍ͯΔؔ਺ΛϓϩύςΟʹηοτ͢Δ ͜ͱͰ୅ସ͢ΔʢԼهࢀরʣͷͰɺݺͼग़͚ͩ͠ͰΑ͍
 GVODUJPOPCK@N \SFUVSO^
 PCKNPCK@N