Commit 54759ed9 authored by Tom Niget's avatar Tom Niget

Rename Py to Ty

parent 4b427fb6
...@@ -59,46 +59,46 @@ template <PySmartPtr T> struct RealType<T> { ...@@ -59,46 +59,46 @@ template <PySmartPtr T> struct RealType<T> {
using type = typename T::element_type; using type = typename T::element_type;
}; };
//template <typename T> using PyObj = std::shared_ptr<typename RealType<T>::type>; //template <typename T> using TyObj = std::shared_ptr<typename RealType<T>::type>;
template<typename T> template<typename T>
class PyObj : public std::shared_ptr<typename RealType<T>::type> { class TyObj : public std::shared_ptr<typename RealType<T>::type> {
public: public:
using inner = typename RealType<T>::type; using inner = typename RealType<T>::type;
/*template<typename... Args> /*template<typename... Args>
PyObj(Args&&... args) : std::shared_ptr<inner>(std::make_shared<inner>(std::forward<Args>(args)...)) {}*/ TyObj(Args&&... args) : std::shared_ptr<inner>(std::make_shared<inner>(std::forward<Args>(args)...)) {}*/
PyObj() : std::shared_ptr<inner>() {} TyObj() : std::shared_ptr<inner>() {}
PyObj(std::nullptr_t) : std::shared_ptr<inner>(nullptr) {} TyObj(std::nullptr_t) : std::shared_ptr<inner>(nullptr) {}
PyObj(inner *ptr) : std::shared_ptr<inner>(ptr) {} TyObj(inner *ptr) : std::shared_ptr<inner>(ptr) {}
PyObj(const std::shared_ptr<inner> &ptr) : std::shared_ptr<inner>(ptr) {} TyObj(const std::shared_ptr<inner> &ptr) : std::shared_ptr<inner>(ptr) {}
PyObj(std::shared_ptr<inner> &&ptr) : std::shared_ptr<inner>(ptr) {} TyObj(std::shared_ptr<inner> &&ptr) : std::shared_ptr<inner>(ptr) {}
PyObj(const PyObj &ptr) : std::shared_ptr<inner>(ptr) {} TyObj(const TyObj &ptr) : std::shared_ptr<inner>(ptr) {}
PyObj( PyObj &ptr) : std::shared_ptr<inner>(ptr) {} TyObj( TyObj &ptr) : std::shared_ptr<inner>(ptr) {}
PyObj(PyObj &&ptr) : std::shared_ptr<inner>(ptr) {} TyObj(TyObj &&ptr) : std::shared_ptr<inner>(ptr) {}
PyObj &operator=(const PyObj &ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; } TyObj &operator=(const TyObj &ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; }
PyObj &operator=(PyObj &&ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; } TyObj &operator=(TyObj &&ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; }
PyObj &operator=(std::nullptr_t) { std::shared_ptr<inner>::operator=(nullptr); return *this; } TyObj &operator=(std::nullptr_t) { std::shared_ptr<inner>::operator=(nullptr); return *this; }
PyObj &operator=(inner *ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; } TyObj &operator=(inner *ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; }
PyObj &operator=(const std::shared_ptr<inner> &ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; } TyObj &operator=(const std::shared_ptr<inner> &ptr) { std::shared_ptr<inner>::operator=(ptr); return *this; }
template<typename U> template<typename U>
PyObj(const PyObj<U> &ptr) : std::shared_ptr<inner>(ptr) {} TyObj(const TyObj<U> &ptr) : std::shared_ptr<inner>(ptr) {}
//PyObj(PyObj<U> &&ptr) : std::shared_ptr<inner>(ptr) {} //TyObj(TyObj<U> &&ptr) : std::shared_ptr<inner>(ptr) {}
// using make_shared // using make_shared
/*template<class U> /*template<class U>
PyObj(U&& other) : std::shared_ptr<inner>(std::make_shared<inner>(other)) {}*/ TyObj(U&& other) : std::shared_ptr<inner>(std::make_shared<inner>(other)) {}*/
template<class U> template<class U>
bool operator==(const PyObj<U> &other) const { bool operator==(const TyObj<U> &other) const {
// check null // check null
if (this->get() == other.get()) { if (this->get() == other.get()) {
return true; return true;
...@@ -121,17 +121,17 @@ public: ...@@ -121,17 +121,17 @@ public:
} }
template<class U> template<class U>
bool py_is(const PyObj<U> &other) const { bool py_is(const TyObj<U> &other) const {
return this->get() == other.get(); return this->get() == other.get();
} }
}; };
template <typename T, typename... Args> auto pyobj(Args &&...args) -> PyObj<typename RealType<T>::type> { template <typename T, typename... Args> auto tyObj(Args &&...args) -> TyObj<typename RealType<T>::type> {
return std::make_shared<typename RealType<T>::type>( return std::make_shared<typename RealType<T>::type>(
std::forward<Args>(args)...); std::forward<Args>(args)...);
} }
template <typename T, typename... Args> auto pyobj_agg(Args &&...args) -> PyObj<T> { template <typename T, typename... Args> auto pyobj_agg(Args &&...args) -> TyObj<T> {
return std::make_shared<typename RealType<T>::type>((typename RealType<T>::type) { std::forward<Args>(args)... }); return std::make_shared<typename RealType<T>::type>((typename RealType<T>::type) { std::forward<Args>(args)... });
} }
...@@ -209,11 +209,11 @@ static constexpr auto PyNone = std::nullopt; ...@@ -209,11 +209,11 @@ static constexpr auto PyNone = std::nullopt;
struct file_s { struct file_s {
struct py_type { struct py_type {
METHOD( METHOD(
typon::Task<PyStr>, read, (Self self, size_t size = -1), { typon::Task<TyStr>, read, (Self self, size_t size = -1), {
if (size == -1) { if (size == -1) {
size = self->len; size = self->len;
} }
PyStr buf(size, '\0'); TyStr buf(size, '\0');
int nbytes = co_await typon::io::read(self->fd, buf.data(), size); int nbytes = co_await typon::io::read(self->fd, buf.data(), size);
if (nbytes < 0) { if (nbytes < 0) {
system_error(-nbytes, "read()"); system_error(-nbytes, "read()");
...@@ -256,10 +256,10 @@ struct file_s { ...@@ -256,10 +256,10 @@ struct file_s {
} file; } file;
namespace typon { namespace typon {
using PyFile = PyObj<decltype(file)::py_type>; using PyFile = TyObj<decltype(file)::py_type>;
} }
typon::Task<typon::PyFile> open(const PyStr &path, std::string_view mode) { typon::Task<typon::PyFile> open(const TyStr &path, std::string_view mode) {
const char *path_c = path.c_str(); const char *path_c = path.c_str();
size_t len = 0; size_t len = 0;
struct statx statxbuf; struct statx statxbuf;
...@@ -307,7 +307,7 @@ typon::Task<typon::PyFile> open(const PyStr &path, std::string_view mode) { ...@@ -307,7 +307,7 @@ typon::Task<typon::PyFile> open(const PyStr &path, std::string_view mode) {
std::cerr << path << "," << flags << std::endl; std::cerr << path << "," << flags << std::endl;
system_error(-fd, "openat()"); system_error(-fd, "openat()");
} }
co_return pyobj<typon::PyFile>(fd, len); co_return tyObj<typon::PyFile>(fd, len);
} }
#include <typon/generator.hpp> #include <typon/generator.hpp>
...@@ -375,7 +375,7 @@ auto mapFilter(Map map, Seq seq, Filt filt = Filt()) { ...@@ -375,7 +375,7 @@ auto mapFilter(Map map, Seq seq, Filt filt = Filt()) {
std::vector<return_type> result{}; std::vector<return_type> result{};
for (auto i : seq | std::views::filter(filt) for (auto i : seq | std::views::filter(filt)
| std::views::transform(map)) result.push_back(i); | std::views::transform(map)) result.push_back(i);
return typon::PyList(std::move(result)); return typon::TyList(std::move(result));
} }
namespace PYBIND11_NAMESPACE { namespace PYBIND11_NAMESPACE {
......
...@@ -5,22 +5,22 @@ ...@@ -5,22 +5,22 @@
#ifndef TYPON_BYTES_H #ifndef TYPON_BYTES_H
#define TYPON_BYTES_H #define TYPON_BYTES_H
class PyStr; class TyStr;
class PyBytes : public std::string { class TyBytes : public std::string {
public: public:
PyBytes() : std::string() {} TyBytes() : std::string() {}
PyBytes(const char *s) : std::string(s) {} TyBytes(const char *s) : std::string(s) {}
PyBytes(const std::string &s) : std::string(s) {} TyBytes(const std::string &s) : std::string(s) {}
PyBytes(std::string &&s) : std::string(std::move(s)) {} TyBytes(std::string &&s) : std::string(std::move(s)) {}
PyBytes(size_t count, char ch) : std::string(count, ch) {} TyBytes(size_t count, char ch) : std::string(count, ch) {}
template <class InputIterator> template <class InputIterator>
PyBytes(InputIterator first, InputIterator last) : std::string(first, last) {} TyBytes(InputIterator first, InputIterator last) : std::string(first, last) {}
//PyStr decode_inner(const std::string &encoding = "utf-8") const; //TyStr decode_inner(const std::string &encoding = "utf-8") const;
METHOD(PyStr, decode, (Self self, const std::string &encoding = "utf-8"), ;) METHOD(TyStr, decode, (Self self, const std::string &encoding = "utf-8"), ;)
}; };
#endif // TYPON_BYTES_H #endif // TYPON_BYTES_H
......
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
#include "print.hpp" #include "print.hpp"
using PyComplex = std::complex<double>; using TyComplex = std::complex<double>;
PyComplex operator+(int a, const PyComplex &b) { return PyComplex(a) + b; } TyComplex operator+(int a, const TyComplex &b) { return TyComplex(a) + b; }
PyComplex operator-(int a, const PyComplex &b) { return PyComplex(a) - b; } TyComplex operator-(int a, const TyComplex &b) { return TyComplex(a) - b; }
template <> void print_to<PyComplex>(const PyComplex &x, std::ostream &s) { template <> void print_to<TyComplex>(const TyComplex &x, std::ostream &s) {
if (x.real() == 0) { if (x.real() == 0) {
s << x.imag() << "j"; s << x.imag() << "j";
} else { } else {
......
...@@ -10,12 +10,12 @@ ...@@ -10,12 +10,12 @@
/* /*
template <typename K, typename V> template <typename K, typename V>
class PyDict : public std::unordered_map<K, V> { class TyDict : public std::unordered_map<K, V> {
public: public:
PyDict(std::unordered_map<K, V> &&m) TyDict(std::unordered_map<K, V> &&m)
: std::unordered_map<K, V>(std::move(m)) {} : std::unordered_map<K, V>(std::move(m)) {}
PyDict(std::initializer_list<std::pair<const K, V>> m) TyDict(std::initializer_list<std::pair<const K, V>> m)
: std::unordered_map<K, V>(m) {} : std::unordered_map<K, V>(m) {}
operator std::unordered_map<K, V>() const { operator std::unordered_map<K, V>() const {
...@@ -83,22 +83,22 @@ public: ...@@ -83,22 +83,22 @@ public:
}; };
template <typename K, typename V> template <typename K, typename V>
PyDict(std::initializer_list<std::pair<K, V>>) -> PyDict<K, V>;*/ TyDict(std::initializer_list<std::pair<K, V>>) -> TyDict<K, V>;*/
namespace typon { namespace typon {
template <typename K, typename V> class PyDict { template <typename K, typename V> class TyDict {
public: public:
PyDict(std::shared_ptr<std::unordered_map<K, V>> &&m) : _m(std::move(m)) {} TyDict(std::shared_ptr<std::unordered_map<K, V>> &&m) : _m(std::move(m)) {}
PyDict(std::unordered_map<K, V> &&m) TyDict(std::unordered_map<K, V> &&m)
: _m(std::move( : _m(std::move(
std::make_shared<std::unordered_map<K, V>>(std::move(m)))) {} std::make_shared<std::unordered_map<K, V>>(std::move(m)))) {}
PyDict(std::initializer_list<std::pair<const K, V>> &&m) TyDict(std::initializer_list<std::pair<const K, V>> &&m)
: _m(std::make_shared<std::unordered_map<K, V>>(std::move(m))) {} : _m(std::make_shared<std::unordered_map<K, V>>(std::move(m))) {}
PyDict() : _m(std::make_shared<std::unordered_map<K, V>>()) {} TyDict() : _m(std::make_shared<std::unordered_map<K, V>>()) {}
template <typename... Args> template <typename... Args>
PyDict(Args &&...args) TyDict(Args &&...args)
: _m(std::make_shared<std::unordered_map<K, V>>( : _m(std::make_shared<std::unordered_map<K, V>>(
std::forward<Args>(args)...)) {} std::forward<Args>(args)...)) {}
...@@ -139,7 +139,7 @@ private: ...@@ -139,7 +139,7 @@ private:
namespace PYBIND11_NAMESPACE { namespace PYBIND11_NAMESPACE {
namespace detail { namespace detail {
template <typename K, typename V> template <typename K, typename V>
struct type_caster<typon::PyDict<K, V>> : map_caster<typon::PyDict<K, V>, K, V> {}; struct type_caster<typon::TyDict<K, V>> : map_caster<typon::TyDict<K, V>, K, V> {};
}} }}
#endif // TYPON_DICT_HPP #endif // TYPON_DICT_HPP
...@@ -7,18 +7,18 @@ ...@@ -7,18 +7,18 @@
#include "str.hpp" #include "str.hpp"
struct PyException_s { struct TyException_s {
struct py_type { struct py_type {
PyStr message; TyStr message;
}; };
auto operator()(const PyStr &message) const { auto operator()(const TyStr &message) const {
return py_type{message}; return py_type{message};
} }
}; };
namespace typon { namespace typon {
using PyException = PyObj<PyException_s>; using TyException = TyObj<TyException_s>;
} }
#endif // TYPON_EXCEPTION_HPP #endif // TYPON_EXCEPTION_HPP
...@@ -13,17 +13,17 @@ ...@@ -13,17 +13,17 @@
namespace typon { namespace typon {
template <typename T> class PyList { template <typename T> class TyList {
public: public:
using value_type = T; using value_type = T;
PyList(std::shared_ptr<std::vector<T>> &&v) : _v(std::move(v)) {} TyList(std::shared_ptr<std::vector<T>> &&v) : _v(std::move(v)) {}
PyList(std::vector<T> &&v) TyList(std::vector<T> &&v)
: _v(std::move(std::make_shared<std::vector<T>>(std::move(v)))) {} : _v(std::move(std::make_shared<std::vector<T>>(std::move(v)))) {}
PyList(std::initializer_list<T> &&v) TyList(std::initializer_list<T> &&v)
: _v(std::make_shared<std::vector<T>>(std::move(v))) {} : _v(std::make_shared<std::vector<T>>(std::move(v))) {}
PyList() : _v(std::make_shared<std::vector<T>>()) {} TyList() : _v(std::make_shared<std::vector<T>>()) {}
auto begin() const { return _v->begin(); } auto begin() const { return _v->begin(); }
...@@ -70,16 +70,16 @@ public: ...@@ -70,16 +70,16 @@ public:
void py_print(std::ostream &s) const { py_repr(s); } void py_print(std::ostream &s) const { py_repr(s); }
PyList<T> operator+(const PyList<T> &other) const { TyList<T> operator+(const TyList<T> &other) const {
std::vector<T> v; std::vector<T> v;
v.reserve(_v->size() + other._v->size()); v.reserve(_v->size() + other._v->size());
v.insert(v.end(), _v->begin(), _v->end()); v.insert(v.end(), _v->begin(), _v->end());
v.insert(v.end(), other._v->begin(), other._v->end()); v.insert(v.end(), other._v->begin(), other._v->end());
return PyList<T>(std::move(v)); return TyList<T>(std::move(v));
} }
PyList<T> operator*(size_t n) const { TyList<T> operator*(size_t n) const {
PyList<T> v{}; TyList<T> v{};
v._v->reserve(this->_v->size() * n); v._v->reserve(this->_v->size() * n);
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
v._v->insert(v._v->end(), this->_v->begin(), this->_v->end()); v._v->insert(v._v->end(), this->_v->begin(), this->_v->end());
...@@ -97,21 +97,21 @@ private: ...@@ -97,21 +97,21 @@ private:
} }
template <typename T> typon::PyList<T> list(std::initializer_list<T> &&v) { template <typename T> typon::TyList<T> list(std::initializer_list<T> &&v) {
return typon::PyList<T>(std::move(v)); return typon::TyList<T>(std::move(v));
} }
namespace PYBIND11_NAMESPACE { namespace PYBIND11_NAMESPACE {
namespace detail { namespace detail {
template <typename Type> template <typename Type>
struct type_caster<typon::PyList<Type>> : list_caster<typon::PyList<Type>, Type> {}; struct type_caster<typon::TyList<Type>> : list_caster<typon::TyList<Type>, Type> {};
}} }}
/*NAMESPACE_BEGIN(NB_NAMESPACE) /*NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail) NAMESPACE_BEGIN(detail)
template <typename Type> struct type_caster<PyList<Type>> template <typename Type> struct type_caster<TyList<Type>>
: list_caster<PyList<Type>, Type> { }; : list_caster<TyList<Type>, Type> { };
NAMESPACE_END(detail) NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE) NAMESPACE_END(NB_NAMESPACE)
......
...@@ -28,11 +28,11 @@ template <PyPrint T> void print_to(const T &x, std::ostream &s) { ...@@ -28,11 +28,11 @@ template <PyPrint T> void print_to(const T &x, std::ostream &s) {
} }
template <typename T> template <typename T>
concept PyRepr = requires(const T &x, std::ostream &s) { concept TyRepr = requires(const T &x, std::ostream &s) {
{ x.py_repr(s) } -> std::same_as<void>; { x.py_repr(s) } -> std::same_as<void>;
}; };
template <PyRepr T> void repr_to(const T &x, std::ostream &s) { x.py_repr(s); } template <TyRepr T> void repr_to(const T &x, std::ostream &s) { x.py_repr(s); }
template <typename T> template <typename T>
concept Printable = requires(const T &x, std::ostream &s) { concept Printable = requires(const T &x, std::ostream &s) {
......
...@@ -9,11 +9,11 @@ ...@@ -9,11 +9,11 @@
namespace typon { namespace typon {
template <typename T> class PySet : public std::unordered_set<T> { template <typename T> class TySet : public std::unordered_set<T> {
public: public:
PySet(std::unordered_set<T> &&s) : std::unordered_set<T>(std::move(s)) {} TySet(std::unordered_set<T> &&s) : std::unordered_set<T>(std::move(s)) {}
PySet(std::initializer_list<T> &&s) : std::unordered_set<T>(std::move(s)) {} TySet(std::initializer_list<T> &&s) : std::unordered_set<T>(std::move(s)) {}
operator std::unordered_set<T>() const { operator std::unordered_set<T>() const {
return std::unordered_set<T>(this->begin(), this->end()); return std::unordered_set<T>(this->begin(), this->end());
...@@ -73,8 +73,8 @@ public: ...@@ -73,8 +73,8 @@ public:
} }
template <typename T> typon::PySet<T> set(std::initializer_list<T> &&s) { template <typename T> typon::TySet<T> set(std::initializer_list<T> &&s) {
return typon::PySet<T>(std::move(s)); return typon::TySet<T>(std::move(s));
} }
#endif // TYPON_SET_HPP #endif // TYPON_SET_HPP
...@@ -10,14 +10,14 @@ ...@@ -10,14 +10,14 @@
#include <Python.h> #include <Python.h>
#include <stdint.h> #include <stdint.h>
struct PySlice { struct TySlice {
PySlice() = default; TySlice() = default;
PySlice(const PySlice &) = default; TySlice(const TySlice &) = default;
PySlice(PySlice &&) = default; TySlice(TySlice &&) = default;
PySlice &operator=(const PySlice &) = default; TySlice &operator=(const TySlice &) = default;
PySlice &operator=(PySlice &&) = default; TySlice &operator=(TySlice &&) = default;
PySlice(std::optional<ssize_t> start, std::optional<ssize_t> stop, std::optional<ssize_t> step) : start(start), stop(stop), step(step) { TySlice(std::optional<ssize_t> start, std::optional<ssize_t> stop, std::optional<ssize_t> step) : start(start), stop(stop), step(step) {
if (step == 0) { if (step == 0) {
throw std::runtime_error("slice step cannot be zero"); throw std::runtime_error("slice step cannot be zero");
} }
......
...@@ -18,26 +18,26 @@ using namespace std::literals; ...@@ -18,26 +18,26 @@ using namespace std::literals;
#include <fmt/format.h> #include <fmt/format.h>
#include <pybind11/cast.h> #include <pybind11/cast.h>
class PyStr : public std::string { class TyStr : public std::string {
public: public:
PyStr() : std::string() {} TyStr() : std::string() {}
PyStr(const std::string &s) : std::string(s) {} TyStr(const std::string &s) : std::string(s) {}
PyStr(std::string &&s) : std::string(std::move(s)) {} TyStr(std::string &&s) : std::string(std::move(s)) {}
constexpr PyStr(const char *s, size_t count) : std::string(s, count) {} constexpr TyStr(const char *s, size_t count) : std::string(s, count) {}
constexpr PyStr(size_t count, char ch) : std::string(count, ch) {} constexpr TyStr(size_t count, char ch) : std::string(count, ch) {}
template<typename... Args> template<typename... Args>
PyStr(Args&&... args) : std::string(std::forward<Args>(args)...) {} TyStr(Args&&... args) : std::string(std::forward<Args>(args)...) {}
template <class InputIterator> template <class InputIterator>
PyStr(InputIterator first, InputIterator last) : std::string(first, last) {} TyStr(InputIterator first, InputIterator last) : std::string(first, last) {}
METHOD(PyBytes, encode, (Self self, const std::string &encoding = "utf-8"), { METHOD(TyBytes, encode, (Self self, const std::string &encoding = "utf-8"), {
return PyBytes(self.begin(), self.end()); return TyBytes(self.begin(), self.end());
}) })
METHOD_GEN((typename... T), PyStr, format, (Self self, T &&...args), { METHOD_GEN((typename... T), TyStr, format, (Self self, T &&...args), {
return PyStr(fmt::format(fmt::runtime(self), std::forward<T>(args)...)); return TyStr(fmt::format(fmt::runtime(self), std::forward<T>(args)...));
}) })
METHOD(bool, startswith, (Self self, const std::string &s), { METHOD(bool, startswith, (Self self, const std::string &s), {
...@@ -57,10 +57,10 @@ public: ...@@ -57,10 +57,10 @@ public:
return self.std::string::find(x) != std::string::npos; return self.std::string::find(x) != std::string::npos;
}) })
PyStr operator[](PySlice slice) const { TyStr operator[](TySlice slice) const {
auto [len, new_slice] = slice.adjust_indices(this->size()); auto [len, new_slice] = slice.adjust_indices(this->size());
PyStr result; TyStr result;
result.reserve(len); result.reserve(len);
if (new_slice.start < new_slice.stop) { if (new_slice.start < new_slice.stop) {
...@@ -82,12 +82,12 @@ public: ...@@ -82,12 +82,12 @@ public:
return result; return result;
} }
PyStr operator[](ssize_t index) const { TyStr operator[](ssize_t index) const {
if (index < 0) { if (index < 0) {
index += this->size(); index += this->size();
} }
return PyStr(1, std::string::operator[](index)); return TyStr(1, std::string::operator[](index));
} }
char char_at(ssize_t index) const { char char_at(ssize_t index) const {
...@@ -103,29 +103,29 @@ public: ...@@ -103,29 +103,29 @@ public:
} }
}; };
inline constexpr PyStr operator""_ps(const char *s, size_t len) noexcept { inline constexpr TyStr operator""_ps(const char *s, size_t len) noexcept {
return PyStr(s, len); return TyStr(s, len);
} }
template<typename Self> template<typename Self>
PyStr PyBytes::decode_s::operator()(Self self, const std::string &encoding) const { TyStr TyBytes::decode_s::operator()(Self self, const std::string &encoding) const {
return PyStr(self.begin(), self.end()); return TyStr(self.begin(), self.end());
} }
template <typename T> PyStr str(const T &x) { template <typename T> TyStr str(const T &x) {
std::stringstream s; std::stringstream s;
print_to(x, s); print_to(x, s);
return s.str(); return s.str();
} }
template <typename T> PyStr repr(const T &x) { template <typename T> TyStr repr(const T &x) {
std::stringstream s; std::stringstream s;
::repr_to(x, s); ::repr_to(x, s);
return s.str(); return s.str();
} }
template <> struct std::hash<PyStr> { template <> struct std::hash<TyStr> {
std::size_t operator()(const PyStr &s) const noexcept { std::size_t operator()(const TyStr &s) const noexcept {
return std::hash<std::string>()(s); return std::hash<std::string>()(s);
} }
}; };
...@@ -133,20 +133,20 @@ template <> struct std::hash<PyStr> { ...@@ -133,20 +133,20 @@ template <> struct std::hash<PyStr> {
namespace PYBIND11_NAMESPACE { namespace PYBIND11_NAMESPACE {
namespace detail { namespace detail {
template<> template<>
struct type_caster<PyStr> struct type_caster<TyStr>
: string_caster<PyStr> {}; : string_caster<TyStr> {};
}} }}
template <> void repr_to(const PyStr &x, std::ostream &s) { template <> void repr_to(const TyStr &x, std::ostream &s) {
s << '"' << x << '"'; s << '"' << x << '"';
} }
template <> void print_to<PyStr>(const PyStr &x, std::ostream &s) { s << x; } template <> void print_to<TyStr>(const TyStr &x, std::ostream &s) { s << x; }
struct { struct {
PyStr operator()(const PyStr& s = ""_ps) { TyStr operator()(const TyStr& s = ""_ps) {
std::cout << s; std::cout << s;
PyStr input; TyStr input;
std::getline(std::cin, input); std::getline(std::cin, input);
return input; return input;
} }
......
...@@ -22,7 +22,7 @@ struct hashlib_t { ...@@ -22,7 +22,7 @@ struct hashlib_t {
struct _Hash_s { struct _Hash_s {
struct py_type { struct py_type {
py_type(PyObj<void> context, openssl_update update, openssl_final final, py_type(TyObj<void> context, openssl_update update, openssl_final final,
int diglen) int diglen)
: _context(context), _update(update), _final(final), _diglen(diglen) { : _context(context), _update(update), _final(final), _diglen(diglen) {
} }
...@@ -33,7 +33,7 @@ struct hashlib_t { ...@@ -33,7 +33,7 @@ struct hashlib_t {
: _context(other._context), _update(other._update), : _context(other._context), _update(other._update),
_final(other._final), _diglen(other._diglen) {} _final(other._final), _diglen(other._diglen) {}
PyObj<void> _context; TyObj<void> _context;
openssl_update _update; openssl_update _update;
openssl_final _final; openssl_final _final;
int _diglen; int _diglen;
...@@ -57,31 +57,31 @@ struct hashlib_t { ...@@ -57,31 +57,31 @@ struct hashlib_t {
} _Hash; } _Hash;
FUNCTION(auto, md5, (), { FUNCTION(auto, md5, (), {
auto ctx = pyobj<MD5_CTX>(); auto ctx = tyObj<MD5_CTX>();
MD5_Init(ctx.get()); MD5_Init(ctx.get());
return pyobj<_Hash_s>(ctx, (openssl_update)MD5_Update, return tyObj<_Hash_s>(ctx, (openssl_update)MD5_Update,
(openssl_final)MD5_Final, MD5_DIGEST_LENGTH); (openssl_final)MD5_Final, MD5_DIGEST_LENGTH);
}) })
FUNCTION(auto, sha1, (), { FUNCTION(auto, sha1, (), {
auto ctx = pyobj<SHA_CTX>(); auto ctx = tyObj<SHA_CTX>();
SHA1_Init(ctx.get()); SHA1_Init(ctx.get());
return pyobj<_Hash_s>(ctx, (openssl_update)SHA1_Update, return tyObj<_Hash_s>(ctx, (openssl_update)SHA1_Update,
(openssl_final)SHA1_Final, SHA_DIGEST_LENGTH); (openssl_final)SHA1_Final, SHA_DIGEST_LENGTH);
}) })
FUNCTION(auto, sha256, (), { FUNCTION(auto, sha256, (), {
auto ctx = pyobj<SHA256_CTX>(); auto ctx = tyObj<SHA256_CTX>();
SHA256_Init(ctx.get()); SHA256_Init(ctx.get());
return pyobj<_Hash_s>(ctx, (openssl_update)SHA256_Update, return tyObj<_Hash_s>(ctx, (openssl_update)SHA256_Update,
(openssl_final)SHA256_Final, (openssl_final)SHA256_Final,
SHA256_DIGEST_LENGTH); SHA256_DIGEST_LENGTH);
}) })
FUNCTION(auto, sha512, (), { FUNCTION(auto, sha512, (), {
auto ctx = pyobj<SHA512_CTX>(); auto ctx = tyObj<SHA512_CTX>();
SHA512_Init(ctx.get()); SHA512_Init(ctx.get());
return pyobj<_Hash_s>(ctx, (openssl_update)SHA512_Update, return tyObj<_Hash_s>(ctx, (openssl_update)SHA512_Update,
(openssl_final)SHA512_Final, (openssl_final)SHA512_Final,
SHA512_DIGEST_LENGTH); SHA512_DIGEST_LENGTH);
}) })
...@@ -92,7 +92,7 @@ auto &get_all() { return all; } ...@@ -92,7 +92,7 @@ auto &get_all() { return all; }
} // namespace py_hashlib } // namespace py_hashlib
namespace typon { namespace typon {
using Py_Hash = PyObj<py_hashlib::hashlib_t::_Hash_s>; using Py_Hash = TyObj<py_hashlib::hashlib_t::_Hash_s>;
} }
#endif // TYPON_HASHLIB_HPP #endif // TYPON_HASHLIB_HPP
...@@ -46,15 +46,15 @@ struct os_t { ...@@ -46,15 +46,15 @@ struct os_t {
struct DirEntry_s { struct DirEntry_s {
struct py_type { struct py_type {
PyStr name; TyStr name;
PyStr path; TyStr path;
}; };
} DirEntry; } DirEntry;
struct _Scandiriterator_s { struct _Scandiriterator_s {
struct py_type { struct py_type {
using value_type = PyObj<DirEntry_s>; using value_type = TyObj<DirEntry_s>;
using reference = PyObj<DirEntry_s>; using reference = TyObj<DirEntry_s>;
METHOD(auto, py_enter, (Self self), { return self; }) METHOD(auto, py_enter, (Self self), { return self; })
METHOD(void, py_exit, (Self self), { METHOD(void, py_exit, (Self self), {
...@@ -68,11 +68,11 @@ struct os_t { ...@@ -68,11 +68,11 @@ struct os_t {
{ return py_type(self->basepath, self->namelist, self->n, self->n); }) { return py_type(self->basepath, self->namelist, self->n, self->n); })
auto operator*() { auto operator*() {
auto name = PyStr(this->namelist[this->current]->d_name); auto name = TyStr(this->namelist[this->current]->d_name);
return pyobj_agg<DirEntry_s>(name, this->basepath + name); return pyobj_agg<DirEntry_s>(name, this->basepath + name);
} }
py_type(const PyStr &basepath, struct dirent **namelist, int n, py_type(const TyStr &basepath, struct dirent **namelist, int n,
int current = 0) int current = 0)
: basepath(basepath), namelist(namelist), n(n), current(current) { : basepath(basepath), namelist(namelist), n(n), current(current) {
if (this->basepath[this->basepath.size() - 1] != '/') { if (this->basepath[this->basepath.size() - 1] != '/') {
...@@ -88,14 +88,14 @@ struct os_t { ...@@ -88,14 +88,14 @@ struct os_t {
void operator++() { this->current++; } void operator++() { this->current++; }
PyStr basepath; TyStr basepath;
struct dirent **namelist; struct dirent **namelist;
int n; int n;
int current; int current;
}; };
} _Scandiriterator; } _Scandiriterator;
FUNCTION(typon::Task<PyObj<Stat_Result_s>>, stat, (const PyStr &path), FUNCTION(typon::Task<TyObj<Stat_Result_s>>, stat, (const TyStr &path),
{ {
const char *path_c = path.c_str(); const char *path_c = path.c_str();
struct statx statxbuf; struct statx statxbuf;
...@@ -103,7 +103,7 @@ struct os_t { ...@@ -103,7 +103,7 @@ struct os_t {
STATX_SIZE, &statxbuf)) { STATX_SIZE, &statxbuf)) {
system_error(-err, "statx()"); system_error(-err, "statx()");
} }
co_return PyObj<Stat_Result_s>(new Stat_Result_s::py_type{ co_return TyObj<Stat_Result_s>(new Stat_Result_s::py_type{
statxbuf.stx_mode, statxbuf.stx_mode,
statxbuf.stx_ino, statxbuf.stx_ino,
makedev(statxbuf.stx_dev_major, statxbuf.stx_dev_minor), makedev(statxbuf.stx_dev_major, statxbuf.stx_dev_minor),
...@@ -121,24 +121,24 @@ struct os_t { ...@@ -121,24 +121,24 @@ struct os_t {
statxbuf.stx_btime.tv_sec + statxbuf.stx_btime.tv_nsec / 1e9}); statxbuf.stx_btime.tv_sec + statxbuf.stx_btime.tv_nsec / 1e9});
}) })
FUNCTION(PyObj<_Scandiriterator_s>, scandir, (const PyStr &path), { FUNCTION(TyObj<_Scandiriterator_s>, scandir, (const TyStr &path), {
const char *path_c = path.c_str(); const char *path_c = path.c_str();
struct dirent **namelist; struct dirent **namelist;
int n = ::scandir(path_c, &namelist, no_special, alphasort); int n = ::scandir(path_c, &namelist, no_special, alphasort);
if (n < 0) { if (n < 0) {
system_error(-n, "scandir()"); system_error(-n, "scandir()");
} }
return pyobj<_Scandiriterator_s>(path, namelist, n); return tyObj<_Scandiriterator_s>(path, namelist, n);
}); });
FUNCTION(PyStr, readlink, (const PyStr &path), { FUNCTION(TyStr, readlink, (const TyStr &path), {
const char *path_c = path.c_str(); const char *path_c = path.c_str();
char buf[PATH_MAX]; char buf[PATH_MAX];
ssize_t nbytes = ::readlink(path_c, buf, sizeof(buf)); ssize_t nbytes = ::readlink(path_c, buf, sizeof(buf));
if (nbytes < 0) { if (nbytes < 0) {
system_error(-nbytes, "readlink()"); system_error(-nbytes, "readlink()");
} }
return PyStr(buf, nbytes); return TyStr(buf, nbytes);
}) })
} all; } all;
...@@ -146,8 +146,8 @@ auto &get_all() { return all; } ...@@ -146,8 +146,8 @@ auto &get_all() { return all; }
} // namespace py_os } // namespace py_os
namespace typon { namespace typon {
using PyStat_Result = PyObj<py_os::os_t::Stat_Result_s>; using PyStat_Result = TyObj<py_os::os_t::Stat_Result_s>;
using Py_Scandiriterator = PyObj<py_os::os_t::_Scandiriterator_s>; using Py_Scandiriterator = TyObj<py_os::os_t::_Scandiriterator_s>;
} }
#endif // TYPON_OS_HPP #endif // TYPON_OS_HPP
...@@ -25,12 +25,12 @@ struct socket_t { ...@@ -25,12 +25,12 @@ struct socket_t {
struct socket_s { struct socket_s {
struct py_type { struct py_type {
METHOD(typon::Task<std::tuple<PyObj<py_type> COMMA() std::string>>, accept, (Self self), { METHOD(typon::Task<std::tuple<TyObj<py_type> COMMA() std::string>>, accept, (Self self), {
int connfd = co_await typon::io::accept(self->fd, NULL, NULL); int connfd = co_await typon::io::accept(self->fd, NULL, NULL);
if (connfd < 0) { if (connfd < 0) {
system_error(-connfd, "accept()"); system_error(-connfd, "accept()");
} }
co_return std::make_tuple(pyobj<py_type>(connfd), std::string("")); // TODO co_return std::make_tuple(tyObj<py_type>(connfd), std::string("")); // TODO
}) })
METHOD(typon::Task<void>, close, (Self self), METHOD(typon::Task<void>, close, (Self self),
...@@ -61,13 +61,13 @@ struct socket_t { ...@@ -61,13 +61,13 @@ struct socket_t {
} }
}) })
METHOD(typon::Task<PyBytes>, recv, (Self self, int bufsize), { METHOD(typon::Task<TyBytes>, recv, (Self self, int bufsize), {
PyBytes buf(bufsize, '\0'); TyBytes buf(bufsize, '\0');
co_await typon::io::recv(self->fd, buf.data(), buf.size(), 0); co_await typon::io::recv(self->fd, buf.data(), buf.size(), 0);
co_return std::move(buf); co_return std::move(buf);
}) })
METHOD(typon::Task<void>, send, (Self self, PyBytes data), { METHOD(typon::Task<void>, send, (Self self, TyBytes data), {
if (int sbytes = co_await typon::io::send(self->fd, data, 0); if (int sbytes = co_await typon::io::send(self->fd, data, 0);
sbytes < 0) { sbytes < 0) {
co_await dotp(self, close)(); co_await dotp(self, close)();
...@@ -85,7 +85,7 @@ struct socket_t { ...@@ -85,7 +85,7 @@ struct socket_t {
auto operator()(int family, int type_) { auto operator()(int family, int type_) {
if (int fd = ::socket(family, type_, 0); fd >= 0) { if (int fd = ::socket(family, type_, 0); fd >= 0) {
return pyobj<py_type>(fd); return tyObj<py_type>(fd);
} else { } else {
system_error(errno, "socket()"); system_error(errno, "socket()");
} }
...@@ -113,7 +113,7 @@ auto &get_all() { return all; } ...@@ -113,7 +113,7 @@ auto &get_all() { return all; }
} // namespace py_socket } // namespace py_socket
namespace typon { namespace typon {
using PySocket = PyObj<py_socket::socket_t::socket_s>; using PySocket = TyObj<py_socket::socket_t::socket_s>;
} }
#endif // TYPON_SOCKET_HPP #endif // TYPON_SOCKET_HPP
...@@ -13,7 +13,7 @@ struct sys_t { ...@@ -13,7 +13,7 @@ struct sys_t {
static constexpr auto &stdin = std::cin; static constexpr auto &stdin = std::cin;
static constexpr auto &stdout = std::cout; static constexpr auto &stdout = std::cout;
static constexpr auto &stderr = std::cerr; static constexpr auto &stderr = std::cerr;
typon::PyList<PyStr> argv; typon::TyList<TyStr> argv;
FUNCTION(void, exit, (int code), { FUNCTION(void, exit, (int code), {
std::exit(code); std::exit(code);
......
...@@ -37,14 +37,14 @@ class ExpressionVisitor(NodeVisitor): ...@@ -37,14 +37,14 @@ class ExpressionVisitor(NodeVisitor):
elif isinstance(node.value, float): elif isinstance(node.value, float):
yield repr(node.value) yield repr(node.value)
elif isinstance(node.value, complex): elif isinstance(node.value, complex):
yield f"PyComplex({node.value.real}, {node.value.imag})" yield f"TyComplex({node.value.real}, {node.value.imag})"
elif node.value is None: elif node.value is None:
yield "PyNone" yield "PyNone"
else: else:
raise NotImplementedError(node, type(node)) raise NotImplementedError(node, type(node))
def visit_Slice(self, node: ast.Slice) -> Iterable[str]: def visit_Slice(self, node: ast.Slice) -> Iterable[str]:
yield "PySlice(" yield "TySlice("
yield from join(", ", (self.visit(x or ast.Constant(value=None)) for x in (node.lower, node.upper, node.step))) yield from join(", ", (self.visit(x or ast.Constant(value=None)) for x in (node.lower, node.upper, node.step)))
yield ")" yield ")"
...@@ -193,7 +193,7 @@ class ExpressionVisitor(NodeVisitor): ...@@ -193,7 +193,7 @@ class ExpressionVisitor(NodeVisitor):
def visit_List(self, node: ast.List) -> Iterable[str]: def visit_List(self, node: ast.List) -> Iterable[str]:
if node.elts: if node.elts:
yield "typon::PyList{" yield "typon::TyList{"
yield from join(", ", map(self.reset().visit, node.elts)) yield from join(", ", map(self.reset().visit, node.elts))
yield "}" yield "}"
else: else:
...@@ -202,7 +202,7 @@ class ExpressionVisitor(NodeVisitor): ...@@ -202,7 +202,7 @@ class ExpressionVisitor(NodeVisitor):
def visit_Set(self, node: ast.Set) -> Iterable[str]: def visit_Set(self, node: ast.Set) -> Iterable[str]:
if node.elts: if node.elts:
yield "typon::PySet{" yield "typon::TySet{"
yield from join(", ", map(self.reset().visit, node.elts)) yield from join(", ", map(self.reset().visit, node.elts))
yield "}" yield "}"
else: else:
...@@ -293,4 +293,4 @@ class ExpressionVisitor(NodeVisitor): ...@@ -293,4 +293,4 @@ class ExpressionVisitor(NodeVisitor):
# res_item_type = visitor.expr().visit(node.elt) # res_item_type = visitor.expr().visit(node.elt)
# for if_ in gen.ifs: # for if_ in gen.ifs:
# visitor.expr().visit(if_) # visitor.expr().visit(if_)
# return PyList(res_item_type) # return TyList(res_item_type)
\ No newline at end of file \ No newline at end of file
...@@ -63,7 +63,7 @@ class NodeVisitor(UniversalVisitor): ...@@ -63,7 +63,7 @@ class NodeVisitor(UniversalVisitor):
case types.TY_NONE: case types.TY_NONE:
yield "void" yield "void"
case types.TY_STR: case types.TY_STR:
yield "PyStr" yield "TyStr"
case types.TypeVariable(name): case types.TypeVariable(name):
raise UnresolvedTypeVariableError(node) raise UnresolvedTypeVariableError(node)
case _: case _:
......
...@@ -65,10 +65,10 @@ class NodeVisitor(UniversalVisitor): ...@@ -65,10 +65,10 @@ class NodeVisitor(UniversalVisitor):
elif node is TY_NONE: elif node is TY_NONE:
yield "void" yield "void"
elif node is TY_STR: elif node is TY_STR:
yield "PyStr" yield "TyStr"
elif isinstance(node, UserType): elif isinstance(node, UserType):
# if node.is_reference: # if node.is_reference:
# yield "PyObj<" # yield "TyObj<"
#yield "auto" #yield "auto"
yield f"referencemodel::Rc<__main____oo<>::{node.name}__oo<>::Obj>" yield f"referencemodel::Rc<__main____oo<>::{node.name}__oo<>::Obj>"
# if node.is_reference: # if node.is_reference:
......
...@@ -54,7 +54,7 @@ class ClassVisitor(NodeVisitor): ...@@ -54,7 +54,7 @@ class ClassVisitor(NodeVisitor):
yield "};" yield "};"
yield "template<typename... T> auto operator()(T&&... args) {" yield "template<typename... T> auto operator()(T&&... args) {"
yield "return pyobj<py_type>(std::forward<T>(args)...);" yield "return tyObj<py_type>(std::forward<T>(args)...);"
yield "}" yield "}"
# outer = ClassOuterVisitor(node.inner_scope) # outer = ClassOuterVisitor(node.inner_scope)
......
...@@ -72,14 +72,14 @@ class ExpressionVisitor(NodeVisitor): ...@@ -72,14 +72,14 @@ class ExpressionVisitor(NodeVisitor):
elif isinstance(node.value, float): elif isinstance(node.value, float):
yield repr(node.value) yield repr(node.value)
elif isinstance(node.value, complex): elif isinstance(node.value, complex):
yield f"PyComplex({node.value.real}, {node.value.imag})" yield f"TyComplex({node.value.real}, {node.value.imag})"
elif node.value is None: elif node.value is None:
yield "PyNone" yield "PyNone"
else: else:
raise NotImplementedError(node, type(node)) raise NotImplementedError(node, type(node))
def visit_Slice(self, node: ast.Slice) -> Iterable[str]: def visit_Slice(self, node: ast.Slice) -> Iterable[str]:
yield "PySlice(" yield "TySlice("
yield from join(", ", (self.visit(x or ast.Constant(value=None)) for x in (node.lower, node.upper, node.step))) yield from join(", ", (self.visit(x or ast.Constant(value=None)) for x in (node.lower, node.upper, node.step)))
yield ")" yield ")"
...@@ -228,7 +228,7 @@ class ExpressionVisitor(NodeVisitor): ...@@ -228,7 +228,7 @@ class ExpressionVisitor(NodeVisitor):
def visit_List(self, node: ast.List) -> Iterable[str]: def visit_List(self, node: ast.List) -> Iterable[str]:
if node.elts: if node.elts:
yield "typon::PyList{" yield "typon::TyList{"
yield from join(", ", map(self.reset().visit, node.elts)) yield from join(", ", map(self.reset().visit, node.elts))
yield "}" yield "}"
else: else:
...@@ -237,7 +237,7 @@ class ExpressionVisitor(NodeVisitor): ...@@ -237,7 +237,7 @@ class ExpressionVisitor(NodeVisitor):
def visit_Set(self, node: ast.Set) -> Iterable[str]: def visit_Set(self, node: ast.Set) -> Iterable[str]:
if node.elts: if node.elts:
yield "typon::PySet{" yield "typon::TySet{"
yield from join(", ", map(self.reset().visit, node.elts)) yield from join(", ", map(self.reset().visit, node.elts))
yield "}" yield "}"
else: else:
...@@ -328,4 +328,4 @@ class ExpressionVisitor(NodeVisitor): ...@@ -328,4 +328,4 @@ class ExpressionVisitor(NodeVisitor):
# res_item_type = visitor.expr().visit(node.elt) # res_item_type = visitor.expr().visit(node.elt)
# for if_ in gen.ifs: # for if_ in gen.ifs:
# visitor.expr().visit(if_) # visitor.expr().visit(if_)
# return PyList(res_item_type) # return TyList(res_item_type)
\ No newline at end of file \ No newline at end of file
...@@ -55,7 +55,7 @@ class FileVisitor(BlockVisitor): ...@@ -55,7 +55,7 @@ class FileVisitor(BlockVisitor):
yield "}" yield "}"
yield "#else" yield "#else"
yield "int main(int argc, char* argv[]) {" yield "int main(int argc, char* argv[]) {"
yield "py_sys::all.argv = typon::PyList<PyStr>(std::vector<PyStr>(argv, argv + argc));" yield "py_sys::all.argv = typon::TyList<TyStr>(std::vector<TyStr>(argv, argv + argc));"
yield "PROGRAMNS::__main__.root().call();" yield "PROGRAMNS::__main__.root().call();"
yield "}" yield "}"
yield "#endif" yield "#endif"
...@@ -310,4 +310,4 @@ class ScoperExprVisitor(ScoperVisitor): ...@@ -310,4 +310,4 @@ class ScoperExprVisitor(ScoperVisitor):
for if_ in gen.ifs: for if_ in gen.ifs:
visitor.expr().visit(if_) visitor.expr().visit(if_)
gen.ifs_node = ast.BoolOp(ast.And(), gen.ifs, **linenodata(node)) gen.ifs_node = ast.BoolOp(ast.And(), gen.ifs, **linenodata(node))
return PyList(node.item_type) return TyList(node.item_type)
\ No newline at end of file \ No newline at end of file
...@@ -58,7 +58,7 @@ def transpile(source, name: str, path: Path): ...@@ -58,7 +58,7 @@ def transpile(source, name: str, path: Path):
yield f"co_await dot(PROGRAMNS::{module.name()}, main)();" yield f"co_await dot(PROGRAMNS::{module.name()}, main)();"
yield "}" yield "}"
yield "int main(int argc, char* argv[]) {" yield "int main(int argc, char* argv[]) {"
yield "py_sys::all.argv = typon::PyList<PyStr>(std::vector<PyStr>(argv, argv + argc));" yield "py_sys::all.argv = typon::TyList<TyStr>(std::vector<TyStr>(argv, argv + argc));"
yield f"root().call();" yield f"root().call();"
yield "}" yield "}"
yield "#endif" yield "#endif"
......
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