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

Go でインタプリタを 書いてみよう

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Kenshi Kamata
November 05, 2017

Go でインタプリタを 書いてみよう

Go Conference 2017 Autumn Lightning Talk

Kenshi Kamata

November 05, 2017

Transcript

14 slides
  1. 自己紹介 • 鎌田 健史 • @knsh14 (twitter, GitHub) • KLab

    株式会社 ◦ Unity のエディタ拡張書いたり ◦ JavaScript でゲーム書いたり • 技術書典ではバイトしてました
  2. なにをしたのか • Writing An Interpreter In Go 読んだ ◦ https://interpreterbook.com/

    • 実際にインタプリタ作ってみた • 約1400行 でインタプリタを書こう! ◦ テストコード込だと2500行くらい
  3. DEMO let x = 0; let array = [0, 10,

    20, 30, 40]; array[1]; let fib = fn(x) { if(x == 0) { 0; } else { if (x == 1) { 1;} else { fib(x-1) + fib(x-2);}}}; let loop = fn(i, max, f) { if(i == max){return 0;} f(); loop(i +1, max, f);}; let p = fn() { puts(“hello world”);};
  4. 言語の仕様 • Integer, Boolean, String, Function • Array, Hash •

    if 文が使える • Lexer、Parser を yacc などの既存の ツールを使わずに実装する
  5. Go のプラクティスに従わない • なるべくすぐにエラーを返す / Error 型も同時に返す をやらない事がある • Monkey

    コードだとこの辺 • 実は go/parser でも似たような仕組みになってる • https://golang.org/src/go/parser/parser.go “p.error” で検索かけると引っかかる

More Decks by Kenshi Kamata

Other Decks in Programming