Commit 776ac174 authored by Tom Niget's avatar Tom Niget

Format

parent c0f3f227
---
Language: Cpp Language: Cpp
BasedOnStyle: LLVM BasedOnStyle: LLVM
IndentWidth: 2 IndentWidth: 2
UseTab: Never UseTab: Never
\ No newline at end of file
---
\ No newline at end of file
...@@ -5,51 +5,43 @@ ...@@ -5,51 +5,43 @@
#ifndef TYPON_BUILTINS_HPP #ifndef TYPON_BUILTINS_HPP
#define TYPON_BUILTINS_HPP #define TYPON_BUILTINS_HPP
#include <ostream>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <ostream>
#include <string> #include <string>
using namespace std::literals; using namespace std::literals;
// typon_len
template <typename T>
template<typename T>
concept PyIterator = requires(T t) { concept PyIterator = requires(T t) {
{ t.py_next() } -> std::same_as<std::optional<T>>; { t.py_next() } -> std::same_as<std::optional<T>>;
}; };
template<typename T> template <typename T>
concept PyIterable = requires(T t) { concept PyIterable = requires(T t) {
{ t.py_iter() } -> PyIterator; { t.py_iter() } -> PyIterator;
}; };
template<PyIterable T, PyIterator U> template <PyIterable T, PyIterator U> U iter(const T &t) { return t.py_iter(); }
U iter(const T &t) {
return t.py_iter();
}
template<typename T> template <typename T>
concept PyLen = requires(const T &t) { concept PyLen = requires(const T &t) {
{ t.py_len() } -> std::same_as<size_t>; { t.py_len() } -> std::same_as<size_t>;
}; };
template<PyLen T> template <PyLen T> size_t len(const T &t) { return t.py_len(); }
size_t len(const T &t) {
return t.py_len();
}
bool is_cpp() { bool is_cpp() { return true; }
return true;
}
#include "builtins/bool.hpp" #include "builtins/bool.hpp"
#include "builtins/complex.hpp" #include "builtins/complex.hpp"
#include "builtins/dict.hpp" #include "builtins/dict.hpp"
#include "builtins/list.hpp" #include "builtins/list.hpp"
#include "builtins/print.hpp" #include "builtins/print.hpp"
#include "builtins/range.hpp"
#include "builtins/set.hpp" #include "builtins/set.hpp"
#include "builtins/str.hpp" #include "builtins/str.hpp"
#endif //TYPON_BUILTINS_HPP #endif // TYPON_BUILTINS_HPP
...@@ -9,9 +9,8 @@ ...@@ -9,9 +9,8 @@
#include "print.hpp" #include "print.hpp"
template<> template <> void print_to<bool>(const bool &x, std::ostream &s) {
void print_to<bool>(const bool &x, std::ostream &s) {
s << (x ? "True" : "False"); s << (x ? "True" : "False");
} }
#endif //TYPON_BOOL_HPP #endif // TYPON_BOOL_HPP
...@@ -12,16 +12,11 @@ ...@@ -12,16 +12,11 @@
using PyComplex = std::complex<double>; using PyComplex = std::complex<double>;
PyComplex operator+(int a, const PyComplex &b) { PyComplex operator+(int a, const PyComplex &b) { return PyComplex(a) + b; }
return PyComplex(a) + b;
}
PyComplex operator-(int a, const PyComplex &b) { PyComplex operator-(int a, const PyComplex &b) { return PyComplex(a) - b; }
return PyComplex(a) - b;
}
template<> template <> void print_to<PyComplex>(const PyComplex &x, std::ostream &s) {
void print_to<PyComplex>(const PyComplex &x, std::ostream &s) {
if (x.real() == 0) { if (x.real() == 0) {
s << x.imag() << "j"; s << x.imag() << "j";
} else { } else {
...@@ -29,4 +24,4 @@ void print_to<PyComplex>(const PyComplex &x, std::ostream &s) { ...@@ -29,4 +24,4 @@ void print_to<PyComplex>(const PyComplex &x, std::ostream &s) {
} }
} }
#endif //TYPON_COMPLEX_HPP #endif // TYPON_COMPLEX_HPP
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
#include <unordered_map> #include <unordered_map>
template<typename K, typename V> template <typename K, typename V>
class PyDict : public std::unordered_map<K, V> { class PyDict : public std::unordered_map<K, V> {
public: public:
PyDict(std::unordered_map<K, V> &&m) : std::unordered_map<K, V>(std::move(m)) {} PyDict(std::unordered_map<K, V> &&m)
: std::unordered_map<K, V>(std::move(m)) {}
PyDict(std::initializer_list<std::pair<const K, V>> m) : std::unordered_map<K, V>(m) {} PyDict(std::initializer_list<std::pair<const K, V>> m)
: std::unordered_map<K, V>(m) {}
operator std::unordered_map<K, V>() const { operator std::unordered_map<K, V>() const {
return std::unordered_map<K, V>(this->begin(), this->end()); return std::unordered_map<K, V>(this->begin(), this->end());
...@@ -22,13 +24,9 @@ public: ...@@ -22,13 +24,9 @@ public:
return *reinterpret_cast<std::unordered_map<K, V> *>(this); return *reinterpret_cast<std::unordered_map<K, V> *>(this);
} }
std::size_t py_len() const { std::size_t py_len() const { return this->size(); }
return this->size();
}
bool py_contains(const K &k) const { bool py_contains(const K &k) const { return this->find(k) != this->end(); }
return this->find(k) != this->end();
}
class iterator { class iterator {
public: public:
...@@ -51,29 +49,19 @@ public: ...@@ -51,29 +49,19 @@ public:
return tmp; return tmp;
} }
bool operator==(const iterator &rhs) const { bool operator==(const iterator &rhs) const { return _it == rhs._it; }
return _it == rhs._it;
}
bool operator!=(const iterator &rhs) const { bool operator!=(const iterator &rhs) const { return _it != rhs._it; }
return _it != rhs._it;
}
const std::pair<K, V> &operator*() const { const std::pair<K, V> &operator*() const { return *_it; }
return *_it;
}
const std::pair<K, V> *operator->() const { const std::pair<K, V> *operator->() const { return &*_it; }
return &*_it;
}
private: private:
typename std::unordered_map<K, V>::iterator _it; typename std::unordered_map<K, V>::iterator _it;
}; };
iterator py_iter() const { iterator py_iter() const { return this->begin(); }
return this->begin();
}
void py_print(std::ostream &s) const { void py_print(std::ostream &s) const {
s << '{'; s << '{';
...@@ -92,8 +80,7 @@ public: ...@@ -92,8 +80,7 @@ public:
} }
}; };
template<typename K, typename V> template <typename K, typename V>
PyDict(std::initializer_list<std::pair<K, V>>) -> PyDict<K, V>; PyDict(std::initializer_list<std::pair<K, V>>) -> PyDict<K, V>;
#endif // TYPON_DICT_HPP
#endif //TYPON_DICT_HPP
...@@ -5,11 +5,12 @@ ...@@ -5,11 +5,12 @@
#ifndef TYPON_LIST_HPP #ifndef TYPON_LIST_HPP
#define TYPON_LIST_HPP #define TYPON_LIST_HPP
#include <vector>
#include <ostream> #include <ostream>
#include <vector>
template<typename T> // TODO: Hyrum's Law, maybe consider using composition instead to not expose
class PyList : public std::vector<T> { // that this is a std::vector?
template <typename T> class PyList : public std::vector<T> {
public: public:
PyList(std::vector<T> &&v) : std::vector<T>(std::move(v)) {} PyList(std::vector<T> &&v) : std::vector<T>(std::move(v)) {}
...@@ -23,9 +24,7 @@ public: ...@@ -23,9 +24,7 @@ public:
return *reinterpret_cast<std::vector<T> *>(this); return *reinterpret_cast<std::vector<T> *>(this);
} }
size_t py_len() const { size_t py_len() const { return this->size(); }
return this->size();
}
void py_print(std::ostream &s) const { void py_print(std::ostream &s) const {
s << '['; s << '[';
...@@ -57,9 +56,8 @@ public: ...@@ -57,9 +56,8 @@ public:
} }
}; };
template<typename T> template <typename T> PyList<T> list(std::initializer_list<T> &&v) {
PyList<T> list(std::initializer_list<T> &&v) {
return PyList<T>(std::move(v)); return PyList<T>(std::move(v));
} }
#endif //TYPON_LIST_HPP #endif // TYPON_LIST_HPP
...@@ -8,50 +8,44 @@ ...@@ -8,50 +8,44 @@
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
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 &>;
}; };
template<Streamable T> template <Streamable T> void print_to(const T &x, std::ostream &s) { s << x; }
void print_to(const T &x, std::ostream &s) {
s << x;
}
template<typename T> template <typename T>
concept FunctionPointer = std::is_function_v<T> concept FunctionPointer =
or std::is_member_function_pointer_v<T> std::is_function_v<T> or std::is_member_function_pointer_v<T> or
or std::is_function_v<std::remove_pointer_t<T>>; std::is_function_v<std::remove_pointer_t<T>>;
template<Streamable T> template <Streamable T>
requires (FunctionPointer<T>) requires(FunctionPointer<T>)
void print_to(const T &x, std::ostream &s) { void print_to(const T &x, std::ostream &s) {
s << "<function at 0x" << std::hex << (size_t) x << ">"; s << "<function at 0x" << std::hex << (size_t)x << ">";
} }
template<typename T> template <typename T>
concept PyPrint = requires(const T &x, std::ostream &s) { concept PyPrint = requires(const T &x, std::ostream &s) {
{ x.py_print(s) } -> std::same_as<void>; { x.py_print(s) } -> std::same_as<void>;
}; };
template<PyPrint T> template <PyPrint T> void print_to(const T &x, std::ostream &s) {
void print_to(const T &x, std::ostream &s) {
x.py_print(s); x.py_print(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) {
{ 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) { void print(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() { void print() { std::cout << '\n'; }
std::cout << '\n'; #endif // TYPON_PRINT_HPP
}
#endif //TYPON_PRINT_HPP
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
#include <unordered_set> #include <unordered_set>
template<typename T> template <typename T> class PySet : public std::unordered_set<T> {
class PySet : public std::unordered_set<T> {
public: public:
PySet(std::unordered_set<T> &&s) : std::unordered_set<T>(std::move(s)) {} PySet(std::unordered_set<T> &&s) : std::unordered_set<T>(std::move(s)) {}
...@@ -18,13 +17,9 @@ public: ...@@ -18,13 +17,9 @@ public:
return std::unordered_set<T>(this->begin(), this->end()); return std::unordered_set<T>(this->begin(), this->end());
} }
std::size_t py_len() const { std::size_t py_len() const { return this->size(); }
return this->size();
}
bool py_contains(const T &t) const { bool py_contains(const T &t) const { return this->find(t) != this->end(); }
return this->find(t) != this->end();
}
class iterator { class iterator {
public: public:
...@@ -47,29 +42,19 @@ public: ...@@ -47,29 +42,19 @@ public:
return tmp; return tmp;
} }
bool operator==(const iterator &rhs) const { bool operator==(const iterator &rhs) const { return _it == rhs._it; }
return _it == rhs._it;
}
bool operator!=(const iterator &rhs) const { bool operator!=(const iterator &rhs) const { return _it != rhs._it; }
return _it != rhs._it;
}
const T &operator*() const { const T &operator*() const { return *_it; }
return *_it;
}
const T *operator->() const { const T *operator->() const { return &*_it; }
return &*_it;
}
private: private:
typename std::unordered_set<T>::iterator _it; typename std::unordered_set<T>::iterator _it;
}; };
iterator py_iter() const { iterator py_iter() const { return this->begin(); }
return this->begin();
}
void py_print(std::ostream &s) const { void py_print(std::ostream &s) const {
s << '{'; s << '{';
...@@ -84,9 +69,8 @@ public: ...@@ -84,9 +69,8 @@ public:
} }
}; };
template<typename T> template <typename T> PySet<T> set(std::initializer_list<T> &&s) {
PySet<T> set(std::initializer_list<T> &&s) {
return PySet<T>(std::move(s)); return PySet<T>(std::move(s));
} }
#endif //TYPON_SET_HPP #endif // TYPON_SET_HPP
...@@ -5,16 +5,15 @@ ...@@ -5,16 +5,15 @@
#ifndef TYPON_STR_HPP #ifndef TYPON_STR_HPP
#define TYPON_STR_HPP #define TYPON_STR_HPP
#include <string>
#include <sstream> #include <sstream>
#include <string>
using namespace std::literals; using namespace std::literals;
template<typename T> template <typename T> std::string str(const T &x) {
std::string str(const T &x) {
std::stringstream s; std::stringstream s;
print_to(x, s); print_to(x, s);
return s.str(); return s.str();
} }
#endif //TYPON_STR_HPP #endif // TYPON_STR_HPP
...@@ -8,11 +8,11 @@ ...@@ -8,11 +8,11 @@
#include <iostream> #include <iostream>
namespace py_sys { namespace py_sys {
struct sys_t { 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;
} all; } all;
} } // namespace py_sys
#endif //TYPON_SYS_HPP #endif // TYPON_SYS_HPP
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