User Tools

Site Tools


Sidebar

Jump to
AmbientTalk
CRIME
iScheme

at:tutorial:metaprogramming

This is an old revision of the document!


This tutorial is under heavy construction!

Metaprogramming

AT Zero - AmbientTalk without syntactic sugar

In AmbientTalk, everything is an object, also native values such as numbers, booleans, strings and tables. Hence, the only way to interact with these values is by sending messages to them. However, there are some constructs in AmbientTalk that do not seem to send a message at all. Examples are table access/assignment, control structures and arithmic operations:

>[5,6,7][2]
>>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
>1.+(2.*(3))
>>7

Quasiquoting and splicing

In AmbientTalk, it is possible to quote an expression. In that case the quoted expression is not evaluated, but literally returned instead.

Quoting and expression is done with the ` operator. Quoting a number or a string results in the same number or string.

>`3 == 3
>>true
>`3 + 8
>>11
>`"text" == "text"
>>true

Quoting an identifer results in a symbol:

>foo
Undefined variable access: foo
>`foo
>>foo
>def o := object: { def m() { 5 } }
>><object:5800647>
>o.m()
>>5
>`(o.m())
>>o.m()

First-class abstract grammar

at/tutorial/metaprogramming.1176995213.txt.gz · Last modified: 2007/04/19 19:12 (external edit)