GithubHelp home page GithubHelp logo

jlox's Introduction

Crafting an Interpreter

1 Top Level Structure

The main program runs either as a REPL or as a script executor. The source code is a string.

The run(string source) has four steps:

  • scan source to return a list of tokens
  • parse the list of tokens to return a list of statements
  • resolve the list of statements to add variable resolving data to the interpreter.
  • interpret/execute the list of statements

The first steps return compiling errors and the last step returns runtime errors. It uses the exit error code is defined in Unix's sysexits.

  • 64: wrong command line arguments.
  • 65: incorrect input data (compiling error)
  • 70: an internal software error (runtime error)

2 Grammars and Parser

The scanner uses a regular language grammar that can be implemented as a finite state automaton.

The parser uses a context-free language grammar that can be implemented as a pushdown automaton because it has recursive rules. The grammar is carefully written to avoid ambiguity and enable levels of precedenceallow and a top-down parser using recursive descent implementation.

It uses meta-programming to create two class trees representing the expressions and statements. The visitor pattern is used to process the trees.

The parser creates a list of statements using the tokens from the scanner.

3 Interpreter

Using the visitor pattern, the interpreter execute each statement and evaluate each expression. It creates environment to enable scoped variable binding.

4 Semantic Analysis

A resovler is used to walk the tree, visisting each node and conduct semantic analysis. It can find grammar errors and build meta-data used by the interpreter.

jlox's People

Contributors

yingliu4203 avatar

Watchers

 avatar  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.