Slide 1

Slide 1 text

石塚 圭樹 [email protected] Keiju Ishitsuka ペンタアドバンストラボラトリーズ 研究所長 Penta Advanced Laboratories, Inc. Usage and implemantation of Reish which is an Unix shell for rubyist

Slide 2

Slide 2 text

Usage and Implementation of Reish which an Unix shell for rubyist # #2 2 RubyKaigi2015 • 石塚圭樹 Keiju Ishitsuka – MLでは、けいじゅ@いしつか ででていま す。 といっても、最近ご無沙汰していますが… • Rubyの名付け親 Ruby's godfather • Irbの作者 Irb's author • RubyKaigiでは結構常連 自己紹介 祝!! メインステージ登壇

Slide 3

Slide 3 text

Usage and Implementation of Reish which an Unix shell for rubyist # #3 3 RubyKaigi2015 自己紹介 - 現在 • ペンタ アドバンスト ラボラトリーズ において – OO系システムの研究開発(Rubyを含む) – OO関連技術の導入コンサルティング – などなど

Slide 4

Slide 4 text

Usage and Implementation of Reish which an Unix shell for rubyist # #4 4 RubyKaigi2015 はじめに Introduction • Reishを開発することになったきっかけ – RubyKaigi2013で発表した RubyArchaeology(Ruby考古学)で過去文献を 漁っていて • その発表時に、こんな物できちゃいましたと発表したく なり、開発をはじめたがほとんど進まずじまい – RubyKaigi2013での松本さんの発表で、ちょっ とモチベーションが上がり、再度開発をはじめた が、あまり進まず – RubyKaigi2014の話になって、やる気が出てき て、前後半年ぐらい開発。 – さらに、RubyKaigi2015の話がやっと出てきて 、再々々度頑張っているところ

Slide 5

Slide 5 text

Usage and Implementation of Reish which an Unix shell for rubyist # #5 5 RubyKaigi2015 なぜ、RubyistのためのShellか? Why, Shell for Rubyist? • bashとかいろいろなshellがあるじゃ ん? • Rubyistは、shellを使うにしても当然: – Enumerable系メソッドが使いたい – イテレータ(iterator)を使いたい – やっぱ、オブジェクト指向じゃなくちゃ • いろいろなクラスの利用 – そもそも、Rubyの機能が使えると便利

Slide 6

Slide 6 text

Usage and Implementation of Reish which an Unix shell for rubyist # #6 6 RubyKaigi2015 名前の由来 Origin of name • やはり一文字目は'R'にしたいのが人情 – 一番最初に思いつく rsh は当然NG • shell系言語では人の名前が入っている ことが多いので Reish となった。 – Reish is not Reish's shell. – Reish is not 冷酒. – Reish – 発音は、[reiʃ] → れいしゅ → けいじゅ → Keiju

Slide 7

Slide 7 text

Usage and Implementation of Reish which an Unix shell for rubyist # #7 7 RubyKaigi2015 Reishの文法 Syntax of Reish • Simple Command – command_header arg1 arg2 ... • パイプライン式(pipeline expression) – command1 | command2 ... • 論理式(logical expression) – pipeline1 OP pipeline2 ... – OP: &&, || • command sequence – logical_exp1 [;&\n] logical_exp2 ... [;&\n]

Slide 8

Slide 8 text

Usage and Implementation of Reish which an Unix shell for rubyist # #8 8 RubyKaigi2015 Reishの文法 - SimpleCommand Syntax of Reish - SimpleCommand • Simple Command – command_header arg1 arg2 ... • command headerになれるもの – 英数字か'/'が含まれる文字列 • Commandの引数(command argument)になれる もの – 変数参照(variable reference) – リテラル(iteral) – グループ(group) • SimpleCommandはイテレータ(iterator)にできる – cat /etc/passwd do |e| puts e end – ls{|e| puts e}

Slide 9

Slide 9 text

Usage and Implementation of Reish which an Unix shell for rubyist # #9 9 RubyKaigi2015 Reishの文法 - コマンド引数 Syntax of Reish - Command Argument • 文字列(string)、ワイルドカード(wild card) – UnquotedString: ダブルクォート等でくくられていない文 字列も文字列になる。 • 変数参照(variable reference) – $local_var, $$gloval_var, $@inst_var, $@@class_var • リテラル(literal) – 文字列、数値(Number, Integer, FID) – array: %[e1 e2 ...], $[e1 e2 ...] – hash: %{k1 => v1 k2 => v2...}, ${k1 => v1 k2 => v2...} – 正規表現(regular exp): %/.../, $/.../ – RubyExp: $(ruby文) • グループ(group) – (command_1[;\n] command_2; ...l; command_n) – かっこ内を実行し、最後のcommand_nの実行結果

Slide 10

Slide 10 text

Usage and Implementation of Reish which an Unix shell for rubyist # #10 10 RubyKaigi2015 Reishの文法 - 制御コマンド Syntax of Reish - Control Command • begin command – begin/rescue/else/ensure/end • if command – if logical_exp [then;\n] sequence else sequence end • while command – while logical_exp [do;\n] sequence end • その他、制御コマンド(control command) – for, case/when, unless, break, next, redo, retry, raise, return, yield • 修飾子コマンド(modifier command) – if修飾子、unless修飾子, while修飾子, until修飾子, rescue 修飾子

Slide 11

Slide 11 text

Usage and Implementation of Reish which an Unix shell for rubyist # #11 11 RubyKaigi2015 Reishの文法 - Commandその他。 Syntax of Reish - Command etc. • リテラルコマンド(literal command) – 文字列(string)、数値(number) – 配列(array) • %[e1 e2 ...]、 $[e1 e2 ...]、 [e1 e2 ...] – ハッシュ(hash) • %{k1 => v1 k2 => v2...}、 ${k1 => v1 k2 => v2...}、 {k1 => v1 k2 => v2...} – 正規表現(regular expression) • %/.../, $/.../ – RubyExp • $(ruby文) • $begin ... end

Slide 12

Slide 12 text

Usage and Implementation of Reish which an Unix shell for rubyist # #12 12 RubyKaigi2015 Reishの文法 - Commandその他。 Syntax of Reish - Command etc. • 代入コマンド(assgin command) – var = command_element – var[idx] = command_element • グループコマンド(group command) – 引数に使われるグループと同じ表現、こちらはあ くまでもコマンド – (command1; command2; ... command_n) • Special command – ファイルテスト系 -e, -f, -d,..., --exists?, ... – 演算系 ||, &&, <, <=, +, -, ...

Slide 13

Slide 13 text

Usage and Implementation of Reish which an Unix shell for rubyist # #13 13 RubyKaigi2015 Reishのアーキテクチャ - 論理ビュー Architecture of Reish - Logical View • 主なクラス

Slide 14

Slide 14 text

Usage and Implementation of Reish which an Unix shell for rubyist # #14 14 RubyKaigi2015 Reishのアーキテクチャ - プロセスビュー Architecture of Reish - Process View • プロセス図

Slide 15

Slide 15 text

Usage and Implementation of Reish which an Unix shell for rubyist # #15 15 RubyKaigi2015 Reishのアーキテクチャ - 論理ビュー Architecture of Reish - Logical View • メインループ

Slide 16

Slide 16 text

Usage and Implementation of Reish which an Unix shell for rubyist # #16 16 RubyKaigi2015 構文木 - 全体図 Syntax Tree - Whole Diag.

Slide 17

Slide 17 text

Usage and Implementation of Reish which an Unix shell for rubyist # #17 17 RubyKaigi2015 構文木 - InputUnit Syntax Tree • InputUnit - Reishの入力単位

Slide 18

Slide 18 text

Usage and Implementation of Reish which an Unix shell for rubyist # #18 18 RubyKaigi2015 構文木 - Command Syntax Tree • Command - コマンド相当

Slide 19

Slide 19 text

Usage and Implementation of Reish which an Unix shell for rubyist # #19 19 RubyKaigi2015 構文木 - Sequence Syntax Tree • Sequence - コマンド列

Slide 20

Slide 20 text

Usage and Implementation of Reish which an Unix shell for rubyist # #20 20 RubyKaigi2015 構文木 - LogicalCommand Syntax Tree • LogicalCommand - 論理コマンド

Slide 21

Slide 21 text

Usage and Implementation of Reish which an Unix shell for rubyist # #21 21 RubyKaigi2015 構文木 - PipeLineCommand Syntax Tree • PipeLineCommand - パイプライン

Slide 22

Slide 22 text

Usage and Implementation of Reish which an Unix shell for rubyist # #22 22 RubyKaigi2015 構文木 - SimpleCommand Syntax Tree • SimpleCommand - コマンド

Slide 23

Slide 23 text

Usage and Implementation of Reish which an Unix shell for rubyist # #23 23 RubyKaigi2015 コード生成 Code Generation • 構文木を上から下にたどりながら、再帰的にコード を生成して行く – SimpleCommand • そのままRubyのメソッドに変換 – ls -l → ls("-l") • 例外、パス付きコマンド (path command) – /command – 後述のSystemCommandに直接変換 » /bin/grep → SystemCommand("/bin/grep") » /grep → SystemCommand("grep") – PipeLineCommand • メッセージセンドに変換 – ls | wc → ls().wc() – Sequence – ls ; ls → ls(); ls()

Slide 24

Slide 24 text

Usage and Implementation of Reish which an Unix shell for rubyist # #24 24 RubyKaigi2015 実行 - レシーバ Execution - Receiver • 実行にはメソッドを受け付けるレシー バが必要 – Reish::Main – Reish::ExEnv • 実行環境 • シェルオプション

Slide 25

Slide 25 text

Usage and Implementation of Reish which an Unix shell for rubyist # #25 25 RubyKaigi2015 実行 - Simple Command Execution - SimpleCommand • simple commandの実行 – 常に、Rubyのmethodとして評価(eval) • メソッドが存在しないとき(MethidMissing) – PATHをたどってunix commandを検索、存在する ときには、それに対応するSystemCommandオブ ジェクトを返す – Rubyのメソッドの方が優先度が高いことになる。 – 引数について • そのままオブジェクトとして渡す

Slide 26

Slide 26 text

Usage and Implementation of Reish which an Unix shell for rubyist # #26 26 RubyKaigi2015 実行 - SystemCommand Execution - SystemCommand • SystemCommand – Unix Commandを表し、Enumerable • ls -l → SystemCommand("ls", "-l") – 引数について • 適当に文字列に変換して渡す – ワイルドカード(wildcard) – 配列(array) • オプション化 – シンボル(symbol) » :sym → --sym – ハッシュ(hash) » {:key -> value} → --key=value

Slide 27

Slide 27 text

Usage and Implementation of Reish which an Unix shell for rubyist # #27 27 RubyKaigi2015 実行 - SystemCommand 続き Execution - SystemCommand cont. • 外部コマンドの標準入力(stdin) – 上流オブジェクトに対しeachメソッドを呼び出し、その結 果を送る • 外部コマンドの実行結果、標準出力(stdout) – 下流からeachメソッドが呼び出された時点で、対応する外 部コマンドの標準出力に出された実行結果の一行づつを eachブロックに渡す 下流 /grep 上流 each grep pipe out pipe in each gets puts stdin stdout

Slide 28

Slide 28 text

Usage and Implementation of Reish which an Unix shell for rubyist # #28 28 RubyKaigi2015 実行 - Pipeline Command Exection - Pipeline Command • パイプライン(|)はメッセージセンドと して実行される – upper | collect | select | down • Ruby: upper.collect.select.down down select collect each each upper each

Slide 29

Slide 29 text

Usage and Implementation of Reish which an Unix shell for rubyist # #29 29 RubyKaigi2015 実行 - Pipeline Command Exection - Pipeline Command • パイプライン(|)はメッセージセンドと して実行される – SystemCommand混在の場合 – upper | collect | /grep | down down /grep collect each grep pipe out pipe in each gets puts stdin stdout upper each

Slide 30

Slide 30 text

Usage and Implementation of Reish which an Unix shell for rubyist # #30 30 RubyKaigi2015 実行 - Pipeline Command 続き Exection - Pipeline Command cont. • SystemCommandが複数つながった 場合 – upper | cat | /grep | down down /grep cat each grep pipe out pipe in each get s puts stdin stdout upper cat pipe out pipe in stdin stdout each いまいち

Slide 31

Slide 31 text

Usage and Implementation of Reish which an Unix shell for rubyist # #31 31 RubyKaigi2015 CompSystemCommand 実行 - Pipeline Command 続き Exection - Pipeline Command cont. • SystemCommandが複数つながった 場合 – upper | cat | /grep | down down each grep pipe in gets puts stdin stdout upper cat pipe out stdin stdout each

Slide 32

Slide 32 text

Usage and Implementation of Reish which an Unix shell for rubyist # #32 32 RubyKaigi2015 実行 - コマンドの出力 Exection - Command Outputs • コマンドの出力先 – 端末 – 標準出力 – 次のコマンド • 実行結果ではなく実行ステータスがほしい場 合もある • 話はそれほど単純でもない – ls -l – ls -l; ls -l – (ls -l; ls -l) | grep ... – if /grep ...

Slide 33

Slide 33 text

Usage and Implementation of Reish which an Unix shell for rubyist # #33 33 RubyKaigi2015 実行 - コマンドの出力 Exection - Command Output • ls -l → SystemCommand("ls", "-l") – これだと、 SystemCommandオブジェクトが返 るだけで、実際の"ls"の実行結果が返るわけでは ない • 端末に出力するときには、reish_termを付加 したコードを生成する – ls("-l").reish_term – reish_termは、eachで実行結果を取り出し、そ の結果を端末へ出力し、実行ステータスを戻り値 とする

Slide 34

Slide 34 text

Usage and Implementation of Reish which an Unix shell for rubyist # #34 34 RubyKaigi2015 コード生成再論 Code Generation again • ls -l → ls("-l").reish_term • ls -l; ls -l → ls("-ls").reish_term; ls("-l").reish_term • (ls -l; ls -l) | grep ... → (ls("-l").reish_result; ls("-l").reish_term).grep ... • if /grep ... → if (grep(...).reish_status) • ls -l > /tmp/foo → reish_send_with_redirection('ls', ["-l"], [Reish::Redirect(-1, '>', "/tmp/foo")]).reish_term

Slide 35

Slide 35 text

Usage and Implementation of Reish which an Unix shell for rubyist # #35 35 RubyKaigi2015 考えなくちゃいけないこと • パーザー(parser) – メソッド定義、クラス定義、多重代入… • ジョブ制御(job control) – command& – ^Z • ユーザビリティ(usabiliry)に関するもの – コンプレーション(completion)とか • エラー処理(error processing) – 面倒だけど… • というか、まだまだ出来ていません。

Slide 36

Slide 36 text

Usage and Implementation of Reish which an Unix shell for rubyist # #36 36 RubyKaigi2015 今後の予定 • 前回のRubyKaigi2014では: – 完成版の紹介はRubyKaigi2015で!! • とお話ししましたが、今回も – 完成版の紹介はRubyKaigi2016で!!(^^;