-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.cpp
More file actions
562 lines (528 loc) · 16 KB
/
lang.cpp
File metadata and controls
562 lines (528 loc) · 16 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include "lang.h"
#include <cctype>
#include <iostream>
#include <stack>
#include <type_traits>
#include <unordered_set>
const int kIdMaxLength = 255;
const std::unordered_set<std::string> keywords = {
"set", "if", "for", "block", "return", "function",
};
const std::unordered_set<std::string> builtinFunctions = {
"+", "-", "*", "/", "%", "<", ">", "<=", ">=", "==", "!=", "||", "&&", "!",
"scan", "print", "array.create", "array.get", "array.set", "array.scan", "array.print",
};
bool isTruthy(const BaseObject *ctx, ValuePtr value) {
auto iv = std::dynamic_pointer_cast<IntValue>(value);
if (iv == nullptr) {
throw RuntimeError(ctx, "Type error: if condition should be an int");
}
return iv->value != 0;
}
ArrayValue::ArrayValue(int length) : length(length) {
if (length > 1000000) throw RuntimeError(nullptr, "Out of memory");
contents = new int[length];
for (int i = 0; i < length; ++i) contents[i] = 0;
}
ArrayValue::~ArrayValue() { delete[] contents; }
struct VariableSet {
std::unordered_map<std::string, ValuePtr> values;
ValuePtr getOrThrow(const BaseObject *ctx, const std::string &name) {
if (values.count(name) == 0) {
throw RuntimeError(ctx, "Use of undefined variable: " + name);
}
if (values[name] == nullptr) {
throw RuntimeError(ctx, "Use of uninitialized variable: " + name);
}
return values[name];
}
};
struct Context {
std::istream &is;
std::ostream &os;
std::stack<VariableSet> callStack;
Program *program;
int timeLeft;
VariableSet ¤tFrame() { return callStack.top(); }
ValuePtr getOrThrow(const BaseObject *ctx, const std::string &name) {
return currentFrame().getOrThrow(ctx, name);
}
void set(const BaseObject *ctx, const std::string &name, ValuePtr value) {
if (program->index.count(name) > 0 || builtinFunctions.count(name) > 0) {
throw RuntimeError(ctx, "Assigning to function " + name);
}
currentFrame().values[name] = value;
}
void tick() {
--timeLeft;
if (timeLeft < 0) {
throw RuntimeError(nullptr, "Time limit exceeded");
}
}
};
std::string IntegerLiteral::toString() const { return std::to_string(value); }
ValuePtr IntegerLiteral::eval(Context &ctx) const {
ctx.tick();
return std::make_shared<IntValue>(value);
}
std::string Variable::toString() const { return name; }
ValuePtr Variable::eval(Context &ctx) const {
ctx.tick();
return ctx.getOrThrow(this, name);
}
std::string ExpressionStatement::toString() const { return expr->toString(); }
void ExpressionStatement::eval(Context &ctx) const { expr->eval(ctx); }
struct ReturnFromCall {
ValuePtr value;
};
std::string CallExpression::toString() const {
std::string str = std::string("(") + func;
for (const auto &arg : args) {
str += " ";
str += arg->toString();
}
str += ")";
return str;
}
ValuePtr CallExpression::eval(Context &ctx) const {
ctx.tick();
std::vector<ValuePtr> argValues;
for (const auto &arg : args) {
argValues.push_back(arg->eval(ctx));
}
auto requireArity = [&](int arity) {
if (args.size() != arity) {
throw RuntimeError(this, "Function arity mismatch at " + func);
}
};
auto readInt = [&](int ix) {
auto iv = std::dynamic_pointer_cast<IntValue>(argValues[ix]);
if (!iv) throw RuntimeError(this, "Type error: int expected");
return iv->value;
};
if (func == "+") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x + y);
} else if (func == "-") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x - y);
} else if (func == "*") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x * y);
} else if (func == "/") {
requireArity(2);
int x = readInt(0), y = readInt(1);
if (y == 0) {
throw RuntimeError(this, "Divide by zero");
}
return std::make_shared<IntValue>(x / y);
} else if (func == "%") {
requireArity(2);
int x = readInt(0), y = readInt(1);
if (y == 0) {
throw RuntimeError(this, "Mod by zero");
}
return std::make_shared<IntValue>(x % y);
} else if (func == "<") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x < y);
} else if (func == ">") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x > y);
} else if (func == "<=") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x <= y);
} else if (func == ">=") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x >= y);
} else if (func == "==") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x == y);
} else if (func == "!=") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x != y);
} else if (func == "||") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x || y);
} else if (func == "&&") {
requireArity(2);
int x = readInt(0), y = readInt(1);
return std::make_shared<IntValue>(x && y);
} else if (func == "!") {
requireArity(1);
int x = readInt(0);
return std::make_shared<IntValue>(!x);
} else if (func == "scan") {
requireArity(0);
int x;
ctx.is >> x;
return std::make_shared<IntValue>(x);
} else if (func == "print") {
requireArity(1);
ctx.os << readInt(0) << '\n';
return std::make_shared<IntValue>(0);
} else if (func == "array.create") {
requireArity(1);
return std::make_shared<ArrayValue>(readInt(0));
} else if (func == "array.scan") {
requireArity(1);
int length = readInt(0);
auto array = std::make_shared<ArrayValue>(length);
for (int i = 0; i < length; ++i) {
ctx.is >> array->contents[i];
}
return array;
} else if (func == "array.print") {
requireArity(1);
auto array = std::dynamic_pointer_cast<ArrayValue>(argValues[0]);
if (!array)
throw RuntimeError(this, "Type error at array.get: array expected");
for (int i = 0; i < array->length; ++i) {
ctx.os << array->contents[i] << std::endl;
}
return std::make_shared<IntValue>(0);
} else if (func == "array.get") {
requireArity(2);
auto array = std::dynamic_pointer_cast<ArrayValue>(argValues[0]);
int index = readInt(1);
if (!array)
throw RuntimeError(this, "Type error at array.get: array expected");
if (index >= array->length || index < 0) {
throw RuntimeError(this, "Index out of bounds at array.get");
}
return std::make_shared<IntValue>(array->contents[index]);
} else if (func == "array.set") {
requireArity(3);
auto array = std::dynamic_pointer_cast<ArrayValue>(argValues[0]);
int index = readInt(1);
int value = readInt(2);
if (!array)
throw RuntimeError(this, "Type error at array.set: array expected");
if (index >= array->length || index < 0) {
throw RuntimeError(this, "Index out of bounds at array.set");
}
array->contents[index] = value;
return std::make_shared<IntValue>(0);
}
auto *funcObject = ctx.program->index[func];
if (!funcObject) throw RuntimeError(this, "No such function: " + func);
requireArity(funcObject->params.size());
ctx.callStack.push({});
for (int i = 0; i < args.size(); ++i) {
const auto &name = funcObject->params[i];
if (ctx.program->index.count(name->name) > 0) {
throw RuntimeError(
this, "Function parameter name is global identifier: " + name->name);
}
ctx.set(this, name->name, argValues[i]);
}
try {
funcObject->body->eval(ctx);
} catch (ReturnFromCall r) {
ctx.callStack.pop();
return r.value;
}
ctx.callStack.pop();
return std::make_shared<IntValue>(0);
}
std::string indent(const std::string &s) {
std::string res = " ";
for (char ch : s) {
res += ch;
if (ch == '\n') {
res += " ";
}
}
return res;
}
std::string SetStatement::toString() const {
return std::string("(set ") + name->toString() + " " + value->toString() + ")";
}
void SetStatement::eval(Context &ctx) const {
ctx.tick();
ctx.set(this, name->name, value->eval(ctx));
}
std::string IfStatement::toString() const {
return std::string("(if ") + condition->toString() + "\n" +
indent(body->toString()) + ")";
}
void IfStatement::eval(Context &ctx) const {
ctx.tick();
bool ok = isTruthy(this, condition->eval(ctx));
if (ok) {
body->eval(ctx);
}
}
std::string ForStatement::toString() const {
return std::string("(for\n") + indent(init->toString()) + "\n" +
indent(test->toString()) + "\n" + indent(update->toString()) + "\n" +
indent(body->toString()) + ")";
}
void ForStatement::eval(Context &ctx) const {
ctx.tick();
for (init->eval(ctx); isTruthy(this, test->eval(ctx)); update->eval(ctx)) {
body->eval(ctx);
}
}
std::string BlockStatement::toString() const {
std::string str = "(block";
for (const auto &stmt : body) {
str += "\n";
str += indent(stmt->toString());
}
str += ")";
return str;
}
void BlockStatement::eval(Context &ctx) const {
for (auto stmt : body) {
stmt->eval(ctx);
}
}
std::string ReturnStatement::toString() const {
return std::string("(return ") + value->toString() + ")";
}
void ReturnStatement::eval(Context &ctx) const {
throw ReturnFromCall{value->eval(ctx)};
}
std::string FunctionDeclaration::toString() const {
std::string str = "(function (";
str += name;
for (const auto ¶m : params) {
str += " ";
str += param->toString();
}
str += ")\n";
str += indent(body->toString());
str += ")";
return str;
}
Program::Program(std::vector<FunctionDeclaration *> body)
: body(std::move(body)) {
for (auto el : this->body) {
const auto &name = el->name;
if (builtinFunctions.count(name) > 0) {
throw SyntaxError(nullptr, "Redefining built-in function: " + name);
}
if (index.count(name) > 0) {
throw SyntaxError(nullptr, "Duplicate function declaration: " + name);
}
index[name] = el;
}
}
std::string Program::toString() const {
std::string str;
for (auto el : body) {
str += el->toString();
str += "\n\n";
}
return str;
}
int Program::eval(int timeLimit, std::istream &is, std::ostream &os) {
Context ctx{
.is = is,
.os = os,
.callStack = {},
.program = this,
.timeLeft = timeLimit,
};
CallExpression("main", {}).eval(ctx);
return timeLimit - ctx.timeLeft;
}
bool isValidIdentifier(const std::string &name) {
if (name.length() > kIdMaxLength) return false;
if (name.empty()) return false;
if (isdigit(name[0])) return false;
if (name[0] == '-') {
if (name.length() == 1) return true;
bool isNumber = true;
for (int i = 1; i < name.length(); ++i) {
if (!isdigit(name[i])) {
isNumber = false;
break;
}
}
if (isNumber) return false;
}
for (char ch : name) {
if (ch == ')' || ch == '(' || ch == ';') return false;
if (!isgraph(ch)) return false;
}
if (keywords.count(name) > 0) return false;
return true;
}
void removeWhitespaces(std::istream &is) {
while (is && isspace(is.peek())) is.get();
if (is.peek() == ';') {
int ch;
do {
ch = is.get();
} while (ch != EOF && ch != '\n');
removeWhitespaces(is);
}
}
void expectClosingParens(std::istream &is) {
removeWhitespaces(is);
int ch = is.get();
if (ch != ')') {
throw SyntaxError(
nullptr, std::string("Closing parenthesis expected, got ") + char(ch));
}
}
std::string scanToken(std::istream &is) {
removeWhitespaces(is);
std::string token;
for (int next = is.peek(); !isspace(next) && next != ')' && next != ';';
next = is.peek()) {
token += is.get();
}
return token;
}
std::string scanIdentifier(std::istream &is) {
auto name = scanToken(is);
if (!isValidIdentifier(name))
throw SyntaxError(nullptr, "Invalid identifier: " + name);
return name;
}
template <typename T>
static T *scanT(std::istream &is) {
auto *construct = scan(is);
if (construct == nullptr) throw SyntaxError(nullptr, "Unexpected EOF");
if (!construct->is<T>()) {
if constexpr (std::is_same_v<T, Statement>) {
if (construct->is<Expression>()) {
return new ExpressionStatement(construct->as<Expression>());
}
}
throw SyntaxError(nullptr, std::string("Wrong construct type; ") +
typeid(*construct).name() + " found, " +
typeid(T).name() + " expected");
}
return construct->as<T>();
}
BaseObject *scan(std::istream &is) {
// ignore whitespaces
removeWhitespaces(is);
if (!is || is.peek() == EOF) return nullptr;
if (is.peek() != '(') {
// variable or literal
auto name = scanToken(is);
if (name.empty()) return nullptr;
if (name[0] == '-') {
bool isLiteral = true;
if (name.length() == 1) {
isLiteral = false;
} else {
for (char ch : name.substr(1)) {
if (!isdigit(ch)) {
isLiteral = false;
break;
}
}
}
if (isLiteral) {
int value = std::stoi(name);
return new IntegerLiteral(value);
}
}
if (isdigit(name[0])) {
for (char ch : name) {
if (!isdigit(ch)) {
throw SyntaxError(nullptr, "Invalid literal: " + name);
}
}
int value = std::stoi(name);
return new IntegerLiteral(value);
}
if (isValidIdentifier(name)) {
return new Variable(name);
}
throw SyntaxError(nullptr, "Invalid identifier: " + name);
}
is.get();
auto type = scanToken(is);
if (type == "set") {
auto name = scanIdentifier(is);
auto *value = scanT<Expression>(is);
expectClosingParens(is);
return new SetStatement(new Variable(name), value->as<Expression>());
} else if (type == "if") {
auto *cond = scanT<Expression>(is);
auto *body = scanT<Statement>(is);
expectClosingParens(is);
return new IfStatement(cond, body);
} else if (type == "for") {
auto *init = scanT<Statement>(is);
auto *test = scanT<Expression>(is);
auto *update = scanT<Statement>(is);
auto *body = scanT<Statement>(is);
expectClosingParens(is);
return new ForStatement(init, test, update, body);
} else if (type == "block") {
std::vector<Statement *> body;
removeWhitespaces(is);
while (is.peek() != ')') {
body.push_back(scanT<Statement>(is));
removeWhitespaces(is);
}
expectClosingParens(is);
return new BlockStatement(body);
} else if (type == "return") {
auto *value = scanT<Expression>(is);
expectClosingParens(is);
return new ReturnStatement(value);
} else if (type == "function") {
removeWhitespaces(is);
if (is.get() != '(') {
throw SyntaxError(nullptr, "Opening parenthesis expected");
}
auto name = scanIdentifier(is);
std::vector<Variable *> params;
removeWhitespaces(is);
while (is.peek() != ')') {
params.push_back(new Variable(scanIdentifier(is)));
removeWhitespaces(is);
}
expectClosingParens(is);
auto *body = scanT<Statement>(is);
expectClosingParens(is);
return new FunctionDeclaration(name, params, body);
} else {
// call expression
auto &name = type;
if (!isValidIdentifier(name))
throw SyntaxError(nullptr, "Invalid identifier: " + name);
std::vector<Expression *> args;
removeWhitespaces(is);
while (is.peek() != ')') {
args.push_back(scanT<Expression>(is));
removeWhitespaces(is);
}
expectClosingParens(is);
return new CallExpression(name, args);
}
}
Program *scanProgram(std::istream &is) {
std::vector<FunctionDeclaration *> body;
while (true) {
auto *el = scan(is);
if (el == nullptr) break;
if (!el->is<FunctionDeclaration>()) {
if (el->is<Variable>() && el->as<Variable>()->name == "endprogram") {
break;
}
throw SyntaxError(nullptr, "Invalid program element");
}
body.push_back(el->as<FunctionDeclaration>());
}
return new Program(body);
}