[PREVIOUS] [NEXT] [UP

Visual programs

In this chapter you will learn the basic steps to create simple visual programs:

How to create units

A new unit is created by dragging (with the left mouse button) the appropriate icon from the folder window into the circuit window. For instance, drag from the stdr folder the prog_unit icon into the circuit window. A dialog box will open up, requiring you to enter parameters required for the creation of the chosen type of unit. For the prog_unit, this is the text of a C program that specifies what the prog_unit instance is going to do. For a simple example, enter the line

printf("hello world\n");

(if the input box is not empty, clear everything with CTRL-L before) and select OK. For some units, this will already complete the creation of the instance. In many cases, including our example, a second dialog box will appear, querying you further parameters (the parameters for the first dialog are creation parameters that cannot be changed later without remaking the instance from scratch; the parameters in the second dialog affect only properties that can be dynamically modified during the lifetime of the instance without causing its re-creation from scratch). For the present example, just accept the suggested defaults with OK. As a result, a little icon labelled prog_unit appears in the circuit window. This is the just created, new instance. Make a few more instances of the prog_unit by repeating the entire procedure. Note that each new attempt will provide you with dialog boxes filled with your previous input as a suggested default. Use CTRL-L to clear an input field entirely, or edit it suitably (e.g., create a few prog_units that print different text lines).

How to execute units

You have now created your first (very simple) "visual program". It is very simple, since there is not yet any communication between its objects, the created prog_unit instances (we will come to that in the next chapter). To execute your "program" select the "Step" button in the menu bar. This will execute the units in left-to-right (reading) order. In the present case, each prog_unit will execute its C-program, i.e., the output of the printf statements will appear in the terminal window from which you had started Neo. Note that in the case of two units occupying the same horizontal position, the "Step" command will execute the upper instance first. Since movement of an icon by a single pixel may change the execution order in this case, such exact horizontal alignment should be avoided when execution order is important. You also can execute each created instance individually, by clicking at its 'x'-field (try it).

How to inspect and modify units

If you wish to inspect or alter some of the run-time parameters of a unit (those parameters that were set with the second dialog window when the unit is created), just select the '#'-mark on its icon (if there is no '#' mark, the particular unit has no parameters that can be modified during run-time). In the case of the prog_unit, the only run-time parameter is a label that is displayed on the icon, and you may change this label to any other string to make the instances better distinguishable in the circuit window. If you wish to inspect the creation parameters of a unit, select the 'm' field on the instance. This will bring up again the creation dialog window. You may now, for instance, change the printf statement to something else, e.g.,

printf("i=%d\n",i++);

If you then select OK (try it), this will destroy the current instance and replace it by an instance for the new specification (if you select CANCEL, the old specification will become restored and the unit remains unchanged). You also will have to fill in or accept the entries for the second dialog box in this case. When you now execute the newly created instance several times, you will see the outputs

i=0
i=1
i=2
...

i.e., the prog_unit (as most other units) preserves its state information (in this case the value of the variable i) between invocations (note also that the prog_unit does not force you to declare any (scalar) variables; undeclared variables are by default assumed to be of type float). You now can try out the difference between modifying run-time parameters and creation parameters: If you just change the label of the instance (run-time parameter, select '#' for accessing the second dialog window), executing the instance after the change will continue with the previous state, e.g.

i=3
i=4
...

If, however, you change something to the program text (creation parameter, select 'm' for the first dialog), even if it is only the addition of a comment:

printf("i=%d\n",i++); // a comment

subsequent execution will show that the value of the variable has been reset to 0, since the instance has been completely remade.

More prog_unit functions

The prog_unit is one of the most versatile NST-units. It offers many functions familiar from the C-language, such as printf() above. However, not all functions are fixed built-ins. Many functions are implemented as extensions to the prog_unit. Such extension functions are imported from shared libraries (consult the prog_unit manual page for an overview of existing shared libraries). The import is achieved with the special "#import" directive, which must appear at the beginning of the prog_unit program. For instance, functions like fopen(), fclose(), fprintf() for accessing files (as well as important constants, such as stdin, stderr and stdout), are implemented in a shared library named "file" and will become available with the line

#import "file"

To avoid name collisions, you can also restrict the import, e.g., to the subset of functions starting with "f" and the single function "rewind" by the syntax

#import "file.f*"
#import "file.rewind"

The next example consists of a prog_unit that demonstrates the use of #import directive, together with some file-io function calls (note that in the prog_unit file pointers are implemented simply as float values, with 0 for "no file", so again, no declarations are necessary). An important use of the #import directive is the import C-functions from self-written shared libraries. This requires only the addition of a few lines of wrapper code to the implementation of your library, for a detailed explanation consult chapter Importing library objects and the prog_unit manual page.


[PREVIOUS] [NEXT] [UP]