Numbers, characters and string constants have pretty much the same syntax in Cicada as they do in C: characters are flanked by single quotes ('C'), strings by double-quotes ("my_string"), and numbers are read as either integer or floating-point depending on how they are written. Likewise, numeric expressions look the same between C and Cicada, with the exception that Cicada provides an exponentiation operator ‘^’: for example 2^3 gives 8. In Cicada the standard six trig functions along with log(), floor() and ceil() can be used without including a math library.
The usual way to define variables in Cicada is to use the ‘::’ operator, as in:
x :: int
Here x was the variable and int was the type. The other possible allowed primitive types are double, char, string and bool. All Cicada variables must be defined before they can be used.
In order to define an array, add the size of each dimension in square brackets just before the type definition. For example,
myTable :: [5] [7] double
defines a two-dimensional (5x7) array of floating-point numbers. The syntax for accessing an array element again uses square brackets, in the same way as in C:
print(myTable[2][3])
The only difference is that in Cicada array indexing begins at 1.
It’s usually convenient to define several variables or arrays of the same type together in one long command. For example:
x :: y :: z :: int
table1 :: table2 :: [5] double
This example script shows basic usage of variables and arrays. It’s easiest to type this into a file: say, ”pythagoras.cicada”..
| program to find the length of the hypotenuse of a right triangle
sides :: [2] double
hypotenuse :: double
response :: string
print("Side 1: ")
response = input()
read_string(response, sides[1])
print("Side 2: ")
response = input()
read_string(response, sides[2])
hypotenuse = (sides[1]^2 + sides[2]^2)^0.5
print("The hypotenuse has length ", hypotenuse, ".\n")
(The first line in this example is a comment, because of the vertical bar |. print(), input() and read_string() are three of Cicada’s 30-odd built-in functions.) To run our script from Cicada’s command prompt we type
run("pythagoras")
If we want to define our own functions we again use the :: operator, but with curly braces containing the function code in place of a variable type. A standard Cicada function consists of three parts: 1) variable definitions; 2) a code command; 3) the executable code of the function. Unlike C functions, a Cicada function doesn’t predefine either its arguments or its return value. Its arguments are accessed through an args variable, and they can be overwritten by the function. The return command works the same way as in C. Finally, the basic syntax for calling a function is the same as in C, although in Cicada fancier kinds of function calls are also possible.
Here is a more elaborate version of our previous example that uses functions, as well as if statements and for loops whose syntax is a bit different from C.
| program to find the length of the hypotenuse of a right triangle
GetSide :: {
response :: string
side_length :: double
code
side_length = 0
print("Side ", args[1], ": ")
response = input()
read_string(response, side_length)
if side_length > 0 then (
return side_length )
else (
print("Side length must be positive\n")
exit )
}
sides :: [2] double
hypotenuse :: double
counter :: int
for counter in <1, 2> (
sides[counter] = GetSide(counter) )
hypotenuse = (sides[1]^2 + sides[2]^2)^0.5
print("The hypotenuse has length ", hypotenuse, ".\n")
Cicada also has while and loop ... until loops. Notice that although the equality test ‘==’ is the same as in C, the not-equal test uses the symbol ‘/=’ which is different from C. There is no ‘goto’ statement.
To exit Cicada type “exit” at the command prompt. If there is no command prompt then Cicada is probably stuck (e.g. in an infinite loop), in which case we need to hit Ctrl-C (UNIX/Mac) or Ctrl-Alt-Delete (Windows).
Last update: May 8, 2024