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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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.4k
Let’s Create An Interpreter In Go
knsh14
0
160
Go Code Review Comment を翻訳した話
knsh14
0
7.7k
tvOS Leaderboard
knsh14
0
1.2k
Other Decks in Programming
See All in Programming
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
250
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
6.6k
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
7
4.5k
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
340
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
160
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
200
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
2
650
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
110
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
ふつうのFeature Flag実践入門
irof
7
3.9k
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
Featured
See All Featured
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
590
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4k
The untapped power of vector embeddings
frankvandijk
2
1.8k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
How to Think Like a Performance Engineer
csswizardry
28
2.6k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
290
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
200
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
170
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
530
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
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 を起動してコード書いてみるとめっちゃ達成感 がある ◦ ゲーム作るのとはまた違う感覚 • 普段あまり洋書を読まない方にもおすすめ ◦
英語がそんなに難しくない ◦ 最悪読み飛ばしてもあとで手を動かして理解でき る
ぜひあなたも インタプリタ書いてみよう!