-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.h
146 lines (122 loc) · 3.5 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include <string>
#include <iostream>
enum class ExpressionType {
CONST_EXP,
VAR_EXP,
IF_EXP,
BIN_EXP,
MON_EXP,
LET_EXP,
};
class Expression {
protected:
ExpressionType type;
public:
Expression(ExpressionType t) : type(t) {}
virtual ~Expression() {}
ExpressionType get_signature() const {
return type;
}
};
union Value {
int int_val;
bool bool_val;
std::string* string_val;
~Value() {}
};
enum class ConstType {
IntConst,
BoolConst,
StringConst
};
class ConstExp : public Expression {
private:
ConstType const_type;
ExpressionType exp_type;
public:
Value value;
ConstExp(int c) : Expression(ExpressionType::CONST_EXP), const_type(ConstType::IntConst) {
value.int_val = c;
}
ConstExp(bool c) : Expression(ExpressionType::CONST_EXP), const_type(ConstType::BoolConst) {
value.bool_val = c;
}
ConstExp(const std::string& c) : Expression(ExpressionType::CONST_EXP), const_type(ConstType::StringConst) {
value.string_val = new std::string(c);
}
ConstType get_type() const {
return const_type;
}
Value get_val() {
return value;
}
};
class VarExp : public Expression {
private:
std::string var_name;
public:
VarExp(std::string v) : Expression(ExpressionType::VAR_EXP), var_name(v) {}
std::string get_var_name() { return var_name; }
};
enum class MonadicOperator {
NotOp,
IntNegOp,
PrintOp
};
class MonadicExpression : public Expression {
private:
Expression* exp;
MonadicOperator mon_op;
public:
MonadicExpression(MonadicOperator op, Expression * e) : Expression(ExpressionType::MON_EXP), exp(e), mon_op(op) {}
MonadicOperator get_type() { return mon_op; }
Expression * get_right() { return exp; }
};
enum class BinaryOperator {
IntPlusOp,
IntMinusOp,
IntDivOp,
IntTimesOp,
ModOp,
EqualityOp,
GtOp,
GteOp,
LtOp,
LteOp,
EqualsOp,
NotEqualsOp,
AndOp,
OrOp
};
class BinaryExpression : public Expression {
private:
Expression * e1;
Expression * e2;
BinaryOperator bin_op;
public:
BinaryExpression(BinaryOperator op, Expression * exp1, Expression * exp2) : Expression(ExpressionType::BIN_EXP), bin_op(op), e1(exp1), e2(exp2) {}
BinaryOperator get_type() { return bin_op; }
Expression * get_left() { return e1; }
Expression * get_right() { return e2; }
};
class AssignmentExpression : public Expression {
private:
std::string ident;
Expression * exp;
public:
AssignmentExpression(std::string identifier, Expression * e) : Expression(ExpressionType::LET_EXP), ident(identifier), exp(e) {}
std::string get_id() { return ident; }
Expression * get_right() { return exp; }
};
class IfExpression : public Expression {
private:
Expression * conditional;
Expression * if_exp;
Expression * else_exp;
public:
IfExpression(Expression * e1, Expression * e2, Expression * e3): Expression(ExpressionType::IF_EXP), conditional(e1), if_exp(e2), else_exp(e3) {}
Expression * get_conditional() {return conditional; }
Expression * get_if_exp() {return if_exp; }
Expression * get_else_exp() {return else_exp; }
};