4. Code Generation: From Model to Code
Model
★ structured representation (abstract syntax)
★ produced by parser
★ checked for consistency
★ transformed to core language
Code
★ program text
★ no structure
★ for consumption by interpreter or compiler
5. Example: Data Model to Database Schema
entity Blog {
name :: String
entries :: List<BlogEntry>
}
CREATE TABLE IF NOT EXISTS ‘Blog‘ (
‘id‘ int(11) NOT NULL auto_increment,
‘name‘ text,
PRIMARY KEY (‘id‘)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS ‘Blog_entries_BlogEntry‘ (
‘parent‘ int(11) NOT NULL,
‘value‘ int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
9. Printing Code
Advantages
★ accessible (easy to implement in any language)
★ concrete syntax
★ good performance
Disadvantages
★ order of evaluation
★ code fragments disrupted to splice meta-data
★ no formatting
14. Template Engine
<<DEFINE entity_to_sql FOR entity>>
DROP TABLE IF EXISTS ‘<<entity.name>>‘;
CREATE TABLE IF NOT EXISTS ‘<<entity.name>>‘ (
‘id‘ int(11) NOT NULL auto_increment,
<<EXPAND type_to_sqltype FOREACH entity.fields>>
PRIMARY KEY (‘id‘)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
<<ENDDEFINE>
DSL for model to text generation
15. Template Engine
Advantages
★ concrete syntax
★ less quoting
★ long code fragments
★ anti-quotation instead of string concatenation
★ no escapes of string meta characters
Disadvantages
★ no syntax check
★ result is string / printed
★ no structure
★ integrate separate language
18. String Interpolation
Advantages
★ concrete syntax
★ assembly of code fragments independent of order of evaluation
★ long code fragments
★ anti-quotation instead of string concatenation
★ no escapes of string meta characters
★ formatting, relative indentation
Disadvantages
★ escaping of string interpolation meta characters
20. Code Generation by Model Transformation
Code represented as AST
★ (typechecked) structured representation
Text generation by pretty-printing
★ separate code formatting from code generation
Apply transformations after generation
★ aspect weaving
★ optimization
21. Composing Abstract Syntax Trees
grammar-to-signature :
Grammar(ss, ps) -> Signature(ss, cds)
where cds := <map(production-to-consdecl)> ps
production-to-consdecl :
Prod(symbols, sym, annos) -> ConsDecl(x, types, sort)
where x := <annos-to-constructor> annos
; types := <filter(symbol-to-type)> symbols
; sort := sym
annos-to-constructor :
[Anno(Application("cons", [Constant(c)]))] -> constr
where constr := <unescape; un-double-quote> c
22. Composing Abstract Syntax Trees
Advantages
★ syntax check through type system (if available)
★ automatic formatting
★ transformation after generation
Disadvantages
★ no concrete syntax
29. Extending the Meta-Language (1)
module Stratego
exports
context-free syntax
Int -> Term {cons("Int")}
String -> Term {cons("Str")}
Var -> Term {cons("Var")}
Id "(" {Term ","}* ")" -> Term {cons("Op")}
Term "->" Term -> Rule {cons("RuleNoCond")}
Term "->" Term "where" Strategy -> Rule {cons("Rule")}
32. Summary of Code Generation Techniques
Print statements String interpolation
★ easy ★ template engine built-in
String composition Abstract syntax
★ out of order evaluation ★ precise, transformation
after generation
Template engines
Concrete object syntax
★ less quoting
★ precise
★ concrete
33. Schedule
Case 3
★ Deadline extension: May 9 23:59
Case 4
★ `take home exam’
★ Questions about lectures 11 to 14
★ Deadline: June 15
Design 2
★ Syntax for your DSL
Next
★ No lecture next week (May 11)
★ Lecture 11: code generation policies