-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpr.h
More file actions
217 lines (134 loc) · 4.24 KB
/
Expr.h
File metadata and controls
217 lines (134 loc) · 4.24 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
//
// Created by aniru on 1/19/2022.
//
#ifndef ASSIGNMENT02_EXPR_H
#define ASSIGNMENT02_EXPR_H
#include <string>
#include "pointer.h"
#include <iostream>
#include "Env.h"
class Val;
CLASS(Expr) {
public:
/// to_string - This function will return a string of the expression, in a reduced form or full form.
/// \param isPretty true will print in reduced form and false will print in full form.
/// \return string form of the expression.
std::string to_string(bool isPretty);
virtual void print(std::ostream &out) = 0;
virtual void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence) = 0;
virtual bool equals(PTR(Expr)(e)) = 0;
// Troubleshooting tip: If you interp a free variable exception, then remove default value
// and check the if the implementation has any errors, the key is to pass in 'env' in all
// recursive calls.
virtual PTR(Val)interp(PTR(Env)env = Env::empty) = 0;
};
class NumExpr :
public Expr {
int val;
public:
NumExpr(int val);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
class BoolExpr :
public Expr {
bool boolean;
public:
BoolExpr(bool boolean);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
class AddExpr :
public Expr {
public:
PTR(Expr)lhs;
PTR(Expr)rhs;
AddExpr(PTR(Expr)lhs, PTR(Expr)rhs);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
class MultExpr :
public Expr {
public:
PTR(Expr)lhs;
PTR(Expr)rhs;
MultExpr(PTR(Expr)lhs, PTR(Expr)rhs);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
class VarExpr :
public Expr {
public:
std::string val;
VarExpr(std::string val);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env = Env::empty);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
class LetExpr :
public Expr {
public:
PTR(VarExpr)lhs;
PTR(Expr)rhs;
PTR(Expr)in;
LetExpr(PTR(VarExpr)lhs, PTR(Expr)rhs, PTR(Expr)in);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
};
class EqualExpr :
public Expr {
public:
PTR(Expr)lhs;
PTR(Expr)rhs;
EqualExpr(PTR(Expr)lhs, PTR(Expr)rhs);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
class IfExpr :
public Expr {
public:
PTR(Expr)ifExpr;
PTR(Expr)thenExpr;
PTR(Expr)elseExpr;
IfExpr(PTR(Expr)ifExpr, PTR(Expr)thenExpr, PTR(Expr)elseExpr);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
};
class FunExpr :
public Expr {
std::string formal_arg;
PTR(Expr)body;
public:
FunExpr(std::string formal_arg, PTR(Expr)body);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
class CallExpr :
public Expr {
PTR(Expr)to_be_called;
PTR(Expr)actual_arg;
public:
CallExpr(PTR(Expr)to_be_called, PTR(Expr)actual_arg);
bool equals(PTR(Expr)(e));
PTR(Val)interp(PTR(Env)env);
void print(std::ostream &out);
void pretty_print(std::ostream &out, int precedence, int &n_position, bool letPrecedence);
};
#endif //ASSIGNMENT02_EXPR_H