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:-
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.
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 ---- 5Congratulations! You have just run your first program through 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.
Click on the 'step into' button in the interpreter window once more. You should see
---- 1appear 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!
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).
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.