Why Macros?Language designers have to stop somewhereScheme-Style Macros: Patterns and Lexical Scope(544 pages)Matthew FlattNo language can provide every possible useful constructUniversity of UtahMacros let a programmer fill in gapsMacros versus Arbitrary Program Generators Macros versus Arbitrary Program GeneratorsMacros extend the language without extending the tool chain Macros extend the language without extending the tool chainJack (YACC for Java) requires a new tool chain: Scheme-YACC is a macro:Grammar Grammar Grammarjack.jack .class .scmmzcInterp SYACC Interp.jar .scm .exeRun Run Runjavac.java .class .scm? Jack doesn't play nice with all Java environments ? SYACC automatically plays nice with all Scheme environments... in principle1-9Macros and LibrariesMacros = hook in tool chain to extend a language Macros In GeneralPattern-Based MacrosScheme ensures that macros play nice with the tool chainScheme macro basicsExtended ExampleSome libraries include macrosLexical ScopeScheme ensures that library macros play nice with each otherGeneral TransformersState of the ArtPattern-Based Macros Scheme Macro BasicsMost popular API for macros: patterns (define-syntax swap )#define swap(x, y) (tmp=y, y=x, x=tmp)swap(c.red, d->blue)?(tmp=d->blue, d->blue=c.red, c.red=tmp)define-syntax indicates a macro definition+ Relatively easy for programmers to understand+ Obvious hook into the tool chain- Pure patterns can't easily express much.. ...
Voir