26
Making DSL with [] @yuya_takeyama

Making DSL with []

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 2: Making DSL with []

自己紹介•@yuya_takeyama

•LAMP でお仕事

•メタラー (Black Sabbath, 聖飢魔II, Eyehategod)•Openpear でライブラリ公開してます•\Text\Ngram•HTTP_Parallel•Cache_Casual

Page 3: Making DSL with []

DSL?

Page 4: Making DSL with []

DSLDomain (ドメイン)

Specific (特化)

Language (言語)

Page 5: Making DSL with []

特定の問題領域に注目し

限られた表現を持つ言語 Domain Specific

Languages(Martin Fowler)

Page 6: Making DSL with []

DSLの例

•SQL (データベース操作)•正規表現(パターンマッチ)•Rake (ビルドの自動化)

Page 7: Making DSL with []

PHP だとどうなる?

Page 8: Making DSL with []

DSL としては構文が冗長で不向き ※個人の感想です

Page 9: Making DSL with []

PHP5.3ェ...

Page 10: Making DSL with []

PHP5.4 alpha3

Page 11: Making DSL with []

[]

Page 12: Making DSL with []

作ってみた

Page 13: Making DSL with []

•テンプレートエンジン•PHP array markup Language

•[] (array) で HTML が書ける

•PHP 5.4 でしか動かない•元ネタは Clojure の Hiccup

Pamlhttps://github.com/yuya-takeyama/php-HTML_Paml

Page 14: Making DSL with []

デモ

Page 15: Making DSL with []

[] (PHP) vs JSON

•JSON なら PHP 5.4 未満でも使える

•JSON なら他の言語でも使える

Page 16: Making DSL with []

配列とハッシュの区別が無い

PHP には...

Page 17: Making DSL with []

[ "foo", "bar", "hoge" => "piyo"]

Page 18: Making DSL with []

array(3) { [0]=> string(3) "foo" [1]=> string(3) "bar" ["hoge"]=> string(4) "piyo"}

Page 19: Making DSL with []

PHP の 配列 にはキーワード引数のような柔軟さがある

Page 20: Making DSL with []

フレームワークへの応用

Page 21: Making DSL with []

public $id = array( 'type' => 'int', 'primary' => true, 'serial' => true);public $name = array( 'type' =>'string', 'required' => true, 'unique' => true);public $birthday = array( 'type' => 'date');

モデル定義

Page 22: Making DSL with []

public $id = [ 'int', 'primary' => true, 'serial' => true];public $name = [ 'string', 'required' => true, 'unique' => true];public $birthday = ['date'];

モデル定義

Page 23: Making DSL with []

まとめ

•PHP 5.4 では [] で配列が作れる•[] で DSL っぽさを演出しよう•[] の導入に関わった皆様に感謝を

Page 24: Making DSL with []

おまけ

Page 25: Making DSL with []

['begin', ['define', 'fib', ['lambda', ['x'], ['if', ['<', ':x', 2], ':x', ['+', ['fib', ['-', ':x', 2]], ['fib', ['-', ':x', 1]]]]]], ['print', 'fib(10) = '], ['println', ['fib', 10]]] => fib(10) = 55

LisPHPhttps://github.com/yuya-takeyama/LisPHP

Page 26: Making DSL with []

ご清聴ありがとうございました