Coder Social home page Coder Social logo

craftinterpreter's Introduction

Crafting interpreters

Working through the delightful book.

Chapter 4

  • use switch statements with disjoint branches to break free from myriad breaks

      case '(' -> addToken(LEFT_PAREN);
      case ')' -> addToken(RIGHT_PAREN);
      // ...
      case ' ', '\r', '\t' -> {}       // whitespace
      case '\n' -> line++;             // line ending
      // ...
      default -> { ... }
  • logically, scanner can be thought of as Source => Stream[Token]

    Lexical Analygator

  • "contiguous" errors can be batched as follows:

    • model errors as: record ScanError(int line, int start, int end, String message)
    • accumulate errors in JDK ArrayList
    • merge current error with last error if they are consecutive characters
    • since errors are appended to this list, insert/delete of last error is very cheap (an array access)

Chapter 5

  • sealed traits and pattern matching y'all

  • breakfast grammar:

    breakfast  -> protein "with" breakfast "on the side" ;
    breakfast  -> protein ;                => protein ("with" breakfast "on the side")? | bread
    breakfast  -> bread ;
               
    protein    -> crispiness "crispy" "bacon" ;
    protein    -> "sausage" ;
    protein    -> cooked "eggs" ;          => ("scrambled" | "poached" | "fried" ) "eggs" ;
               
    crispiness -> "really" ;
    crispiness -> "really" crispiness ;    =>  crispiness -> "really"+ ;
               
    cooked     -> "scrambled" ;
    cooked     -> "poached" ;              => cooked -> "scrambled" | "poached" | "fried" ;
    cooked     -> "fried" ;
               
    bread      -> "toast" ;
    bread      -> "biscuits" ;             => bread -> "toast" | "biscuits" | "English muffin";
    bread      -> "English muffin" ;
    

    in regex terms become:

    breakfast -> protein ( "with" breakfast "on the side" )? | bread ;
    protein   -> "really"+ "crispy" "bacon" | "sausage" | ( "scrambled" | "poached" | "fried" ) "eggs" ;
    bread     -> "toast" | "biscuits" | "English muffin" ;
    
  • lox init grammar:

    expression  ->  literal
                 | unary
                 | binary
                 | grouping ;
    literal     -> NUMBER | STRING | "true" | "false" | "nil" ;
    grouping    -> "(" expression ")" ;
    unary       -> ( "-" | "!" ) expression ;
    binary      -> expression operator expression ;
    operator    -> "==" | "!=" | "<" | "<=" | ">" | ">=" | "+"  | "-"  | "*" | "/" ;
    
  • operators:

    symbol repetition
    * 0 or more
    + at least once - 1 or more
    ? at most once - 0 or 1

craftinterpreter's People

Contributors

kogupta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.