Writing a
Python interpreter from scratch

in half an hour.

Tushar Sadhwani

Python static analysis @ DeepSource

But why?

A long time ago,

I wanted to build a JSON parser.

Values are strings!

2 keys!

So how do we do that?

Grammar:
object := '{' (item ',')* item '}'
item := key ':' value (assume strings)
Object
Grammar:
object := '{' (item ',')* item '}'
item := key ':' value
Item
Grammar:
object := '{' (item ',')* item '}'
item := key ':' value
Object
Key
Value
Grammar:
object := '{' (item ',')* item '}'
item := key ':' value
Object

That's all there is to parsing.

structured input → easily accessible information

Let's look at its code.

Recursive descent parsing.

Quick demo!

Now we'll build a Python interpreter for the JSON parser.

First things first,

Interpreter

Tokenizer
Parser
Interpreter

Tokenizer

Change #1: Newlines are important.

Change #2: Indentation matters.

Change #2: Indentation matters.

Change #2: Indentation matters.

Parser

Parser

Abstract Syntax Tree (AST)

Parsing is best explained Live

A note on blocks

A note on blocks

A note on blocks

A note on blocks

A note on blocks

The Interpreter

How does code run?

Top to bottom,
left to right.

The program:

Visitor pattern.

(demo incoming)

Interpreting functions

(another demo incoming)

Bonus:

And that's it!

Bytecode VM

C extensions

Type hints

Classes

I/O

Threads

Imports

Exceptions

Walrus

Pattern matching

async await

closures

Generators

dunder methods

dis

urllib

asyncio

unittest

concurrent.futures

http

functools

collections

xml

ast

Interested? Read these!

You can help implement these:

Thank you.

Tushar Sadhwani

Python static analysis @ DeepSource

Writing a Python interpreter from scratch, in half an hour.

By Tushar Sadhwani

Writing a Python interpreter from scratch, in half an hour.

  • 174