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
蒼時弦や
February 14, 2018
Programming
1
270
Taipei.rb 寫一個送禮自用兩相宜的 Compiler
蒼時弦や
February 14, 2018
Tweet
Share
More Decks by 蒼時弦や
See All by 蒼時弦や
2023 - RubyConfTW - Rethink Rails Architecture
elct9620
0
62
20230916 - DDDTW - 導入 Domain-Driven Design 的最佳時機
elct9620
0
320
2023 - WebConf - 選擇適合你的技能組合
elct9620
0
520
20230322 - Generative AI 小聚 ft. Happy Designer
elct9620
0
260
2022 - 默默會 - 重新學習 MVC 的 Model
elct9620
1
380
MOPCON 2022 - 從 Domain-Driven Design 看網站開發框架隱藏
elct9620
1
410
2022 - COSCUP - 我想慢慢寫程式該怎麼辦?
elct9620
0
210
2022 - COSCUP - 打造高速 Ruby 專案開發流程
elct9620
0
230
2021 - RubyKaigi - It is time to build your mruby VM on the microcontroller?
elct9620
0
190
Other Decks in Programming
See All in Programming
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
2
81k
Harnessing Large Language Models for Training-free Video Anomaly Detection
tereka114
1
1.3k
CSC307 Lecture 11
javiergs
PRO
0
240
SRE チーム立ち上げ前に考えたこと・取り組んだこと / Considerations and Preparations Before Establishing an SRE Team
mackey0225
3
320
Product Management LT会_クアンド新家
shinshin
0
210
Javaの現状2024夏 / Java current status 2024 summer
kishida
4
1.4k
AWS初心者ってどうやってAWSを学ぶ?〜アプリエンジニアがやってよかったアーキテクチャ学習方法〜
yamanashi_ren01
0
190
Rustのweb開発を助ける 便利なツール紹介
yuki0418
1
190
生成AIをkintoneに連携してみた
hideg
0
230
君たちはどうコードをレビューする (される) か / 大吉祥寺.pm
utgwkk
15
8.5k
APIのない大学ログインWebサービスをWKWebViewとJavaScriptでアプリ化した話
akidon0000
1
330
スクラムマスターって孤独じゃないですか?
yoshitaroyoyo
1
140
Featured
See All Featured
Making Projects Easy
brettharned
111
5.7k
Music & Morning Musume
bryan
43
5.9k
Speed Design
sergeychernyshev
9
270
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
353
29k
Embracing the Ebb and Flow
colly
81
4.3k
For a Future-Friendly Web
brad_frost
173
9.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
18
1.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
44
4.7k
Fantastic passwords and where to find them - at NoRuKo
philnash
42
2.7k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
105
6.8k
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