Program layout and scenes

All code in P* programs sits inside SCENE blocks. A program is required to provide a SCENE called main, which is the first one to be run by the interpreter. P* will crash with an error message if the SCENE main is missing. There may be no duplicate scene names, and they cannot nest.

Scenes, apart from the main scene are called using the #SCENE-pragma. A scene may access all variables from it's caller.

A simple P* program with two scenes might look like this:

#!/usr/bin/wpl -f
SCENE my_scene {
	/* Print out the value of the 'text'-variable */
	echo "This is my_scene: '$text'\n";

SCENE main {
	/* Create a string variable */
	string text = "Hello world!";

	/* Call the other scene */
	#SCENE my_scene;

This program will output

This is my_scene: 'Hello world!'

Scene inheritance

A scene can extend another scene. This is useful for instance if we want to put configuration stuff inside a scene and include it in other scenes.

In a scene inheritance setup, we can use the keywords private and protected to restrict access to variables and functions. If we don't specify any keyword, private is assumed.

A variable or function which is private can only be accessed from within the same scene, but protected ones can be accessed from derived scenes.

Base scenes are specified by putting a colon : after the scene name followed by a comma separated list of the base scenes, like SCENE main : b, c. Base scenes are run prior to the derived scene in the order in which they are defined.

The example below has a total of four scenes which inherits from each other. The main scene inherits from scene b and c, which makes them base scenes of main. Scene b inherits further from scene a, and the order of execution of all the scenes will be a -> b -> c -> main

If we call a function of a base scene from a derived scene, the function can access private variables of the scene in which it is defined.

	echo "This is A\n";

SCENE b : a {
	echo "This is B\n";

	/* This variable is private, and can only be accessed from within b */
	int var_b = 1;

	/* This function is protected, and can be accessed from derived scenes */
	protected int b_function(int argument) {
		echo "This is B's function:\n";

		/* The function can access the private variable var_b */
		echo "\tvar_b is " . var_b . "\n";

	/* This variable is protected, and can be accessed from derived scenes */
	protected int var_c = 2;

	echo "This is C: var_c is " . var_c . "\n";

SCENE main : b, c {
	/* Here, the protected variable var_c is accessed */
	echo "This is Main: var_c is " . var_c . "\n";

	/* Call the protected function b_function */

When this program is run, it will output:

This is A
This is B
This is C: var_c is 2
This is Main: var_c is 2
This is B's function:
	var_b is 1