Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Let's Write an Interpreter - Ryan Davis (Seattle.rb)
- - Setting Expectations
- - 168 slides in 30 minutes
- - Almost all content is code (I'm not going to copy it over... seriously checkout the slides)
- - Why would you want to write an interpreter
- - Because we can
- - The "Uby" Interpreter
- - branching logic, while loops, local states and functions
- - roadmap (in descending order)
- - parser
- - runtime
- - environment
- - loops, variables and conditionals
- - functions
- - parser
- - ruby_parser, as uby is a subset of ruby anyway
- - takes input and produces an abstract syntax tree (AST)
- - s-expression vocabulary (aka SEXP)
- - s = sexp
- - head/type (car)
- - <too fast, read the slides>
- - not going into the parser since we're ruby_parser
- - runtime (how to get 7 from 3+4)
- - start with the source, process into AST, and then execute
- - applicative order evaluation (not all languages do that)
- - we'll do it inner values first
- - using ruby to deal with internals is often referred to as a meta-circular interpreter
- - test-driven
- - spans whole implementation
- - sanity test
- - in the runtime, implement test infrastructure (TestUbyInterpreter assigned to iVar)
- - assert_eval 3, "3"
- - assert_eval 7, "3+4"
- - error, nothing implemented
- - implement
- - subclass from SexpInterpreter
- - define eval (parses text first, and passes off to process method)
- - #process knows how to dispatch into the AST
- - "Unknown node type :lit"
- - "Unknown node type :call"
- - Defined #process_lit and #process_call
- - in #process_call
- - unpack arguments ``` _, recv, msg, *args = s ```
- - recv = process recv
- - map arguments through process sub
- - send msg to recv
- - we have sanity, examples pass
- - conditionals and truthyness
- - truthyness is ruby, nil and false are falsy and everything else is true
- - add some if tests test_if
- - "Unknown node type :if"
- - define generic handler #process_if
- - _, c, t, f = s
- - c = process c
- - define #process_nil and #process_false and use nil and false from ruby
- - we have conditionals and truthyness
- - add a failing test -> error node-type -> generic handler -> implement test data -> pass test --<
- - minitest in use
- - [Vizzini] Where was I!
- - local variables
- - "Unknown node type :block"
- - you need a container to handle multiple statements
- - define #process_block
- - subsexp lasgn (local assign)
- - define #process_lasgn (after creating @env hash to hold variables)
- - "Unknown node type :lvar"
- - define #process_lvar , it's a reader so pull it out of @env
- - functions
- - add defn test
- - "Unknown node type :defn"
- - unpack and store: because this is a function and we're defining it we're extracting and storing the function information in a function table
- - "Undefined method double for nil"
- - recv.send is a hack, use only for ruby derived functions
- - use a table to handle sending messages to user defined functions
- - Process for call
- - set up environment
- - call function
- - ensure last executed value is returned out of the function
- - Note that we blow away the environment
- - Getting to green means you don't have enough tests. - Kent Beck
- - fixing the environment
- - Stacked Hash -> Environment Class
- - replace stupid environment hash with Environment Class
- - while loops
- - Refactoring Fibonacci into iterative form
- - "Unknown node type :while"
- - define generic implementation #process_while
- - _, cond, *body = s
- - pop body because we don't care about the pre/post condition
- - while process cond is truthy call function implementation
- - Uby overview
- - basic numeric types, true, false, nil
- - conditional branching and looping
- - primitive and user defined functions
- - <too fast>
- - Extensible
- - richer types: strings, arrays, hashes
- - enforce different scoping rules
- - implement recursive tail-calls or closures
- - add and object system
- - change the way functions are called
- - Further study
- - SICP (structure and interpretation of computer programs)
- - [Neo Matrix Meme] I know SICP
- - The Little Schemer and The Reasoned Schemer <missed one title, sorry>
- - Prolog (the language)
- - Correcting Twitter re: Seattle-style
- - <I may not have gotten all of these, speaker moves too fast>
- - we don't use warn... it craps on test output
- - Seattle-style
- - no extra syntax (parentheticals especially)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement