-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.h
132 lines (106 loc) · 1.73 KB
/
expression.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
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <memory>
#include "pmccontext.h"
class BinaryExpr;
class ConstExpr;
class PmcExpr;
class ExprVisitor
{
public:
virtual void visit(BinaryExpr &expr) = 0;
virtual void visit(ConstExpr &expr) = 0;
virtual void visit(PmcExpr &expr) = 0;
};
class PostOrderExprVisitor : public ExprVisitor
{
};
class Expression
{
double value;
public:
virtual void accept(PostOrderExprVisitor &v) = 0;
virtual double getValue() const
{
return (value);
}
virtual void setValue(double v)
{
value = v;
}
};
class BinaryExpr : public Expression
{
public:
enum BinaryOp { ADD, SUB, MULT, DIV };
private:
std::auto_ptr<Expression> left;
BinaryOp op;
std::auto_ptr<Expression> right;
public:
BinaryExpr(Expression * l, BinaryOp o, Expression * r)
: left(l), op(o), right(r)
{
}
virtual void accept(PostOrderExprVisitor &v)
{
left.get()->accept(v);
right.get()->accept(v);
v.visit(*this);
}
Expression & getLeft() const
{
return (*left.get());
}
Expression & getRight() const
{
return (*right.get());
}
BinaryOp getOp() const
{
return (op);
}
};
class PmcExpr : public Expression
{
std::auto_ptr<std::string> pmc;
public:
PmcExpr(std::string *p)
: pmc(p)
{
}
PmcExpr(const std::string &p)
: pmc(new std::string(p))
{
}
void accept(PostOrderExprVisitor &v)
{
v.visit(*this);
}
const std::string &getPmc() const
{
return (*pmc.get());
}
};
class ConstExpr : public Expression
{
double constant;
public:
ConstExpr(double c)
: constant(c)
{
}
void accept(PostOrderExprVisitor &v)
{
v.visit(*this);
}
double getValue() const
{
return (constant);
}
void setValue()
{
throw std::runtime_error("setValue called on a ConstExpr");
}
};
#endif