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
Taipei.rb 寫一個送禮自用兩相宜的 Compiler
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
蒼時弦や
February 14, 2018
Programming
420
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Taipei.rb 寫一個送禮自用兩相宜的 Compiler
蒼時弦や
February 14, 2018
More Decks by 蒼時弦や
See All by 蒼時弦や
2024 - COSCUP - Clean Architecture in Rails
elct9620
2
220
2023 - RubyConfTW - Rethink Rails Architecture
elct9620
0
280
20230916 - DDDTW - 導入 Domain-Driven Design 的最佳時機
elct9620
0
520
2023 - WebConf - 選擇適合你的技能組合
elct9620
0
750
20230322 - Generative AI 小聚 ft. Happy Designer
elct9620
0
500
2022 - 默默會 - 重新學習 MVC 的 Model
elct9620
1
520
MOPCON 2022 - 從 Domain-Driven Design 看網站開發框架隱藏
elct9620
1
550
2022 - COSCUP - 我想慢慢寫程式該怎麼辦?
elct9620
0
320
2022 - COSCUP - 打造高速 Ruby 專案開發流程
elct9620
0
360
Other Decks in Programming
See All in Programming
AHC070解法紹介
eijirou
0
120
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
600
Intent as Code
shoppingjaws
6
1k
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
180
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
130
FreeBSDでZabbixを動かす
kenkino
0
300
Webの地図
yosuke_furukawa
PRO
6
4.6k
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
280
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
4.7k
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
130
XHTMLが残したもの
yosuke_furukawa
PRO
2
810
Security issues being discussed on Web Platforms
petamoriken
0
960
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Evolving SEO for Evolving Search Engines
ryanjones
0
300
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
730
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
420
Discover your Explorer Soul
emna__ayadi
2
1.3k
How GitHub (no longer) Works
holman
316
150k
RailsConf 2023
tenderlove
30
1.5k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
670
How to train your dragon (web standard)
notwaldorf
97
6.8k
Speed Design
sergeychernyshev
33
2.1k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
540
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
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