josa techtalks - compilers, transpilers, and why you should care

22
Compilers, Transpilers, and Why You Should Care @amasad Facebook JS Infra

Upload: jordan-open-source-association

Post on 14-Aug-2015

200 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Compilers, Transpilers, and Why You Should

Care@amasad

Facebook JS Infra

Page 2: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Rich Programmer Food• Steve Yegge’s blogpost http://bit.ly/1ftuLcl

• Learning the basics of compilers isn’t about compilers; it’s about manipulating code

• In the same way that we manipulate data in our code we should be able to manipulate code

• It’s almost like a superpower

Page 3: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Real life situation• A new language feature came out. Say Arrow

Functions in JavaScript

• Your editor doesn’t support it and it’s acting weird

• Your codebase uses old function expressions and you want to be consistent

• You want to disallow new code to use function expressions

• IE7 doesn’t support it

Page 4: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Intro to compilers

• Compiler phases: Lexer, Parser, Optional Analysis, Code Generation (or interpreter in this case)

• I’ll use DOMQL, a toy language I built in 2011 to learn about compilers

Page 5: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

DOMQL

• A satirical language that allows DBAs to become web developers

• An SQL-like language for querying and manipulating the DOM

Page 6: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Example Queries

• SELECT DIV FROM BODY WHERE ID=‘container'

• UPDATE (SELECT H1 FROM (SELECT DIV FROM BODY).ALL) SET CLASS='active' WHERE CLASS=‘disabled'

• DELETE A FROM BODY.ALL WHERE HREF LIKE "google.com" AND ROWNUM BETWEEN 5 AND 10

Page 7: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Lexer

Page 8: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Whitespace

Page 9: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Keywords

Page 10: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Keywords

Page 11: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Literal

Page 12: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Parser Grammar

Page 13: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Select Node

Page 14: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Where Node

Page 15: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Compiled CSS Selectors

Page 16: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Demo

Page 17: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Problems Solved• Your editor doesn’t support new syntax. Add it to

the lexer and style it

• Your codebase uses old function expressions. Write a codemod transform

• You want to disallow new code to use function expressions. Write a Lint rule

• IE7 doesn’t support it. Write a compiler transform

Page 18: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Let’s write a lint rule

Page 19: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Let’s write a transformer

Page 20: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Babel Transformer

Page 21: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

One Language to Rule them All

• JavaScript ES2015+ (No HTML, No CSS, No nonsense)

• JSX

• Flow

• Inline styles

• React

• React Native

Page 22: JOSA TechTalks - Compilers, Transpilers, and Why You Should Care

Build your own

• Learn the basics of compilers (superpower)

• Build DSL, Tools, Codemods etc.

• Build your own language

• Think about ways to improve the web