swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 1 | #include "StdLib.h" |
| 2 | #include "PPrint.h" |
| 3 | |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 4 | #include <QEventLoop> |
| 5 | #include <QTimer> |
| 6 | |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 7 | StdLib::StdLib() |
| 8 | { |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 9 | _print.addNativeSentence("e.Expr", [](VarContext args) |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 10 | { |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 11 | auto expr = args.expressionVar("Expr"); |
| 12 | sout(pprint(args.expressionVar("Expr"))); |
| 13 | return expr; |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 14 | }); |
| 15 | |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 16 | _prout.addNativeSentence("e.Expr", [](VarContext args) |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 17 | { |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 18 | auto expr = args.expressionVar("Expr"); |
| 19 | sout(pprint(expr)); |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 20 | return QList<Token>(); |
| 21 | }); |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 22 | |
| 23 | _sleep.addNativeSentence("s.Time", [](VarContext args) |
| 24 | { |
| 25 | Token time = args.singleVar("Time"); |
| 26 | |
| 27 | if (time.type() != Token::INTEGER) |
| 28 | rtError("Invalid argument", "First argument to <Time> must be an integer, got " + pprint(time)); |
| 29 | |
| 30 | QEventLoop loop; |
| 31 | QTimer timer; |
| 32 | timer.connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); |
| 33 | timer.start(time.integer()); |
| 34 | loop.exec(); |
| 35 | |
| 36 | return QList<Token>(); |
| 37 | }); |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void StdLib::load(Evaluator &eval) |
| 41 | { |
| 42 | eval.addFunction(_print); |
| 43 | eval.addFunction(_prout); |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 44 | eval.addFunction(_sleep); |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 45 | } |