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