Algosim documentation: Expressions

Expressions

Everything in the Algosim language is an expression. An expression is made up of symbols, function calls, and operators; however, every operator is mapped to (or “implemented by”) a function, so when an expression has been parsed, it consists only of symbols and function calls. When evaluated, an expression returns an object (or “value”).

Operators

Algosim supports many operators:

Sometimes, special brackets are also considered as circumfix operators:

Some symbols are mapped to different operators depending on the context. For instance, can be both a prefix (unary) and an infix (binary) operator:

−2^10
5264 − 1054

In order of precedence, the available (non-circumfix) operators are

1 . (infix) 2 (postfix) 3 ° (postfix) 4 ! (postfix) 5 ? (postfix)
6 % (postfix) 7 * (postfix) 8 # (postfix) 9 (prefix) 10 ¬ (prefix)
11 # (prefix) 12 § (prefix) 13 & (prefix) 14 (prefix) 15 (prefix)
16 + (prefix) 17 ^ (infix) 18 + (prefix) 19 (prefix) 20 (infix)
21 (infix) 22 × (infix) 23 @ (infix) 24 / (infix) 25 (infix)
26 (infix) 27 (infix) 28 + (infix) 29 (infix) 30 (infix)
31 (infix) 32 (infix) 33 (infix) 34 (infix) 35 (infix)
36 (infix) 37 (infix) 38 (infix) 39 (infix) 40 (infix)
41 (infix) 42 (infix) 43 (infix) 44 (infix) 45 (infix)
46 (infix) 47 (infix) 48 (infix) 49 = (infix) 50 (infix)
51 > (infix) 52 (infix) 53 < (infix) 54 (infix) 55 (infix)
56 (infix) 57 (infix) 58 (infix) 59 (infix) 60 (infix)
61 (infix) 62 (infix) 63 (infix) 64 (infix) 65 (infix)
66 : (infix) 67 (infix) 68 \ (infix) 69 | (infix) 70 (infix)
71 ; (infix)

Collapsing operators

Some infix operators are effectively n-ary, meaning that they can take any number of operands. One typical example is <, which can be used like this:

3 < π < 4
true

This is parsed as FCN_LessThan(3, π, 4) and returns true since 3 < π and π < 4.

Classically, most computer languages would treat < as a pure binary operator, so that 3 < π would first evaluate to true and then true < 4 would yield a type error.

Algosim’s use of true n-ary operators like this mimics the notation used in ordinary mathematics and this can be used in all contexts; for instance, you may plot

plot(arctan(x) < y < 1 + cos(x^2), 0, π / 2)

Image 1

Operators that, like <, can collapse a sequence of binary operations to a single n-ary function call are called “collapsing”. The following operators are collapsing:

+ × ; < > =

Notice that you can prevent a sequence of collapsing operators from combining into a single function call by using parentheses:

(3 < π) < 4

will be parsed as FCN_LessThan(FCN_LessThan(3, π), 4) and yield a type error.