Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hacking on a Swift Compiler

Hacking on a Swift Compiler

This is a talk I've presented on Swift Warsaw #6 during MCE Meetup Night 2016.

It tells a story of how I implemented a "unless" keyword by making the parser both aware of the new token and parsing it as a valid if statement.

Michał Kałużny

April 20, 2016
Tweet

Other Decks in Programming

Transcript

  1. I HAVE NO IDEA WHAT I'M DOING: Typing Random Stuff

    Till It Compiles Michał Kałużny
 @justMaku
  2. - (UIView *)findSuperViewWithClass:(Class)superViewClass { UIView *superView = self.superview; UIView *foundSuperView

    = nil; while (nil != superView && nil == foundSuperView) { if ([superView isKindOfClass:superViewClass]) { foundSuperView = superView; } else { superView = superView.superview; } } return foundSuperView; }
  3. Consume the "if" token Parse body Try to consume "else"

    keyword If it exists, parse "else" body Return IfStmt object
  4. makeParserResult(Status, new (Context) IfStmt(LabelInfo, IfLoc, Condition, NormalBody.get(), ElseLoc, ElseBody.getPtrOrNull())); makeParserResult(Status,

    new (Context) IfStmt(LabelInfo, UnlessLoc, Condition, EmptyBody.get(), UnlessLoc, NormalBody.get()));
  5. Hot takes • Swift's code is well written and properly

    commented. • Compilers are quite complicated.
  6. Hot takes • Swift's code is well written and properly

    commented. • Compilers are quite complicated. • But they're fun to play with!
  7. More info • Swift High Level IR: A case study

    Complementing LLVM IR with Language-Specific Optimization (Joeff Groff & Chris Lattner)
 http://bit.ly/llvm_sil
 • Contributing to Open Source Swift by Jesse Squires
 http://bit.ly/swift_contributing