This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:metaprogramming [2007/04/19 20:34] stimberm |
at:tutorial:metaprogramming [2009/11/21 07:44] tvcutsem |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
- | ===== Metaprogramming ===== | ||
- | |||
- | ==== AT Zero - AmbientTalk without syntactic sugar ==== | ||
- | |||
- | In AmbientTalk, | ||
- | < | ||
- | > | ||
- | >>6 | ||
- | >if: 1 == 2 then: { 13 } else: { 42 } | ||
- | >>42 | ||
- | >1 + 2 * 3 | ||
- | >>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: | ||
- | < | ||
- | >[5, 6, 7].at(2) | ||
- | >>6 | ||
- | >(1 == 2).ifTrue: { 13 } ifFalse: { 42 } | ||
- | >>42 | ||
- | > | ||
- | >>7 | ||
- | </ | ||
- | |||
- | ==== Quasiquoting and splicing ==== | ||
- | |||
- | === Qouting === | ||
- | |||
- | Any valid AmbientTalk expression can be quoted. This prevents the expression from being evaluated. Instead, it is returned literally. | ||
- | |||
- | Quoting and expression is done with the '' | ||
- | |||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | |||
- | == Literal values == | ||
- | |||
- | Quoting a literal value such as a number or a string results in the exact same number or string. | ||
- | < | ||
- | >`3 == 3 | ||
- | >> | ||
- | >`3 + 8 | ||
- | >>11 | ||
- | > | ||
- | >> | ||
- | </ | ||
- | |||
- | An identifer can also be quoted. This returns a symbol object: | ||
- | < | ||
- | >foo | ||
- | Undefined variable access: foo | ||
- | >`foo | ||
- | >>foo | ||
- | > | ||
- | >>" | ||
- | </ | ||
- | |||
- | Note that in '' | ||
- | |||
- | == Expressions == | ||
- | |||
- | To quote a complete expression, it has to be wrapped in parantheses: | ||
- | |||
- | < | ||
- | > | ||
- | >> | ||
- | > | ||
- | >> | ||
- | >`(1+2) | ||
- | >> | ||
- | > | ||
- | >> | ||
- | </ | ||
- | |||
- | == Statements == | ||
- | |||
- | Statements (definitions, | ||
- | |||
- | < | ||
- | >`{ def a := 4 } | ||
- | >>def a := 4 | ||
- | >`{ def tab[5] { m() }; tab[3] := n() } | ||
- | >>def tab[5] { m()}; tab[3] := n() | ||
- | </ | ||
- | |||
- | If you actually want to quote a literal closure, you should use parantheses, | ||
- | < | ||
- | `{ | foo | foo := 4 } gives a literal closure AG???? | ||
- | </ | ||
- | |||
- | == Tables == | ||
- | |||
- | Literal tables can already be defined as follows: | ||
- | < | ||
- | >def tab := [ 1+2, 3+4, 5+6 ] | ||
- | >>[3, 7, 11] | ||
- | </ | ||
- | 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: | ||
- | < | ||
- | >def tab := `[ 1+2, 3+4, 5+6 ] | ||
- | >> | ||
- | </ | ||
- | |||
- | === Splicing === | ||
- | |||
- | |||
- | ==== First-class abstract grammar ==== |