DrakonTech
DrakonTech
Download

C Code Generation

C Logo

Exported Functions Are Automatically Added to the Header

For a DRAKON-C module, DrakonTech generates two files: a source .c file and a header .h file. Function bodies are placed in the source file. The generator inserts declarations of exported functions into the header file.

Arbitrary Code at the Beginning of a File

To place arbitrary code at the beginning of the header file, create a function with the special name begin_header. To insert arbitrary code at the beginning of the source file, create a function named begin_source.

Structures

To create a structure (an object of type struct), create a function and place the keyword struct in the Formal Parameters icon to the right of the diagram title. Only Action icons are allowed in structure diagrams. The contents of the Action icons will be placed inside the structure body.

Structure diagram

Structure diagram

struct intarray
{
    int * elements;
    int size;
};

By default, structures are placed in the source file before the functions. If a structure diagram is marked as exported, the generator places the corresponding structure in the header file before the declarations of exported functions.

For Loop

The code generator automatically adds the for keyword before the expression contained in the For Loop icon. There is no need to write the for keyword manually.

It is sufficient to write i = 0; i < count; i++, and the code generator will produce the expression for (i = 0; i < count; i++) { … }.

For loop in DRAKON-C

For loop in DRAKON-C

If the expression in the For Loop icon ends with a closing parenthesis, DrakonTech will not add the for keyword. This is useful for iterating over containers using macros, for example FOREACH_ARRAY(val, my_data, length).

Macro loop

Macro loop

Variable Declarations

Variables in DRAKON-C diagrams must be declared at the beginning of the function. In diagrams of the "silhouette" type, it is best to place variable declarations in the very first branch of the silhouette. If the logic requires the first branch to execute multiple times, create a dedicated branch used only for variable declarations. Other branches of the silhouette must not reference this branch.

Releasing Resources and Memory

Programming in pure C, unlike C++ and many other languages, is enjoyable because it provides a feeling of complete control over the computer. The computer will do exactly what you tell it to do. C frees the programmer from having to visualize magical code that many other languages insert extensively into a program.

The price of this control is additional responsibility. Memory and resources must be released manually. Pointers should be marked as owned or borrowed.

A common pattern for releasing resources in C is a block at the end of a function under the cleanup label. All execution paths through the algorithm should eventually reach this label.

In DRAKON-C, this pattern looks different. If a function requires mandatory resource cleanup, the function should be converted into a silhouette. In the right part of the silhouette, before the exit, create a cleanup branch. All paths through the diagram must end with an Address icon that points to the cleanup branch.

Freeing resources in the cleanup branch

Freeing resources in the cleanup branch

Feedback

drakon.editor@gmail.com