-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.h
125 lines (110 loc) · 3.94 KB
/
script.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
#ifndef SCRIPT_H
#define SCRIPT_H
#include <QString>
#include <QVector>
#include <QSharedPointer>
#include <QMap>
#include <QObject>
class Environment;
// Base class for all expression types
class Expression {
public:
virtual ~Expression() = default;
virtual QString toString() const = 0;
virtual QSharedPointer<Expression> evaluate(QSharedPointer<Environment> env) = 0;
};
// Number expression
class Number : public Expression {
double value;
public:
Number(double val);
QString toString() const override {
return QString::number(value);
}
QSharedPointer<Expression> evaluate(QSharedPointer<Environment>) override {
return QSharedPointer<Number>::create(*this);
}
double getValue() const { return value; }
};
// Symbol expression
class Symbol : public Expression {
QString name;
public:
Symbol(const QString& n) : name(n) {}
QString toString() const override {
return name;
}
QSharedPointer<Expression> evaluate(QSharedPointer<Environment> env) override;
const QString& getName() const { return name; }
};
// List expression
class List : public Expression {
QVector<QSharedPointer<Expression>> elements;
public:
List(const QVector<QSharedPointer<Expression>>& elems) : elements(elems) {}
QString toString() const override;
QSharedPointer<Expression> evaluate(QSharedPointer<Environment> env) override;
const QVector<QSharedPointer<Expression>>& getElements() const { return elements; }
};
// Function expression
class Function : public Expression {
QVector<QString> parameters;
QSharedPointer<Expression> body;
QSharedPointer<Environment> closure;
public:
Function(const QVector<QString>& params, QSharedPointer<Expression> bod, QSharedPointer<Environment> env)
: parameters(params), body(bod), closure(env) {}
QString toString() const override {
return "<function>";
}
QSharedPointer<Expression> evaluate(QSharedPointer<Environment>) override {
return QSharedPointer<Function>::create(*this);
}
QSharedPointer<Expression> apply(const QVector<QSharedPointer<Expression>>& args);
};
// Class expression
class Class : public Expression {
QMap<QString, QSharedPointer<Expression>> methods;
public:
void addMethod(const QString& name, QSharedPointer<Expression> method);
QString toString() const override;
QSharedPointer<Expression> evaluate(QSharedPointer<Environment> env) override;
QSharedPointer<Expression> getMethod(const QString& name) const;
};
// Instance expression
class Instance : public Expression {
QSharedPointer<Class> cls;
QMap<QString, QSharedPointer<Expression>> attributes;
public:
Instance(QSharedPointer<Class> c) : cls(c) {}
QString toString() const override {
return "<instance>";
}
QSharedPointer<Expression> evaluate(QSharedPointer<Environment>) override {
return QSharedPointer<Instance>::create(*this);
}
void setAttribute(const QString& name, QSharedPointer<Expression> value) {
attributes[name] = value;
}
QSharedPointer<Expression> getAttribute(const QString& name) const;
};
// Environment to store variable bindings
class Environment {
QMap<QString, QSharedPointer<Expression>> bindings;
QSharedPointer<Environment> parent;
public:
Environment(QSharedPointer<Environment> p = nullptr) : parent(p) {}
void define(const QString& name, QSharedPointer<Expression> value);
QSharedPointer<Expression> lookup(const QString& name) const;
};
// Script manager
class Script : public QObject {
public:
explicit Script(QObject *parent = nullptr);
QVector<QString> tokenize(const QString& str);
QSharedPointer<Expression> parse(QVector<QString>::iterator& it, QVector<QString>::iterator end);
void repl();
private:
QString scriptEscapeString(const QString& str);
};
#endif // SCRIPT_H