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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Kenshi Kamata
November 05, 2017
Programming
0
3k
Go でインタプリタを 書いてみよう
Go Conference 2017 Autumn Lightning Talk
Kenshi Kamata
November 05, 2017
Tweet
Share
More Decks by Kenshi Kamata
See All by Kenshi Kamata
500万ユーザーを支える残高の冪等性 / The idempotency of the balance for 5 million Merpay users
knsh14
0
2.9k
チャネルの仕組み
knsh14
6
5.5k
Go1.10 strings.Builder の紹介
knsh14
2
1.4k
Let’s Create An Interpreter In Go
knsh14
0
150
Go Code Review Comment を翻訳した話
knsh14
0
7.6k
tvOS Leaderboard
knsh14
0
1.2k
Other Decks in Programming
See All in Programming
Cyrius ーLinux非依存にコンテナをネイティブ実行する専用OSー
n4mlz
0
240
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
230
それはエンジニアリングの糧である:AI開発のためにAIのOSSを開発する現場より / It serves as fuel for engineering: insights from the field of developing open-source AI for AI development.
nrslib
1
510
エンジニアの「手元の自動化」を加速するn8n 2026.02.27
symy2co
0
180
[PHPerKaigi 2026]PHPerKaigi2025の企画CodeGolfが最高すぎて社内で内製して半年運営して得た内製と運営の知見
ikezoemakoto
0
280
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
150
Redox OS でのネームスペース管理と chroot の実現
isanethen
0
430
PHPのバージョンアップ時にも役立ったAST(2026年版)
matsuo_atsushi
0
240
nuget-server - あなたが必要だったNuGetサーバー
kekyo
PRO
0
440
Agentic AI: Evolution oder Revolution
mobilelarson
PRO
0
190
どんと来い、データベース信頼性エンジニアリング / Introduction to DBRE
nnaka2992
1
330
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
120
Featured
See All Featured
The Spectacular Lies of Maps
axbom
PRO
1
640
Building AI with AI
inesmontani
PRO
1
820
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
HDC tutorial
michielstock
1
580
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
390
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.5k
Designing for humans not robots
tammielis
254
26k
Writing Fast Ruby
sferik
630
63k
Everyday Curiosity
cassininazir
0
170
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
820
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 を起動してコード書いてみるとめっちゃ達成感 がある ◦ ゲーム作るのとはまた違う感覚 • 普段あまり洋書を読まない方にもおすすめ ◦
英語がそんなに難しくない ◦ 最悪読み飛ばしてもあとで手を動かして理解でき る
ぜひあなたも インタプリタ書いてみよう!