-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.cpp
More file actions
1168 lines (1008 loc) · 31.5 KB
/
expression.cpp
File metadata and controls
1168 lines (1008 loc) · 31.5 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include "expression.h"
#include "globals.h"
#include "sector.h"
// Define statics
vector<string> Expression::names;
vector<string> Expression::freeVars;
unordered_map<int,Expression::Attribs> Expression::dict;
unordered_map<string, Expression*> Expression::definitions;
unordered_map<string, Expression*> Expression::probes;
unordered_map<string, Expression*> Expression::specials;
Expression::OpDesc *Expression::operators = 0;
bool Expression::initialised = false;
int Expression::level = 0;
void crash(int line)
{
cout << "*** Program fault in file " << __FILE__ << " at " << line << "***" << endl;
exit(999);
}
/// @class Expression
/// Parses and resolves equations consisting of a dependent variable (LHS or
/// lvalue) and an rvalue that can consist of a sequence of terms and operators;
/// the name of a sector whose balance is required; or a special built-in
/// quantity such as the periodic time.
/// @brief Defines an expression identifying a sectoral balance with a dependent
/// variable (LHS or lvalue).
Expression::Expression(string &lhs, Sector *sect, double init)
{
this->lhs = lhs; // useful for diagnostics
error = Error::none;
if (!initialised) {
initialise();
}
defined = -1;
prev = init;
sector = sect;
is_sector_bal = true; // see note in header
is_special = false;
is_parametric = false;
orig = "Sector " + lhs;
res = init;
definitions[lhs] = this;
return;
}
void Expression::_in(string fn)
{
diags(string("->") + fn);
++level;
}
void Expression::_out()
{
--level;
}
void Expression::diags(string s)
{
for (int i = 0; i < level; i++) {
cout << " ";
}
cout << s << endl;
}
/// @brief Defines an expression consisting of a dependent variable (LHS or
/// lvalue) and a predicate (RHS or rvalue) containing a number of terms connected
/// by infix operators. If an expression having the given name on the lhs already
/// exists, its rhs will be replace by the new predicate rather than creating an
/// entirely new expression.
Expression::Expression(string &lhs, string &expr, ExpressionType t, double init)
{
_in("Expression::Expression(");
diags(lhs + "," + expr + ")");
this->lhs = lhs; // useful for diagnostics
error = Error::none;
is_sector_bal = false;
is_special = false;
// Initialise on first instantiation only
if (!initialised) {
initialise();
}
orig = expr;
defined = -1;
prev = init;
res = init;
// Tokenise RHS
int num_tokens = tokenise(expr);
// Check OK
if (num_tokens < 0) {
error = Error::invalid_operator;
return;
} else if (num_tokens == 0) {
error = Error::no_tokens;
return;
} else if (num_tokens == 1) {
// Check if the sole token is a number and if so flag the expression as
// a parameter (i.e. it can be changed dynamically)
Attribs a = dict[tokens[0]];
if (a.category == number) {
is_parametric = true;
dflt = a.value.d;
res = dflt;
prev = dflt;
}
}
// Convert to RPN
error = Error::none;
reversePolish();
if (error != Error::none) {
_out();
return;
}
// Record as definition or probe
if (t == exp_normal) {
//Expression *old_exp = definitions[lhs];
definitions[lhs] = this;
stringstream str;
for (auto it : definitions) {
str << it.first << " ";
}
diags(str.str());
//delete old_exp;
} else {
//delete probes[lhs]; // remove any existing entry
probes[lhs] = this;
}
_out();
}
/// @brief Defines an expression connecting a dependent variable with a special
/// 'built-in' value.
///
/// @see SpecialType
Expression::Expression(string &lhs, SpecialType t)
{
_in("Expression::Expression");
string s;
stringstream str(s);
str << lhs << "," << t << ")";
diags(s);
this->lhs = lhs; // useful for diagnostics
error = Error::none;
orig = "[Time]"; /// @todo (david#5#) add other variations later
defined = -1;
prev = -1;
res = -1;
special_type = t;
is_special = true;
is_sector_bal = false;
specials[lhs] = this;
_out();
}
Expression::~Expression()
{
}
bool Expression::isParameter()
{
return is_parametric;
}
double Expression::getDefault()
{
return dflt;
}
void Expression::revert(double init)
{
_in("Expression::revert");
defined = -1;
if (!is_parametric) {
prev = -1;
res = init;
}
stringstream str;
str << "init = " << init << ": lhs = " << lhs << " prev = " << prev
<< ", res = " << res << ", defined = " << defined;
diags(str.str());
_out();
}
void Expression::initialise()
{
operators = new OpDesc[Optype::__count];
operators[nullop] = {string(""), 0};
operators[plus] = {string("+"), 1};
operators[minus] = {string("-"), 1};
operators[mult] = {string("*"), 2};
operators[divide] = {string("/"), 2};
operators[lparen] = {string("("), 0};
operators[rparen] = {string(")"), 0};
initialised = true;
}
/// @brief Whether or not this variable refers to a sector balance
///
/// @return bool True if a sector balance; false otherwise.
bool Expression::isSectorBalance()
{
return is_sector_bal;
}
/// @brief Whether or not this variable refers to a built-in function
///
/// @return bool True if built-in function; false otherwise.
bool Expression::isSpecial()
{
return is_special;
}
/// @brief returns the sector associated with this expression. If the expression
/// does not refer to a sectoral balance the result is undefined and not useful.
Sector *Expression::getSector()
{
return sector;
}
/// @brief Check whether an expression exists having the specified name.
///
/// @param s string: the name of the required expression
/// @return Expression*: a pointer to the required expression if it exists, or
/// nullptr otherwise.
///
/// @see Expression::evaluated(string)
Expression *Expression::find(string s)
{
Expression *exp;
unordered_map<string,Expression*>::const_iterator it_specials = specials.find(s);
if (it_specials == specials.end()) {
unordered_map<string,Expression*>::const_iterator it_definitions = definitions.find(s);
if (it_definitions == definitions.end()) {
unordered_map<string,Expression*>::const_iterator it_probes = probes.find(s);
if (it_probes == probes.end()) {
exp = (Expression*)nullptr;
} else {
exp = it_probes->second;
}
} else {
exp = it_definitions->second;
}
} else {
exp = it_specials->second;
}
return exp;
}
void Expression::replace(string s, Expression *exp)
{
delete definitions[s];
definitions[s] = exp;
}
void Expression::clearAll()
{
// Clear tokens
dict.clear();
// Delete expressions
for (auto it : definitions) {
delete it.second;
}
// Clear definitions
definitions.clear();
// Clear names
names.clear();
// We don't clear specials as they are always available
}
void Expression::revertAll()
{
for (auto it : definitions) {
it.second->revert();
}
for (auto it : specials) {
it.second->revert();
}
}
bool Expression::remove(string s)
{
return bool(definitions.erase(s));
}
/// @brief error_string returns a message describing the last error encountered.
///
/// @return string: printable error message.
/// @see error_info
string Expression::error_string()
{
string s;
switch (error) {
case none:
s = "no error";
break;
case invalid_operator:
s = "invalid operator " + error_info;
break;
case too_few_operands:
s = "too few operands";
break;
case unmatched_rparen:
s = "right parenthesis (\')\') without matching left parenthesis";
break;
case unmatched_lparen:
s = "left parenthesis (\'(\') without matching right parenthesis";
break;
case undefined_expression:
s = "cannot evaluate " + error_info;
break;
case incomplete_expression:
s = "cannot evaluate " + error_info;
break;
default:
s = "unknown error";
break;
}
return s;
}
string Expression::getRHS()
{
return (is_sector_bal ? sector->getDescription() + " sector balance" : orig);
}
/// @brief Returns the value of the expression if it has been evaluated
///
/// @return int: the value of the expression if it has been evaluated, otherwise
/// undefined.
///
/// @see evaluate(string), evaluated(), evaluate()
double Expression::value()
{
return res;
}
double Expression::previousValue(int serial)
{
// If the expression is marked as defined for this serial the previous value
// will have been stored; otherwise we must use the current value. It's
// worth checking that it's been defined up to 0, as otherwise there won't
// be a current value either. In this case we chould really evaluate it,
// but for now we'll just assume everything starts at zero. This should be
// fixed later.
_in("Expression::previousValue(int serial)");
stringstream str;
str << "defined = " << defined << ", serial = " << serial << ", prev = " << prev;
diags(str.str());
if (defined == serial) {
return defined > 0 ? prev : 0;
} else if (serial < defined) {
return 0;
} else if (defined == -1) {
evaluate(serial);
return prev;
} else {
return (is_sector_bal ? sector->getBalance() : res);
}
_out();
}
/// @brief Evaluates the operand represented by the token
///
/// @param tok int : the token
/// @param val double& : the result will be stored here iff successful
/// @param serial int : interval counter (clock ticks)
///
/// @return bool : true iff successful
///
/// This function adds a bit of housekeeping to the evaluation process so it can
/// be used when processing the RPN. It checks the token type and returns in good
/// order when things don't work out. It doesn't update *define* as it is only
/// producing an intermediate result.
///
/// @see evaluate()
///
/// @todo (david#5#) Improve error reporting; at present failure to evaluate a sub-expression
/// is only recorded as a failure in the calling expression. Needs investigating.
bool Expression::evaluate_token(int tok, double &val, int serial)
{
_in("evaluate_token");
Attribs a;
Expression *expr;
bool ok = false;
assert(!is_sector_bal);
a = dict[tok];
if (a.category == var)
{
// Diagnostics only (remove later)
string var_name = "";
if (a.category == var) {
var_name = names[a.id];
if (var_name == "fs") {
int x = 1234;
x += 12;
}
}
expr = Expression::find(names[a.id]);
if (expr == nullptr)
{
error = undefined_expression;
error_info = names[a.id];
return false;
}
else
{
if (a.value.i < 0)
{
diags("getting previous value");
// Refers to previous value of the expression. No evaluation needed
// as previous value is always available. However, we do not set
// defined as is relates to the current value, which may not have
// been evaluated or defined.
val = expr->previousValue(serial); // evaluates if necessary
_out();
return true;
}
else if (expr->evaluate(serial))
{
val = expr->value();
ok = true;
}
else
{
error = incomplete_expression;
error_info = names[a.id];
}
}
}
else
{
assert(a.category == number);
val = a.value.d;
ok = true;
}
_out();
return ok;
}
void Expression::setValue(double d)
{
if (is_parametric) {
res = d;
prev = d;
}
}
/// @brief Evaluates the expression
///
/// @param serial int: interval counter. Incremented once for each clock tick
///
/// @return bool: *true* if a result was obtained or is available, otherwise *false*
///
/// @todo (david#5#) Tidy up distinction between undefined and incomplete expressions
///
/// @see evaluate(string), value(), evaluate_token(int, int&), error
///
/// If the expression has already been evaluated (check using the evaluated()
/// function) the function simply returns *true*. Otherwise it tries to evaluate
/// itself by interpreting its RPN representation. If successful the result is
/// stored, the function is marked as evaluated, and *true* is returned. If not,
/// *false* is returned, *error* is set to indicate the type of error,and if
/// appropriate additional information is stored in *error_info*. For example
/// if an expression was encountered that has not been defined, its name is
/// stored as *error_info*.
bool Expression::evaluate(int serial)
{
_in("Expression::evaluate");
stringstream str;
str << "lhs = " << lhs << ", serial = " << serial << ", defined = " << defined;
diags(str.str());
/// @note (david#9#) If defined > serial we are reverting to an earlier state. Generally
/// this would be a restart, in which case we should set prev and res back
/// to their starting value -- essentially a partial re-initialisation. I
/// think this should do it, but it should be kept under review. Also, we
/// will need something much more sophisticated if we want to revert to an
/// intermediate state.
if (defined > serial) {
revert();
} else if (defined == serial) {
_out();
return true;
} else /* defined < serial */ {
stringstream str;
str << "serial = " << serial << ": " << "prev = " << prev << ", res = " << res;
diags(str.str());
prev = res;
}
defined = serial;
if (lhs == "c") {
stringstream str;
str << "defined = " << defined;
diags(str.str());
}
if (is_sector_bal) {
res = sector->getBalance();
defined = serial;
_out();
return true;
} else if (is_special) {
/// @todo (david#5#) Add special fumction R returning a random number
// At present there's only one kind of special function -- the time
// function, which just returns the value of serial.
defined = serial;
res = serial;
prev = serial - 1;
_out();
return true;
} else if (is_parametric) {
// Value (res) will have been set from SimXFrame so no need to evaluate
// anything
_out();
return true;
}
stack<int> st;
int tok, tok1, tok2, n;
double res0, res1, res2;
Attribs a;
// For diagnostics
if (rpn.size() < 1 || rpn.size() > 100) {
wxMessageBox(_("Faulty RPN"), _("PROGRAM FAULT"), wxICON_EXCLAMATION);
}
if (rpn.size() == 1)
{
// The expression consists of a single token. Evaluate it, leaving the
// result in res. If OK update defined to the current serial.
double temp;
if (evaluate_token(rpn[0], temp, serial)) {
res = temp;
_out();
return true;
} else {
/// @todo (david#5#) Should set error here...
error_info = names[dict[rpn[0]].id];
return false;
}
}
else for (unsigned int i = 0; i < rpn.size(); i++)
{
// The expression consists of a number of terms represented as tokens
// in the RPN array. We must reduce this to a single value, evaluating
// individual terms as we go
// Get next token and process according to token type (category)
tok = rpn[i]; // next token
a = dict[tok]; // get attributes
if (a.category == var || a.category == number)
{
st.push(tok); // number or variable -- push onto stack
}
else if (a.category == opr)
{
diags("Operator found" );
// Operators require two operands
if (st.size() < 2) {
error = Error::too_few_operands;
return false;
}
// Get second operand (token) from stack
tok2 = st.top();
st.pop();
// Evaluate it
if (!evaluate_token(tok2, res2, serial)) {
error = undefined_expression;
error_info = names[dict[tok2].id];
return false;
}
// Get first operand (token) from stack
tok1 = st.top();
st.pop();
// Evaluate it
if (!evaluate_token(tok1, res1, serial)) {
error = undefined_expression;
error_info = names[dict[tok1].id];
return false;
}
// Combine the operands...
stringstream str;
str << "lhs = " << lhs << ", serial = " << serial << ": "
<< "Operands: " << res1 << ", " << res2;
diags(str.str());
if (a.value.i == plus) {
res0 = res1 + res2;
} else if (a.value.i == minus) {
res0 = res1 - res2;
} else if (a.value.i == mult) {
res0 = res1 * res2;
} else if (a.value.i == divide) {
if (res2 == 0) {
// Compiler might complain, but it's OK because division
// by res2 ~ 0 is not dangerous, and division by res2 == 0
// will be rejected...
error = Error::zero_divisor;
return false;
} else {
res0 = res1 / res2;
}
}
/* Diags only
cout << "Expression[" << lhs << "]::evaluate(" << serial << "): "
<< "Operator produces temporary result = " << res0
<< " (stacked)" << endl;
*/
// ... and push the result onto the stack.
n = makeToken(res0);
st.push(n);
/// @note (david#5#) It's pretty inefficient to make a new token for every
/// number but avoiding duplicates would be an unnecessary
/// complication at this stage. Consider modifying makeToken(int)
/// to avoid duplicates later.
}
}
if (lhs == "c") {
stringstream str;
str << "* OUT * defined = " << defined << " ***";
diags(str.str());
}
res = res0;
_out();
return true;
}
int Expression::makeName(string s)
{
unsigned int i;
for (i = 0; i < names.size(); i++) {
if (names[i] == s) {
return i;
}
}
names.push_back(s);
return i;
}
int Expression::makeToken(double num)
{
/** @todo (David#9#): Avoid putting number tokens in dictionary
* This puts a new entry in the dictionary every time a number is used,
* which is messy and inefficient. However, we do need to store the Attribs
* somewhere, and it doesn't seem to be a very good idea to look for a match
* every time we want to store a number. On the other hand I don't think
* we should have to do this for storing the result of an evaluation since
* that's a final value.
*/
Attribs a;
a.category = number;
a.value.d = num;
a.id = -1;
int n = dict.size();
dict[n] = a;
return n;
}
int Expression::makeToken(string s)
{
int ix = makeName(s);
unsigned int i;
for (i = 0; i < dict.size(); i++) {
if (dict[i].category == var && dict[i].id == ix) {
return i;
}
}
Attribs a;
a.category = var;
a.value.i = 0;
a.id = ix;
dict[i] = a;
return i;
}
void Expression::reversePolish()
{
_in("Expression::reversePolish()");
stack<int> st;
int tok1, tok2;
Attribs a1, a2;
Category cat1;
int prec1, prec2;
for (unsigned int i = 0; i < tokens.size(); i++) {
tok1 = tokens[i];
a1 = dict[tok1];
//val1 = a1.value;
cat1 = a1.category;
if (cat1 == opr) {
prec1 = operators[a1.value.i].prec;
}
if (cat1 == var || cat1 == number)
{
rpn.push_back(tok1);
}
else if (cat1 == opr)
{
if (a1.value.i == lparen)
{
st.push(tok1);
}
else if (a1.value.i == rparen)
{
tok2 = -1;
while (st.size() > 0)
{
tok2 = st.top();
st.pop();
if (dict[tok2].value.i == lparen)
{
break;
}
else
{
rpn.push_back(tok2);
}
}
if (dict[tok2].value.i != lparen)
{
cout << "Expression::reversePolish(): Unmatched rparen" << endl;
error = unmatched_rparen;
return;
}
}
else
{
while (st.size() > 0)
{
tok2 = st.top();
a2 = dict[tok2];
wxASSERT(a2.category == opr);
prec2 = operators[a2.value.i].prec;
// We haven't accounted for associativity, so we simply assume
// all operators are left-associative
if (prec1 > prec2) {
break;
}
st.pop();
rpn.push_back(tok2);
}
st.push(tok1);
}
}
else
{
if (st.size() < 2) {
cout << "Expression::reversePolish(): Too few operands" << endl;
error = Error::too_few_operands;
_out();
return;
}
}
}
while (st.size() > 0)
{
tok2 = st.top();
if (dict[tok2].value.i == lparen)
{
cout << "Expression::reversePolish(): Unmatched lparen" << endl;
error = unmatched_lparen;
_out();
return;
}
else
{
rpn.push_back(tok2);
st.pop();
}
}
_out();
}
unordered_map<string, Expression*> &Expression::getDefinitions()
{
return definitions;
}
unordered_map<string, Expression*> &Expression::getProbes()
{
return probes;
}
unordered_map<string, Expression*> &Expression::getSpecials()
{
return specials;
}
unordered_map<int,Expression::Attribs> &Expression::getDictionary()
{
return dict;
}
vector<string> &Expression::getFreeVariables()
{
freeVars.clear();
for (auto it : dict) {
Attribs a = it.second;
if (a.category == Category::var) {
string name = getName(a.id);
if (find(name) == nullptr) {
freeVars.push_back(name);
}
}
}
return freeVars;
}
bool Expression::haveFreeVariables()
{
for (auto it : dict) {
Attribs a = it.second;
if (a.category == Category::var) {
string name = getName(a.id);
if (find(name) == nullptr) {
return true;
}
}
}
return false;
}
string &Expression::getName(int id)
{
return names.at(id);
}
// Stores the term in the dictionary and returns the dictionary index or -1 if
// there's an error (currently this just means an invalid operator).
int Expression::term(string s, Category cat)
{
//assert(sector == nullptr);
_in("Expression::term(string s, Category cat)");
stringstream str;
str << "s = " << s << ", " << cat;
diags(str.str());
union {
int i;
double d;
} id;
id.i = -1;
int dashed = 0;
// Validate and evaluate
switch(cat) {
case opr:
// See if s is listed as an operator
for (unsigned int i = 0; i < Optype::__count; i++) {
if (operators[i].name == s) {
stringstream str;
str << "operator " << s << " has id " << i;
diags(str.str());
id.i = i;
break;
}
}
if (id.i < 0) {
error = invalid_operator;
error_info = s;
stringstream str;
str << "Unrecognised operator " << s;
diags(str.str());
_out();
return -1;
}
break;
case number:
// Convert to number
{
id.d = atof(s.c_str()); // (only positive integers handled at present)
stringstream str;
str << "number " << s << " has id " << id.d;
diags(str.str());
}
/// @todo (david#5#) Allow unary operator '-'. Currently we have to write '-1' as
/// (0 - 1)
break;
case var:
// Check whether the last character is '
if (s.back() == '\'') {
s.pop_back();
dashed = -1;
}
// See if s is listed as a name
for (unsigned int i = 0; i < names.size(); i++) {
if (names[i] == s) {
id.i = i;
stringstream str;
str << "existing variable " << s << " has id " << id.i;
diags(str.str());
}
}
// If not, add it
if (id.i == -1) {
names.push_back(s);
id.i = names.size() - 1; // (first element is names[0])
stringstream str;
str << "new variable " << s << " has id " << id.i;
diags(str.str());
}
break;
}