Download - Metaprogramming
![Page 1: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/1.jpg)
![Page 2: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/2.jpg)
•
•
•
![Page 3: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/3.jpg)
![Page 4: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/4.jpg)
•
•
•
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
![Page 5: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/5.jpg)
•
•
–
–
–
•
–
•
–
•
![Page 6: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/6.jpg)
•
–
•
–
–
–
–
•
–
–
•
–
•
•
–
•
•
![Page 7: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/7.jpg)
•
– #define sqr(x) ((x)*(x))int n = sqr(2);// emits int n = ((2)*(2));
•
–pow(x,n)
•
•
–#define
•
•
•
•
![Page 8: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/8.jpg)
![Page 9: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/9.jpg)
class MyMacro(AbstractAstMacro)
{
override def Expand(macro as MacroStatement)
{
// macro.Argument[n]
// macro.Body
yield/return
}
}
• MyMacro
• MyMacro 1 2
• MyMacro:print “test”
![Page 10: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/10.jpg)
class MyMacro(AbstractAstMacro)
{
override def Expand(macro as MacroStatement)
{
// macro.Argument[0]
// macro.Body
yield/return
}
}
macro MyMacro (n as int):
// MyMacro.Argument[n]
// MyMacro.Body
yield/return
![Page 11: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/11.jpg)
k
•
•
•
•
–
•
– →
– →
•
–
–
–
•
![Page 12: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/12.jpg)
![Page 13: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/13.jpg)
macro dateNow:
now = DateTime.Now
return [|
print $(now.ToLocalTime().ToString())
|]
![Page 14: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/14.jpg)
macro RepeatCStyle(c as int):
return [|
for i in range($c):
$(RepeatCStyle.Body)
|]
macro RepeatInline(c as int):
body = RepeatInline.Body
yieldAll body for i in range(c)
RepeatXxx 3:print “Go”
Console.WriteLine("Go");Console.WriteLine("Go");Console.WriteLine("Go");
int ___temp14 = 0;while (___temp14 < 3){int i = ___temp14;___temp14++;Console.WriteLine("Go");
}
![Page 15: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/15.jpg)
•
•
•
•
–
![Page 16: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/16.jpg)
[meta] def assert(condition as Expression):
return [|
if not $condition:
raise AssertionFailedException( $(condition.ToCodeString()))
|]
![Page 17: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/17.jpg)
•
•
–
•
•
–
•
![Page 18: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/18.jpg)
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
![Page 19: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/19.jpg)
•
•
– [Timestamp]public class C:public def constructor():pass
•
– c = C()print c.GetTimestamp()
![Page 20: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/20.jpg)
![Page 21: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/21.jpg)
j
•
–
–
•
•
•–
•
–
–
•
–
–
•
![Page 22: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/22.jpg)
•
–
–
•
–
–
–
•
–
![Page 23: Metaprogramming](https://reader033.vdocument.in/reader033/viewer/2022060119/558d44f7d8b42a4d318b4697/html5/thumbnails/23.jpg)