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/05/04 02:41] 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 | ||
- | </ | ||
- | |||
- | Another special AmbientTalk construct is the literal closure. A literal closure is just an object with a special //apply// method that will execute the body of the literal closure. | ||
- | |||
- | ==== Quasiquoting and splicing ==== | ||
- | |||
- | === Qouting === | ||
- | |||
- | Any valid AmbientTalk expression can be quoted. This prevents the expression from being evaluated. Instead, it is returned literally. | ||
- | |||
- | Quoting an 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() | ||
- | </ | ||
- | |||
- | < | ||
- | Quoting a literal closure can be done in two ways: as an expression: < | ||
- | To quote a literal closure with the latter form, the vertical bars may not be ommited, even if the closure takes no arguments. < | ||
- | </ | ||
- | |||
- | == 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 ] | ||
- | >> | ||
- | </ | ||
- | |||
- | === Unquoting === | ||
- | |||
- | Inside a quotation, an expression can be unquoted as well. An unquotation escapes from the quotation and causes the unquoted expression to be evaluated. The return value is then used as the quotation of the unquotation. | ||
- | Unquoting an expression is done with the ''#'' | ||
- | |||
- | < | ||
- | >def msg() { `foo } | ||
- | >>< | ||
- | >def arg(n) { n+5 } | ||
- | >>< | ||
- | > | ||
- | >> | ||
- | </ | ||
- | |||
- | === Splicing === | ||
- | |||
- | Splicing can already be used without quoting: | ||
- | < | ||
- | >def upTo(n) { def idx := 0; def tab[n] { idx := idx + 1 } } | ||
- | >>< | ||
- | >[ 7, 8, 9, @upTo(4) ] | ||
- | >>[7, 8, 9, 1, 2, 3, 4] | ||
- | </ | ||
- | |||
- | Splicing can also be used in combination with quoting and unquoting. AmbientTalk provides the // | ||
- | < | ||
- | > | ||
- | >> | ||
- | >`[ @upTo(3), # | ||
- | >> | ||
- | </ | ||
- | |||
- | ==== First-class abstract grammar ==== | ||
- | |||
- | Quoting an AmbientTalk expression results in an the parse tree of that expression. Like any value in AmbientTalk, | ||
- | |||
- | < | ||
- | >`(f(1, 2, 3)).function | ||
- | >>f | ||
- | >`(f(1, 2, 3)).arguments | ||
- | >>[1, 2, 3] | ||
- | </ | ||
- | |||
- | Any abstract grammar element can serve as a prototype for a new one: | ||
- | < | ||
- | >def application := `(f(1, 2, 3)) | ||
- | >> | ||
- | > | ||
- | >> | ||
- | </ | ||
- | |||
- | ==== Read / Eval / Print ==== | ||
- | |||
- | AmbientTalk reifies the read, eval and print operations. This means that you can read any string and get the responding syntax tree for it, evaluate any syntax tree and get a value for it, and print any value and get a string representation of the value. | ||
- | |||
- | < | ||
- | >read: " | ||
- | >> | ||
- | >eval: `(1+2) in: self | ||
- | >>3 | ||
- | >print: self | ||
- | >>"< | ||
- | </ | ||
- | |||
- | Eval is a keyworded message that takes another parameter, namely the object in whose scope the expression must be evaluated. | ||
- | |||
- | < | ||
- | >def o := object: { def x := 4 } | ||
- | >>< | ||
- | >eval: `x in: o | ||
- | >>4 | ||
- | </ |