Commit 081e3685 authored by Tom Niget's avatar Tom Niget

Add basic coroutine+await support

parent e643dbd0
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <ostream> #include <ostream>
#include <string> #include <string>
#include <typon/typon.hpp>
#ifdef __cpp_lib_unreachable #ifdef __cpp_lib_unreachable
#include <utility> #include <utility>
[[noreturn]] inline void TYPON_UNREACHABLE() { std::unreachable(); } [[noreturn]] inline void TYPON_UNREACHABLE() { std::unreachable(); }
...@@ -58,7 +60,7 @@ std::ostream &operator<<(std::ostream &os, std::optional<T> const &opt) { ...@@ -58,7 +60,7 @@ std::ostream &operator<<(std::ostream &os, std::optional<T> const &opt) {
return opt ? os << opt.value() : os << "None"; return opt ? os << opt.value() : os << "None";
} }
bool is_cpp() { return true; } typon::Task<bool> is_cpp() { co_return true; }
class NoneType { class NoneType {
public: public:
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <typon/typon.hpp>
template <typename T> template <typename T>
concept Streamable = requires(const T &x, std::ostream &s) { concept Streamable = requires(const T &x, std::ostream &s) {
{ s << x } -> std::same_as<std::ostream &>; { s << x } -> std::same_as<std::ostream &>;
...@@ -39,13 +41,28 @@ template <typename T> ...@@ -39,13 +41,28 @@ template <typename T>
concept Printable = requires(const T &x, std::ostream &s) { concept Printable = requires(const T &x, std::ostream &s) {
{ print_to(x, s) } -> std::same_as<void>; { print_to(x, s) } -> std::same_as<void>;
}; };
/*
template <Printable T, Printable... Args> template <Printable T, Printable... Args>
void print(T const &head, Args const &...args) { typon::Task<void> print(T const &head, Args const &...args) {
print_to(head, std::cout);
(((std::cout << ' '), print_to(args, std::cout)), ...);
std::cout << '\n'; co_return;
}*/
struct {
void sync() { std::cout << '\n'; }
template <Printable T, Printable... Args>
void sync(T const &head, Args const &...args) {
print_to(head, std::cout); print_to(head, std::cout);
(((std::cout << ' '), print_to(args, std::cout)), ...); (((std::cout << ' '), print_to(args, std::cout)), ...);
std::cout << '\n'; std::cout << '\n';
} }
void print() { std::cout << '\n'; } template<Printable... Args>
typon::Task<void> operator()(Args const &...args) {
co_return sync(args...);
}
} print;
//typon::Task<void> print() { std::cout << '\n'; co_return; }
#endif // TYPON_PRINT_HPP #endif // TYPON_PRINT_HPP
from typon import fork, sync from typon import fork, sync
def fibo(n):
if n < 2:
return n
a = fibo(n - 1)
b = fibo(n - 2)
return a + b
#def fibo(n: int) -> int: #def fibo(n: int) -> int:
# if n < 2: # if n < 2:
...@@ -11,4 +18,4 @@ from typon import fork, sync ...@@ -11,4 +18,4 @@ from typon import fork, sync
if __name__ == "__main__": if __name__ == "__main__":
print("res=", 5, ".") print(fibo(30)) # should display 832040
\ No newline at end of file \ No newline at end of file
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment