Skip to content
jckarter edited this page Nov 17, 2010 · 23 revisions

A large amount Clay syntax, including almost every operator, is actually simplified into plain function calls (or sequences of function calls) by the Clay compiler. This page aims to document all of these transformations.

In the following tables, single-letter identifiers (a, b, etc.) represent arbitrary single-value subexpressions. ...a etc. indicate zero or more arbitrary subexpressions yielding zero or more values. foo, bar, bas represent literal identifiers. 0, 1, etc. represent literal integers. All referenced function names are looked up in the prelude module unless otherwise indicated. Note that if, for example, you create a new function named add in a different module, that new function will not affect the behavior of the + operator. When overloading operators, you must use the overload keyword to modify the existing operator functions in their original modules.

Operator expressions

Operator expression Equivalent function expression Notes
+a plus(a)
-a minus(a)
&a __primitives__.addressOf(a) (1)
*a (see Variant Dispatch) (1)
a[...b] index(a, ...b)
a(...b) call(a, ...b) (2)
a.foo fieldRef(a, #foo)
a.0 staticIndex(a, static 0)
a^ dereference(a)
a * b multiply(a, b)
a / b divide(a, b)
a % b remainder(a, b)
a + b add(a, b)
a - b subtract(a, b)
a < b lesser?(a, b)
a <= b lesserEquals?(a, b)
a > b greater?(a, b)
a >= b greaterEquals?(a, b)
a == b equals?(a, b)
a != b notEquals?(a, b)
new a allocateShared(a)
if (a) b else c ifExpression(a, b, c) (3)
[...a] arrayLiteral(...a)
() tupleLiteral()
(a, ...b) tupleLiteral(a, ...b)

Notes:

  1. Not overloadable.
  2. In the absence of a call overload for a static callable, the callable is invoked directly.
  3. ifExpression only affects the expression form of if. if statements do not desugar into a function form.

Assignment operators

Assignment in Clay is not an expression; however, the assignment operators still desugar into function calls:

Assignment statement Equivalent function statement Notes
a = b; assign(a, b); (1)
a <-- b; (see Initialization)
a *= b; multiplyAssign(a, b);
a /= b; divideAssign(a, b);
a %= b; remainderAssign(a, b);
a += b; addAssign(a, b);
a -= b; subtractAssign(a, b);

Notes:

  1. var a = b; initializes a as a new value and behaves like <-- instead of calling assign. See Initialization. ref a = b; and alias a = b; bind a to b respectively as a greedy or lazy alias; there is no function equivalent to this operation.

Control Flow

Initialization

Variant Dispatch

Clone this wiki locally