IUCompilerCourse / Essentials-of-Compilation

A book about compiling Racket and Python to x86-64 assembly
1.31k stars 141 forks source link

Struct-based syntax? #30

Closed rrnewton closed 4 years ago

rrnewton commented 5 years ago

Why do we use S-expressions for syntax? It has a certain convenience, but the students pay for it in any number of small ways. How many times do they end up with "extra parentheses", because non-type-safe operations are applied to expressions in list form?

If anything, struct-based pattern matching is actually cleaner in racket than quasiquoted list patterns:

#lang racket
(struct foo (a b))
(match (foo 1 2)
  [(foo a b) a])

Struct based ASTs would be a major change to the book, so it shouldn't be undertaken lightly. As a stop-gap I have recommended that students make more defensive helper functions like "append two lists of instructions" that use a contract to (deeply) check the validity of their inputs. Presently, list-based S-expressions combine with the slightly underspecified grammars (#29) to create deviations between the student compilers and the textbook (more importantly, the expectations of the testing-framework / interpreters).

Disadvantages / Alternatives

Aside from the cost of the change itself:

jsiek commented 4 years ago

I've gone ahead and switched to struct-based ASTs in the course-compiler and now I'm updating the book.