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 | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame^] | 62 | class AssertionException : public QException |
| 63 | { |
| 64 | public: |
| 65 | AssertionException(QString message = ""); |
| 66 | AssertionException(const AssertionException &other) = default; |
| 67 | |
| 68 | QString message() const; |
| 69 | |
| 70 | void raise() const override; |
| 71 | AssertionException *clone() const override; |
| 72 | |
| 73 | operator QString() const; |
| 74 | |
| 75 | private: |
| 76 | QString _message; |
| 77 | }; |
| 78 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 79 | class Evaluator { |
| 80 | public: |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 81 | Evaluator(); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 82 | |
| 83 | void addFunction(Function func); |
| 84 | void clearFunction(QString name); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 85 | RuntimeResult evaluate(AstNode node, VarContext ctx, int recursionDepth = 0); |
| 86 | RuntimeResult callFunction(QString name, QList<Token> args, int recursionDepth); |
| 87 | |
| 88 | // Throws an EvalQuitException |
| 89 | void quit(); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | QMap<QString, Function> _functions; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 93 | QMap<QString, QStack<QList<Token>>> _vars; |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 94 | bool _shouldContinue = true; |
| 95 | int _recursionLimit = 1024; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 96 | |
| 97 | protected: |
| 98 | QList<Token> dig(QString name); |
| 99 | QList<Token> copy(QString name); |
| 100 | void bury(QString name, QList<Token> expression); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 101 | }; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 102 | |
| 103 | void rtError(QString brief, QString details); |