OSDC.TW - Gutscript for PHP haters

1,438 views
1,297 views

Published on

Published in: Technology

OSDC.TW - Gutscript for PHP haters

  1. 1. Gutscript A new language for PHP haters @c9s
  2. 2. PHP
  3. 3. 說到 PHP,你有什麼感覺?
  4. 4. PHP 哩來!哩來!來來來!來來來來來來 來來來來來來來來來來來來來來 來來來來來來來來來來來來來來 來來來來來來來來來來來來來來 來來來來來來來來來來來來來來 來來來來來來來來來來來來來來 來來來來來來來來來來來來來來
  5. 5. 《獨家直擊》來來哥的⼀一天【胖⻁虎黨PHP】
  6. 6. 先談 PHP
  7. 7. function foo() { 累贅的函數表⽰示法
  8. 8. namespace FooBar; 醜到歪七扭⼋八的 namespace
  9. 9. if (…) { } else { } foreach($list as $item) { 太多括號
  10. 10. array("key" => “value", "foo" => "bar"); [“key" => “value”, "foo" => “bar”]; 煩⼈人的 “=>”
  11. 11. array array_map ( callable $callback , array $array1 [, array $... ] ) array array_filter ( array $array = array() [, callable $callback = Function() ] ) 不⼀一致的函數原形 / 不直覺的呼叫⽅方式
  12. 12. 難道沒有優點嗎?
  13. 13. Dynamic
  14. 14. Easy To Deploy
  15. 15. Plenty Open Source Library
  16. 16. Easy To Learn & Development
  17. 17. 就算是升天的 Haskeller 也是可以被抓回來寫 PHP
  18. 18. 程式語⾔言設計的嘗試
  19. 19. ⼀一直找不到理由
  20. 20. CoffeeScript by Jeremy Ashkenas Facebook HipHop / HHVM
  21. 21. HHVM 改善了執⾏行效能
  22. 22. HHVM Benchmark & Compatibility http://www.hhvm.com/blog/2813/we-are-the-98-5-and-the-16
  23. 23. Requests per second, middle response The higher the better http://blog.liip.ch/archive/2013/10/29/hhvm-and-symfony2.html
  24. 24. PHP VM 改善了,但語 ⾔言還是⼀一樣爛
  25. 25. A new language beyond PHP
  26. 26. For Fun
  27. 27. 拋棄 PHP 的向後相容
  28. 28. 重新設計
  29. 29. The Language • Concise • Easy to learn, Easy to write, Readability • Brings benefits from Ruby and Perl
  30. 30. The Generated Code • Can reuse existing PHP libraries • Compatible with PHP 5.4 • Generate PHPDoc format comments automatically.
  31. 31. PHP Gutscript HHVM Zend VM HipHop Compiler C++ Compile to PHP and run on HHVM or ZendVM
  32. 32. Optimization PHP 5.4 PHP 5.3 Javascript PHP C Extension Gutscript Gutscript (Future)
  33. 33. Synopsis
  34. 34. class Person # Print the name say :: (name) -> "Hello #{name}, Good morning" ! getPhone :: -> "12345678" ! setName :: (string name) -> @name = name ! if str =~ /[a-z]/ say "matched!"
  35. 35. <?php class Person { /** * Print the name * * @param mixed $name */ function say($name) { return "Hello " . $name . ', Good morning'; } ! function getPhone() { return "12345678"; } ! /** * @param string $name */ function setName($name) { $this->name = $name; } } if ( preg_match('[a-z]',$str) ) { echo "matched!"; }
  36. 36. Expression
  37. 37. a = 3 + 5 b = 3.1415 c = "Hello" ++ "World"
  38. 38. a = 3 + 5 b = 3.1415 c = "Hello" ++ "World" <?php $a = 3 + 5; $b = 3.1415; $c = "Hello" . "World"
  39. 39. Control Flow
  40. 40. say "Hello" if a > 10
  41. 41. say "Hello" if a > 10 <?php if ( $a > 10 ) { echo “Hello"; }
  42. 42. say i for i in [ 1..10 ]
  43. 43. say i for i in [ 1..10 ] <?php for ( $i = 1; $i < 10 ; $i++ ) { echo $i; }
  44. 44. Function
  45. 45. area :: (x,y) -> x * y
  46. 46. area :: (x,y) -> x * y <?php function area($x, $y) { return $x * $y; }
  47. 47. Class
  48. 48. class Person getName :: () -> "John" getPhone :: () -> "12345678"
  49. 49. class Person getName :: () -> "John" getPhone :: () -> "12345678" <?php class Person { function getName() { return "John"; } function getPhone() { return "12345678"; } }
  50. 50. Class Inheritance
  51. 51. Bring ideas from Perl 6
  52. 52. class Person is Object does ArrayIterator getName :: () -> "name"
  53. 53. class Person is Object does ArrayIterator getName :: () -> "name" <?php class Person extends Object implements ArrayIterator { function getName() { return "name"; } }
  54. 54. Regular Expression
  55. 55. say $1 if foo =~ /([a-z])+/ <?php if ( preg_match('/([a-z])+/', $foo, $regs) ) { echo $regs[1]; }
  56. 56. Map & Grep
  57. 57. phones = map (x) -> { x.phone } contacts <?php $phones = array_map(function($x) { return $x->phone; }, $contacts);
  58. 58. sort (a,b) -> { a <=> b } list <?php function __sort1($a, $b) { if ( $a == $b ) { return 0; } return ( $a < $b ) ? -1 : 1; } sort($list, “__sort1”);
  59. 59. Auto-generated PHPDoc
  60. 60. # method description # @param name contact’s name setName :: (string name) -> @name = name <?php /** * method description * @param string $name contact’s name */ function setName($name) { $this->name = $name; }
  61. 61. Optimization
  62. 62. for Fun
  63. 63. Inline Expansion
  64. 64. Function calls are pretty slow in PHP
  65. 65. Refactoring produces more functions
  66. 66. function foo($a, $b) { return $a + $b; } $ret = foo(1,2) + foo(3,4);
  67. 67. $ret = (1+2) + (3+4);
  68. 68. Dead Code Elimination
  69. 69. function foo($a, $b) { return $a + $b; } function bar() { … } $ret = foo(1,2) + foo(3,4);
  70. 70. function foo($a, $b) { return $a + $b; } $ret = foo(1,2) + foo(3,4);
  71. 71. Useful when using only small part functions of large libraries.
  72. 72. Constant Folding
  73. 73. $ret = (1+2) + (3+4);
  74. 74. $ret = 10;
  75. 75. Code Minifying
  76. 76. compress all PHP source files => phar
  77. 77. Implementation
  78. 78. Implemented in Go
  79. 79. pre-compiled and no dependency
  80. 80. pre-compiled
  81. 81. static linking
  82. 82. No dependency
  83. 83. Concurrency Support
  84. 84. • hand-written lexer in Go • pretty simple go yacc - LALR parser derived from Inferno's utils/iyacc/yacc.c
  85. 85. Parsing with concurrency • Using Go channel to send tokens from Tokenizer • Parser reads tokens from Go channel asynchronously.
  86. 86. Tokenizer Parser Async token through channel
  87. 87. ParserTokenizer ASTFile File File File File Go Channel ParserTokenizer AST Worker (Go Routine) Worker (Go Routine) ....... ....... Result
  88. 88. Setup
  89. 89. # fork this project…
  90. 90. # fork this project… $ git clone git@github.com:you/gutscript.git
  91. 91. # fork this project… $ git clone git@github.com:you/gutscript.git $ cd gutscript
  92. 92. # fork this project… $ git clone git@github.com:you/gutscript.git $ cd gutscript $ source goenv
  93. 93. # fork this project… $ git clone git@github.com:you/gutscript.git $ cd gutscript $ source goenv $ make
  94. 94. Join Us! https://github.com/c9s/gutscript

×