Commit 1f9e68f2 authored by Tom Niget's avatar Tom Niget

Add basic support for generators (co_yield)

parent 776ac174
......@@ -33,6 +33,19 @@ concept PyLen = requires(const T &t) {
template <PyLen T> size_t len(const T &t) { return t.py_len(); }
template <typename T>
concept PyNext = requires(T t) {
t.py_next();
};
template <PyNext T> auto next(T &t) { return t.py_next(); }
template<typename T>
std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt)
{
return opt ? os << opt.value() : os << "None";
}
bool is_cpp() { return true; }
#include "builtins/bool.hpp"
......@@ -44,4 +57,6 @@ bool is_cpp() { return true; }
#include "builtins/set.hpp"
#include "builtins/str.hpp"
#include "../typon/generator.hpp"
#endif // TYPON_BUILTINS_HPP
//
// Created by Tom on 13/03/2023.
//
#ifndef TYPON_RANGE_HPP
#define TYPON_RANGE_HPP
#include <ranges>
// todo: proper range support
template <typename T> auto range(T stop) { return std::views::iota(0, stop); }
template <typename T> auto range(T start, T stop) {
return std::views::iota(start, stop);
}
#endif // TYPON_RANGE_HPP
......@@ -10,6 +10,9 @@ from transpiler.format import format_code
def run_tests():
for path in Path('tests').glob('*.py'):
print(path.name)
if path.name.startswith('_'):
print("Skipping")
continue
with open(path, "r", encoding="utf-8") as f:
res = format_code(transpile(f.read()))
name_cpp = path.with_suffix('.cpp')
......
# coding: utf-8
def fib():
a = 0
b = 1
while True:
yield a
a, b = b, a + b
if __name__ == "__main__":
f = fib()
for i in range(10):
print(next(f))
\ 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