1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
.template 0
# fsm_c.gsl
#
# Generates a finite state machine engine in C
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This provides much of the function of the Libero state machine model
# written by Pieter Hintjens, which you can still see in GSL's source
# code. Just for comparison, Libero was 11K lines of code, and this is
# under 500.
#
# For a worked example, see the zs_lex class in this directory.
#
class.title ?= "No title"
# Resolve includes
for class.include
if defined (include.filename)
my.include_file = class.load_file (filename)
if defined (my.include_file)
move my.include_file after include
else
echo "E: error loading include file: $(filename): $(xml.error?)"
endif
else
echo "E: required attribute 'filename' not defined"
endif
endfor
# Lowercase state/event/action names
for class.state
state.name = "$(name:c)"
state.comma = last()?? ""? ","
for event
if name <> "*"
event.name = "$(name:c)"
endif
if defined (event.next)
event.next = "$(next:c)"
if count (class.state, next = "$(name:c)") = 0
abort "Next state $(next) is not defined"
endif
endif
for action
action.name = "$(name:c)"
endfor
endfor
# Collect all events and actions at class level
for event where name <> "*"
# Copy event to class if not yet defined there
if count (class.event, name = -1.name) = 0
copy event to class
endif
endfor
for event
for action where count (class.action, name = -1.name) = 0
copy action to class
endfor
endfor
endfor
# Process super states
function resolve_inheritance (state)
if defined (my.state.inherit)
my.state.inherit = "$(my.state.inherit:c)"
for class.state as superstate where name = my.state.inherit
resolve_inheritance (superstate)
for event where count (my.state.event, name = -1.name) = 0
copy event to my.state
endfor
else
echo "E: superstate $(inherit) isn't defined"
endfor
endif
endfunction
for class.state
resolve_inheritance (state)
endfor
# Collect prototypes that we need
for class.action
new class.prototype
prototype.name = "$(action.name)"
prototype.exists = 0
prototype.args = "$(class.name)_t *self"
endnew
endfor
.endtemplate
.output "$(class.name)_fsm.h"
/* =========================================================================
$(class.name)_fsm - $(class.title:) state machine engine
** WARNING *************************************************************
THIS SOURCE FILE IS 100% GENERATED. If you edit this file, you will lose
your changes at the next build cycle. This is great for temporary printf
statements. DO NOT MAKE ANY CHANGES YOU WISH TO KEEP. The correct places
for commits are:
* The XML model used for this code generation: $(filename), or
* The code generation script that built this file: $(script)
************************************************************************
. for class.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
// ---------------------------------------------------------------------------
// State machine constants
typedef enum {
.for class.state
. state.comma = last()?? ""? ","
$(name)_state = $(index ())$(comma)
.endfor
} state_t;
typedef enum {
NULL_event = 0,
.for class.event
. event.comma = last()?? ""? ","
$(name)_event = $(index ())$(comma)
.endfor
} event_t;
// Names for state machine logging and error reporting
static char *
s_state_name [] = {
"(NONE)",
.for class.state
"$(name)"$(comma)
.endfor
};
static char *
s_event_name [] = {
"(NONE)",
.for class.event
"$(name)"$(comma)
.endfor
};
// Action prototypes
.for class.prototype
static void $(name) ($(args));
.endfor
// This is the context block for a FSM thread; use the setter
// methods to set the FSM properties.
typedef struct {
$(class.name)_t *parent; // Parent class
bool animate; // Animate state machine
state_t state; // Current state
event_t event; // Current event
event_t next_event; // The next event
event_t exception; // Exception event, if any
uint64_t cycles; // Track the work done
} fsm_t;
static fsm_t *
fsm_new ($(class.name)_t *parent)
{
fsm_t *self = (fsm_t *) zmalloc (sizeof (fsm_t));
if (self) {
.for class.state where item () = 1
self->state = $(name)_state;
.endfor
self->event = NULL_event;
self->parent = parent;
}
return self;
}
static void
fsm_destroy (fsm_t **self_p)
{
assert (self_p);
if (*self_p) {
fsm_t *self = *self_p;
free (self);
*self_p = NULL;
}
}
static void
fsm_set_next_event (fsm_t *self, event_t next_event)
{
self->next_event = next_event;
}
static void
fsm_set_exception (fsm_t *self, event_t exception)
{
self->exception = exception;
}
static void
fsm_set_animate (fsm_t *self, bool animate)
{
self->animate = animate;
}
static uint64_t
fsm_cycles (fsm_t *self)
{
return self->cycles;
}
.macro output_action_list ()
. for action
if (!self->exception) {
// $(name)
if (self->animate)
zsys_debug ("$(class.name): $ $(name)");
$(name) (self->parent);
}
. endfor
. if !count (action)
// No action - just logging
if (self->animate)
zsys_debug ("$(class.name): $ $(name)");
. endif
. if defined (event.next)
if (!self->exception)
self->state = $(next)_state;
. my.next_state = class->state ("$(name)" = "$(event.next)")
. endif
.endmacro
// Execute state machine until it has no next event. Before calling this
// you must have set the next event using fsm_set_next_event(). Ends when
// there is no next event set.
static void
fsm_execute (fsm_t *self)
{
while (self->next_event != NULL_event) {
self->cycles++;
self->event = self->next_event;
self->next_event = NULL_event;
self->exception = NULL_event;
if (self->animate) {
zsys_debug ("$(class.name): %s:", s_state_name [self->state]);
zsys_debug ("$(class.name): %s", s_event_name [self->event]);
}
.# Nested if is slightly faster than switch, surprisingly perhaps
.for class.state
. if index () > 1
else
. endif
if (self->state == $(name:c)_state) {
. for event where name <> "*"
. if index () > 1
else
. endif
if (self->event == $(name)_event) {
. output_action_list ()
}
. endfor
. for event where name = "*"
. if item () > 1
else {
. else
{
. endif
// Handle all other events
. output_action_list ()
}
. else
else {
// Handle unexpected internal events
zsys_warning ("$(class.name): unhandled event %s in %s",
s_event_name [self->event], s_state_name [self->state]);
assert (false);
}
. endfor
}
.endfor
// If we had an exception event, interrupt normal programming
if (self->exception) {
if (self->animate)
zsys_debug ("$(class.name): ! %s", s_event_name [self->exception]);
self->next_event = self->exception;
}
else
if (self->animate)
zsys_debug ("$(class.name): > %s", s_state_name [self->state]);
}
}
.#
.# Generate source file first time only
.source_file = "$(class.name).h"
.if !file.exists (source_file)
. output source_file
/* =========================================================================
$(class.name) - $(class.title:)
. for class.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
#ifndef $(CLASS.NAME)_H_INCLUDED
#define $(CLASS.NAME)_H_INCLUDED
#include <czmq.h>
#ifdef __cplusplus
extern "C" {
#endif
// Opaque class structure
typedef struct _$(class.name)_t $(class.name)_t;
// @interface
// Create a new $(class.name), return the reference if successful, or NULL
// if construction failed due to lack of available memory.
$(class.name)_t *
$(class.name)_new (void);
// Destroy the $(class.name) and free all memory used by the object.
void
$(class.name)_destroy ($(class.name)_t **self_p);
// Enable verbose tracing
void
$(class.name)_verbose (zs_parser_t *self, bool verbose);
// Self test of this class
void
$(class.name)_test (bool verbose);
// @end
#ifdef __cplusplus
}
#endif
#endif
.endif
.# Generate source file first time only
.source_file = "$(class.name).c"
.if !file.exists (source_file)
. output source_file
/* =========================================================================
$(class.name) - $(class.title:)
. for class.license
$(string.trim (license.):block )
. endfor
=========================================================================
*/
/*
@header
Description of class for man page.
@discuss
Detailed discussion of the class, if any.
@end
*/
// Include the generated state machine engine
#include "$(class.name)_fsm.h"
// Structure of our class
struct _$(class.name)_t {
fsm_t *fsm; // Our finite state machine
int filler; // Add your own properties here
};
// ---------------------------------------------------------------------------
// Create a new $(class.name), return the reference if successful, or NULL
// if construction failed due to lack of available memory.
$(class.name)_t *
$(class.name)_new (void)
{
$(class.name)_t *self = ($(class.name)_t *) zmalloc (sizeof ($(class.name)_t));
if (self) {
self->fsm = fsm_new (self);
// Initialize class properties here
}
return self;
}
// ---------------------------------------------------------------------------
// Destroy the $(class.name) and free all memory used by the object.
void
$(class.name)_destroy ($(class.name)_t **self_p)
{
assert (self_p);
if (*self_p) {
$(class.name)_t *self = *self_p;
fsm_destroy (&self->fsm);
free (self);
*self_p = NULL;
}
}
// ---------------------------------------------------------------------------
// Selftest
void
$(class.name)_test (bool verbose)
{
printf (" * $(class.name): ");
if (verbose)
printf ("\\n");
// @selftest
$(class.name)_t *$(class.name) = $(class.name)_new ();
$(class.name)_destroy (&$(class.name));
// @end
printf ("OK\\n");
}
.endif
.close
.template 0
# Append missing prototypes to source file
input = file.open (source_file)
xline = file.read (input)
while defined (xline)
# Look for function declarations
if regexp.match ("^(\\w+) \\(", xline, token)
for class.prototype where name = token
prototype.exists = 1
endfor
endif
xline = file.read (input)?
endwhile
file.close (input)
append source_file
for class.prototype where exists = 0
echo "Generating stub for $(name)..."
>
>
>// ---------------------------------------------------------------------------
>// $(name)
>//
>
>static void
>$(name) ($(args))
>{
>}
endfor
.endtemplate
|