-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.h
More file actions
249 lines (199 loc) · 6.21 KB
/
lang.h
File metadata and controls
249 lines (199 loc) · 6.21 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#pragma once
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// +------------------------------------+
// | Values |
// +------------------------------------+
class Value {
public:
virtual ~Value() {}
};
using ValuePtr = std::shared_ptr<Value>;
class IntValue : public Value {
public:
int value;
IntValue(int value) : value(value) {}
};
class ArrayValue : public Value {
public:
int length;
int *contents;
ArrayValue(int length);
~ArrayValue() override;
};
// +------------------------------------+
// | Program Structures |
// +------------------------------------+
class Program;
struct Context;
class BaseObject {
public:
virtual ~BaseObject() {}
virtual std::string toString() const = 0;
template <typename T>
bool is() const {
return dynamic_cast<const T *>(this) != nullptr;
}
template <typename T>
T *as() {
return dynamic_cast<T *>(this);
}
};
// +------------------------------------+
// | Expressions |
// +------------------------------------+
class Expression : public BaseObject {
public:
virtual ValuePtr eval(Context &ctx) const = 0;
};
class IntegerLiteral : public Expression {
public:
int value;
IntegerLiteral(int value) : value(value) {}
std::string toString() const override;
ValuePtr eval(Context &ctx) const override;
};
class Variable : public Expression {
public:
std::string name;
Variable(std::string name) : name(std::move(name)) {}
std::string toString() const override;
ValuePtr eval(Context &ctx) const override;
};
class CallExpression : public Expression {
public:
std::string func;
std::vector<Expression *> args;
CallExpression(std::string func, std::vector<Expression *> args)
: func(std::move(func)), args(std::move(args)) {}
std::string toString() const override;
ValuePtr eval(Context &ctx) const override;
};
// +------------------------------------+
// | Statements |
// +------------------------------------+
class Statement : public BaseObject {
public:
virtual void eval(Context &ctx) const = 0;
};
class ExpressionStatement : public Statement {
public:
Expression *expr;
ExpressionStatement(Expression *expr) : expr(expr) {}
std::string toString() const override;
void eval(Context &ctx) const override;
};
class SetStatement : public Statement {
public:
Variable *name;
Expression *value;
SetStatement(Variable *name, Expression *value) : name(name), value(value) {}
std::string toString() const override;
void eval(Context &ctx) const override;
};
class IfStatement : public Statement {
public:
Expression *condition;
Statement *body;
IfStatement(Expression *condition, Statement *body)
: condition(condition), body(body) {}
std::string toString() const override;
void eval(Context &ctx) const override;
};
class ForStatement : public Statement {
public:
Statement *init;
Expression *test;
Statement *update;
Statement *body;
ForStatement(Statement *init, Expression *test, Statement *update,
Statement *body)
: init(init), test(test), update(update), body(body) {}
std::string toString() const override;
void eval(Context &ctx) const override;
};
class BlockStatement : public Statement {
public:
std::vector<Statement *> body;
BlockStatement(std::vector<Statement *> body) : body(std::move(body)) {}
std::string toString() const override;
void eval(Context &ctx) const override;
};
class ReturnStatement : public Statement {
public:
Expression *value;
ReturnStatement(Expression *value) : value(value) {}
std::string toString() const override;
void eval(Context &ctx) const override;
};
// +------------------------------------+
// | Global Constructs |
// +------------------------------------+
class FunctionDeclaration : public BaseObject {
public:
std::string name;
std::vector<Variable *> params;
Statement *body;
FunctionDeclaration(std::string name, std::vector<Variable *> params,
Statement *body)
: name(std::move(name)), params(std::move(params)), body(body) {}
std::string toString() const override;
};
class Program : public BaseObject {
public:
std::vector<FunctionDeclaration *> body;
std::unordered_map<std::string, FunctionDeclaration *> index;
Program(std::vector<FunctionDeclaration *> body);
std::string toString() const override;
int eval(int timeLimit, std::istream &is = std::cin, std::ostream &os = std::cout);
};
// +------------------------------------+
// | Exceptions |
// +------------------------------------+
class EvalError : public std::exception {
public:
const BaseObject *location;
std::string reason;
EvalError(const BaseObject *location, const std::string &reason_)
: location(location) {
if (location == nullptr) {
reason = reason_;
return;
}
reason = "At " + location->toString() + ":\n" + reason_;
}
const char *what() const noexcept override { return reason.c_str(); }
};
class SyntaxError : public EvalError {
public:
SyntaxError(const BaseObject *location, const std::string &reason)
: EvalError(location, "Syntax error: " + reason) {}
};
class RuntimeError : public EvalError {
public:
RuntimeError(const BaseObject *location, const std::string &reason)
: EvalError(location, "Runtime error: " + reason) {}
};
// +------------------------------------+
// | Parser |
// +------------------------------------+
BaseObject *scan(std::istream &is);
Program *scanProgram(std::istream &is);
// +------------------------------------+
// | Variables and Helper Functions |
// +------------------------------------+
extern const int kIdMaxLength;
extern const std::unordered_set<std::string> keywords;
extern const std::unordered_set<std::string> builtinFunctions;
bool isTruthy(const BaseObject *ctx, ValuePtr value);
std::string indent(const std::string &s);
bool isValidIdentifier(const std::string &name);
void removeWhitespaces(std::istream &is);
void expectClosingParens(std::istream &is);
std::string scanToken(std::istream &is);
std::string scanIdentifier(std::istream &is);