The Kenya Interpreter

The Kenya interpreter allows you to execute your program natively, without having to perform a translation into Java, compiling the Java, and then executing that. The interpreter can also be used as a debugging tool for your Kenya programs.

Lets do an example to get you up and running with using the interpreter. First, click on the 'new' button of the toolbar, if it asks you whether you want to save the current Kenya program, click 'no'. Now, make sure that the Kenya editing area is selected by clicking on the Kenya tab. Now enter the code below:-

void main()
{
    println("---- 1");
    println("---- 2");
    println("---- 3");
    println("---- 4");
    println("---- 5");
}
Click the 'save' button on the toolbar, and save your program as numbers.k.

To start the interpreter, click on the 'Interpreter' button on the toolbar. The window below should be displayed:-

the interpreter window

This is the interpreter window, you use this window to control the Kenya interpreter. Like the main Kenya window, a row of buttons on a toolbar provides all the functionality you need to access for the interpreter. The screenshot below shows the functions of each button.

the interpreter toolbar

Click on the 'Run' button on the toolbar, you should see almost immediately, the following output in the output area below the toolbar:-

---- 1
---- 2
---- 3
---- 4
---- 5
Congratulations! You have just run your first program through the Kenya interpreter!

Stepping Through a Kenya program

The ability to execute your program a line at a time is a great help in debugging your programming of errors. Luckily, you can do this with the Kenya interpreter!

If you have the numbers.k program still open, click on the 'Kenya interpreter' button on the toolbar to open the interpreter window. Otherwise, click on the 'open file' button, and navigate to where you saved the numbers.k program, and open it. Now, in the interpreter window, click on the 'Step into' button once.

Now look back at the main Kenya window, you see the a line in your program highlighted like in the previous diagram. This shows you the line of your program the interpreter is ready to execute next.

Click on the 'step into' button in the interpreter window once more. You should see

---- 1
appear in the output window. If you now look back at the main interpreter window again, you should notice that the line just after the line that was previously highlighted, now highlighted instead. This tells you that the interpreter has finished executing that line, as is now ready to execute the next line.

Click on the 'step into' button four more times, until the all five lines of output have been printed to the output box in the interpreter window.

Well done! You have just stepped through your first Kenya program!

Setting Breakpoints

Often you will want your program to execute up until a certain point before you start stepping through the code. This can be done by setting a breakpoint in your program. The Kenya interpreter will run and execute your program until it hits the breakpoint, at which point it will stop and highlight the line in blue, indicating that it is ready to execute that line. From this point, you can step through your code line by line, or continue execution of the interpreter. Try in yourself by following these steps:-
  1. Open up the numbers.k program if you don't already have it open.
  2. Click on the 'Kenya interpreter' button on the toolbar to open the interpreter window.
  3. Click on the
    println("---- 3");
    line in the Kenya editing area of the main Kenya window, and the click the 'set breakpoint' button in the interpreter window. You should see a red marker appear in the gutter to the left of the main Kenya window. This indicates that a breakpoint has been set. on that line (see screenshot below).

  4. Click on the 'Run' button of the interpreter window. You should see two lines of output in the output area of the interpreter window. The interpreter has stopped execution on the line you set the breakpoint, and if you look back at the main Kenya window, you should see the line on which you set the breakpoint highlighted.
  5. At this point you can either click the 'Run' button again to make the interpreter run the rest of the program to completion, or click on the 'Step into' button several times to step through the code line by line until the program ends.

The variable watch panel

The variable watch panel of the interpreter window allows you to watch the values of your variables in your program change as the interpreter executes your program. This facility can be invaluable to the debugging of your programs.

To try this facility out, first create a new file, saving anything that you wish to keep. Now, type the following program into the Kenya editing area:-

int factorial (int x)
{
    if(x == 0){
        return 1;
    }else{
        return x*factorial(x-1);
    }
}

void main(){
    println("Factorial of 3 is: " + factorial(3));
}
Open the interpreter window, and click on the variable watch button on the toolbar. You will see a panel appear on the right hand side of the interpreter window. This is the variable watch panel. The variable watch panel is split into two parts, there is a drop down list towards the top, and below this is the main variable watch area. The drop down list is known as the scopes list, it contains an entry for each function call that hasn't returned yet - you will see what this means in a minute when you step through the factorial program presented above. The variable watch window displays a tree view of the variables in your program and their values. With class variables and array variables, you can expand and collapse the trees to show or hide information as you wish, this reduces screen clutter. As a program runs, the tree is continually updated.

Now, lets run the program to see it in action! Click on the 'step into' button six times such that the program enters the second recursive call of the factorial function. As you should have noticed, the variable watch window continually changes with each step you make. Now, click on the scopes list, you should see something like the screen shot below:-

You will see that there are three entries for factorial, and one entry for a main. The entry for main represents the variables within the scope of the main function. Similarly, the three factorial entries each represent one of the calls to factorial. The one called 'factorial' is the original call, while 'factorial_2' and 'factorial_3' are the first and second recursive calls, respectively. For each recursive call that is made to a function, an underscore and a number are appended to the function name. This number is incremented by one for each recursive call made.

If you click on the 'main' item in the list, you will be presented with a blank variable watch window. This is because there are no variables declared in the main() function. If you click on any of the 'factorial' entries, you will see a tree with one element in it - a variable called 'x'. This represents the formal parameter x of each of the factorial functions. (Check that each one is correct for that recursive call!).

Now, continue stepping through the factorial program. As you step, you should notice that after the third recursive call, the scopes list suddenly becomes empty, and the output of the program is displayed in the output window of the interpreter window. The scopes has suddenly become empty because all the functions have returned - the scopes list only maintains a list of functions which have not returned.

The Other buttons

You will notice that there are other buttons on the toolbar of the variable watch window, we now briefly describe their functionality:-

Entering input to programs

At the bottom of both the output window and the interpreter window, just below their output boxes, is a small box known as the input box. This box is used for providing input to a Kenya program. To provide input to a Kenya program when it waiting for some, simply type what you want to input, and then press the ENTER key.

There is also an End of file (EOF) button next to the input box on the output window and the interpreter window. This button is used for entering a EOF character as input into a executing program if it is waiting for input.