At its most basic level, a Cicada script consists of commands which are usually written on separate lines:
a := 2*7 + 9
print(a)
although they may also be separated using commas:
a := 2*7 + 9, print(a)
(Commas are useful when entering a multi-line command, like a for loop, from the command prompt.)
Unlike in C, we don’t need a semicolon at the end of each command -- the line break automatically does that for us. If we don’t want the line break to mark the end of the command, then we need to use the line-continuation symbol ‘&’:
a := 2* &
7 + 9
This even works from the command prompt (just make sure that the & is the very last character on the line -- no trailing spaces). A & can appear between any two operators in a command, but not within an operator, name, symbol or string.
Each command consists of variables and constants, glued together by operators. For example, the command a := 2*7 + 9 contains the variable a, three integer constants 2, 7, and 9, and three other operators: +, * and := which both defines a variable and sets its value. The operators are grouped in the following way:
a := ( (2*7) + 9 )
That is to say, the multiplication has the highest precedence (i.e. it is done first), followed by the addition, and lastly by the define-equate operation which sets a to the final value. If we want to change the default grouping of operators, we use parentheses just as in C: for example a := 2*(7 + 9) would do the addition first.
Table 2 gives the precedence levels of all Cicada operators. The final column determines how operators of the same precedence level are grouped. For example, the division operator falls within precedence level 11 which has left-to-right grouping, meaning that 8 / 4 / 2 is equivalent to (8 / 4) / 2. On the other hand, the define and equate operators all have right-to-left grouping, so a := b = c = 2 is equivalent to a := (b = (c = 2)).
precedence | commands | symbols | grouping |
1 | command breaks | \n , ; | right to left |
2 | commands | return remove | N/A |
if for while loop | |||
3 | logical and, or, xor | and or xor | left to right |
4 | logical not | not | right to left |
5 | define/equate | :: ::@ := :=@ @:: *:: | |
= <- =@ | right to left | ||
forced equate | =! <-! | ||
6 | comparisons | == /= > >= < <= | N/A |
7 | substitute code | << | left to right |
8 | array type | [] | right to left |
9 | inheritance | : | left to right |
10 | add, subtract | + - | left to right |
11 | multiply, divide, mod | * / mod | left to right |
12 | negate | - | right to left |
13 | raise to power | ^ | left to right |
14 | function calls | () | |
step to member | . | left to right | |
step to index/indices | [] [<>] [+] [-] [^] | ||
code number | # | ||
15 | backstep | \ parent | left to right |
All of the information in Table 2 comes from the cicada.c source file. That means that anyone can change the Cicada language by just editing that file (see Chapter 4). In particular the cicadaLanguageAssociativity[] array controls the left/right grouping of operators within a precedence level.
Last update: May 8, 2024