forked from perabrahamsen/daisy-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean.C
306 lines (272 loc) · 7.53 KB
/
boolean.C
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
// boolean.C --- Booleans in Daisy.
//
// Copyright 2005 Per Abrahamsen and KVL.
//
// This file is part of Daisy.
//
// Daisy is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Daisy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with Daisy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define BUILD_DLL
#include "boolean.h"
#include "block_model.h"
#include "frame.h"
#include "assertion.h"
#include "memutils.h"
#include "librarian.h"
#include "treelog.h"
#include <sstream>
#include <vector>
const char *const Boolean::component = "boolean";
symbol
Boolean::library_id () const
{
static const symbol id (component);
return id;
}
const std::string&
Boolean::title () const
{ return name.name (); }
Boolean::Boolean (const BlockModel& al)
: name (al.type_name ())
{ }
Boolean::~Boolean ()
{ }
struct BooleanTrue : public Boolean
{
// Simulation.
void tick (const Units&, const Scope&, Treelog&)
{ }
bool missing (const Scope&) const
{ return false; }
bool value (const Scope&) const
{ return true; }
// Create.
bool initialize (const Units& units, const Scope& scope, Treelog&)
{ return true; }
bool check (const Units&, const Scope&, Treelog&) const
{ return true; }
BooleanTrue (const BlockModel& al)
: Boolean (al)
{ }
};
static struct BooleanTrueSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new BooleanTrue (al); }
BooleanTrueSyntax ()
: DeclareModel (Boolean::component, "true",
"Always true.")
{ }
void load_frame (Frame&) const
{ }
} BooleanTrue_syntax;
struct BooleanFalse : public Boolean
{
// Simulation.
void tick (const Units&, const Scope&, Treelog&)
{ }
bool missing (const Scope&) const
{ return false; }
bool value (const Scope&) const
{ return false; }
// Create.
bool initialize (const Units& units, const Scope& scope, Treelog&)
{ return true; }
bool check (const Units&, const Scope&, Treelog&) const
{ return true; }
BooleanFalse (const BlockModel& al)
: Boolean (al)
{ }
};
static struct BooleanFalseSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new BooleanFalse (al); }
BooleanFalseSyntax ()
: DeclareModel (Boolean::component, "false",
"Always false.")
{ }
void load_frame (Frame&) const
{ }
} BooleanFalse_syntax;
struct BooleanOperands : public Boolean
{
const std::vector<Boolean*> operand;
// Simulation.
void tick (const Units& units, const Scope& scope, Treelog& msg)
{
for (size_t i = 0; i < operand.size (); i++)
operand[i]->tick (units, scope, msg);
}
bool missing (const Scope& scope) const
{
for (size_t i = 0; i < operand.size (); i++)
if (operand[i]->missing (scope))
return true;
return false;
}
// Create.
bool initialize (const Units& units, const Scope& scope, Treelog& msg)
{
bool ok = true;
for (size_t i = 0; i < operand.size (); i++)
if (!operand[i]->initialize (units, scope, msg))
{
std::ostringstream tmp;
tmp << name << "[" << i << "]";
Treelog::Open nest (msg, tmp.str ());
ok = false;
}
return ok;
}
bool check (const Units& units, const Scope& scope, Treelog& msg) const
{
Treelog::Open nest (msg, name);
bool ok = true;
for (size_t i = 0; i < operand.size (); i++)
if (!operand[i]->check (units, scope, msg))
ok = false;
return ok;
}
BooleanOperands (const BlockModel& al)
: Boolean (al),
operand (Librarian::build_vector<Boolean> (al, "operands"))
{ }
~BooleanOperands ()
{ sequence_delete (operand.begin (), operand.end ()); }
};
static struct BooleanOperandsSyntax : public DeclareBase
{
BooleanOperandsSyntax ()
: DeclareBase (Boolean::component, "operands", "\
Base class for boolean expressions involving multiple boolean operands.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_object ("operands", Boolean::component,
Attribute::Const, Attribute::Variable, "\
List of operands to compare.");
frame.order ("operands");
}
} BooleanOperands_syntax;
struct BooleanAnd : public BooleanOperands
{
bool value (const Scope& scope) const
{
for (size_t i = 0; i < operand.size (); i++)
if (!operand[i]->value (scope))
return false;
return true;
}
BooleanAnd (const BlockModel& al)
: BooleanOperands (al)
{ }
};
static struct BooleanAndSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new BooleanAnd (al); }
BooleanAndSyntax ()
: DeclareModel (Boolean::component, "and", "operands",
"True if and only if all operands are true.")
{ }
void load_frame (Frame&) const
{ }
} BooleanAnd_syntax;
struct BooleanOr : public BooleanOperands
{
bool value (const Scope& scope) const
{
for (size_t i = 0; i < operand.size (); i++)
if (operand[i]->value (scope))
return true;
return false;
}
BooleanOr (const BlockModel& al)
: BooleanOperands (al)
{ }
};
static struct BooleanOrSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new BooleanOr (al); }
BooleanOrSyntax ()
: DeclareModel (Boolean::component, "or", "operands",
"True if and only if any operand is true.")
{ }
void load_frame (Frame&) const
{ }
} BooleanOr_syntax;
struct BooleanXOr : public BooleanOperands
{
bool value (const Scope& scope) const
{
daisy_assert (operand.size () == 2);
return operand[0]->value (scope) != operand[1]->value (scope);
}
BooleanXOr (const BlockModel& al)
: BooleanOperands (al)
{ }
};
static struct BooleanXOrSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new BooleanXOr (al); }
BooleanXOrSyntax ()
: DeclareModel (Boolean::component, "xor",
"True if and only if one operand is true, and one false.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_object ("operands", Boolean::component,
Attribute::Const, 2, "\
The two operands to compare.");
frame.order ("operands");
}
} BooleanXOr_syntax;
struct BooleanNot : public BooleanOperands
{
bool value (const Scope& scope) const
{
daisy_assert (operand.size () == 1);
return !operand[0]->value (scope);
}
BooleanNot (const BlockModel& al)
: BooleanOperands (al)
{ }
};
static struct BooleanNotSyntax : DeclareModel
{
Model* make (const BlockModel& al) const
{ return new BooleanNot (al); }
BooleanNotSyntax ()
: DeclareModel (Boolean::component, "not",
"True if and only if the operand is not true.")
{ }
void load_frame (Frame& frame) const
{
frame.declare_object ("operands", Boolean::component,
Attribute::Const, 1, "\
The operand to check.");
frame.order ("operands");
}
} BooleanNot_syntax;
static struct BooleanInit : public DeclareComponent
{
BooleanInit ()
: DeclareComponent (Boolean::component, "\
Generic representation of booleans.")
{ }
} Boolean_init;
// boolean.C ends here.