Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
3.1k
チャネルの仕組み
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に頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
610
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
1.3k
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
190
Java 27新機能 / Java 27 new features
kishida
2
120
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
450
高専キャリア LT 発表内容
crysta1221
6
5.7k
スマート反転とウェブアクセシビリティ
camiha
0
200
Foundry Localでエージェント開発
seosoft
0
190
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
370
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
4.2k
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
460
一参加者から『中の人』へ 〜全通PHPerがブースに立って学んだ、カンファレンスを100倍楽しむコツ〜
wp_daisuke
0
150
Featured
See All Featured
The Language of Interfaces
destraynor
162
27k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
240
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
400
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
The World Runs on Bad Software
bkeepers
PRO
72
12k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.6k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
430
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Side Projects
sachag
456
43k
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 を起動してコード書いてみるとめっちゃ達成感 がある ◦ ゲーム作るのとはまた違う感覚 • 普段あまり洋書を読まない方にもおすすめ ◦
英語がそんなに難しくない ◦ 最悪読み飛ばしてもあとで手を動かして理解でき る
ぜひあなたも インタプリタ書いてみよう!