types of macros
play

Types of macros What is a macro? Short for macroinstruction. Text - PDF document

9/16/14 CS152 Advanced Programming Language Principles Prof. Tom Austin, Fall 2014 For next class, install LaTeX Macros (pronounced "LAH-tech") Types of macros What is a macro? Short for macroinstruction. Text


  1. 9/16/14 ¡ CS152 – Advanced Programming Language Principles Prof. Tom Austin, Fall 2014 For next class, install LaTeX Macros (pronounced "LAH-tech") Types of macros What is a macro? • Short for macroinstruction. Text substitution • A rule or pattern that specifies how a certain input sequence should be Procedural macros mapped to a replacement sequence. Syntactic macros Text Substitution Macros A Review of Compilers This type of macro works by expanding text. Lexer/ Fast, but limited power. Some examples: source tokens Parser code Tokenizer • C preprocessor • Embedded languages (PHP, Ruby's erb, etc.) are similar, but more powerful Abstract Compiler Interpreter Syntax Tree (AST) Machine code Commands 1 ¡

  2. 9/16/14 ¡ Some variants work at C preprocessor example the token level, but the concept is the same. #define PI 3.14159 expanded Lexer/ Pre- source code tokens Parser code processor Tokenizer #define SWAP(a,b) {int tmp=a;a=b;b=tmp;} int main(void) { Abstract int x=4, y=5, diam=7, circum=diam*PI; Compiler Interpreter Syntax Tree SWAP(x,y); (AST) } Machine code Commands Procedural macros int main(void) { int x=4, y=5, diam=7, circum=diam*PI; SWAP(x,y); Procedural macros execute preprocessor Preprocessor } statements at compilation time. • Allows macros to be written in a procedural language. int main(void) { • More powerful than text substitution… int x=4, y=5, diam=7, circum=diam*3.14159; • …but slower and more complex compiler. {int tmp=x;x=y;y=tmp;}; } Macro expansion process Syntactic macros • Work at the level of abstract syntax trees Abstract Abstract Macro • From the Lisp family (including Scheme) Syntax Tree Syntax Tree Expander (AST) (AST) – Why Lisp? Because Lisp programs are ASTs • Powerful, but expensive • Where our attention will be focused Essentially this is a source-to-source compiler 2 ¡

  3. 9/16/14 ¡ Many macro systems suffer from a problem with inadvertent variable capture. Accidental Capture Example (in class) Let's look at an example… Hygiene Macros in Scheme • Scheme is noted for its powerful (and Hygienic macros are macros whose hygienic) macro system. expansion is guaranteed not to cause • Is it needed? Aren't lambdas enough? the accidental capture of identifiers . Pattern Based Macros (define (swap x y) (let ([tmp x]) • Preserves hygiene • define-syntax-rule matches the given (set! x y) pattern and transforms is into the specified (set! y tmp))) template • define-syntax is allows you to specify (let ([tmp 7][b 3]) multiple patters, including those with a (swap tmp b) variable number of arguments (using the … What is the result? syntax) (cons tmp b)) 3 ¡

  4. 9/16/14 ¡ Define-syntax swap function ( define-syntax-rule (swap x y) ( define-syntax swap (let ([tmp x]) ( syntax-rules () (set! x y) (set! y tmp))) [(swap x y) (let ([tmp x]) (let ([tmp 7][b 3]) (set! x y) (swap tmp b) What is the result (set! y tmp))])) now? (cons tmp b)) Lab For more reading on macros: • Matthew Flatt, "Pattern-based macros", section Using define-syntax, create a switch statement. 16.1 of "The Racket Guide". Sample usage: http://docs.racket-lang.org/guide/pattern- (define x 99) macros.html (switch x • Greg Hendershott, "Fear of Macros". [3 (displayln "x is 3")] http://www.greghendershott.com/fear-of- [4 (displayln "x is 4")] macros/index.html [5 (displayln "x is 5")] ['default (displayln "none of the above")]) 4 ¡

Recommend


More recommend