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
Taipei.rb 寫一個送禮自用兩相宜的 Compiler
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
蒼時弦や
February 14, 2018
Programming
410
1
Share
Taipei.rb 寫一個送禮自用兩相宜的 Compiler
蒼時弦や
February 14, 2018
More Decks by 蒼時弦や
See All by 蒼時弦や
2024 - COSCUP - Clean Architecture in Rails
elct9620
2
200
2023 - RubyConfTW - Rethink Rails Architecture
elct9620
0
230
20230916 - DDDTW - 導入 Domain-Driven Design 的最佳時機
elct9620
0
470
2023 - WebConf - 選擇適合你的技能組合
elct9620
0
680
20230322 - Generative AI 小聚 ft. Happy Designer
elct9620
0
450
2022 - 默默會 - 重新學習 MVC 的 Model
elct9620
1
510
MOPCON 2022 - 從 Domain-Driven Design 看網站開發框架隱藏
elct9620
1
530
2022 - COSCUP - 我想慢慢寫程式該怎麼辦?
elct9620
0
290
2022 - COSCUP - 打造高速 Ruby 專案開發流程
elct9620
0
320
Other Decks in Programming
See All in Programming
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
2.1k
KMP × Kotlin 2.3 - How Android Got Slower While iOS Builds Improved by 47%
rio432
0
100
GNU Makeの使い方 / How to use GNU Make
kaityo256
PRO
16
5.6k
リセットCSSを1行消したらアクセシビリティが向上した話
pvcresin
4
420
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
110
GoogleCloudとterraform完全に理解した
terisuke
1
180
UIの境界線をデザインする | React Tokyo #15 メイントーク
sasagar
2
410
ついに来た!本格的なマルチクラウド時代の Google Cloud
maroon1st
0
340
Kingdom of the Machine
yui_knk
2
1.3k
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
180
mruby on C#: From VM Implementation to Game Scripting (RubyKaigi 2026)
hadashia
2
1.4k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
370
Featured
See All Featured
Ethics towards AI in product and experience design
skipperchong
2
270
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
510
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
160
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
The Cost Of JavaScript in 2023
addyosmani
55
9.9k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
110
What's in a price? How to price your products and services
michaelherold
247
13k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.4k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
Speed Design
sergeychernyshev
33
1.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Transcript
用 Ruby 寫一個送禮自用 兩相宜的 Compiler
襟 儘 䓛⛳ @elct9620 WEB DEVELOPER GAME DEVELOPER
None
None
#I_feel_border ⼤大家都在寫 Compier 作業
C4 不夠⽤用就⽤用 C5 啊! https://github.com/elct9620/5compiler Demo First Talk
Lexer Tokenizer Parser Source Code AST
簡單說就是照規則把字串串切開來來 Lexer
Lexer.new("count = 1 if reset") # ['count', '=', '1', 'if',
'reset'] Lexer
class Lexer < Array KEYWORDS = %w[if else end puts].freeze
KEYWORD_RULE = /(?<keyword>#{KEYWORDS.join('|')})/ end Lexer
RULE = Regexp.union( LINEFEED_RULE, SYMBOL_RULE, KEYWORD_RULE, STRING_RULE, NUMBER_RULE, WORD_RULE )
Lexer
script.scan(RULE).flatten.compact # ['count', '=', '1', 'if', 'reset'] Lexer
把 Lexer 切好的⽂文字片段做簡單的分類 Tokenizer
Tokeinzer.new(['if', 'reset']) # [ # <Token @type=“keyword” # @value="if"> #
<Token @type=“identifier" # @value="reset"> # ] Tokenizer
MATCH_RULES.each do |type, rule| if word.match?(rule) return Token.new(type, word) end
end Token.new(:literal, word) Tokenizer
class Token # ... def symbol? type == :symbol end
# ... end Tokenizer
依照 Token 的狀狀況轉換成⽤用樹狀狀的⽅方式表⽰示程式運作 Parser
IF TRUE FALSE BLOCK BLOCK
class Parser def parse tokens = @tokenizer.to_enum @context = Node::Context.new(tokens)
@ast = context.ast end end Parser
loop do break if t.peek.else? || #... #… if t.peek.fn?
next @ast << Node::Function.new(t) end #... end Parser
Context Parser Function Enter Context State Exit State Next token
is ‘else’ Enter Function State Exit State Next token is ‘end’
基於 Parser ⽣生成的 Abstract Syntax Tree 執⾏行行程式 Virtual Machine
將 AST 轉換為機器碼或者 VM 可以直接讀取的規格 Bytecode
借⽤用 Ruby VM 直接執⾏行行 `Kernel.send(‘puts’, ‘Hello World’)` 5Compiler’s VM
在 Router 中可以⽤用來來判斷⽬目前請求的網址對應的 Controller Rails’s AST
在 ActiveRecord (Arel) 可以⽤用來來組成 SQL 語法 Rails’s AST
⽤用來來動態執⾏行行 ActiveRecord 相關的動作 Ex. JSON Query, Update … Tamashii::TLang’s AST
很可惜困難的是 VM 怎麼動起來來喔⋯⋯ 聽起來來好像很簡單,對吧?
None