forked from douglascraigschmidt/CPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
860 lines (709 loc) · 18.8 KB
/
Copy pathInterpreter.cpp
File metadata and controls
860 lines (709 loc) · 18.8 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
#ifndef _INTERPRETER_CPP_
#define _INTERPRETER_CPP_
#include <iostream>
#include <memory>
#include <stdlib.h>
#include "Component_Node.h"
#include "Leaf_Node.h"
#include "Composite_Negate_Node.h"
#include "Composite_Add_Node.h"
#include "Composite_Subtract_Node.h"
#include "Composite_Divide_Node.h"
#include "Composite_Multiply_Node.h"
#include "Interpreter.h"
/**
* @class Symbol
* @brief Abstract base class of all parse tree nodes.
*/
class Symbol
{
public:
/// constructor
Symbol (Symbol *left, Symbol *right, int precedence_ = 0);
/// destructor
virtual ~Symbol (void);
/// abstract method for returning precedence level (higher
/// value means higher precedence
virtual int precedence (void)
{
return precedence_;
}
virtual int add_precedence (int accumulated_precedence) = 0;
/// abstract method for building an Expression Expression_Tree Node
virtual Component_Node *build (void) = 0;
/// left and right pointers
Symbol *left_;
Symbol *right_;
int precedence_;
};
/**
* @class Operator
* @brief Abstract base class for all parse tree node operators
* @see Add, Subtract, Multiply, Divide
*/
class Operator : public Symbol
{
public:
/// constructor
Operator (Symbol *left, Symbol *right, int precedence_ = 1);
/// destructor
~Operator (void);
};
/**
* @class Unary_Operator
* @brief Abstract base class for all parse tree node operators
* @see Add, Subtract, Multiply, Divide
*/
class Unary_Operator : public Symbol
{
public:
/// constructor
Unary_Operator (Symbol *right, int precedence_ = 1);
/// destructor
~Unary_Operator (void);
};
/**
* @class Number
* @brief Leaf node of parse tree
*/
class Number : public Symbol
{
public:
/// constructors
Number (std::string input);
Number (int input);
/// destructor
virtual ~Number (void);
/// returns the precedence level
//virtual int precedence (void);
virtual int add_precedence (int accumulated_precedence);
/// builds an equivalent Expression_Tree node
virtual Component_Node *build (void);
private:
/// contains the value of the leaf node
int item_;
};
/**
* @class Subtract
* @brief Subtraction node of the parse tree
*/
class Subtract : public Operator
{
public:
/// constructor
Subtract (void);
/// destructor
virtual ~Subtract (void);
/// returns the precedence level
//virtual int precedence (void);
virtual int add_precedence (int accumulated_precedence);
/// builds an equivalent Expression_Tree node
virtual Component_Node *build (void);
};
/**
* @class Add
* @brief Addition node of the parse tree
*/
class Add : public Operator
{
public:
/// constructor
Add (void);
/// destructor
virtual ~Add (void);
/// returns the precedence level
//virtual int precedence (void);
virtual int add_precedence (int accumulated_precedence);
/// builds an equivalent Expression_Tree node
virtual Component_Node *build (void);
};
/**
* @class Negate
* @brief Negate node of the parse tree
*/
class Negate : public Unary_Operator
{
public:
/// constructor
Negate (void);
/// destructor
virtual ~Negate (void);
/// returns the precedence level
//virtual int precedence (void);
virtual int add_precedence (int accumulated_precedence);
/// builds an equivalent Expression_Tree node
virtual Component_Node *build (void);
};
/**
* @class Multiply
* @brief Multiplication node of the parse tree
*/
class Multiply : public Operator
{
public:
/// constructor
Multiply (void);
/// destructor
virtual ~Multiply (void);
/// returns the precedence level
//virtual int precedence (void);
virtual int add_precedence (int accumulated_precedence);
/// builds an equivalent Expression_Tree node
virtual Component_Node *build (void);
};
/**
* @class Divide
* @brief Division node of the parse tree
*/
class Divide : public Operator
{
public:
/// constructor
Divide (void);
/// destructor
virtual ~Divide (void);
/// returns the precedence level
//virtual int precedence (void);
virtual int add_precedence (int accumulated_precedence);
/// builds an equivalent Expression_Tree node
virtual Component_Node *build (void);
};
// constructor
Interpreter_Context::Interpreter_Context (void)
{
}
// destructor
Interpreter_Context::~Interpreter_Context (void)
{
}
// return the value of a variable
int
Interpreter_Context::get (std::string variable)
{
return map_[variable];
}
// set the value of a variable
void
Interpreter_Context::set (std::string variable, int value)
{
map_[variable] = value;
}
// print all variables and their values
void
Interpreter_Context::print (void)
{
for (std::map<std::string, int>::iterator i = map_.begin ();
i != map_.end ();
++i)
std::cout << i->first << "=" << i->second << "\n";
}
// clear all variables and their values
void
Interpreter_Context::reset (void)
{
map_.clear ();
}
// constructor
Symbol::Symbol (Symbol *left, Symbol *right, int precedence)
: left_ (left), right_ (right), precedence_ (precedence)
{
}
// destructor
Symbol::~Symbol (void)
{
delete left_;
delete right_;
}
// constructor
Operator::Operator (Symbol *left, Symbol *right, int precedence)
: Symbol (left, right, precedence)
{
}
// destructor
Operator::~Operator (void)
{
}
// constructor
Unary_Operator::Unary_Operator (Symbol *right, int precedence)
: Symbol (0, right, precedence)
{
}
// destructor
Unary_Operator::~Unary_Operator (void)
{
}
// constructor
Number::Number (std::string input)
: Symbol (0, 0, 4)
{
item_ = ::atoi (input.c_str ());
}
// constructor
Number::Number (int input)
: Symbol (0, 0, 4),
item_ (input)
{
}
// destructor
Number::~Number (void)
{
}
// returns the precedence level
int
Number::add_precedence (int precedence)
{
return this->precedence_ = 4 + precedence;
}
// builds an equivalent Expression_Tree node
Component_Node *
Number::build (void)
{
return new Leaf_Node (item_);
}
// constructor
Negate::Negate (void)
: Unary_Operator (0, 3)
{
}
// destructor
Negate::~Negate (void)
{
}
// returns the precedence level
int
Negate::add_precedence (int precedence)
{
return this->precedence_ = 3 + precedence;
}
// builds an equivalent Expression_Tree node
Component_Node *
Negate::build ()
{
return new Composite_Negate_Node (right_->build ());
}
// constructor
Add::Add (void)
: Operator (0, 0, 1)
{
}
// destructor
Add::~Add (void)
{
}
// returns the precedence level
int
Add::add_precedence (int precedence)
{
return this->precedence_ = 1 + precedence;
}
// builds an equivalent Expression_Tree node
Component_Node *
Add::build (void)
{
return new Composite_Add_Node (left_->build (), right_->build ());
}
// constructor
Subtract::Subtract (void)
: Operator (0, 0, 1)
{
}
// destructor
Subtract::~Subtract (void)
{
}
// returns the precedence level
int
Subtract::add_precedence (int precedence)
{
return this->precedence_ = 1 + precedence;
}
// builds an equivalent Expression_Tree node
Component_Node *
Subtract::build (void)
{
return new Composite_Subtract_Node (left_->build (), right_->build ());
}
// constructor
Multiply::Multiply (void)
: Operator (0, 0, 2)
{
}
// destructor
Multiply::~Multiply (void)
{
}
// returns the precedence level
int
Multiply::add_precedence (int precedence)
{
return this->precedence_ = 2 + precedence;
}
// builds an equivalent Expression_Tree node
Component_Node *
Multiply::build (void)
{
return new Composite_Multiply_Node (left_->build (), right_->build ());
}
// constructor
Divide::Divide (void)
: Operator (0, 0, 2)
{
}
// destructor
Divide::~Divide (void)
{
}
// returns the precedence level
int
Divide::add_precedence (int precedence)
{
return this->precedence_ = 2 + precedence;
}
// builds an equivalent Expression_Tree node
Component_Node *
Divide::build (void)
{
return new Composite_Divide_Node (left_->build (), right_->build ());
}
// constructor
Interpreter::Interpreter (void)
{
}
// destructor
Interpreter::~Interpreter (void)
{
}
// method for checking if a character is a valid operator
bool
Interpreter::is_operator (char input)
{
return input == '+'
|| input == '-'
|| input == '*'
|| input == '/';
}
// method for checking if a character is a number
bool
Interpreter::is_number (char input)
{
return input >= '0' && input <= '9';
}
// method for checking if a character is a candidate
// for a part of a variable name
bool
Interpreter::is_alphanumeric (char input)
{
return (input >= 'a' && input <= 'z')
|| (input >= 'A' && input <= 'Z')
|| (input == '_')
|| (input >= '0' && input <= '9');
}
// inserts a terminal into the parse tree
void
Interpreter::terminal_insert (Symbol *terminal,
std::list<Symbol *>& list)
{
if (!list.empty ())
{
// Something exists in the list, so make this number the rightmost child
Symbol *symbol = list.back ();
if (symbol)
{
// while there is a right child, continue down
for (; symbol->right_; symbol = symbol->right_)
continue;
// symbol right will be this terminal and that's all we have
// to do.
symbol->right_ = terminal;
}
}
else
// a number appeared first
list.push_back (terminal);
}
// inserts a variable (leaf node / number) into the parse tree
void
Interpreter::variable_insert (Interpreter_Context &context,
const std::string &input,
std::string::size_type &i,
int & accumulated_precedence,
std::list<Symbol *>& list,
Symbol *& lastValidInput)
{
// merge all consecutive number chars into a single
// Number symbol, eg '123' = int (123). Scope of j needs
// to be outside of the for loop.
std::string::size_type j = 1;
for (; i + j <= input.length () && is_alphanumeric (input[i + j]); ++j)
continue;
// lookup the variable in the context
int value = context.get (input.substr (i,j));
// make a Number out of the integer
Number *number = new Number (value);
number->add_precedence (accumulated_precedence);
lastValidInput = number;
// update i to the last character that was a number. the ++i will
// update the i at the end of the loop to the next check.
i += j - 1;
precedence_insert (number, list);
}
// inserts a leaf node / number into the parse tree
void
Interpreter::number_insert (const std::string &input,
std::string::size_type &i,
int & accumulated_precedence,
std::list<Symbol *>& list,
Symbol *& lastValidInput)
{
// merge all consecutive number chars into a single Number symbol,
// eg '123' = int (123). Scope of j needs to be outside of the for
// loop.
std::string::size_type j = 1;
for (; i + j <= input.length () && is_number (input[i + j]); ++j)
continue;
Number *number = new Number (input.substr (i,j));
number->add_precedence (accumulated_precedence);
lastValidInput = number;
// update i to the last character that was a number. the ++i will
// update the i at the end of the loop to the next check.
i += j - 1;
precedence_insert (number, list);
}
// inserts a multiplication or division into the parse tree
void
Interpreter::precedence_insert (Symbol *op,
std::list<Symbol *>& list)
{
if (!list.empty ())
{
// if last element was a number, then make that our left_
Symbol *parent = list.back ();
Symbol *child = parent->right_;
if (child)
{
// while there is a child of parent, keep going down the right side
for (;
child && child->precedence () < op->precedence ();
child = child->right_)
parent = child;
}
if (parent->precedence () < op->precedence ())
{
// op left will be the old child. new parent child will be
// the op. To allow infinite negations, we have to check for unary_operator.
// **************** This is the culprit
if (!op->left_)
op->left_ = child;
parent->right_ = op;
}
else
{
// this can be one of two things, either we are the same
// precedence or we are less precedence than the parent.
// this also means different things for unary ops. The
// most recent unary op (negate) has a higher precedence
if (dynamic_cast <Unary_Operator *> (op))
{
for (;
child && child->precedence () == op->precedence ();
child = child->right_)
parent = child;
// I can't think of a valid reason that parent->right_ would
// be possible !0
parent->right_ = op;
}
else
{
// everything else is evaluated the same. For instance, if
// this is 5 * 4 / 2, and we currently have Mult (5,4) in the
// list, we need to make parent our left child.
op->left_ = parent;
list.pop_back ();
list.push_back (op);
}
}
}
else
{
list.push_back (op);
}
}
void
Interpreter::main_loop (Interpreter_Context & context,
const std::string &input,
std::string::size_type &i,
Symbol *& lastValidInput,
bool & handled,
int & accumulated_precedence,
std::list<Symbol *>& list)
{
handled = false;
if (is_number (input[i]))
{
handled = true;
// leaf node
number_insert (input, i, accumulated_precedence,
list, lastValidInput);
}
else if (is_alphanumeric (input[i]))
{
handled = true;
// variable leaf node
variable_insert (context, input, i, accumulated_precedence,
list, lastValidInput);
}
else if (input[i] == '+')
{
handled = true;
// addition operation
Add *op = new Add ();
op->add_precedence (accumulated_precedence);
lastValidInput = 0;
// insert the op according to left-to-right relationships
precedence_insert (op, list);
}
else if (input[i] == '-')
{
handled = true;
Symbol * op = 0;
// subtraction operation
Number *number = 0;
if (!lastValidInput)
{
// Negate
op = new Negate ();
op->add_precedence (accumulated_precedence);
}
else
{
// Subtract
op = new Subtract ();
op->add_precedence (accumulated_precedence);
}
lastValidInput = 0;
// insert the op according to left-to-right relationships
precedence_insert (op, list);
}
else if (input[i] == '*')
{
handled = true;
// multiplication operation
Multiply *op = new Multiply ();
op->add_precedence (accumulated_precedence);
lastValidInput = 0;
// insert the op according to precedence relationships
precedence_insert (op, list);
//associative_insert (op);
}
else if (input[i] == '/')
{
handled = true;
// division operation
Divide *op = new Divide ();
op->add_precedence (accumulated_precedence);
lastValidInput = 0;
// insert the op according to precedence relationships
precedence_insert (op, list);
}
else if (input[i] == '(')
{
handled = true;
handle_parenthesis (context, input, i, lastValidInput,
handled, accumulated_precedence, list);
}
else if (input[i] == ' ' || input[i] == '\n')
{
handled = true;
// skip whitespace
}
}
void
Interpreter::handle_parenthesis (Interpreter_Context & context,
const std::string &input,
std::string::size_type &i,
Symbol *& lastValidInput,
bool & handled,
int & accumulated_precedence,
std::list<Symbol *>& master_list)
{
/* handle parenthesis is a lot like handling a new interpret.
the difference is that we have to worry about how the calling
function has its list setup */
//std::cerr << "Handling an opening parenthesis.\n";
accumulated_precedence += 5;
std::list<Symbol *> list;
handled = false;
for (++i; i < input.length (); ++i)
{
main_loop (context, input, i, lastValidInput,
handled, accumulated_precedence, list);
if (input[i] == ')')
{
//std::cerr << "Handling a closing parenthesis.\n";
handled = true;
//++i;
accumulated_precedence -= 5;
break;
}
}
if (master_list.size () > 0 && list.size () > 0)
{
Symbol * lastSymbol = master_list.back ();
Operator * op = dynamic_cast <Operator *> (lastSymbol);
Unary_Operator * unary = dynamic_cast <Unary_Operator *>
(lastSymbol);
// is it a node with 2 children?
if (op)
{
precedence_insert (list.back (), master_list);
}
else if (unary)
// is it a unary node (like negate)
{
precedence_insert (list.back (), master_list);
}
else
{
// is it a terminal node (Number)
// error
}
}
else if (list.size () > 0)
master_list = list;
list.clear ();
}
// converts a string and context into a parse tree, and builds an
// expression tree out of the parse tree
Expression_Tree
Interpreter::interpret (Interpreter_Context &context,
const std::string &input)
{
std::list<Symbol *> list;
//list.clear ();
Symbol * lastValidInput = 0;
bool handled = false;
int accumulated_precedence = 0;
for (std::string::size_type i = 0;
i < input.length (); ++i)
{
main_loop (context, input, i, lastValidInput,
handled, accumulated_precedence, list);
// store last valid input symbol. this is useful to the '-' operator
// and will help us determine if '-' is a subtraction or a negation
//if (input[i] != ' ' && input[i] != '\n')
// lastValidInput = input[i];
}
// if the list has an element in it, then return the back of the list.
if (!list.empty ())
{
// Invoke a recursive Expression_Tree build starting with the root
// symbol. This is an example of the builder pattern. See pg 97
// in GoF book.
Expression_Tree tree = Expression_Tree (list.back ()->build ());
delete list.back ();
return tree;
}
// If we reach this, we didn't have any symbols.
return Expression_Tree ();
}
#endif // _INTERPRETER_CPP_