news.volyx.in

Writing a C compiler in 500 lines of Python (vgel.me)

510 points by vgel · 1086 days ago · 165 comments on HN

Article summary

The article discusses the author's experience writing a C compiler in 500 lines of Python, which required making significant decisions about what features to include and exclude. The compiler is a single-pass compiler, meaning it generates code during parsing, and targets WebAssembly. The author notes that this approach has limitations, such as poor code generation and limited error handling. Despite these limitations, the compiler is able to compile and run a subset of C programs.

Main themes

  • compiler design
  • single-pass compilation
  • C language
  • WebAssembly
  • optimization
  • parsing approaches
  • language design
  • compiler implementation
  • productivity and learning

What commenters say

  • Some commenters argue that the choice of language for the compiler does not affect the efficiency of the final output, while others suggest that it could impact compilation time.
  • The single-pass approach is seen as a limitation by some, who argue that it leads to poor code generation and makes optimization difficult.
  • Others note that C was designed to allow single-pass compilation, which made it easier to implement compilers in the past when memory was limited.
  • There is disagreement about whether the =+ operator, which was originally considered for C, would have been a good idea, with some arguing it would have been ambiguous and others suggesting it would have been useful.
  • Some commenters discuss the trade-offs between different parsing approaches, such as recursive descent and AST-based parsing, and their implications for compiler design.
  • The importance of optimization and analysis in modern compilers is highlighted, and it is noted that these tasks can be more complicated than simple compilation.
  • The idea that writing a compiler, or other complex projects, can be a path to becoming a highly productive developer is suggested, but it is also noted that this requires a significant amount of time and learning.
  • The relationship between language design and compiler implementation is discussed, with some arguing that certain language features, such as those in Rust, make single-pass compilation difficult or impossible.