Upcoming SlideShare
×

# Metaprogramming

1,579 views

Published on

Metaprogramming with the Boo programming language.

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,579
On SlideShare
0
From Embeds
0
Number of Embeds
49
Actions
Shares
0
8
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Metaprogramming

1. 1. • • •
2. 2. • template<int n> struct Factorial { enum { RET = n * Factorial<n-1>::RET • } }; template<> struct Factorial<0> { enum { • RET = 1 } }; ⋮ int n = Factorial<5>::RET; // 120
3. 3. • • • – – – • – • –
4. 4. • • – – • – • – • – – – • • • – –
5. 5. • • – #define sqr(x) ((x)*(x)) int n = sqr(2); // emits int n = ((2)*(2)); – • #define • – pow(x,n) • • • •
6. 6. class • MyMacro MyMacro(AbstractAstMacro) { override def Expand • MyMacro 1 2 (macro as MacroStatement) { // macro.Argument[n] • MyMacro: // macro.Body print “test” yield/return } }
7. 7. class MyMacro(AbstractAstMacro) { macro MyMacro (n as int): override def Expand (macro as MacroStatement) // MyMacro.Argument[n] { // MyMacro.Body // macro.Argument[0] yield/return // macro.Body yield/return } }
8. 8. k • • – • • – → – → • • – – – •
9. 9. macro dateNow: now = DateTime.Now return [| print \$(now.ToLocalTime().ToString()) |]
10. 10. macro RepeatCStyle(c as int): macro RepeatInline(c as int): return [| body = RepeatInline.Body for i in range(\$c): yieldAll body for i in range(c) \$(RepeatCStyle.Body) |] RepeatXxx 3: print “Go” int ___temp14 = 0; Console.WriteLine("Go"); while (___temp14 < 3) Console.WriteLine("Go"); { Console.WriteLine("Go"); int i = ___temp14; ___temp14++; Console.WriteLine("Go"); }
11. 11. • • • • –
12. 12. [meta] def assert(condition as Expression): return [| if not \$condition: raise AssertionFailedException(  \$(condition.ToCodeString())) |]
13. 13. • • • • – • –
14. 14. public class  TimestampAttribute(AbstractAstAttribute): public override def Apply(node as Node): c = node as ClassDefinition now = DateTime.Now m = [| public def GetTimestamp(): return \$(now.ToLocalTime().ToString()) |] c.Members.Add(m) // a new member added to // an existing class
15. 15. • • – [Timestamp] public class C: public def constructor(): pass • – c = C() print c.GetTimestamp()
16. 16. j • • – – •  – – • – • •  – – •
17. 17. • – – • – – – • –