Yazoo ---> Online Help Docs ---> Yazoo scripting

Introduction to Yazoo scripting

To begin our talk on Yazoo scripting we'll first throw out, in no particular order, some of the simple and common programming operations that take up the lion's share of many source files. The following sections will elaborate on each of these more systematically.

The first thing to know when you are in Yazoo is how to get back out again in a dignified way. The command to escape Yazoo is "exit". Therefore, if the user is being prompted for a command (by the character "> "), then typing exit followed by <enter> or <return> will cause Yazoo to end gracefully. There is unfortunately no way to force the command prompt to appear, so if Yazoo gets trapped in an infinite loop, the user has to stop the program by some external means like Ctrl-C (UNIX/Mac) or Ctrl-Alt-Delete (Windows).

Simple Yazoo expressions involving numbers, variables and strings follow a pretty standard format. Numeric expressions are computed in the usual way with +, -, etc. Strings are surrounded by double-quotes, as in "my_string". There is no use for the single-quotation mark in Yazoo. All variables must be defined before they are used; for this we use the define operator :, as in var :: type. The most commonly used types are slong (signed long integers), double (floating point), and string. To make the variable an array, add the size of each dimension in square brackets following the array name. For example,


    MyTable[5][7] :: double
   

defines a two-dimensional array. To access an array element, again use the square brackets.


    print(MyTable[2][3])
   

It's usually convenient to define several variables or arrays of the same type together in the same `sentence'. We can do this by defining variable 1 to be of the same type as variable 2, which is defined as ... as variable N, which is defined as, say, an unsigned long.


    var1 :: var2 :: var3 :: ulong
   

The following script shows some basic manipulations of variables and arrays. This one can actually be entered line-by-line from Yazoo's command prompt.


    | program to find the length of the hypotenuse of a right triangle
   
    sides[2] :: hyp :: double
    ans :: string
   
    print("Side 1: ")
    ans = input()
    read_string(ans, sides[1])
   
    print("Side 2: ")
    ans = input()
    read_string(ans, sides[2])
   
    hyp = (sides[1]^2 + sides[2]^2)^0.5
    print("The hypotenuse has length ", hyp, ".\n")
   

The first line in this example is a comment, because of the vertical bar |. The functions print(), input() and read_string() (along with some 40 others) come hardwired into Yazoo. We can also define our own scripted functions, for which we again use the define operator, but with the function script inside braces in place of a variable type. Inside the braces, variable definitions come first, then a code command or a semicolon, and lastly the executable code of the function. To exit the function (as opposed to the bailing out of the whole program) use the canonical return statement. One calls the function in the same way as one would in C, with the arguments in parentheses immediately after the function name. As we will see below, the function accesses these arguments through something called the args variable, which has one member for every argument that was passed. All arguments are passed by reference in Yazoo: if we call f(x) then the function f() can change the value of `x' by overwriting its args[1] variable.

Let's make our earlier example a bit more sophisticated by using functions, and also by the way demonstrate if commands and for loops. Unfortunately we cannot enter this example line-by-line, because neither the function definition nor the if/while blocks fit on one line. Instead we would have to enter it into a file: say, "pythagoras.zoo".


    | program to find the length of the hypotenuse of a right triangle
   
    GetSide :: {
       ans :: string
       side_length :: double
       
       code
       
       side_length = 0
       print("Side ", args[1], ": ")
       ans = input()
       read_string(ans, side_length)
       
       if side_length > 0
           return side_length
       else
           print("Side length must be positive\n")
           exit
       end if
    }
   
    sides[2] :: hyp :: double
    counter :: ulong
   
    for counter in [1, 2]
       sides[counter] = GetSide(counter)
    end for
    hyp = (sides[1]^2 + sides[2]^2)^0.5
   
    print("The hypotenuse has length ", hyp, ".\n")
   

Now to run our script from Yazoo's command prompt, we would type


    run("pythagoras.zoo")
   

where we are assuming that the script is in the same directory as Yazoo.

In addition to if and for, Yazoo has while...end while and do...until loops. There is no `goto' statement. One quick note: the logical `if-equal' symbol is the same double-equate `==' that you find in C, in order to distinguish it from the assignment operator; thus for example if a == b is syntactically correct. The `if-not-equal' operator is `/='.


Prev: Building and running Yazoo   Next: Expressions


Last update: July 28, 2013

Get Yazoo scripting language at SourceForge.net. Fast, secure and Free Open Source software downloads