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 | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 5 | |
| 6 | #include "Token.h" |
| 7 | #include "AstNode.h" |
| 8 | #include "Function.h" |
| 9 | #include "VarContext.h" |
| 10 | |
| 11 | class RuntimeResult |
| 12 | { |
| 13 | public: |
| 14 | RuntimeResult(QList<Token> result); |
| 15 | RuntimeResult(QString message); |
| 16 | |
| 17 | bool success() const; |
| 18 | QString message() const; |
| 19 | QList<Token> result() const; |
| 20 | |
| 21 | operator QString() const; |
| 22 | |
| 23 | private: |
| 24 | bool _success = true; |
| 25 | QString _errorMessage; |
| 26 | QList<Token> _result; |
| 27 | }; |
| 28 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 29 | class Evaluator { |
| 30 | public: |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 31 | Evaluator(); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 32 | |
| 33 | void addFunction(Function func); |
| 34 | void clearFunction(QString name); |
| 35 | RuntimeResult evaluate(AstNode node, VarContext ctx); |
| 36 | RuntimeResult callFunction(QString name, QList<Token> args); |
| 37 | |
| 38 | private: |
| 39 | QMap<QString, Function> _functions; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 40 | QMap<QString, QStack<QList<Token>>> _vars; |
| 41 | |
| 42 | protected: |
| 43 | QList<Token> dig(QString name); |
| 44 | QList<Token> copy(QString name); |
| 45 | void bury(QString name, QList<Token> expression); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 46 | }; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 47 | |
| 48 | void rtError(QString brief, QString details); |