-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.h
79 lines (69 loc) · 1.82 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
/*
* File: expression.h
* -----------
* This file export class Expression and three concrete
* subclasses: ConstantExp, IdentifierExp, CompoundExp.
*/
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include "evaluation.h"
/* Type of three Expression subclass */
enum ExpressionType { CONSTANT, IDENTIFIER, COMPOUND };
/*
* Class: Expression
* -----------------
* This class is used to represent a node in an expression tree.
* Three concrete subclasses of Expression are used to represent
* there different cases:
*
* 1. ConstantExp -- an integer constant
* 2. IdentifierExp -- a string representing an identifier
* 3. CompoundExp -- two expressions combined by an operator
*
*/
class Expression
{
public:
Expression();
virtual ~Expression();
/*
* Method: evaluation
* -----------------
* Get the value of the expression.
*/
virtual int evaluation(Evaluation *eval) = 0;
/*
* Method: getType
* -----------------
* Get the type of the expression.
*/
virtual ExpressionType getType() = 0;
};
class ConstantExp: public Expression {
private:
int value;
public:
ConstantExp(int value);
virtual int evaluation(Evaluation *eval);
virtual ExpressionType getType(){return CONSTANT;}
};
class IdentifierExp: public Expression {
private:
QString variable;
public:
IdentifierExp(QString variable);
virtual int evaluation(Evaluation *eval);
QString getIdentifierName(){return variable;}
virtual ExpressionType getType(){return IDENTIFIER;}
};
class CompoundExp: public Expression {
private:
Expression* left;
Expression* right;
QString op;
public:
CompoundExp(QString op, Expression* left, Expression* right);
virtual int evaluation(Evaluation *eval);
virtual ExpressionType getType(){return COMPOUND;}
};
#endif // EXPRESSION_H