Skip to content
strickyak edited this page May 26, 2012 · 18 revisions

TerseTalk Grammar Summary, from the bottom (binding tightest) up.

Comments

Comments are in double quotes.

Primitives

Numbers:

If you use a decimal point, have digits on both sides.

-1
42
0.0
3.14
6.022e23 

Strings:

Delimited by single quotes. no escapes, except double the single quote to mean a single quote.

'hello world' 
'Don''t Panic!'

Parenthesized Expressions:

Just what you expect.

Macros:

An identifier followed by a parenthesized expression, one or more times.

IF( a < b )THEN( a )ELSE( b )  "example of the IF-THEN-ELSE macro."

Sending Messages

Unary Messages

The message follows the receiver.

3.14 sin
(9, 16, 25) len
'Hello World' lower
x cls name            "sends 'cls' to object x, then sends 'name' to that" 

Binary Operators

The usual C meaning of these binary operators. The multiplicative operators * / % & bind tighter than the additive operators + - | ^, which bind tighter than relational operators == != < <= > >=.

Unary messages bind tighter than binary, so

( x sin * y cos + z * z ) sqrt

means the sin of x times the cos of y, add the square of z to that, then take sqrt.

Warning: For lexical reasons, always put spaces around operators "-" and "/".

Keyword Messages

Messages with 1 or more arguments (in addition to the receiver) (that are not the Binary Operator messages above) use an identifier followed by a colon followed by an argument, for each argument. Unary Messages and Binary Operators bind tighter than Keyword Messages.

Here's an example using both "at:" and "at:put:" messages.

  resultVec at: s len + 1 put: (anotherVec at: j - 3)

"len" is sent to "s", then 1 is added, to make the first argument to "at:put:". Then 3 is subtracted from j, and the "at:" message is sent to "anotherVec", to make the second argument to "at:put:". Finally the "at:put:" message is sent to "resultVec" with two arguments.

List Constructors (also called Tuples)

All of the messages above bind tighter than List Constructors. There are two List Constructors, "," and ";". The constructor "," binds tighter than ";". Multiple occurrences of "," or ";" at the same grammatical level are recognized together to construct lists with more than 2 elements. Redundant "," and ";" at the front or end of the list are allowed.

10, 10; 100, 200; 111, 222; 888,999

defines a list of 4 elements, each of which is a list of 2 elements.

Note: "List", "Vec", and "Tuple" are all synonymous.
They are the same thing in TerseTalk. They are all instances of class "Vec".

Assignment Expression

i= i + 1.     "simple assignment"
x,y= 500, 600.  "destructuring assignment"

Sequence Expressions

Expressions in a sequence are separated by periods. Redundant periods at the front or end of a sequence of expressions are allowed. The value of a sequence of expressions is the value of the last expression. Warning: Each such period should be followed by white space.

self color: 'blue'. self size: (100, 200).
self redraw.

MACROS

Here are the most common macros.

Control Macros

p= IF( x )THEN( y )ELSE( z ).  "The macro returns a value, either y or z."

FOR( i: 100 )DO( self at: i put: i * i. ). "i ranges 0 to 99."
squares = FOR( i: 100 )MAP( i * i ). "The macro returns a list of 100 elements."

"Iterating items in a list:"
FOR( x: v )DO( self say: ('"', x str, '" is an item.')jam ).

"Iterating key-value pairs in a dict:"
FOR( k,v: d )DO( self say: ('"', k str, '" maps to "', v str, '"')jam ).

Constructor Macros

v= VEC( a, b, c ).      "Same as v= a, b, c."
d= DICT( 'color', 'red';  'size', 'XL';  'corner', (100, 200); ).

VEC() is a list constructor. Notice the alternation of "," and ";" in the DICT constructor. Keys and Values have "," in between; Key-Value pairs are separated by ";"s.