用 Ruby 编写一个解释器
Write an interpreter in Ruby
Mario Arias
July 26, 2024
Technology
0
- 6k 下载 PDF
Write an interpreter in Ruby
这是为 RedDotRuby Conference 2024 准备的演讲。
Mario Arias
July 26, 2024 Tweet Share
Mario Arias 的更多decks
查看 Mario Arias 的所有decks
Kotlin Coroutines
marioariasc
4
720
IntelliJ IDEA: Productivity tips
marioariasc
0
370
Functional Programming in Kotlin with funKTionale
marioariasc
8
3k
Functional Programming in Kotlin with funKTionale
marioariasc
3
- 7k
Introduction to RxKotlin
marioariasc 4 530 Functional programming in Kotlin with funKTionale
marioariasc 11
- 2k
Technology分类下的其他decks
查看 Technology 分类下的所有 decks
アセスメントで紐解く、10Xのデータマネジメントの軌跡
10xinc
1
440
Writing Ruby Scripts with TypeProf
mame
0
170
AWSで作るセキュアな認証基盤with OAuth mTLS / Secure Authentication Infrastructure with OAuth mTLS on AWS
kaminashi
0
180
DETR手法の変遷と最新動向(CVPR2025)
tenten0727
2
- 4k
意思決定を支える検索体験を目指してやってきたこと
hinatades PRO 0 150 AWS Control Towerを 数年運用してきての気づきとこれから/aws-controltower-ops-tips
tadayukinakamura 0 160 持続可能なドキュメント運用のリアル: 1年間の成果とこれから
akitok_ 1 190 JPOUG Tech Talk #12 UNDO Tablespace Reintroduction
nori_shinoda 2 150 AIでめっちゃ便利になったけど、結局みんなで学ぶよねっていう話
kakehashi PRO 0 180 【Λ(らむだ)】最近のアプデ情報 / RPALT20250422
lambda 0 110 CodePipelineのアクション統合から学ぶAWS CDKの抽象化技術 / codepipeline-actions-cdk-abstraction
gotok365 5 200 LLM as プロダクト開発のパワードスーツ
layerx PRO 1 240
特色decks
查看所有特色decks
Typedesign – Prime Four
hannesfritz
41
2. 6k
Building Adaptive Systems
keathley
41
2. 5k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
GraphQLとの向き合い方2022年版
quramy
46
14k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
47
2. 5k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3. 1k
KATA
mclloyd
29
14k
Music & Morning Musume
bryan
47
6. 5k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3. 4k
Practical Orchestrator
shlominoach
186
11k
演讲稿
-
[Mario Arias RedDotRuby Conference 2024 Write an interpreter in Ruby](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_0.jpg "Write an interpreter in Ruby Mario Arias
RedDotRuby Conference 2024 Write an...")
-
[About me • Not a Ruby expert • Ruby-curious? •](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_1.jpg "Write an interpreter in Ruby About me
• Not a Ruby expert • Ruby-curious? • ...")
一个 Kotlin 爱好者 • 拥有近 20 年的经验 • OSS 贡献者 2
-
[Why do you want to write an interpreter? … is](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_2.jpg "Write an interpreter in Ruby Why do you want to write an interpreter?
… is e...")
在家一切都好吗? • 这很有趣 • 刷新或获取有关计算机科学的新知识。 • 有用的案例: • 表达式语言 • 业务规则 • 外部 DSL 3
-
[The Monkey Language … it doesn’t exists, sort of, in](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_3.jpg "Write an interpreter in Ruby The Monkey Language
… it doesn’t exists, sort o...")
某种程度上它并不存在 • 由 Thorsten Ball 创建 • 它在你编写时才存在 • 类似 C、类似 JS 的语法 • 一等公民和高阶函数 • 整数、布尔值、数组和哈希表 • 它在 Go 中有一个参考实现,带有单元测试 4
-
[A f ibonacci function Recursive let fibonacci = fn(x) {](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_4.jpg "Write an interpreter in Ruby A
f ibonacci function Recursive let fibonacci =...")
if (x < 2) { return x; } else { fibonacci(x - 1) + fibonacci(x - 2); } }; fibonacci(35);
5
-
[Pepa … like the character from the movie Encanto •](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_5.jpg "Write an interpreter in Ruby Pepa
… like the character from the movie Encant...")
Monkey 语言的 Ruby 实现 • Ruby 3.1 及以上版本 • 完全测试 • RBS • https://github.com/MarioAriasC/pepa 6
-
[The interpreter f low …a simpli fi ed version of](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_6.jpg "Write an interpreter in Ruby The interpreter
f low …a simpli fi ed version o...")
它的简化版本 源代码 -> 词法分析器(Lexer) -> 词法单元(Tokens) -> 语法分析器(Parser) -> 抽象语法树(AST) -> 求值器(Evaluator) -> 执行 7
-
[Tokens … not the LLM ones https://github.com/M a rioAri a](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_7.jpg "Write an interpreter in Ruby Tokens
… not the LLM ones https://github.com/M ...")
不是 LLM 的 tokens https://github.com/MarioAriasC/pepa/blob/main/lib/tokens.rb
let fibonacci = fn(x) { if (x < 2) { return x; } else { fibonacci(x - 1) + fibonacci(x - 2); } }; fibonacci(35); LET ^ “let” FUNCTION ^ “fn” RETURN ^ “return” ELSE ^ “else” IF ^ “if” IDENT ^ “ f ibon a cci” IDENT ^ “ f ibon a cci” IDENT ^ “ f ibon a cci” IDENT ^ “ f ibon a cci” IDENT ^ “x” IDENT ^ “x” ASSIGN ^ “=” LPAREN ^ “(” RPAREN ^ “)” LBRACE ^ “(”
8
-
[Lexer One character at the time https://github.com/M a rioAri a](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_8.jpg "Write an interpreter in Ruby Lexer
One character at the time https://github....")
一次一个字符 https://github.com/MarioAriasC/pepa/blob/main/lib/lexers.rb 我们需要管理几种情况 • 单字符 tokens:=, (, ), {, } 等 • 双字符 tokens:!=, == • 字符串 • 整数 • 一个字母字符 + 一个或多个字母数字字符 • 保留字 • 标识符 9
-
[AST let fibonacci = fn(x) { if (x < 2)](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_9.jpg "Write an interpreter in Ruby AST
let fibonacci = fn(x) { if (x < 2) { return...")
{ return x; } else { fibonacci(x - 1) + fibonacci(x - 2); } }; fibonacci(35);
https://github.com/MarioAriasC/pepa/blob/main/lib/ast.rb Program • statements • let statement • function literal • parameters • block statement • if expression • condition • infix expression • identifier • operator • integer literal … 10
-
[AST https://github.com/M a rioAri a sC/pep a /blob/m a in/lib/](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_10.jpg "Write an interpreter in Ruby AST
https://github.com/M a rioAri a sC/pep a /b...")
https://github.com/MarioAriasC/pepa/blob/main/lib/ast.rb Program: List of Statements Statement LetStatement: let foo = 1; ExpressionStatement: A statement that contains an expression ReturnStatement: return foo; BlockStatement: List of Statements inside a block Expression PrefixExpression: !true; InfixExpression: 1 + 1; CallExpression: myFunction(1); IndexExpression: myHash[“foo”] IfExpression: if (x > 2) {“x”} else {“y”} Identifier IntegerLiteral: 1; BooleanLiteral: true; ArrayLiteral: [1, 2, 3]; FunctionLiteral fn(x) {x + 2} StringLiteral: “Hello” HashLiteral: {“foo”: 1, true:2, 3:3} 11
-
[Parser One token at the time https://github.com/M a rioAri a](https://speakerdeck.com/marioariasc/https:/files.speakerdeck.com/presentations/8cc39f81a4f0467086f0088135c5841e/slide_11.jpg "Write an interpreter in Ruby Parser
One token at the time https://github.com...")
一次一个 token [https://github.com/MarioAriasC/pepa/blob/main/lib/parsers