sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Tue, 10 May 2016 10:00:50 +0200
changeset 580 99cd18ba8a1b
parent 579 99a3e946928a
child 581 fdfcb223d56b
permissions -rw-r--r--
distinct
     1 // generate state machine code
     2 
     3 // Copyleft (c) 2016, p≡p foundation
     4 
     5 // Written by Volker Birk
     6 
     7 include yslt.yml2
     8 
     9 tstylesheet {
    10     template "/protocol/fsm"
    11     ||
    12     // state machine for «@name»
    13 
    14     #include "pEpEngine.h"
    15 
    16     typedef enum _fsm_error {
    17         invalid_state = -1,
    18         invalid_event = -2
    19     } fsm_error;
    20 
    21     typedef enum _«@name»_state {
    22     `` for "set:distinct(state/@name)" |> «.»`if "position()!=last()" > , `
    23     } «@name»_state;
    24 
    25     typedef enum _«@name»_event {
    26     `` for "set:distinct(state/event/@name)" |> «.»`if "position()!=last()" > , `
    27     } «@name»_event;
    28 
    29     «@name»_state fsm_«@name»(«@name»_state state, «@name»_event event)
    30     {
    31         switch (state) {
    32             `` apply "state"
    33             default:
    34                 return invalid_state;
    35         }
    36     }
    37 
    38     ||
    39 
    40     template "state"
    41     ||
    42     case «@name»:
    43         switch (event) {
    44         `` apply "event", 2
    45         default:
    46             return invalid_event;
    47         }
    48         break;
    49 
    50     ||
    51 
    52     template "event"
    53     ||
    54     case «@name»:
    55     `` apply "action|transition";
    56         break;
    57     ||
    58 
    59     template "action" | «@name»();
    60     template "transition" | return «@target»;
    61 }
    62