This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
at:tutorial:metaprogramming [2007/05/04 02:41] stimberm |
at:tutorial:metaprogramming [2008/08/01 14:31] tvcutsem * |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
- | |||
===== Metaprogramming ===== | ===== Metaprogramming ===== | ||
Line 14: | Line 12: | ||
>>7 | >>7 | ||
</ | </ | ||
- | However, these constructs are all **syntactic sugar**. Behind the scenes, they all perform message sends. The following code shows the equivalents of the previous code, but with the actual message sends: | + | However, these constructs are all **syntactic sugar** |
< | < | ||
>[5, 6, 7].at(2) | >[5, 6, 7].at(2) | ||
Line 24: | Line 22: | ||
</ | </ | ||
- | Another special AmbientTalk construct | + | In the first example above the message // |
==== Quasiquoting and splicing ==== | ==== Quasiquoting and splicing ==== | ||
+ | Quasiquoting and splicing are an advanced and powerful metaprogramming techniques that control the evaluation process and allow one to manipulate the evaluation process of the abstract syntax tree. | ||
- | === Qouting | + | === Quoting |
Any valid AmbientTalk expression can be quoted. This prevents the expression from being evaluated. Instead, it is returned literally. | Any valid AmbientTalk expression can be quoted. This prevents the expression from being evaluated. Instead, it is returned literally. | ||
Line 61: | Line 60: | ||
</ | </ | ||
- | Note that in '' | + | Note that in '' |
== Expressions == | == Expressions == | ||
- | To quote a complete expression, it has to be wrapped in parantheses: | + | To quote a complete expression, it has to be wrapped in parentheses: |
< | < | ||
Line 80: | Line 79: | ||
== Statements == | == Statements == | ||
- | Statements (definitions, | + | Statements (definitions, |
< | < | ||
Line 103: | Line 102: | ||
With this construct, all the elements of the literal table are evaluated. By quoting a literal table, all the elements are quoted instead of evaluated: | With this construct, all the elements of the literal table are evaluated. By quoting a literal table, all the elements are quoted instead of evaluated: | ||
< | < | ||
- | > | + | > |
>> | >> | ||
</ | </ | ||
Line 129: | Line 128: | ||
>[ 7, 8, 9, @upTo(4) ] | >[ 7, 8, 9, @upTo(4) ] | ||
>>[7, 8, 9, 1, 2, 3, 4] | >>[7, 8, 9, 1, 2, 3, 4] | ||
+ | >[ 7, 8, 9, upTo(4) ] | ||
+ | >>[7, 8, 9, [1, 2, 3, 4]] | ||
</ | </ | ||
+ | In the example above the elements of the table returned by invoking **upTo(4)** are added in place to the table in which the expression was spliced. | ||
Splicing can also be used in combination with quoting and unquoting. AmbientTalk provides the // | Splicing can also be used in combination with quoting and unquoting. AmbientTalk provides the // | ||
Line 156: | Line 158: | ||
> | > | ||
>> | >> | ||
+ | </ | ||
+ | |||
+ | The following example uses meta-programming and reflection to generate a proxy for an object that provides a given interface. The interface' | ||
+ | |||
+ | < | ||
+ | TODO: Update code | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | def isMethodDefinition: | ||
+ | |||
+ | def policyOf: object with: interface { | ||
+ | |||
+ | def policyDefinition := interface.method.bodyExpression.statements; | ||
+ | |||
+ | policyDefinition.each: | ||
+ | if: (isMethodDefinition: | ||
+ | def methodBody := statement.bodyExpression.statements; | ||
+ | if: ((methodBody.length == 1).and: { methodBody[1] == `nil }) then: { | ||
+ | def bodyExpression := `{ #(object) ^ # | ||
+ | statement.bodyExpression := bodyExpression; | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | object: interface; | ||
+ | |||
+ | }; | ||
</ | </ | ||
Line 165: | Line 195: | ||
>read: " | >read: " | ||
>> | >> | ||
- | >eval: `(1+2) in: self | + | >def result := eval: `(1+2) in: self |
>>3 | >>3 | ||
- | > | + | > |
- | >>" | + | >>" |
</ | </ | ||
Line 175: | Line 205: | ||
< | < | ||
>def o := object: { def x := 4 } | >def o := object: { def x := 4 } | ||
- | >>< | + | >>< |
>eval: `x in: o | >eval: `x in: o | ||
>>4 | >>4 | ||
</ | </ |