Language From Scratch
You'll build a tokenizer, a parser and an evaluator, and run a recursive Fibonacci in a language you designed yourself.
About
Build a small programming language. Not a toy calculator, but a real one with variables, functions, closures, conditionals and loops, able to run a recursive Fibonacci you wrote in it. Start from your Expression Parser if you have one. Crafting Interpreters by Bob Nystrom is the free book for this. Closures are where most people stall for two weeks, because the environment has to outlive the call that made it. It takes a season of evenings, and after it every language you touch looks like a set of choices someone made. The version that compiles to bytecode and runs on a garbage collector you wrote is over in Systems Programming.
At a glance
- Per requirement
- 50 XP
- Completion bonus
- 600 XP
- Requirements
- 6
- Takes
- A season
- Skill level
- Advanced (years of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Write a one-page spec for your language, with the syntax, the types and five example programs. Decide what it looks like before you write any code. Dynamic typing is fine, and pick a name while you're at it. The five examples are your test suite for the next three months. Take a photo of the spec. Photo
-
2
Extend your parser with statements, blocks, if, while and function declarations, and print the tree for all five examples. Use recursive descent, or Pratt parsing for the expressions. If you skipped the Expression Parser badge, do it first, because this one assumes it. Give your error messages line numbers. Take a screenshot of a tree for one example and a parse error with a line number. Photo
-
3
Implement an evaluator with variables, scoping and control flow. A tree-walking interpreter is enough. Use nested scopes with an environment chain. Get a loop that prints 1 to 10 working, then a nested if inside it. Film a short clip of three programs running. Video
-
4
Add functions with closures and recursion, and run Fibonacci of 25 in your language. Write a function that returns a function that remembers a variable from the enclosing scope. That's the test that separates a real language from a calculator. Then run fib(25) with a timing. Take a screenshot of the closure example and the fib output. Photo
-
5
Write a test suite of at least 50 programs with expected output, and run it with one command. Each test is a source file and an expected output file, and a runner compares them. Take a screenshot of the runner reporting 50 passing. Photo
-
6
Write a 200-line program in your language that does something useful, and publish the interpreter. A to-do list, a unit converter, or a Conway's Life simulation all work. Put the interpreter on a public repo with the spec and the examples. Take a screenshot of the program running and the repo page. Photo