swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 3 | #include <QMap> |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 4 | #include <QStack> |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame^] | 5 | #include <QException> |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 6 | |
| 7 | #include "Token.h" |
| 8 | #include "AstNode.h" |
| 9 | #include "Function.h" |
| 10 | #include "VarContext.h" |
| 11 | |
| 12 | class RuntimeResult |
| 13 | { |
| 14 | public: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame^] | 15 | RuntimeResult(const RuntimeResult &other) = default; |
| 16 | RuntimeResult(QList<Token> result = {}); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 17 | RuntimeResult(QString message); |
| 18 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame^] | 19 | RuntimeResult &operator =(const RuntimeResult &other); |
| 20 | RuntimeResult operator +(const RuntimeResult &other); |
| 21 | RuntimeResult &operator +=(const RuntimeResult &other); |
| 22 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 23 | bool success() const; |
| 24 | QString message() const; |
| 25 | QList<Token> result() const; |
| 26 | |
| 27 | operator QString() const; |
| 28 | |
| 29 | private: |
| 30 | bool _success = true; |
| 31 | QString _errorMessage; |
| 32 | QList<Token> _result; |
| 33 | }; |
| 34 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame^] | 35 | class EvalQuitException : public QException |
| 36 | { |
| 37 | public: |
| 38 | EvalQuitException() = default; |
| 39 | EvalQuitException(const EvalQuitException &other) = default; |
| 40 | |
| 41 | void raise() const override; |
| 42 | EvalQuitException *clone() const override; |
| 43 | }; |
| 44 | |
| 45 | class StackOverflowException : public QException |
| 46 | { |
| 47 | public: |
| 48 | StackOverflowException(AstNode failedAt = AstNode()); |
| 49 | StackOverflowException(const StackOverflowException &other) = default; |
| 50 | |
| 51 | AstNode failedAt() const; |
| 52 | |
| 53 | void raise() const override; |
| 54 | StackOverflowException *clone() const override; |
| 55 | |
| 56 | operator QString() const; |
| 57 | |
| 58 | private: |
| 59 | AstNode _failedAt; |
| 60 | }; |
| 61 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 62 | class Evaluator { |
| 63 | public: |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 64 | Evaluator(); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 65 | |
| 66 | void addFunction(Function func); |
| 67 | void clearFunction(QString name); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame^] | 68 | RuntimeResult evaluate(AstNode node, VarContext ctx, int recursionDepth = 0); |
| 69 | RuntimeResult callFunction(QString name, QList<Token> args, int recursionDepth); |
| 70 | |
| 71 | // Throws an EvalQuitException |
| 72 | void quit(); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | QMap<QString, Function> _functions; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 76 | QMap<QString, QStack<QList<Token>>> _vars; |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame^] | 77 | bool _shouldContinue = true; |
| 78 | int _recursionLimit = 1024; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 79 | |
| 80 | protected: |
| 81 | QList<Token> dig(QString name); |
| 82 | QList<Token> copy(QString name); |
| 83 | void bury(QString name, QList<Token> expression); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 84 | }; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 85 | |
| 86 | void rtError(QString brief, QString details); |