(bernsteinbear.com) Writing a Lisp, Part 0: Fundamentals | Max Bernstein

ROAM_REFS: https://bernsteinbear.com/blog/lisp/00_fundamentals/

October 27, 2016

So you want to write a Lisp interpreter. I did too, and then I wanted to write about it, so here we are with this post series.

I initially wanted to write a Lisp interpreter as an exercise. I decided to write V1 in C (based on these two series of blog posts). After much pointer shenanigans and an unholy amount of curly braces later, it worked — but was significantly longer and harder to read than a Lisp interpreter should be. So I decided to write V2 in OCaml, a functional programming language descended from SML. The SML family of languages has some features like pattern matching and an extensive type system that make writing interpreters and compilers an absolute dream.

I'm going to walk through writing a fully-functional Lisp interpreter in OCaml. If you're following along with the OCaml, great. If you want to try and build it in SML or Haskell or whatever, it shouldn't be too hard to translate. If you want to follow along in C… well, good luck. It'll be a lot more code.

I'm going to build up the interpreter in stages, starting with symbols and other literals, then moving to simple math, then finally writing the features to support a metacircular evaluator (meaning that you can write another Lisp interpreter in the Lisp you just wrote!).

Links to:

Local Graph

org-roam 6b8df371-8461-41c3-ac08-69c6c1b75cbb (bernsteinbear.com) Writing a Lisp, P... //en.wikipedia.org/wiki/Lisp_(programming_language) https://en.wikipedia.org/wiki/Lisp_(programming_language) 6b8df371-8461-41c3-ac08-69c6c1b75cbb->//en.wikipedia.org/wiki/Lisp_(programming_language) //www.lwh.jp/lisp/ http://www.lwh.jp/lisp/ 6b8df371-8461-41c3-ac08-69c6c1b75cbb->//www.lwh.jp/lisp/ //peter.michaux.ca/articles/scheme-from-scratch-introduction http://peter.michaux.ca/articles/scheme-from-scratch-introduction 6b8df371-8461-41c3-ac08-69c6c1b75cbb->//peter.michaux.ca/articles/scheme-from-scratch-introduction //www.ocaml.org/ https://www.ocaml.org/ 6b8df371-8461-41c3-ac08-69c6c1b75cbb->//www.ocaml.org/ //en.wikipedia.org/wiki/Standard_ML https://en.wikipedia.org/wiki/Standard_ML 6b8df371-8461-41c3-ac08-69c6c1b75cbb->//en.wikipedia.org/wiki/Standard_ML 4be7aff1-9262-464c-aca9-5d9ad2745fb3 (www.lwh.jp) Building LISP 6b8df371-8461-41c3-ac08-69c6c1b75cbb->4be7aff1-9262-464c-aca9-5d9ad2745fb3 54a4a1ee-e04c-4119-90b5-a7cdbe7db42c (peter.michaux.ca) peter.michaux.ca -... 6b8df371-8461-41c3-ac08-69c6c1b75cbb->54a4a1ee-e04c-4119-90b5-a7cdbe7db42c c776bdf1-f65b-4c24-83a7-39e5d9d74e57 (troydm.github.io) Writing Micro Comp... 6b8df371-8461-41c3-ac08-69c6c1b75cbb->c776bdf1-f65b-4c24-83a7-39e5d9d74e57 9e9cb555-071b-4810-bacb-b4f3ef676728 (www2.cs.sfu.ca) LISP Tutorial 1: Bas... 6b8df371-8461-41c3-ac08-69c6c1b75cbb->9e9cb555-071b-4810-bacb-b4f3ef676728 64baaaf8-4641-4011-b2ab-364a132bf3e4 (buildyourownlisp.com) Contents • Bui... 6b8df371-8461-41c3-ac08-69c6c1b75cbb->64baaaf8-4641-4011-b2ab-364a132bf3e4 b172aa84-f547-455c-8771-1a32ce727293 (howtowriteaprogram.blogspot.com) How... 6b8df371-8461-41c3-ac08-69c6c1b75cbb->b172aa84-f547-455c-8771-1a32ce727293 //github.com/kanaka/mal/blob/master/process/guide.md https://github.com/kanaka/mal/blob/master/process/guide.md 6b8df371-8461-41c3-ac08-69c6c1b75cbb->//github.com/kanaka/mal/blob/master/process/guide.md f4759fd5-ce72-4517-b6e5-789626ae0c2f (github.com) kanaka/mal: mal - Make a... 6b8df371-8461-41c3-ac08-69c6c1b75cbb->f4759fd5-ce72-4517-b6e5-789626ae0c2f