Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Usage and implementation of Reish which is an Unix shell for Rubyist

Usage and implementation of Reish which is an Unix shell for Rubyist

Reish is an Unix shell for Rubyist. It was a language that was realized Ruby in the syntax of the shell. I will introduce usage and implementation of Reish.

Keiju Ishitsuka

September 22, 2017
Tweet

More Decks by Keiju Ishitsuka

Other Decks in Technology

Transcript

  1. 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では結構常連 自己紹介 祝!! メインステージ登壇
  2. Usage and Implementation of Reish which an Unix shell for

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

    rubyist # #4 4 RubyKaigi2015 はじめに Introduction • Reishを開発することになったきっかけ – RubyKaigi2013で発表した RubyArchaeology(Ruby考古学)で過去文献を 漁っていて • その発表時に、こんな物できちゃいましたと発表したく なり、開発をはじめたがほとんど進まずじまい – RubyKaigi2013での松本さんの発表で、ちょっ とモチベーションが上がり、再度開発をはじめた が、あまり進まず – RubyKaigi2014の話になって、やる気が出てき て、前後半年ぐらい開発。 – さらに、RubyKaigi2015の話がやっと出てきて 、再々々度頑張っているところ
  4. 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の機能が使えると便利
  5. 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
  6. 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]
  7. 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}
  8. 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の実行結果
  9. 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 修飾子
  10. 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
  11. 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?, ... – 演算系 ||, &&, <, <=, +, -, ...
  12. Usage and Implementation of Reish which an Unix shell for

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

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

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

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

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

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

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

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

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

    rubyist # #22 22 RubyKaigi2015 構文木 - SimpleCommand Syntax Tree • SimpleCommand - コマンド
  22. 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()
  23. Usage and Implementation of Reish which an Unix shell for

    rubyist # #24 24 RubyKaigi2015 実行 - レシーバ Execution - Receiver • 実行にはメソッドを受け付けるレシー バが必要 – Reish::Main – Reish::ExEnv • 実行環境 • シェルオプション
  24. 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のメソッドの方が優先度が高いことになる。 – 引数について • そのままオブジェクトとして渡す
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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 いまいち
  30. 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
  31. 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 ...
  32. 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で実行結果を取り出し、そ の結果を端末へ出力し、実行ステータスを戻り値 とする
  33. 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
  34. Usage and Implementation of Reish which an Unix shell for

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

    rubyist # #36 36 RubyKaigi2015 今後の予定 • 前回のRubyKaigi2014では: – 完成版の紹介はRubyKaigi2015で!! • とお話ししましたが、今回も – 完成版の紹介はRubyKaigi2016で!!(^^;