-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.h
120 lines (76 loc) · 2.13 KB
/
exp.h
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
#ifndef EXP_H
#define EXP_H
#include <string>
#include <bits/stdc++.h>
#include "tokenizer.h"
#include "context.h"
/* Forward reference */
class SymbolTable;
/*
* Type: ExpressionType
* --------------------
* This enumerated type is used to differentiate the three different
* expression types: CONSTANT, IDENTIFIER, and COMPOUND.
*/
enum ExpressionType { CONSTANT, IDENTIFIER, COMPOUND };
/*
* Class: Expression
* -----------------
* This class is used to represent a node in an expression tree.
* Expression itself is an abstract class. Every Expression object
* is therefore created using one of the three concrete subclasses:
* ConstantExp, IdentifierExp, or CompoundExp.
*/
class Expression {
public:
Expression();
virtual ~Expression() = default;
virtual int eval(const SymbolTable & context) = 0;
virtual std::string toString() = 0;
virtual ExpressionType type() = 0;
};
class ConstantExp: public Expression {
public:
explicit ConstantExp(int val);
int eval(const SymbolTable & context) override;
std::string toString() override;
ExpressionType type() override;
int getConstantValue() const;
private:
int value;
};
/*
* Class: IdentifierExp
* --------------------
* This subclass represents a expression corresponding to a variable.
*/
class IdentifierExp: public Expression {
public:
explicit IdentifierExp(std::string name);
int eval(const SymbolTable & context) override;
std::string toString() override;
ExpressionType type() override;
std::string getIdentifierName();
private:
std::string name;
};
/*
* Class: CompoundExp
* ------------------
* This subclass represents a compound expression.
*/
class CompoundExp: public Expression {
public:
CompoundExp(std::string op, Expression *lhs, Expression *rhs);
~CompoundExp() override;
int eval(const SymbolTable & context) override;
std::string toString() override;
ExpressionType type() override;
std::string getOperator();
Expression *getLHS();
Expression *getRHS();
private:
std::string op;
std::unique_ptr<Expression> lhs, rhs;
};
#endif // EXP_H