![Page 1: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/1.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Let’s Wr!e an Interpreter!
Sunday, September 22, 13
![Page 2: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/2.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Thank you so much!
Sunday, September 22, 13
![Page 3: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/3.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Setting Expectations
• 168 Slides in 30 minutes. ~5.6 spm.
• Boatloads of content. Almost all code.
• I hope to “Hurt Brains”.
• TODO
Sunday, September 22, 13
![Page 4: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/4.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
How to solve a rubiks cube in 27
minutes
Sunday, September 22, 13
![Page 5: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/5.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Interpreters in 3 minutes
Sunday, September 22, 13
![Page 6: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/6.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
OMG WHY?!?
Sunday, September 22, 13
![Page 7: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/7.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
BecauseI’m a pervert!
Sunday, September 22, 13
![Page 8: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/8.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Wa!… No!
Sunday, September 22, 13
![Page 9: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/9.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Because we can!
Sunday, September 22, 13
![Page 10: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/10.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
"e “Uby”Interpreter
Sunday, September 22, 13
![Page 11: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/11.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Branching Logic:if/else/elsif
if condition then true_codeelse false_codeend
Sunday, September 22, 13
![Page 12: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/12.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Loops: whilewhile condition codeend
Sunday, September 22, 13
![Page 13: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/13.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Local Variablesmeaning = 42pi = 3
Sunday, September 22, 13
![Page 14: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/14.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Functionsdef fib n if n <= 2 then 1 else fib(n-2) + fib(n-1) endend
Sunday, September 22, 13
![Page 15: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/15.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Functions
recursive function calls
local variables
“primitive” function callsdef fib n if n <= 2 then 1 else fib(n-2) + fib(n-1) endend
Sunday, September 22, 13
![Page 16: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/16.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Uby Roadmap
Variables Conditionals
Runtime
Environment
Functions
Parser
Loops
Sunday, September 22, 13
![Page 17: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/17.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Parsing: ruby_parser
Sunday, September 22, 13
![Page 18: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/18.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
ConditionalsLoops
Runtime
Environment
Functions
Parser
Variables
Sunday, September 22, 13
![Page 19: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/19.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
RubyParser.new.parse " def fib n if n <= 2 then 1 else fib(n-2) + fib(n-1) end end"
Sunday, September 22, 13
![Page 20: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/20.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
:defn
:fib
:args:n
:if
:call
:lvar :n
:<
:lit 2
:lit 1
:call
:call
nil
:fib
:call
:lvar :n
:-
:lit 2:+
:call nil :fib
:call
:lvar :n
:-
:lit 1
Sunday, September 22, 13
![Page 21: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/21.jpg)
Sunday, September 22, 13
![Page 22: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/22.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
:defn
:fib
:args:n
:if
:call
:lvar :n
:<
:lit 2
:lit 1
:call
:call
nil
:fib
:call
:lvar :n
:-
:lit 2:+
:call nil :fib
:call
:lvar :n
:-
:lit 1
Sunday, September 22, 13
![Page 23: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/23.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
:defn
:fib
:args
:n
:if
:call
:lvar :n
:<
:lit 2
:lit 1
:call
:call
nil
:fib
:call
:lvar :n
:-
:lit 2:+
:call nil :fib
:call
:lvar :n
:-
:lit 1
Sunday, September 22, 13
![Page 24: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/24.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
:defn
:fib
:args
:n
:if
:call
:lvar :n
:<
:lit 2
:lit 1
:call
:call
nil
:fib
:call
:lvar :n
:-
:lit 2:+
:call nil :fib
:call
:lvar :n
:-
:lit 1
Sunday, September 22, 13
![Page 25: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/25.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
else
:call nil :fib
:call
:lvar :n
:-
:lit 1
:call nil :fib
:call
:lvar :n
:-
:lit 2
:call :+
condition
:call
:lvar :n
:<
:lit 2
:defn
:fib :args
:n
:if
then
:lit 1
Sunday, September 22, 13
![Page 26: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/26.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
s(:defn, :fib, s(:args, :n), s(:if, s(:call, s(:lvar, :n), :<=, s(:lit, 2)), s(:lit, 1), s(:call, s(:call, nil, :fib, s(:call, s(:lvar, :n), :-, s(:lit, 2))), :+, s(:call, nil, :fib, s(:call, s(:lvar, :n), :-, s(:lit, 1))))))
Sunday, September 22, 13
![Page 27: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/27.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
S-Expression Vocabulary
s(:call, s(:lit, 3), :+, s(:lit, 4))
Sexp Sub-sexps
Rest(cdr)
Head/Type(car)
Sunday, September 22, 13
![Page 28: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/28.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
All for free using ruby_parser
Sunday, September 22, 13
![Page 29: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/29.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
OK…No more talk about
parsing.
Sunday, September 22, 13
![Page 30: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/30.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Further study on parsers:
Michael Jackson’sParsing Expressions in Ruby
MWRC 2011http://www.confreaks.com/videos/582
Sunday, September 22, 13
![Page 31: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/31.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Interpre#ng via sexp_processor
Sunday, September 22, 13
![Page 32: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/32.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
ConditionalsLoops
Environment
Variables
Functions
Parser
Runtime
Sunday, September 22, 13
![Page 33: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/33.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
How to get 7 from 3+4?
73 + 4 ??????
Sunday, September 22, 13
![Page 34: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/34.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Start with the source
3 + 4
Sunday, September 22, 13
![Page 35: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/35.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Parse it
3 + 4s(:call, s(:lit, 3), :+, s(:lit, 4))
Sunday, September 22, 13
![Page 36: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/36.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
s(:call, s(:lit, 3), :+, s(:lit, 4))
Then process by type
3 + 4
process_call
process_lit
Sunday, September 22, 13
![Page 37: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/37.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
s(:call, s(:lit, 3), :+, s(:lit, 4))
First with the inner values
3 + 4
process_lit
Sunday, September 22, 13
![Page 38: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/38.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
s(:call, 3, :+, s(:lit, 4))
First with the inner values
3 + 4
process_lit
Sunday, September 22, 13
![Page 39: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/39.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
s(:call, 3, :+, 4)
First with the inner values
3 + 4
Sunday, September 22, 13
![Page 40: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/40.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
s(:call, 3, :+, 4)
then with the outer
3 + 4
process_call
Sunday, September 22, 13
![Page 41: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/41.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
proc { |a, b| a + b }[3, 4]
then with the outer
3 + 4
Sunday, September 22, 13
![Page 42: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/42.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
proc { |a, b| a + b }[3, 4]
resulting in a final value
3 + 4 7
Sunday, September 22, 13
![Page 43: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/43.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
SexpProcessorclass UbyInterpreter def process_lit s s.last endend s(:lit, 3)
3
Sunday, September 22, 13
![Page 44: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/44.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Test Dr$en Interpreters
Sunday, September 22, 13
![Page 45: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/45.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Loops Conditionals
Runtime
Environment
Functions
Parser
Test
Dri
ven
Variables
Sunday, September 22, 13
![Page 46: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/46.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unit Testsdef test_sanity val = lang.eval("3 + 4") assert_equal 7, valend
Sunday, September 22, 13
![Page 47: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/47.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unit TestsInput Source
Expected Value
def test_sanity val = lang.eval("3 + 4") assert_equal 7, valend
Sunday, September 22, 13
![Page 48: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/48.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Premature Refactoring
Input SourceExpected Value
def assert_eval exp, src assert_equal exp, lang.eval(src)end
def test_sanity assert_eval 7, "3 + 4"end
Sunday, September 22, 13
![Page 49: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/49.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
San% Test:3 + 4
Sunday, September 22, 13
![Page 50: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/50.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
ConditionalsLoops
Environment
Functions
Parser
Test
Dri
ven
Runtime
Variables
Sunday, September 22, 13
![Page 51: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/51.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Test Infrastructureclass TestUbyInterpreter < MiniTest::Unit::TestCase attr_accessor :int
def setup self.int = UbyInterpreter.new end
def assert_eval exp, src, msg = nil assert_equal exp, int.eval(src), msg endend
Sunday, September 22, 13
![Page 52: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/52.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Our First Test:class TestUbyInterpreter < MiniTest::Unit::TestCase attr_accessor :int
def setup self.int = UbyInterpreter.new end
def assert_eval exp, src assert_equal exp, ri.eval(src) end
def test_sanity assert_eval 3, "3" assert_eval 7, "3 + 4" endend
Old Code
New Code
Sunday, September 22, 13
![Page 53: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/53.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Our First Test:class TestUbyInterpreter < MiniTest::Unit::TestCase
# ...
def test_sanity assert_eval 3, "3" assert_eval 7, "3 + 4" endend
Old Code
New Code
Sunday, September 22, 13
![Page 54: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/54.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Our first error:# Running tests:
E
Finished tests in 0.000364s, 2747.2527 tests/s, 0.0000 assertions/s.
1) Error:TestUbyInterpreter#test_sanity:NameError: uninitialized constant TestUbyInterpreter::UbyInterpreter ./test/test_ruby_interpreter.rb:11:in `setup'
Sunday, September 22, 13
![Page 55: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/55.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define the constantclass UbyInterpreter < SexpInterpreter VERSION = "1.0.0"end
Sunday, September 22, 13
![Page 56: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/56.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Wash, rinse…# Running tests:
E
Finished tests in 0.000578s, 1730.1038 tests/s, 0.0000 assertions/s.
1) Error:TestUbyInterpreter#test_sanity:NoMethodError: private method `eval' called for #<UbyInterpreter:0x105be17d8> ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:19:in `test_sanity'
Sunday, September 22, 13
![Page 57: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/57.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Implement eval & parseclass UbyInterpreter < SexpInterpreter # ... attr_accessor :parser
def initialize super
self.parser = Ruby19Parser.new end
def eval src process parse src end
def parse src self.parser.process src endend
Sunday, September 22, 13
![Page 58: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/58.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Finally!# Running tests:
E
Finished tests in 0.001365s, 732.6007 tests/s, 0.0000 assertions/s.
1) Error:TestUbyInterpreter#test_sanity:UnknownNodeError: Bug! Unknown node-type :lit to UbyInterpreter ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:19:in `test_sanity'
Sunday, September 22, 13
![Page 59: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/59.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Implement process_litclass UbyInterpreter < SexpInterpreter
# ...
def process_lit s s.last endend
Sunday, September 22, 13
![Page 60: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/60.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Tada! Second Assertion! 1) Error:TestUbyInterpreter#test_sanity:UnknownNodeError: Bug! Unknown node-type :call to UbyInterpreter ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:20:in `test_sanity'
Sunday, September 22, 13
![Page 61: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/61.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :call genericallyclass UbyInterpreter < SexpInterpreter
# ...
def process_call s raise "Boom: #{s.inspect}" endend
Sunday, September 22, 13
![Page 62: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/62.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
See sub-sexps 1) Error:TestUbyInterpreter#test_sanity:RuntimeError: Boom: s(:call, s(:lit, 3), :+, s(:lit, 4)) ./lib/ruby_interpreter.rb:35:in `process_call' ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:17:in `assert_eval' ./test/test_ruby_interpreter.rb:22:in `test_sanity'
Sunday, September 22, 13
![Page 63: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/63.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unpack sub-sexpsclass UbyInterpreter < SexpInterpreter
# ...
def process_call s _, recv, msg, *args = s
raise "Boom: #{recv}, #{msg}, #{args.inspect}" endend
Sunday, September 22, 13
![Page 64: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/64.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unpacked sub-sexps 1) Error:TestUbyInterpreter#test_sanity:RuntimeError: Boom: s(:lit, 3), +, [s(:lit, 4)] ./lib/ruby_interpreter.rb:31:in `process_call'
Sunday, September 22, 13
![Page 65: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/65.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Process sub-sexpsclass UbyInterpreter < SexpInterpreter def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
raise "Boom: #{recv}, #{msg}, #{args.inspect}" endend
Sunday, September 22, 13
![Page 66: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/66.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Processed Values 1) Error:TestUbyInterpreter#test_sanity:RuntimeError: Boom: 3, +, [4] ./lib/ruby_interpreter.rb:43:in `process_call'
Sunday, September 22, 13
![Page 67: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/67.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Cheat: Send to Rubyclass UbyInterpreter < SexpInterpreter def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
recv.send(msg, *args) # big ol' hack… endend
Sunday, September 22, 13
![Page 68: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/68.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Sanity!# Running tests:
.
Finished tests in 0.001625s, 615.3846 tests/s, 1230.7692 assertions/s.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
Sunday, September 22, 13
![Page 69: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/69.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!class UbyInterpreter < SexpInterpreter VERSION = "1.0.0"
attr_accessor :parser
def initialize super self.parser = Ruby19Parser.new end
def eval src process parse src end
def parse src self.parser.process src end
def process_lit s s.last end
def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
recv.send(msg, *args) # big ol' hack… endend
Yay!
Overblown
Calculator!
Sunday, September 22, 13
![Page 70: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/70.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Cond!ionals&
Truthiness
Sunday, September 22, 13
![Page 71: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/71.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Loops
Runtime
Environment
Variables
Functions
Parser
Test
Dri
ven
Conditionals
Sunday, September 22, 13
![Page 72: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/72.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Add :if testsclass TestUbyInterpreter < MiniTest::Unit::TestCase # ...
def test_if assert_eval 42, "if true then 42 else 24 end" end
def test_if_falsey assert_eval 24, "if nil then 42 else 24 end" assert_eval 24, "if false then 42 else 24 end" endend
Sunday, September 22, 13
![Page 73: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/73.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unknown :if 1) Error:TestUbyInterpreter#test_if:UnknownNodeError: Bug! Unknown node-type :if to UbyInterpreter ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:34:in `test_if'
2) Error:TestUbyInterpreter#test_if_falsey:UnknownNodeError: Bug! Unknown node-type :if to UbyInterpreter ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:38:in `test_if_falsey'
3 tests, 2 assertions, 0 failures, 2 errors, 0 skips
Sunday, September 22, 13
![Page 74: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/74.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :if genericallyclass UbyInterpreter < SexpInterpreter # ...
def process_if s raise "Boom: #{s.inspect}" endend
Sunday, September 22, 13
![Page 75: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/75.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
See sub-sexps 1) Error:TestUbyInterpreter#test_if:RuntimeError: Boom: s(:if, s(:true), s(:lit, 42), s(:lit, 24)) ./lib/ruby_interpreter.rb:38:in `process_if' ... ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:34:in `test_if'
2) Error:TestUbyInterpreter#test_if_falsey:RuntimeError: Boom: s(:if, s(:nil), s(:lit, 42), s(:lit, 24)) ./lib/ruby_interpreter.rb:38:in `process_if' ... ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:38:in `test_if_falsey'
Sunday, September 22, 13
![Page 76: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/76.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Process sub-sexpsclass UbyInterpreter < SexpInterpreter # ...
def process_if s _, c, t, f = s
c = process c
if c then process t else process f end endend
Why evaluate c before t &
f?
if true then puts "happy"else system "rm -rf /"end
Sunday, September 22, 13
![Page 77: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/77.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Sub-sexps fail 1) Error:TestUbyInterpreter#test_if:UnknownNodeError: Bug! Unknown node-type :true to UbyInterpreter ./lib/ruby_interpreter.rb:40:in `process_if' ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:34:in `test_if'
2) Error:TestUbyInterpreter#test_if_falsey:UnknownNodeError: Bug! Unknown node-type :nil to UbyInterpreter ./lib/ruby_interpreter.rb:40:in `process_if' ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:38:in `test_if_falsey'
Sunday, September 22, 13
![Page 78: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/78.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :nil and :trueclass UbyInterpreter < SexpInterpreter # ...
def process_nil s nil end
def process_true s true endend
Sunday, September 22, 13
![Page 79: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/79.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Third failure 1) Error:TestUbyInterpreter#test_if_falsey:UnknownNodeError: Bug! Unknown node-type :false to UbyInterpreter ./lib/ruby_interpreter.rb:40:in `process_if' ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:39:in `test_if_falsey'
Sunday, September 22, 13
![Page 80: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/80.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :falseclass UbyInterpreter < SexpInterpreter # ...
def process_false s false endend
Sunday, September 22, 13
![Page 81: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/81.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Voilà!# Running tests:
...
Finished tests in 0.002860s, 1048.9510 tests/s, 1748.2517 assertions/s.
3 tests, 5 assertions, 0 failures, 0 errors, 0 skips
Sunday, September 22, 13
![Page 82: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/82.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Test Driven Process
Processsub-sexps
Tests Pass
Add afailing test
Errornode-type
Add genericnode-type
Failure sub-sexpcomponents
Sunday, September 22, 13
![Page 83: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/83.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Yes, development this fast…
with autotest.
Sunday, September 22, 13
![Page 84: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/84.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Sunday, September 22, 13
![Page 85: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/85.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Brain Hurt Yet?
Sunday, September 22, 13
![Page 86: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/86.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Good
Sunday, September 22, 13
![Page 87: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/87.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Wait till I get going!
Where was I?
Sunday, September 22, 13
![Page 88: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/88.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Local Variables
Sunday, September 22, 13
![Page 89: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/89.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Loops Conditionals
Runtime
Functions
Parser
Test
Dri
ven
Variables
Environment
Sunday, September 22, 13
![Page 90: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/90.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Local Variables!class TestUbyInterpreter < MiniTest::Unit::TestCase # ...
def test_lvar assert_eval 42, "x = 42; x" endend
Sunday, September 22, 13
![Page 91: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/91.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Block?!? 1) Error:TestUbyInterpreter#test_lvar:UnknownNodeError: Bug! Unknown node-type :block to UbyInterpreter ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:24:in `test_lvar'
Sunday, September 22, 13
![Page 92: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/92.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :blockclass UbyInterpreter < SexpInterpreter # ...
def process_block s raise "Boom: #{s.inspect}" endend
Sunday, September 22, 13
![Page 93: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/93.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Ohhh… 1) Error:TestUbyInterpreter#test_lvar:RuntimeError: Boom: s(:block, s(:lasgn, :x, s(:lit, 42)), s(:lvar, :x)) ./lib/ruby_interpreter.rb:25:in `process_block' ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:24:in `test_lvar'
Sunday, September 22, 13
![Page 94: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/94.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Process all sub-sexpsclass UbyInterpreter < SexpInterpreter # ...
def process_block s result = nil s.rest.each do |sub| result = process sub end result endend
Sunday, September 22, 13
![Page 95: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/95.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
:lasgn is undefined 1) Error:TestUbyInterpreter#test_lvar:UnknownNodeError: Bug! Unknown node-type :lasgn to UbyInterpreter ./lib/ruby_interpreter.rb:27:in `process_block' ./lib/ruby_interpreter.rb:26:in `each' ./lib/ruby_interpreter.rb:26:in `process_block' ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:24:in `test_lvar'
Sunday, September 22, 13
![Page 96: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/96.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :lasgnclass UbyInterpreter < SexpInterpreter # ...
def process_lasgn s raise "Boom: #{s.inspect}" endend
Sunday, September 22, 13
![Page 97: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/97.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Looks like name/value 1) Error:TestUbyInterpreter#test_lvar:RuntimeError: Boom: s(:lasgn, :x, s(:lit, 42)) ./lib/ruby_interpreter.rb:58:in `process_lasgn' ./lib/ruby_interpreter.rb:27:in `process_block' ./lib/ruby_interpreter.rb:26:in `each' ./lib/ruby_interpreter.rb:26:in `process_block' ./lib/ruby_interpreter.rb:17:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:24:in `test_lvar'
Sunday, September 22, 13
![Page 98: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/98.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Add a stupid tableclass UbyInterpreter < SexpInterpreter # ...
attr_accessor :env
def initialize # ... self.env = {} # omg this is horrible end
def process_lasgn s _, n, v = s
self.env[n] = process v endend
EnvironmentEnvironment
x 42
Sunday, September 22, 13
![Page 99: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/99.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Now we need to read 1) Error:TestUbyInterpreter#test_lvar:UnknownNodeError: Bug! Unknown node-type :lvar to UbyInterpreter ./lib/ruby_interpreter.rb:29:in `process_block' ./lib/ruby_interpreter.rb:28:in `each' ./lib/ruby_interpreter.rb:28:in `process_block' ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:24:in `test_lvar'
Sunday, September 22, 13
![Page 100: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/100.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Access the tableclass UbyInterpreter < SexpInterpreter # ...
def process_lvar s _, name = s
self.env[name] endend
Sunday, September 22, 13
![Page 101: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/101.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Voilà# Running tests:
....
Finished tests in 0.003666s, 1091.1075 tests/s, 1636.6612 assertions/s.
4 tests, 6 assertions, 0 failures, 0 errors, 0 skips
Sunday, September 22, 13
![Page 102: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/102.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Sunday, September 22, 13
![Page 103: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/103.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Just wait…
Sunday, September 22, 13
![Page 104: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/104.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
It gets “better”
Sunday, September 22, 13
![Page 105: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/105.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Fun&ions
Sunday, September 22, 13
![Page 106: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/106.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Conditionals
Runtime
Environment
Variables
Parser
Test
Dri
ven
Loops
Functions
Sunday, September 22, 13
![Page 107: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/107.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Add :defn Testclass TestUbyInterpreter < MiniTest::Unit::TestCase # ...
def test_defn assert_eval nil, <<-EOM def double n 2 * n end EOM
assert_eval 42, "double(21)" endend
Sunday, September 22, 13
![Page 108: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/108.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unknown :defn 1) Error:TestUbyInterpreter#test_defn:UnknownNodeError: Bug! Unknown node-type :defn to UbyInterpreter ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:17:in `assert_eval' ./test/test_ruby_interpreter.rb:45:in `test_defn'
Sunday, September 22, 13
![Page 109: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/109.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Generic :defnclass UbyInterpreter < SexpInterpreter # ...
def process_defn s raise "Boom: #{s.inspect}" endend
Sunday, September 22, 13
![Page 110: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/110.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Examine sub-sexps 1) Error:TestUbyInterpreter#test_defn:RuntimeError: Boom: s(:defn, :double, s(:args, :n), s(:call, s(:lit, 2), :*, s(:lvar, :n))) ./lib/ruby_interpreter.rb:44:in `process_defn' ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:17:in `assert_eval' ./test/test_ruby_interpreter.rb:45:in `test_defn'
Sunday, September 22, 13
![Page 111: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/111.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unpack & Storeclass UbyInterpreter < SexpInterpreter # ...
def process_defn s _, name, args, *body = s
self.env[name] = [args, body]
nil endend
EnvironmentEnvironment
n 8
life 42
pi 3
double
[ s(:args, :n),
[s(:call, s(:lit, 2), :*, s(:lvar, :n))]]
Sunday, September 22, 13
![Page 112: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/112.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
What’s up with :call? 1) Error:TestUbyInterpreter#test_defn:NoMethodError: undefined method `double' for nil:NilClass ./lib/ruby_interpreter.rb:40:in `send' ./lib/ruby_interpreter.rb:40:in `process_call' ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:17:in `assert_eval' ./test/test_ruby_interpreter.rb:54:in `test_defn'
Sunday, September 22, 13
![Page 113: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/113.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
No NilClass#doubleclass UbyInterpreter < SexpInterpreter # ...
def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
recv.send(msg, *args) # big ol' hack… endend
Sunday, September 22, 13
![Page 114: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/114.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Isolate the Problemclass UbyInterpreter < SexpInterpreter # ...
def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
if recv then recv.send(msg, *args) # less of a hack else raise "argh" end endend
Sunday, September 22, 13
![Page 115: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/115.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Error, Isolated 1) Error:TestUbyInterpreter#test_defn:RuntimeError: argh ./lib/ruby_interpreter.rb:43:in `process_call' ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:17:in `assert_eval' ./test/test_ruby_interpreter.rb:54:in `test_defn'
Sunday, September 22, 13
![Page 116: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/116.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Set vars & run the codeclass UbyInterpreter < SexpInterpreter # ...
def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
if recv then recv.send(msg, *args) # less of a hack else decls, body = self.env[msg]
decls.rest.zip(args).each do |name, val| self.env[name] = val end
process_block s(:block, *body) end endend
Sunday, September 22, 13
![Page 117: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/117.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
args... decls... zip bwuh?
Sunday, September 22, 13
![Page 118: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/118.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
EnvironmentEnvironment
n 21
life 42
pi 3
double
[ s(:args, :n),
s(:call, s(:lit, 2), :*, s(:lvar, :n))]
EnvironmentEnvironment
n 8
life 42
pi 3
double
[ s(:args, :n),
s(:call, s(:lit, 2), :*, s(:lvar, :n))]
s(:call, nil, :double, s(:lit, 21))
decls.rest.zip(args).each do |k, v| self.env[k] = vend
[:n].zip([21]).each do |k, v| self.env[k] = vend
self.env[:n] = 21
decls
args
Sunday, September 22, 13
![Page 119: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/119.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Voilà!# Running tests:
.....
Finished tests in 0.011826s, 422.7972 tests/s, 676.4756 assertions/s.
5 tests, 8 assertions, 0 failures, 0 errors, 0 skips
Sunday, September 22, 13
![Page 120: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/120.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
“Getting to green just means you don’t have enough
tests.”– Kent Beck
Sunday, September 22, 13
![Page 121: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/121.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Fibonacci
Sunday, September 22, 13
![Page 122: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/122.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Conditionals
Runtime
Parser
Test
Dri
ven
Environment
Loops
Functions
Variables
Sunday, September 22, 13
![Page 123: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/123.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Finally Freakin’ Fibonacci!class TestUbyInterpreter < MiniTest::Unit::TestCase # ...
def test_fib assert_eval nil, <<-END def fib n if n <= 2 then 1 else fib(n-2) + fib(n-1) end end END
assert_eval 8, "fib(6)" endend
Sunday, September 22, 13
![Page 124: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/124.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Whoa… it ran… but failed 1) Failure:TestUbyInterpreter#test_fib [./test/test_ruby_interpreter.rb:71]:Expected: 8 Actual: 3
6 tests, 10 assertions, 1 failures, 0 errors, 0 skips
Sunday, September 22, 13
![Page 125: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/125.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
EnvironmentEnvironment
n 4
life 42
pi 3
fib
[ s(:args, :n),
s(:if, ...)]
EnvironmentEnvironment
n 6
life 42
pi 3
fib
[ s(:args, :n),
s(:if, ...)]
EnvironmentEnvironment
n 8
life 42
pi 3
fib
[ s(:args, :n),
s(:if, ...)]
That stupid Table?Yeah...
def fib n if n <= 2 then 1 else fib(n-2) + fib(n-1) endendn = 8; fib(6)n = 6; fib(4) + fib(5)n = 4; ... and so on...
Sunday, September 22, 13
![Page 126: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/126.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
[ s(:args, :n),
s(:if, ...)]
fib
3pi
42life
n 8
[ s(:args, :n),
s(:if, ...)]
fib
3pi
42life
n 5
[ s(:args, :n),
s(:if, ...)]
fib
3pi
42life
n 3
[ s(:args, :n),
s(:if, ...)]
fib
3pi
42life
n 1
Wanted: Stacked Hashn 1
n 3
n 5
[ s(:args, :n),
s(:if, ...)]
fib
3pi
42life
n 8
fib(1)
fib(3)
fib(5)
original
but acts like:
Sunday, September 22, 13
![Page 127: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/127.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
6z
5y
4x
2b
3c
42x
1a
z 6
y 5
x 42
3c
2b
1a
Environment Classenv[:x] == ?
env[:x] = 42
all:
scope:
scope:
scope:
Sunday, September 22, 13
![Page 128: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/128.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!class UbyInterpreter < SexpInterpreter # ...
class Environment def [] k self.all[k] end
def []= k, v @env.last[k] = v end
def all @env.inject(&:merge) end
def scope @env.push({})
yield ensure @env.pop end
def initialize @env = [{}] end endend
Writes to top layer only
Reads from everything
Flattened. Newest wins
Automatic layering
Sunday, September 22, 13
![Page 129: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/129.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Replace stupid tableclass UbyInterpreter < SexpInterpreter # ...
def initialize # ... self.env = Environment.new end
# ...end
Sunday, September 22, 13
![Page 130: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/130.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Scope every callclass UbyInterpreter < SexpInterpreter # ...
def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
if recv then recv.send(msg, *args) # less of a hack else self.env.scope do decls.rest.zip(args).each do |name, val| self.env[name] = val end
process_block s(:block, *body) end end endend
Sunday, September 22, 13
![Page 131: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/131.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Finally# Running tests:
......
Finished tests in 0.016849s, 356.1042 tests/s, 712.2084 assertions/s.
6 tests, 12 assertions, 0 failures, 0 errors, 0 skips
Sunday, September 22, 13
![Page 132: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/132.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
While Loops
Sunday, September 22, 13
![Page 133: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/133.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Runtime
Environment
Variables
Functions
Parser
Test
Dri
ven
ConditionalsLoops
Sunday, September 22, 13
![Page 134: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/134.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Refactor Fibonnaciclass TestUbyInterpreter < MiniTest::Unit::TestCase # ...
def define_fib assert_eval nil, <<-END def fib n if n <= 2 then 1 else fib(n-2) + fib(n-1) end end END endend
Sunday, September 22, 13
![Page 135: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/135.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Add :while testsclass TestUbyInterpreter < MiniTest::Unit::TestCase # ...
def test_while_sum_of_fibs define_fib
assert_eval 1+1+2+3+5+8+13+21+34+55, <<-EOM n = 1 sum = 0 while n <= 10 sum += fib(n) n += 1 end sum EOM endend
Sunday, September 22, 13
![Page 136: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/136.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Unknown :while 1) Error:TestUbyInterpreter#test_while_fib:UnknownNodeError: Bug! Unknown node-type :while to UbyInterpreter ./lib/ruby_interpreter.rb:29:in `process_block' ./lib/ruby_interpreter.rb:28:in `each' ./lib/ruby_interpreter.rb:28:in `process_block' ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:86:in `test_while_fib'
Sunday, September 22, 13
![Page 137: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/137.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :while genericallyclass UbyInterpreter < SexpInterpreter # ...
def process_while s raise "Boom: #{s.inspect} endend
Sunday, September 22, 13
![Page 138: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/138.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
See sub-sexps 1) Error:TestUbyInterpreter#test_while:RuntimeError: Boom: s(:while, s(:call, s(:lvar, :n), :<, s(:lit, 41)), s(:lasgn, :n, s(:call, s(:lvar, :n), :+, s(:lit, 1))), true) ./lib/ruby_interpreter.rb:104:in `process_while' ./lib/ruby_interpreter.rb:29:in `process_block' ./lib/ruby_interpreter.rb:28:in `each' ./lib/ruby_interpreter.rb:28:in `process_block' ./lib/ruby_interpreter.rb:19:in `eval' ./test/test_ruby_interpreter.rb:15:in `assert_eval' ./test/test_ruby_interpreter.rb:62:in `test_while'
Sunday, September 22, 13
![Page 139: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/139.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Define :whileclass UbyInterpreter < SexpInterpreter # ...
def process_while s _, cond, *body = s body.pop # pre vs post condition -- ignore for now
while process cond process_block s(:block, *body) end endend
Sunday, September 22, 13
![Page 140: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/140.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Voilà# Running tests:
.......
Finished tests in 0.020218s, 346.2261 tests/s, 544.0696 assertions/s.
7 tests, 11 assertions, 0 failures, 0 errors, 0 skips
Sunday, September 22, 13
![Page 141: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/141.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
What Have We Done?
Sunday, September 22, 13
![Page 142: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/142.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!require "ruby_parser"require "sexp_processor"
class UbyInterpreter < SexpInterpreter VERSION = "1.0.0"
attr_accessor :parser attr_accessor :env
def initialize super
self.parser = Ruby19Parser. new self.env = Environment.new end
def eval src process parse src end
def parse src self.parser.process src end
def process_block s result = nil s.rest.each do |sub| result = process sub end result end
def process_call s _, recv, msg, *args = s
recv = process recv args.map! { |sub| process sub }
if recv then recv.send(msg, *args) else d, body = self.env[msg]
self.env.scope do d.rest.zip(args). each do |k, v| self.env[k] = v end
process_block s(:block, *body) end end end
def process_defn s _, name, args, *body = s
self.env[name] = [args, body]
nil end
def process_false s false end
def process_if s _, c, t, f = s
c = process c
if c then process t else process f end end
def process_lasgn s _, name, val = s
self.env[name] = process val end
def process_lit s s.last end
def process_lvar s _, name = s
self.env[name] end
def process_nil s nil end
def process_true s true end
def process_while s _, cond, *body = s body.pop
while process cond process_block s(:block, *body) end end
class Environment def [] k self.all[k] end
def []= k, v @env.last[k] = v end
def all @env.inject(&:merge) end
def scope @env.push({})
yield ensure @env.pop end
def initialize @env = [{}] end endend
Sunday, September 22, 13
![Page 143: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/143.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
class TestUbyInterpreter < MiniTest::Unit::TestCase attr_accessor :int
def setup self.int = UbyInterpreter.new end
def assert_eval exp, src, msg = nil assert_equal exp, int.eval(src), msg end
def define_fib assert_eval nil, <<-END def fib n if n <= 2 then 1 else fib(n-2) + fib(n-1) end end END end
def test_sanity assert_eval 3, "3" assert_eval 7, "3 + 4" end
def test_defn assert_eval nil, <<-EOM n = 24 def double n 2 * n end EOM
assert_eval 42, "double(21)" end
def test_fib define_fib
assert_eval 8, "fib(6)" end
def test_if assert_eval 42, "if true then 42 else 24 end" end
def test_if_falsey assert_eval 24, "if nil then 42 else 24 end" assert_eval 24, "if false then 42 else 24 end" end
def test_lvar assert_eval 42, "x = 42; x" end
def test_while_fib define_fib
assert_eval 1+1+2+3+5+8+13+21+34+55, <<-EOM n = 1 sum = 0 while n <= 10 sum += fib(n) n += 1 end sum EOM endend
Sunday, September 22, 13
![Page 144: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/144.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
“Uby” Language
• Basic numeric types, true, false, nil.• Conditional branching and looping.• Primitive & user defined functions.• Local variables & variable scoping.• Test-driven, extensible, patterns-based design.• ~2 hours, ~130 LOC impl, ~70 LOC test.• Fits in one head.
Sunday, September 22, 13
![Page 145: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/145.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
What Else Can We Do?• Add richer types: strings, arrays, hashes, etc.
• Enforce different scoping rules (eg. ruby's opaque def vs scheme's transparent lambdas).
• Implement recursive tail-calls or closures.
• Add an object system.
• Change the way functions are called.
Sunday, September 22, 13
![Page 146: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/146.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Further Study
Sunday, September 22, 13
![Page 147: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/147.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Sunday, September 22, 13
![Page 148: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/148.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Sunday, September 22, 13
![Page 149: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/149.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Fantastic Books
Sunday, September 22, 13
![Page 150: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/150.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Sunday, September 22, 13
![Page 151: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/151.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
No time for questions.
Please grab me at the afterparty.
Sunday, September 22, 13
![Page 152: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/152.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
"ank You…
Sunday, September 22, 13
![Page 153: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/153.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
This is the last talk!
Sunday, September 22, 13
![Page 154: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/154.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
I get to correct an injustice…
Sunday, September 22, 13
![Page 155: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/155.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
…from twitter.
Sunday, September 22, 13
![Page 156: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/156.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
He who made it…
Sunday, September 22, 13
![Page 157: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/157.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
…defines it.
Sunday, September 22, 13
![Page 158: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/158.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
“Sea!le S"le”
Sunday, September 22, 13
![Page 159: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/159.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Warning Free
Sunday, September 22, 13
![Page 160: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/160.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
No extra syntax
Sunday, September 22, 13
![Page 161: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/161.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Not in 'def' lines
Sunday, September 22, 13
![Page 162: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/162.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Not in calls
Sunday, September 22, 13
![Page 163: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/163.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Nowhere
Sunday, September 22, 13
![Page 164: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/164.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
This:remove_parens while warning_free and passes_tests
Sunday, September 22, 13
![Page 165: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/165.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Not this:remove_parens() while (warning_free() and passes_tests())
Sunday, September 22, 13
![Page 166: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/166.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Not Seattle Styledef method() call()end
def method(arg) call(arg)end
def method(arg1, arg2) call(arg1, arg2)end
call1(call2(call3()))
Sunday, September 22, 13
![Page 167: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/167.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
Seattle Styledef method callend
def method arg call argend
def method arg1, arg2 call arg1, arg2end
call1 call2 call3
call(/regexp/) # ambiguous w/o parens
Sunday, September 22, 13
![Page 168: Let’s Wr e an Interpreter! - zenspider.com · 2020-05-16 · GoGaRuCo 2013, San Francisco, CA Let’s Write an Interpreter! Ryan Davis, Seattle.rb Setting Expectations • 168 Slides](https://reader035.vdocuments.mx/reader035/viewer/2022070822/5f24e55968902772b5717c92/html5/thumbnails/168.jpg)
GoGaRuCo 2013, San Francisco, CA
Ryan Davis, Seattle.rbLet’s Write an Interpreter!
"ank You!
Sunday, September 22, 13