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
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.8k
チャネルの仕組み
knsh14
6
5.4k
Go1.10 strings.Builder の紹介
knsh14
2
1.4k
Let’s Create An Interpreter In Go
knsh14
0
140
Go Code Review Comment を翻訳した話
knsh14
0
7.5k
tvOS Leaderboard
knsh14
0
1.2k
Other Decks in Programming
See All in Programming
あなたとKaigi on Rails / Kaigi on Rails + You
shimoju
0
200
Devoxx BE - Local Development in the AI Era
kdubois
0
150
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
650
他言語経験者が Golangci-lint を最初のコーディングメンターにした話 / How Golangci-lint Became My First Coding Mentor: A Story from a Polyglot Programmer
uma31
0
450
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
1.2k
Amazon Verified Permissions実践入門 〜Cedar活用とAppSync導入事例/Practical Introduction to Amazon Verified Permissions
fossamagna
2
100
What's new in Spring Modulith?
olivergierke
1
170
TransformerからMCPまで(現代AIを理解するための羅針盤)
mickey_kubo
7
5.6k
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
370
20251016_Rails News ~Rails 8.1の足音を聴く~
morimorihoge
3
860
CSC305 Lecture 11
javiergs
PRO
0
300
外接に惑わされない自システムの処理時間SLIをOpenTelemetryで実現した話
kotaro7750
0
110
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
463
33k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Context Engineering - Making Every Token Count
addyosmani
8
310
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.7k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.6k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
Designing for Performance
lara
610
69k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.2k
Visualization
eitanlees
150
16k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.2k
The Language of Interfaces
destraynor
162
25k
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 を起動してコード書いてみるとめっちゃ達成感 がある ◦ ゲーム作るのとはまた違う感覚 • 普段あまり洋書を読まない方にもおすすめ ◦
英語がそんなに難しくない ◦ 最悪読み飛ばしてもあとで手を動かして理解でき る
ぜひあなたも インタプリタ書いてみよう!