$30 off During Our Annual Pro Sale. View Details »

PHP で AST 解析をして Java のオペコードを生成する

memory
PRO
November 22, 2019

PHP で AST 解析をして Java のオペコードを生成する

- Kichijoji.pm in Japan

English Title:
Generates Operation Codes of Java on performed an Abstract Syntax Tree from PHP code.

memory
PRO

November 22, 2019
Tweet

More Decks by memory

Other Decks in Programming

Transcript

  1. 2019/11/22 .pm
    PHP AST Java
    © - BASE, Inc.

    View Slide

  2. © - BASE, Inc.
    Who am I?
    (@m m r )
    BASE
    GameWith
    PHP, TypeScript

    View Slide

  3. © - BASE, Inc.
    Software Design

    View Slide

  4. © - BASE, Inc.
    PHP

    View Slide

  5. © - BASE, Inc.

    View Slide

  6. © - BASE, Inc.
    PHP JVM

    View Slide

  7. © - BASE, Inc.
    PHP JVM 


    View Slide

  8. © - BASE, Inc.

    View Slide

  9. © - BASE, Inc.
    PHP AST
    AST
    PHP AST
    Java

    View Slide

  10. © - BASE, Inc.
    + DEMO

    View Slide

  11. © - BASE, Inc.
    AST

    View Slide

  12. © - BASE, Inc.
    AST
    Abstract Syntax Tree,
    + -

    View Slide

  13. © - BASE, Inc.
    AST
    1 + 2 + 3 AST
    +
    +

    View Slide

  14. © - BASE, Inc.
    AST
    +
    +
    PlusOp
    PlusOp::left PlusOp::right
    Number
    Number
    Number
    PlusOp

    View Slide

  15. © - BASE, Inc.
    AST
    +
    PlusOp
    PlusOp::left PlusOp::right
    Number
    Number

    View Slide

  16. © - BASE, Inc.
    AST
    Number

    View Slide

  17. © - BASE, Inc.

    View Slide

  18. © - BASE, Inc.
    (PHP, Perl,
    Python, etc)
    Lexical Analyzer ( ,
    ) Lexer

    View Slide

  19. © - BASE, Inc.
    1 + 2 + 3
    Value Label Means
    Number
    Whitespace
    + Plus
    Whitespace
    Number
    Whitespace
    + Plus
    Whitespace
    Number

    View Slide

  20. © - BASE, Inc.
    PHP
    Value Label Means
    T_LNUMBER
    T_WHITESPACE
    + - ( )
    Whitespace
    T_LNUMBER
    T_WHITESPACE
    + - ( )
    T_WHITESPACE
    T_LNUMBER

    View Slide

  21. © - BASE, Inc.
    PHP AST

    View Slide

  22. © - BASE, Inc.
    PHP AST

    View Slide

  23. © - BASE, Inc.
    PHP AST
    AST PHP
    AST

    View Slide

  24. © - BASE, Inc.
    PHP AST
    AST PHP
    AST

    View Slide

  25. © - BASE, Inc.

    View Slide

  26. © - BASE, Inc.

    AST

    View Slide

  27. © - BASE, Inc.
    composer require nikic/php-parser

    View Slide

  28. © - BASE, Inc.
    PHP AST

    View Slide

  29. © - BASE, Inc.
    PHP AST

    View Slide

  30. © - BASE, Inc.
    PHP AST
    T_ECHO T_LNUMBER
    T_LNUMBER
    T_LNUMBER

    View Slide

  31. © - BASE, Inc.
    PHP AST
    ZEND_AST_BINARY_OP
    ZEND_AST_BINARY_OP

    View Slide

  32. © - BASE, Inc.
    PHP AST
    Echo_
    (Stmt)
    Plus
    (BinaryOp)
    Plus
    (BinaryOp) (LNumber)
    (LNumber) (LNumber)
    left right
    left right
    expressions ( ) (*1)
    (* ) PHP echo , print ,

    View Slide

  33. © - BASE, Inc.
    PHP AST
    Echo_
    (Stmt)
    Plus
    (BinaryOp)
    Plus
    (BinaryOp) (LNumber)
    (LNumber) (LNumber)
    left right
    left right
    expressions ( ) (*1)
    (* ) PHP echo , print ,

    View Slide

  34. © - BASE, Inc.

    View Slide

  35. © - BASE, Inc.

    View Slide

  36. © - BASE, Inc.
    PHP AST Java

    View Slide

  37. © - BASE, Inc.
    Java

    View Slide

  38. © - BASE, Inc.

    View Slide

  39. © - BASE, Inc.

    View Slide

  40. © - BASE, Inc.

    View Slide

  41. © - BASE, Inc.
    Java 1 + 2 + 3

    View Slide

  42. © - BASE, Inc.
    Java 1 + 2 + 3
    1
    2
    2
    (3 )
    2
    (6 )
    3
    iconst_
    iconst_
    iadd
    iconst_
    iadd

    View Slide

  43. © - BASE, Inc.
    PHP

    View Slide

  44. © - BASE, Inc.
    PHP Java
    PHP AST Java
    PHP echo System.out.println

    View Slide

  45. © - BASE, Inc.
    PHP Java
    Plus
    (BinaryOp)
    Plus
    (BinaryOp) (LNumber)
    (LNumber) (LNumber)
    left right
    left right
    PHP AST

    View Slide

  46. © - BASE, Inc.
    PHP Java
    Plus
    (BinaryOp)
    Plus
    (BinaryOp) (LNumber)
    left right
    left right
    iconst_ iconst_

    View Slide

  47. © - BASE, Inc.
    PHP Java
    Plus
    (BinaryOp)
    (LNumber)
    left right
    iconst_ iconst_
    iadd
    left right

    View Slide

  48. © - BASE, Inc.
    PHP Java
    Plus
    (BinaryOp)
    left right
    iconst_ iconst_
    iadd
    left right
    iconst_

    View Slide

  49. © - BASE, Inc.
    PHP Java
    iconst_ iconst_
    iadd
    left right
    iconst_
    iadd
    left right

    View Slide

  50. © - BASE, Inc.
    PHP Java

    Java
    iconst_
    iconst_
    iadd
    iconst_
    iadd

    View Slide

  51. © - BASE, Inc.
    PHP Java

    View Slide

  52. © - BASE, Inc.
    DEMO

    View Slide

  53. © - BASE, Inc.
    php-java
    Java PHP
    Java PHP
    https://github.com/php-java/php-java

    View Slide

  54. © - BASE, Inc.

    View Slide

  55. © - BASE, Inc.

    View Slide

  56. © - BASE, Inc.

    View Slide

  57. © - BASE, Inc.
    PHP Java (^o^)

    View Slide

  58. © - BASE, Inc.

    View Slide