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