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
蒼時弦や
February 14, 2018
Programming
430
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
240
2023 - RubyConfTW - Rethink Rails Architecture
elct9620
0
300
20230916 - DDDTW - 導入 Domain-Driven Design 的最佳時機
elct9620
0
530
2023 - WebConf - 選擇適合你的技能組合
elct9620
0
760
20230322 - Generative AI 小聚 ft. Happy Designer
elct9620
0
510
2022 - 默默會 - 重新學習 MVC 的 Model
elct9620
1
540
MOPCON 2022 - 從 Domain-Driven Design 看網站開發框架隱藏
elct9620
1
550
2022 - COSCUP - 我想慢慢寫程式該怎麼辦?
elct9620
0
330
2022 - COSCUP - 打造高速 Ruby 專案開發流程
elct9620
0
380
Other Decks in Programming
See All in Programming
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
480
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
210
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
4
4.8k
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
110
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.1k
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
ジョジョの世界をXRで再現する
mofurune
0
160
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
200
APNsからLive Activityを開始する話
yumnumm
0
170
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
170
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
490
Featured
See All Featured
The Mindset for Success: Future Career Progression
greggifford
PRO
0
530
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
The untapped power of vector embeddings
frankvandijk
2
1.9k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
630
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
310
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
520
The SEO Collaboration Effect
kristinabergwall1
1
590
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
320
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
370
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
560
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