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
|
/*
* CENA C++ Utilities
*
* by Stephane Chatty
*
* Copyright 1993-1995
* Centre d'Etudes de la Navigation Aerienne (CENA)
*
* Automata.
*
* $Id$
* $CurLog$
*/
#include "Automaton.h"
IvlBaseLink :: IvlBaseLink (IvlBaseState* s)
: To (s)
{
}
IvlBaseLink :: ~IvlBaseLink ()
{
}
IvlBaseState :: IvlBaseState (const char* name, IvlBaseAutomaton& a)
: Name (name),
Links (a.Size, a.Hash, a.Copy, a.Delete, a.Compare),
TheAutomaton (&a)
{
}
IvlBaseState :: ~IvlBaseState ()
{
IvlHashIterOf <IvlBaseLink> l = Links;
while (++l)
delete *l;
}
#if 0
void
IvlBaseState :: CreateLink (IvlBaseState* to, IvlKey* k)
{
IvlHashCellOf <IvlBaseLink>* c = Links.Add (k);
c->SetInfo (new IvlBaseLink (to));
}
#endif
#if 0
void
IvlBaseState :: Add (IvlKey* key, IvlBaseLink* l)
{
IvlHashCellOf <IvlLink>* c = Links.Add (key);
c->SetInfo (l);
}
#endif
IvlBaseState*
IvlBaseState :: Next (IvlToken* tk, bool peek)
{
IvlKey* k = TheAutomaton->GetKey ? (TheAutomaton->GetKey) (tk) : tk;
IvlHashCellOf <IvlBaseLink>* c = Links.Get (k);
if (!c)
return 0;
IvlBaseLink* l = c->GetInfo ();
IvlBaseState* to = l->GetDestination ();
if (!peek) {
Out ();
l->Fire (this, to, tk);
to->In ();
}
return to;
}
IvlBaseAutomaton :: IvlBaseAutomaton (AKEY_F gk, HASH_F hash, HCP_F cp, HDEL_F del, HCMP_F cmp, int sz)
: Size (sz),
Hash (hash),
Copy (cp),
Delete (del),
Compare (cmp),
GetKey (gk),
Initial (0),
AllStates ()
{
}
IvlBaseAutomaton :: ~IvlBaseAutomaton ()
{
IvlListIterOf <IvlBaseState> s = AllStates;
while (++s)
delete *s;
}
#if 0
IvlBaseState*
IvlBaseAutomaton :: CreateState (const char* n);
{
IvlBaseState* s = new IvlBaseState (n, *this);
AllStates.Append (s);
return s;
}
#endif
IvlLink :: IvlLink (IvlState* s, IvlLinkFun fn)
: IvlBaseLink (s),
Fun (fn)
{
}
IvlLink :: ~IvlLink ()
{
}
void
IvlLink :: Fire (IvlBaseState* from, IvlBaseState* to, IvlToken* data)
{
if (Fun)
(*Fun) ((IvlState*) from, (IvlState*) to, data);
}
IvlState :: IvlState (const char* name, IvlAutomaton& a, IvlStateFun* in, IvlStateFun* out)
: IvlBaseState (name, a),
InFun (in),
OutFun (out)
{
}
IvlState :: ~IvlState ()
{
}
void
IvlState :: In ()
{
if (InFun) (*InFun) (this);
}
void
IvlState :: Out ()
{
if (OutFun) (*OutFun) (this);
}
void
IvlState :: CreateLink (IvlState* to, IvlKey* k, IvlLinkFun fn)
{
IvlHashCellOf <IvlBaseLink>* c = Links.Add (k);
c->SetInfo (new IvlLink (to, fn));
}
IvlState*
IvlAutomaton :: CreateState (const char* n, IvlStateFun in, IvlStateFun out)
{
IvlState* s = new IvlState (n, *this, in, out);
AllStates.Append (s);
return s;
}
IvlAutomIter :: IvlAutomIter (IvlBaseAutomaton& a)
: TheAutomaton (&a),
CurState (a.GetInitial ())
{
}
IvlAutomIter :: ~IvlAutomIter ()
{
}
bool
IvlAutomIter :: Step (IvlToken* tk)
{
if (!CurState && !(CurState = TheAutomaton->GetInitial ()))
return false;
IvlBaseState* n = CurState->Next (tk);
if (!n)
return false;
CurState = n;
return true;
}
|