C is the language which benefits more than others of uniting with DRAKON. With DRAGON-C, the power and simplicity of C is combined with the clarity of DRAKON.
The DRAKON language requires that the diagram must have only one exit. This is especially convenient in DRAGON-C because now we have a single place (right before the End icon) where the acquired resources, such as memory or file handles should be deallocated.
This is a basic tutorial in building DRAKON-C programs.
A more advanced tutorial can be found in examples/C folder in the DRAKON Editor .zip file.
Download the tutorial: hello_c.drn.
Create a new file with DRAKON Editor.
Select C as the programming language. In the main menu, go to File / File properties... and choose "C".
Create a new diagram called main
:
Add your own code. In the main menu, go to File / File description... and add the following text:
=== c_header ===
#include <stdio.h>
Generate the source code. In the main menu, go to DRAKON / Generate code or just press Ctrl-B (Command-B on Mac).
DRAKON Editor will create a pair of new .h and .c files and put them next to the .drn file. The C code will be something like this:
#include <stdio.h> int main( int argc, char** argv ) { /* item 171 */ printf("Hello, world!\n"); return 0; }
The formal parameters of the procedure are placed in the special Parameters icon, to the right from the Begin icon.
How to create a Parameters icon:
Add a new Action icon and put it next to the right side of the Begin icon.
Connect the Begin icon with the new Action icon with a horizontal line.
Add parameters, one parameter per line.
The last line of the Parameters icon may contain an optional returns line that specifies the type of the value returned by the function. If there is no returns line, the function returns void.
DRAKON Editor will generate the following code for the diagram:
static void DoubleHello( const char* firstGreeting, const char* secondGreeting ) { /* item 22 */ printf("%s\n", firstGreeting); /* item 23 */ printf("%s\n", secondGreeting); return; }
Note the item comments in the generated code. They tell which icon on the diagram the next piece of code is generated from.
To jump to the specific icon (if you know its number), press Ctrl-I and enter the number. This is really convenient for debugging.
Sometimes it is necessary to add some hand-written code to the generated files. DRAKON Editor allows you to do that using the so called sections in the file description.
To edit the file description, go to File / File description...
The h_header section is added at the beginning of the generated .h file. The h_footer section is added at the end of the generated .h file. c_header and c_footer are inserted in the .c file.
We were using the c_header section in the main example above to include the system header <stdio.h>.
The section content is all text below the section header until the next section of end of file. The text that goes before any sections in the file description is ignored.
Here is an example of a typical file description.
Some text describing the file. It will be ignored during code generation. === c_header === /* This code goes to the top of the .c file. */ #include <stdlib.h> #include <stdio.h> #define ONE 1 static int globalVar = 10; int MyFun(void); === c_footer === /* This code goes to the bottom of the .c file. */ int MyFun(void) { printf("This is a hand-written function.\n"); return 100; }
Sections reference for DRAKON-C file descriptions
Section | Goes to file | Position |
h_header | .h file | Start of file |
h_footer | .h file | End of file |
c_header | .c file | Start of file |
c_footer | .c file | End of file |
By default, the linkage of functions is static. In order to make the function public, put the word public on the first line of the Parameters icon (see the example with main function above).
The declarations of public functions are placed in the generated header (.h) file.
The word inline on the first line of the Parameters icon adds the inline keyword to the function declaration and definition. The definition of public inline functions goes to the header.
Parameters icon |
Declaration | Declaration goes to header |
Definition goes to header |
int p1 int p2 |
static void Foo(int p1, int p2) | no | no |
inline int p1 int p2 |
static inline void Foo(int p1, int p2) | no | no |
public int p1 int p2 |
void Foo(int p1, int p2) | yes | no |
public inline int p1 int p2 |
inline void Foo(int p1, int p2) | yes | yes |
The If icon produces the if statement:
The If icon in the diagram above produces this code:
static void LimitedPrint( const char* text, int maxLength ) { /* item 29 */ int length = strlen(text); /* item 30 */ if (length <= maxLength) { // item 31 printf("The text is short enough.\n"); printf("I can print it:%s\n", text); return; } else { // item 34 printf("The text is too long.\n"); return; } }
It is possible to swap YES and NO exits of an if icon. Right-click on the if icon and choose Swap YES and NO.
Imagine you need to make a choice out of several options based on some value. In C, you would write something like this:
switch (n) { case 0: printf("zero\n"); break; case 1: printf("one\n"); break; case 2: printf("two\n"); break; default: printf("a lot\n"); break; }
In DRAKON, you can use the Select and Case icons like this:
When an Arrow is added to an If or Select, a loop is created.
Do-Check loop:
Check-Do loop:
Do-Check-Do (hybrid) loop:
With DRAKON, it is possible to write standard C-style for constructs. Note the absense of the for keyword.
Which is a syntactically sugared version of the below picture:
It is possible to exclude the whole diagram from code generation.
If the first line of the formal parameters list is
#comment
then no code is generated for this diagram.
Its correctness, however, will still be checked against the DRAKON rules.