-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.cpp
324 lines (295 loc) · 11.4 KB
/
script.cpp
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
#include "script.h"
#include <QDebug>
#include <QTextStream>
#include <QCoreApplication>
#include <QtMath>
Number::Number(double val) : value(val) {}
QString List::toString() const {
QString result = "(";
for (int i = 0; i < elements.size(); ++i) {
if (i > 0) result += " ";
result += elements[i]->toString();
}
return result + ")";
}
void Class::addMethod(const QString& name, QSharedPointer<Expression> method) {
methods[name] = method;
}
QString Class::toString() const {
return "<class>";
}
QSharedPointer<Expression> Class::evaluate(QSharedPointer<Environment>) {
return QSharedPointer<Class>::create(*this);
}
QSharedPointer<Expression> Class::getMethod(const QString& name) const {
auto it = methods.find(name);
if (it != methods.end()) {
return it.value();
}
qCritical() << "Method not found:" << name;
throw std::runtime_error(QString("Method not found: %1").arg(name).toStdString());
}
QSharedPointer<Expression> Instance::getAttribute(const QString& name) const {
auto it = attributes.find(name);
if (it != attributes.end()) {
return it.value();
}
return cls->getMethod(name);
}
void Environment::define(const QString& name, QSharedPointer<Expression> value) {
bindings[name] = value;
}
QSharedPointer<Expression> Environment::lookup(const QString& name) const {
auto it = bindings.find(name);
if (it != bindings.end()) {
return it.value();
}
if (parent) {
return parent->lookup(name);
}
qCritical() << "Undefined symbol:" << name;
throw std::runtime_error(QString("Undefined symbol: %1").arg(name).toStdString());
}
QSharedPointer<Expression> Symbol::evaluate(QSharedPointer<Environment> env) {
return env->lookup(name);
}
QSharedPointer<Expression> List::evaluate(QSharedPointer<Environment> env) {
if (elements.isEmpty()) {
qCritical() << "Cannot evaluate empty list";
throw std::runtime_error("Cannot evaluate empty list");
}
auto first = elements[0]->evaluate(env);
if (auto symbol = qSharedPointerDynamicCast<Symbol>(elements[0])) {
if (symbol->getName() == "define") {
if (elements.size() != 3) {
qCritical() << "Incorrect number of arguments for define";
throw std::runtime_error("Incorrect number of arguments for define");
}
auto name = qSharedPointerDynamicCast<Symbol>(elements[1]);
if (!name) {
qCritical() << "First argument to define must be a symbol";
throw std::runtime_error("First argument to define must be a symbol");
}
auto value = elements[2]->evaluate(env);
env->define(name->getName(), value);
return value;
}
else if (symbol->getName() == "lambda") {
if (elements.size() != 3) {
qCritical() << "Incorrect number of arguments for lambda";
throw std::runtime_error("Incorrect number of arguments for lambda");
}
auto params = qSharedPointerDynamicCast<List>(elements[1]);
if (!params) {
qCritical() << "Second argument to lambda must be a list of parameters";
throw std::runtime_error("Second argument to lambda must be a list of parameters");
}
QVector<QString> paramNames;
for (const auto& param : params->getElements()) {
auto paramSymbol = qSharedPointerDynamicCast<Symbol>(param);
if (!paramSymbol) {
qCritical() << "Lambda parameters must be symbols";
throw std::runtime_error("Lambda parameters must be symbols");
}
paramNames.append(paramSymbol->getName());
}
return QSharedPointer<Function>::create(paramNames, elements[2], env);
}
else if (symbol->getName() == "class") {
if (elements.size() < 2) {
qCritical() << "Incorrect number of arguments for class";
throw std::runtime_error("Incorrect number of arguments for class");
}
auto cls = QSharedPointer<Class>::create();
for (int i = 1; i < elements.size(); i += 2) {
auto methodName = qSharedPointerDynamicCast<Symbol>(elements[i]);
if (!methodName || i + 1 >= elements.size()) {
qCritical() << "Invalid class definition";
throw std::runtime_error("Invalid class definition");
}
auto methodBody = elements[i + 1]->evaluate(env);
cls->addMethod(methodName->getName(), methodBody);
}
return cls;
}
else if (symbol->getName() == "new") {
if (elements.size() != 2) {
qCritical() << "Incorrect number of arguments for new";
throw std::runtime_error("Incorrect number of arguments for new");
}
auto cls = qSharedPointerDynamicCast<Class>(elements[1]->evaluate(env));
if (!cls) {
qCritical() << "First argument to new must be a class";
throw std::runtime_error("First argument to new must be a class");
}
return QSharedPointer<Instance>::create(cls);
}
}
QVector<QSharedPointer<Expression>> evaluatedArgs;
for (int i = 1; i < elements.size(); ++i) {
evaluatedArgs.append(elements[i]->evaluate(env));
}
if (auto function = qSharedPointerDynamicCast<Function>(first)) {
return function->apply(evaluatedArgs);
}
else if (auto instance = qSharedPointerDynamicCast<Instance>(first)) {
if (evaluatedArgs.isEmpty()) {
qCritical() << "Method name must be provided when calling instance method";
throw std::runtime_error("Method name must be provided when calling instance method");
}
auto methodName = qSharedPointerDynamicCast<Symbol>(elements[1]);
if (!methodName) {
qCritical() << "Method name must be a symbol";
throw std::runtime_error("Method name must be a symbol");
}
auto method = instance->getAttribute(methodName->getName());
if (auto methodFunction = qSharedPointerDynamicCast<Function>(method)) {
QVector<QSharedPointer<Expression>> methodArgs = { instance };
methodArgs.append(evaluatedArgs.mid(1));
return methodFunction->apply(methodArgs);
}
qCritical() << "Invalid method call";
throw std::runtime_error("Invalid method call");
}
qCritical() << "Invalid function call";
throw std::runtime_error("Invalid function call");
}
QSharedPointer<Expression> Function::apply(const QVector<QSharedPointer<Expression>>& args) {
if (args.size() != parameters.size()) {
qCritical() << "Incorrect number of arguments";
throw std::runtime_error("Incorrect number of arguments");
}
auto env = QSharedPointer<Environment>::create(closure);
for (int i = 0; i < parameters.size(); ++i) {
env->define(parameters[i], args[i]);
}
return body->evaluate(env);
}
Script::Script(QObject *parent) : QObject(parent) {}
QVector<QString> Script::tokenize(const QString& str) {
QVector<QString> tokens;
QString token;
bool inString = false;
for (QChar c : str) {
if (c == '"') {
inString = !inString;
token += c;
}
else if (inString) {
token += c;
}
else if (c == '(' || c == ')') {
if (!token.isEmpty()) {
tokens.append(token);
token.clear();
}
tokens.append(QString(c));
}
else if (c.isSpace()) {
if (!token.isEmpty()) {
tokens.append(token);
token.clear();
}
}
else {
token += c;
}
}
if (!token.isEmpty()) {
tokens.append(token);
}
return tokens;
}
QSharedPointer<Expression> Script::parse(QVector<QString>::iterator& it, QVector<QString>::iterator end) {
if (it == end) {
qCritical() << "Unexpected end of input";
throw std::runtime_error("Unexpected end of input");
}
QString token = *it++;
if (token == "(") {
QVector<QSharedPointer<Expression>> elements;
while (it != end && *it != ")") {
elements.append(parse(it, end));
}
if (it == end) {
qCritical() << "Mismatched parentheses";
throw std::runtime_error("Mismatched parentheses");
}
++it; // consume the ')'
return QSharedPointer<List>::create(elements);
}
else if (token == ")") {
qCritical() << "Unexpected ')'";
throw std::runtime_error("Unexpected ')'");
}
else if (token.startsWith('"') && token.endsWith('"')) {
// Handle string literals
return QSharedPointer<Symbol>::create(token.mid(1, token.length() - 2));
}
else {
// Try to parse as number, otherwise treat as symbol
bool ok;
double value = token.toDouble(&ok);
if (ok) {
return QSharedPointer<Number>::create(value);
}
else {
return QSharedPointer<Symbol>::create(token);
}
}
}
void Script::repl() {
auto globalEnv = QSharedPointer<Environment>::create();
// Define built-in functions
globalEnv->define("+", QSharedPointer<Function>::create(
QVector<QString>{"a", "b"},
QSharedPointer<List>::create(QVector<QSharedPointer<Expression>>{
QSharedPointer<Symbol>::create("+"),
QSharedPointer<Symbol>::create("a"),
QSharedPointer<Symbol>::create("b")
}),
globalEnv
));
QTextStream qin(stdin);
QTextStream qout(stdout);
while (true) {
qout << "> " << Qt::flush;
QString input = qin.readLine();
if (input == "exit") break;
try {
auto tokens = tokenize(input);
auto it = tokens.begin();
auto expr = parse(it, tokens.end());
auto result = expr->evaluate(globalEnv);
qout << result->toString() << Qt::endl;
}
catch (const std::exception& e) {
qCritical() << "Error:" << e.what();
}
}
}
QString Script::scriptEscapeString(const QString& str) {
QString result;
for (QChar c : str) {
switch (c.unicode()) {
case '"': result += "\\\""; break;
case '\\': result += "\\\\"; break;
case '\n': result += "\\n"; break;
case '\r': result += "\\r"; break;
case '\t': result += "\\t"; break;
default: result += c;
}
}
return result;
}
/*
// Example usage
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
qInfo() << "Enhanced Scheme Interpreter";
qInfo() << "Type 'exit' to quit";
Script script;
script.repl();
return 0;
}
// */