Intro to Compilers¶
What is a Compiler?¶
A function that maps an input string to an output string. Generally, these represent programs.
For examples, most language compilers compile human readable text into bytecode.
- The output program should:
have the same meaning as the input
be runnable on hardware
What do they look like?¶
Source string is parsed into an AST (Abstract Syntax Tree)
AST is checked to make sure it makes sense
AST is simplified into IR (Intermediate Representation)
IR is optimized
IR -> CodeGen -> ASM (Assembly for architecture)
ASM is linked against a runtime (e.g. stdlibs)
Intro to Haskell¶
Haskell is neat.
Quicksort:
sort [] = []
sort x:xs = sort l ++ [x] ++ sort r
where
l = [e | e <- xs, e <= x]
r = [e | e <- xs, e > x]
Programs in Haskell are expressions, not a sequence of statements; it evaluates to a value and has no side effects
Functions are first-class values
See last quarter’s notes.