Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Go でインタプリタを 書いてみよう
Search
Kenshi Kamata
November 05, 2017
Programming
3.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Go でインタプリタを 書いてみよう
Go Conference 2017 Autumn Lightning Talk
Kenshi Kamata
November 05, 2017
More Decks by Kenshi Kamata
See All by Kenshi Kamata
500万ユーザーを支える残高の冪等性 / The idempotency of the balance for 5 million Merpay users
knsh14
0
3k
チャネルの仕組み
knsh14
6
5.6k
Go1.10 strings.Builder の紹介
knsh14
2
1.5k
Let’s Create An Interpreter In Go
knsh14
0
170
Go Code Review Comment を翻訳した話
knsh14
0
7.7k
tvOS Leaderboard
knsh14
0
1.2k
Other Decks in Programming
See All in Programming
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
300
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
210
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
150
片田舎のおっさん、 Swift Buildのダイアモンド問題解決の不具合修正PRを出すが、解決方法がキャッシュをしないようにすることであり、ビルド時間が伸びると言われてマージされないので高速化もする/swiftbuild
yimajo
0
280
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
380
不幸な GC
chencmd
0
740
プロポーザルを書いてもらう
pvcresin
0
580
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
1.1k
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
490
Introducing Stack Pull Request in GitHub
kkamegawa
0
100
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
7.8k
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
620
Featured
See All Featured
Building Adaptive Systems
keathley
44
3.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Ethics towards AI in product and experience design
skipperchong
2
350
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
630
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
670
Leo the Paperboy
mayatellez
8
2.2k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
820
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.1k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
740
Abbi's Birthday
coloredviolet
3
9.5k
Transcript
Go でインタプリタを 書いてみよう Go Conference 2017 Autumn
自己紹介 • 鎌田 健史 • @knsh14 (twitter, GitHub) • KLab
株式会社 ◦ Unity のエディタ拡張書いたり ◦ JavaScript でゲーム書いたり • 技術書典ではバイトしてました
なにをしたのか • Writing An Interpreter In Go 読んだ ◦ https://interpreterbook.com/
• 実際にインタプリタ作ってみた • 約1400行 でインタプリタを書こう! ◦ テストコード込だと2500行くらい
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”);};
なんでやってみたかのか • 人生で一度はオレオレ言語作ってみたい! • 昔 C でインタプリタを書いてみる本は辛くて挫折した • Go で良さそうな本出たらしいので試すぞ!
言語の仕様 • Integer, Boolean, String, Function • Array, Hash •
if 文が使える • Lexer、Parser を yacc などの既存の ツールを使わずに実装する
かかった期間 • 大体1ヶ月くらい • 早い人だと1週間くらいあればできる(らしい?) • 遅い人でも1〜2ヶ月あればできる
ちょい難しかったところ • やっぱり Parser の仕組みは難しい • Pratt Parser と呼ばれる種類のパーサー •
日本語だと演算子順位法とかでググるとわかりやす い
なにが学べたのか • 自作言語楽しい! • Parser の章で(少しだけ) AST の気持ちになれた • Go
のプラクティスとは真逆のことをすることもある
AST の気持ちになれる • Parser の章では最終的にASTを構築する • その昔 AST を使ってごにょごにょするツールを作っ たけど当時はASTなんて理解せずに扱ってた
• 今書いたらもう少しいい感じに書けると思う
Go のプラクティスに従わない • なるべくすぐにエラーを返す / Error 型も同時に返す をやらない事がある • Monkey
コードだとこの辺 • 実は go/parser でも似たような仕組みになってる • https://golang.org/src/go/parser/parser.go “p.error” で検索かけると引っかかる
Go のプラクティスに従わない • 処理を止めたくない • 一旦最後まで読んでから間違っているところを吐き出 したほうがユーザに優しい • エラーリストにどんどん append
しているので、ハンド リングする必要がない
最後に • REPL を起動してコード書いてみるとめっちゃ達成感 がある ◦ ゲーム作るのとはまた違う感覚 • 普段あまり洋書を読まない方にもおすすめ ◦
英語がそんなに難しくない ◦ 最悪読み飛ばしてもあとで手を動かして理解でき る
ぜひあなたも インタプリタ書いてみよう!