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