summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aisa/eval.h16
-rw-r--r--main.cpp17
2 files changed, 24 insertions, 9 deletions
diff --git a/aisa/eval.h b/aisa/eval.h
index 30dc617..a301e93 100644
--- a/aisa/eval.h
+++ b/aisa/eval.h
@@ -1,6 +1,7 @@
1#pragma once 1#pragma once
2 2
3#include <coroutine> 3#include <coroutine>
4#include <memory>
4#include <optional> 5#include <optional>
5#include <utility> 6#include <utility>
6#include <vector> 7#include <vector>
@@ -11,6 +12,12 @@
11namespace aisa { 12namespace aisa {
12 13
13 template<typename CRTP> struct EvalState { 14 template<typename CRTP> struct EvalState {
15 struct EvalContext {
16 task<void> coroutine;
17
18 bool resume() { return coroutine(); }
19 };
20
14 CRTP & crtp() noexcept { return static_cast<CRTP &>(*this); } 21 CRTP & crtp() noexcept { return static_cast<CRTP &>(*this); }
15 22
16 task<regval_t> async_load_reg(regnum_t rn) 23 task<regval_t> async_load_reg(regnum_t rn)
@@ -31,7 +38,7 @@ namespace aisa {
31 } 38 }
32 } 39 }
33 40
34 task<void> eval(const Step &step) 41 task<void> async_evaluate(EvalContext &contex, const Step &step)
35 { 42 {
36 if (step.predicate.has_value()) { 43 if (step.predicate.has_value()) {
37 regval_t pval = co_await async_load_reg(step.predicate->first); 44 regval_t pval = co_await async_load_reg(step.predicate->first);
@@ -46,6 +53,13 @@ namespace aisa {
46 for (unsigned int i = 0; i < step.destination_regs.size(); ++i) 53 for (unsigned int i = 0; i < step.destination_regs.size(); ++i)
47 co_await async_store_reg(step.destination_regs[i], destination_vals[i]); 54 co_await async_store_reg(step.destination_regs[i], destination_vals[i]);
48 } 55 }
56
57 std::unique_ptr<EvalContext> operator()(const Step &step)
58 {
59 auto context = std::make_unique<EvalContext>();
60 context->coroutine = async_evaluate(*context, step);
61 return context;
62 }
49 }; 63 };
50 64
51} 65}
diff --git a/main.cpp b/main.cpp
index df2aca1..e88d740 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,6 +1,7 @@
1#include <initializer_list> 1#include <initializer_list>
2#include <iostream> 2#include <iostream>
3#include <map> 3#include <map>
4#include <memory>
4#include <optional> 5#include <optional>
5#include <string> 6#include <string>
6#include <type_traits> 7#include <type_traits>
@@ -69,18 +70,18 @@ int main(int argc, const char *argv[])
69 70
70 std::cout << "\n\n\n"; 71 std::cout << "\n\n\n";
71 72
72 auto w = state.eval(step); 73 auto w = state(step);
73 std::cout << "run\n"; w(); 74 std::cout << "run\n"; w->resume();
74 std::cout << "run\n"; w(); 75 std::cout << "run\n"; w->resume();
75 std::cout << "run\n"; w(); 76 std::cout << "run\n"; w->resume();
76 std::cout << "set predicate (valid)\n"; state.store_reg(step.predicate->first, step.predicate->second); 77 std::cout << "set predicate (valid)\n"; state.store_reg(step.predicate->first, step.predicate->second);
77 std::cout << "run\n"; w(); 78 std::cout << "run\n"; w->resume();
78 std::cout << "run\n"; w(); 79 std::cout << "run\n"; w->resume();
79 std::cout << "run\n"; w(); 80 std::cout << "run\n"; w->resume();
80 std::cout << "set regs (all)\n"; 81 std::cout << "set regs (all)\n";
81 for (int i = 0; i < 10; ++i) 82 for (int i = 0; i < 10; ++i)
82 state.store_reg(i, 1000 + i); 83 state.store_reg(i, 1000 + i);
83 for (bool done = false; !done; done = w()) 84 for (bool done = false; !done; done = w->resume())
84 std::cout << "run\n"; 85 std::cout << "run\n";
85 std::cout << "huzzah!\n"; 86 std::cout << "huzzah!\n";
86 87