Commit 04557080 authored by Tom Niget's avatar Tom Niget

Update

parent 2355cf46
...@@ -98,10 +98,7 @@ auto len(const T &t) { ...@@ -98,10 +98,7 @@ auto len(const T &t) {
template <PyLen T> size_t len(const T &t) { return t.py_len(); }*/ template <PyLen T> size_t len(const T &t) { return t.py_len(); }*/
template<typename T> template<typename T> auto len(T&& t) { return dot(std::forward<T>(t), oo__len__oo)(); }
auto len(const T& t) {
return dot(t, oo__len__oo)();
}
template <typename T> template <typename T>
concept PyNext = requires(T t) { concept PyNext = requires(T t) {
......
...@@ -93,10 +93,11 @@ struct TyInt__oo : classtype<_Base0, TyInt__oo<>> { ...@@ -93,10 +93,11 @@ struct TyInt__oo : classtype<_Base0, TyInt__oo<>> {
int value; int value;
Obj(int value = 0) : value(value) {} Obj(int value = 0) : value(value) {}
Obj(const Obj &other) : value(other.value) {}
operator int() const { return value; } operator int() const { return value; }
}; };
auto operator()(int value) const { return Obj(value); } auto operator()(auto value) const { return Obj(value); }
}; };
static constexpr TyInt__oo<> TyInt{}; static constexpr TyInt__oo<> TyInt{};
......
...@@ -17,9 +17,7 @@ template <typename _Base0 = object> ...@@ -17,9 +17,7 @@ template <typename _Base0 = object>
struct TyList__oo : classtype<_Base0, TyList__oo<>> { struct TyList__oo : classtype<_Base0, TyList__oo<>> {
struct : method { struct : method {
auto operator()(auto self) const { auto operator()(auto self) const { return TyList__oo<>{}(self->_v); }
return TyList__oo<>{}(self->_v);
}
} static constexpr copy{}; } static constexpr copy{};
struct : method { struct : method {
...@@ -71,17 +69,17 @@ struct TyList__oo : classtype<_Base0, TyList__oo<>> { ...@@ -71,17 +69,17 @@ struct TyList__oo : classtype<_Base0, TyList__oo<>> {
} static constexpr oo__len__oo{}; } static constexpr oo__len__oo{};
// getitem // getitem
struct : method { struct : method {
auto operator()(auto self, auto i) const { auto operator()(auto self, auto i) const {
if (i < 0) { if (i < 0) {
i += self->_v->size(); i += TyInt(self->_v->size());
} }
if (i < 0 || i >= self->_v->size()) { if (i < 0 || i >= self->_v->size()) {
throw std::out_of_range("list index out of range"); throw std::out_of_range("list index out of range");
} }
return self->_v->operator[](i); return self->_v->operator[](i);
} }
} static constexpr oo__getitem__oo{}; } static constexpr oo__getitem__oo{};
template <typename T> struct Obj : instance<TyList__oo<>, Obj<T>> { template <typename T> struct Obj : instance<TyList__oo<>, Obj<T>> {
using value_type = T; using value_type = T;
......
...@@ -174,8 +174,8 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> { ...@@ -174,8 +174,8 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> {
} static constexpr oo__repr__oo{}; } static constexpr oo__repr__oo{};
struct : method { struct : method {
auto operator()(auto self) const { return (self->value.size()); } auto operator()(auto self) const { return (self->value.size()); }
} static constexpr oo__len__oo{}; } static constexpr oo__len__oo{};
struct Obj : value<TyStr__oo<>, Obj> { struct Obj : value<TyStr__oo<>, Obj> {
std::string value; std::string value;
......
...@@ -1052,10 +1052,17 @@ namespace meta { ...@@ -1052,10 +1052,17 @@ namespace meta {
concept Right##DUNDER##able = requires(Left left, Right right) { \ concept Right##DUNDER##able = requires(Left left, Right right) { \
right->oo__r##DUNDER##__oo(right, left); \ right->oo__r##DUNDER##__oo(right, left); \
}; \ }; \
template <typename Left, typename Right> \
concept Aug##DUNDER##able = requires(Left left, Right right) { \
left->oo__i##DUNDER##__oo(left, right); \
}; \
\ \
template <typename Left, typename Right> \ template <typename Left, typename Right> \
concept DUNDER##able = \ concept DUNDER##able = \
Left##DUNDER##able<Left, Right> || Right##DUNDER##able<Left, Right>; \ Left##DUNDER##able<Left, Right> || Right##DUNDER##able<Left, Right>; \
template <typename Left, typename Right> \
concept NormalOrAug##DUNDER##able = \
DUNDER##able<Left, Right> || Aug##DUNDER##able<Left, Right>; \
} \ } \
template <meta::object Left, meta::object Right> \ template <meta::object Left, meta::object Right> \
requires meta::DUNDER \ requires meta::DUNDER \
...@@ -1069,6 +1076,16 @@ namespace meta { ...@@ -1069,6 +1076,16 @@ namespace meta {
oo__r##DUNDER##__oo)(std::forward<Left>(left)); \ oo__r##DUNDER##__oo)(std::forward<Left>(left)); \
} \ } \
} \ } \
} \
template <meta::object Left, meta::object Right> \
requires meta::NormalOrAug \
##DUNDER##able<Left, Right> auto operator OP##=(Left &&left, Right &&right) { \
if constexpr (meta::Aug##DUNDER##able<Left, Right>) { \
return dot(std::forward<Left>(left), \
oo__i##DUNDER##__oo)(std::forward<Right>(right)); \
} else { \
return (left = (left OP right)); \
} \
} }
#define SIMPLE_OP(OP, DUNDER) \ #define SIMPLE_OP(OP, DUNDER) \
...@@ -1127,8 +1144,8 @@ LR_OP(%, mod) ...@@ -1127,8 +1144,8 @@ LR_OP(%, mod)
LR_OP(<<, lshift) LR_OP(<<, lshift)
LR_OP(>>, rshift) LR_OP(>>, rshift)
LR_OP(&, and) LR_OP(&, and)
LR_OP(|, or)
LR_OP(^, xor) LR_OP(^, xor)
LR_OP(|, or)
// TODO: iadd... // TODO: iadd...
...@@ -1139,23 +1156,21 @@ SIMPLE_OP(!=, ne) ...@@ -1139,23 +1156,21 @@ SIMPLE_OP(!=, ne)
SIMPLE_OP(>, gt) SIMPLE_OP(>, gt)
SIMPLE_OP(>=, ge) SIMPLE_OP(>=, ge)
namespace meta { namespace meta {
template <typename Left, typename Right> template <typename Left, typename Right>
concept DUNDERgetitemable = requires(Left left, Right right) { concept DUNDERgetitemable =
left->oo__getitem__oo(left, right); requires(Left left, Right right) { left->oo__getitem__oo(left, right); };
}; template <typename Left, typename Right>
template <typename Left, typename Right> concept DUNDERsetitemable =
concept DUNDERsetitemable = requires(Left left, Right right) { requires(Left left, Right right) { left->oo__setitem__oo(left, right); };
left->oo__setitem__oo(left, right);
};
} } // namespace meta
/* template <meta::object Left, meta::object Right> /* template <meta::object Left, meta::object Right>
requires meta::DUNDERgetitemable<Left, Right> auto operator [](Left &&left, Right &&right) { requires meta::DUNDERgetitemable<Left, Right> auto operator [](Left &&left,
return dot(std::forward<Left>(left), Right &&right) { return dot(std::forward<Left>(left),
oo__getitem__oo)(std::forward<Right>(right)); oo__getitem__oo)(std::forward<Right>(right));
}*/ }*/
// todo: setitem? // todo: setitem?
} // namespace referencemodel } // namespace referencemodel
......
...@@ -27,7 +27,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> { ...@@ -27,7 +27,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
template <typename _Base0 = object> template <typename _Base0 = object>
struct socket_t__oo : referencemodel::classtype<_Base0, socket_t__oo<>> { struct socket_t__oo : referencemodel::classtype<_Base0, socket_t__oo<>> {
template<typename T> template <typename T>
struct Obj : referencemodel::instance<socket_t__oo<>, Obj<T>> { struct Obj : referencemodel::instance<socket_t__oo<>, Obj<T>> {
Obj(int fd = -1) : fd(fd) {} Obj(int fd = -1) : fd(fd) {}
...@@ -37,7 +37,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> { ...@@ -37,7 +37,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
}; };
struct : referencemodel::method { struct : referencemodel::method {
template<typename T=void> template <typename T = void>
auto operator()(auto self) -> typon::Task< auto operator()(auto self) -> typon::Task<
std::tuple<decltype(rc(Obj<T>{-1})), decltype(""_ps)>> const { std::tuple<decltype(rc(Obj<T>{-1})), decltype(""_ps)>> const {
int connfd = co_await typon::io::accept(self->fd, NULL, NULL); int connfd = co_await typon::io::accept(self->fd, NULL, NULL);
...@@ -49,7 +49,6 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> { ...@@ -49,7 +49,6 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
} }
} static constexpr accept{}; } static constexpr accept{};
struct : referencemodel::method { struct : referencemodel::method {
auto operator()(auto self, int level, int optname, int optval) const { auto operator()(auto self, int level, int optname, int optval) const {
if (::setsockopt(self->fd, level, optname, &optval, sizeof(int)) < 0) { if (::setsockopt(self->fd, level, optname, &optval, sizeof(int)) < 0) {
...@@ -138,8 +137,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> { ...@@ -138,8 +137,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
} }
})*/ })*/
template<typename T=void> template <typename T = void> auto operator()(int family, int type_) const {
auto operator()(int family, int type_) const {
if (int fd = ::socket(family, type_, 0); fd >= 0) { if (int fd = ::socket(family, type_, 0); fd >= 0) {
return rc(Obj<T>{fd}); return rc(Obj<T>{fd});
} else { } else {
......
...@@ -47,11 +47,12 @@ def server_loop(sockfd, filepath): ...@@ -47,11 +47,12 @@ def server_loop(sockfd, filepath):
if __name__ == "__main__": if __name__ == "__main__":
PORT = 8000 PORT = 8000
if len(sys.argv) > 2: # if len(sys.argv) > 2:
print("Usage: webserver [ filepath ]") # print("Usage: webserver [ filepath ]")
sys.exit(1) # sys.exit(1)
# filepath = sys.argv[1] if len(sys.argv) == 2 else "requirements.txt" l = len(sys.argv)
filepath = sys.argv[1] if l == 2 else "requirements.txt"
filepath = "requirements.txt" filepath = "requirements.txt"
print("Serving", filepath, "on port", PORT) print("Serving", filepath, "on port", PORT)
......
# coding: utf-8
import ast
from transpiler.phases.utils import make_lnd
from transpiler.utils import linenodata
class DesugarSubscript(ast.NodeTransformer):
def visit_Subscript(self, node: ast.Subscript):
match node.ctx:
case ast.Load():
return ast.Call(
func=ast.Attribute(
value=node.value,
attr="__getitem__",
ctx=ast.Load(),
),
args=[node.slice],
keywords=[],
**linenodata(node)
)
case ast.Store(), ast.Del():
raise NotImplementedError("Subscript assignment and deletion not supported")
case _:
raise ValueError(f"Unexpected context {node.ctx!r}", linenodata(node))
\ No newline at end of file
...@@ -53,7 +53,8 @@ class NodeVisitor(UniversalVisitor): ...@@ -53,7 +53,8 @@ class NodeVisitor(UniversalVisitor):
def fix_name(self, name: str) -> str: def fix_name(self, name: str) -> str:
if name.startswith("__") and name.endswith("__"): if name.startswith("__") and name.endswith("__"):
return f"py_{name[2:-2]}" # return f"py_{name[2:-2]}"
return f"oo{name}oo"
return MAPPINGS.get(name, name) return MAPPINGS.get(name, name)
def visit_BaseType(self, node: BaseType) -> Iterable[str]: def visit_BaseType(self, node: BaseType) -> Iterable[str]:
......
...@@ -10,6 +10,7 @@ import colorful as cf ...@@ -10,6 +10,7 @@ import colorful as cf
from transpiler import error_display from transpiler import error_display
from transpiler.phases.desugar_compare import DesugarCompare from transpiler.phases.desugar_compare import DesugarCompare
from transpiler.phases.desugar_op import DesugarOp from transpiler.phases.desugar_op import DesugarOp
from transpiler.phases.desugar_subscript import DesugarSubscript
from transpiler.phases.desugar_with import DesugarWith from transpiler.phases.desugar_with import DesugarWith
from transpiler.phases.emit_cpp.module import emit_module from transpiler.phases.emit_cpp.module import emit_module
from transpiler.phases.if_main import IfMainVisitor from transpiler.phases.if_main import IfMainVisitor
...@@ -36,6 +37,7 @@ def transpile(source, name: str, path: Path): ...@@ -36,6 +37,7 @@ def transpile(source, name: str, path: Path):
node = DesugarWith().visit(node) node = DesugarWith().visit(node)
node = DesugarCompare().visit(node) node = DesugarCompare().visit(node)
node = DesugarOp().visit(node) node = DesugarOp().visit(node)
node = DesugarSubscript().visit(node)
return node return node
module = parse_module(path.stem, [path.parent, TYPON_STD], preprocess=preprocess) module = parse_module(path.stem, [path.parent, TYPON_STD], preprocess=preprocess)
......
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