swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 1 | #include "Function.h" |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 2 | #include "Parser.h" |
| 3 | #include <QDebug> |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 4 | |
| 5 | template <typename T> |
| 6 | QString join(QList<T> list, QString sep) |
| 7 | { |
| 8 | QStringList strings; |
| 9 | |
| 10 | for (const T &item : list) |
| 11 | { |
| 12 | strings.append(static_cast<QString>(item)); |
| 13 | } |
| 14 | |
| 15 | return strings.join(sep); |
| 16 | } |
| 17 | |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 18 | Sentence::~Sentence() |
| 19 | { |
| 20 | } |
| 21 | |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 22 | Sentence::Sentence(QList<Token> pattern, QList<AstNode> result) |
| 23 | { |
| 24 | _pattern = pattern; |
| 25 | _result = result; |
| 26 | } |
| 27 | |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 28 | Sentence::Sentence(QList<Token> pattern, SentenceResultFn result) |
| 29 | { |
| 30 | _pattern = pattern; |
| 31 | _native = result; |
| 32 | } |
| 33 | |
| 34 | bool Sentence::isExternal() const |
| 35 | { |
| 36 | return _native != nullptr; |
| 37 | } |
| 38 | |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 39 | QList<Token> Sentence::externResult(MatchResult args) const |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 40 | { |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 41 | return _native(args.context); |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 42 | } |
| 43 | |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 44 | QList<Token> Sentence::pattern() const |
| 45 | { |
| 46 | return _pattern; |
| 47 | } |
| 48 | |
| 49 | QList<AstNode> Sentence::result() const |
| 50 | { |
| 51 | return _result; |
| 52 | } |
| 53 | |
| 54 | Sentence::operator QString() const |
| 55 | { |
| 56 | return join(_pattern, " ") + " = " + join(_result, " ") + ";"; |
| 57 | } |
| 58 | |
| 59 | Function::Function(QString name) |
| 60 | : Function(name, {}) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | Function::Function(QString name, QList<Sentence> sentences) |
| 65 | { |
| 66 | _name = name; |
| 67 | _sentences = sentences; |
| 68 | } |
| 69 | |
| 70 | void Function::addSentence(Sentence sentence) |
| 71 | { |
| 72 | _sentences.append(sentence); |
| 73 | } |
| 74 | |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 75 | void Function::addNativeSentence(QString pattern, SentenceResultFn fn) |
| 76 | { |
| 77 | Parser parser(std::move(pattern)); |
| 78 | QList<Token> parsedPattern; |
| 79 | |
| 80 | ParseResult res = parser.parseMany(&parsedPattern); |
| 81 | if (!res) |
| 82 | { |
| 83 | qDebug() << "Failed to parse pattern for native sentence"; |
| 84 | qDebug() << res.message(); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | addSentence(Sentence(parsedPattern, std::move(fn))); |
| 89 | } |
| 90 | } |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 91 | |
| 92 | QString Function::name() const |
| 93 | { |
| 94 | return _name; |
| 95 | } |
| 96 | |
| 97 | QList<Sentence> Function::sentences() const |
| 98 | { |
| 99 | return _sentences; |
| 100 | } |
| 101 | |
| 102 | Function::operator QString() const |
| 103 | { |
| 104 | QString buffer = name() + " { "; |
| 105 | int leftPadding = buffer.length(); |
| 106 | |
| 107 | QString spaces; |
| 108 | for (int i = 0; i < leftPadding; i++) |
| 109 | spaces += " "; |
| 110 | |
| 111 | for (int i = 0; i < _sentences.length(); i++) |
| 112 | { |
| 113 | if (i) |
| 114 | buffer += "\n" + spaces; |
| 115 | |
| 116 | buffer += static_cast<QString>(_sentences[i]); |
| 117 | } |
| 118 | |
| 119 | buffer += " }"; |
| 120 | |
| 121 | return buffer; |
| 122 | } |