swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 1 | #include <QCoreApplication> |
| 2 | |
| 3 | #include "NbRuntime.h" |
| 4 | #include "../Parser.h" |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 5 | #include "../StdLib.h" |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 6 | |
| 7 | NbRuntime::NbRuntime(QObject *parent) |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 8 | : QObject(parent) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 9 | { |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 10 | StdLib().load(_eval); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 11 | } |
| 12 | |
swissChili | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame] | 13 | void NbRuntime::reset() |
| 14 | { |
| 15 | _eval.reset(); |
| 16 | _ctx = {}; |
| 17 | } |
| 18 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 19 | void NbRuntime::queueCell(Cell *cell) |
| 20 | { |
| 21 | if (!_cells.contains(cell)) |
| 22 | { |
| 23 | qInfo() << "Queueing cell"; |
| 24 | |
| 25 | _cells.append(cell); |
| 26 | |
| 27 | emit cellWaiting(cell); |
| 28 | |
| 29 | if (!_running) |
| 30 | evalRemaining(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void NbRuntime::unqueueCell(Cell *cell) |
| 35 | { |
| 36 | if (cell == _running) |
| 37 | { |
| 38 | // Exception should propagate back up to evalRemaining() |
| 39 | _eval.quit(); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | _cells.removeOne(cell); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void NbRuntime::evalRemaining() |
| 48 | { |
| 49 | qInfo() << "evalRemaining"; |
| 50 | |
| 51 | while (!_cells.empty()) |
| 52 | { |
| 53 | QCoreApplication::processEvents(); |
| 54 | |
| 55 | Cell *cell = _cells.first(); |
| 56 | _cells.removeFirst(); |
| 57 | |
| 58 | _running = cell; |
| 59 | |
| 60 | Parser parser(cell->code()); |
| 61 | RuntimeResult result; |
| 62 | |
| 63 | emit cellRunning(cell); |
| 64 | |
| 65 | try |
| 66 | { |
| 67 | // Allow this cell to be quit |
| 68 | QCoreApplication::processEvents(); |
| 69 | |
| 70 | while (true) |
| 71 | { |
| 72 | ParseResult ret; |
| 73 | Function func; |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 74 | QList<AstNode> ast; |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 75 | |
| 76 | if ((ret = parser.parseFunctionDefinition(&func))) |
| 77 | { |
| 78 | _eval.addFunction(func); |
| 79 | } |
| 80 | else if (ret.status() == ParseResult::INCOMPLETE) |
| 81 | { |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 82 | emit cellFailedToParse(cell, ret, parser); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 83 | goto endOfCell; // JANK! |
| 84 | } |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 85 | else if ((ret = parser.parseMany(&ast)) && !ast.empty()) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 86 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 87 | for (const AstNode &node : ast) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 88 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 89 | RuntimeResult nodeRes = _eval.evaluate(node, _ctx); |
| 90 | result += nodeRes; |
| 91 | |
| 92 | if (!nodeRes.success()) |
| 93 | { |
| 94 | goto endOfParseLoop; |
| 95 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | else if (ret.status() == ParseResult::INCOMPLETE) |
| 99 | { |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 100 | emit cellFailedToParse(cell, ret, parser); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | parser.skip(); |
| 106 | |
| 107 | if (!parser.atEnd()) |
| 108 | { |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 109 | emit cellFailedToParse(cell, ParseResult(ParseResult::NO_MATCH, "Garbage at end of input", parser.save()), parser); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 110 | goto endOfCell; |
| 111 | } |
| 112 | |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 117 | endOfParseLoop: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 118 | emit cellFinishedRunning(cell, result); |
| 119 | |
| 120 | endOfCell: |
| 121 | _running = nullptr; |
| 122 | } |
| 123 | catch (EvalQuitException &ex) |
| 124 | { |
| 125 | _running = nullptr; |
| 126 | emit cellQuit(cell); |
| 127 | } |
| 128 | catch (StackOverflowException &ex) |
| 129 | { |
| 130 | _running = nullptr; |
| 131 | emit cellFinishedRunning(cell, RuntimeResult(ex)); |
| 132 | } |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 133 | catch (AssertionException &ex) |
| 134 | { |
| 135 | _running = nullptr; |
| 136 | emit cellFinishedRunning(cell, RuntimeResult(ex)); |
| 137 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 138 | } |
| 139 | } |