Commit 04557080 authored by Tom Niget's avatar Tom Niget

Update

parent 2355cf46
......@@ -98,10 +98,7 @@ auto len(const T &t) {
template <PyLen T> size_t len(const T &t) { return t.py_len(); }*/
template<typename T>
auto len(const T& t) {
return dot(t, oo__len__oo)();
}
template<typename T> auto len(T&& t) { return dot(std::forward<T>(t), oo__len__oo)(); }
template <typename T>
concept PyNext = requires(T t) {
......
......@@ -93,10 +93,11 @@ struct TyInt__oo : classtype<_Base0, TyInt__oo<>> {
int value;
Obj(int value = 0) : value(value) {}
Obj(const Obj &other) : value(other.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{};
......
......@@ -17,9 +17,7 @@ template <typename _Base0 = object>
struct TyList__oo : classtype<_Base0, TyList__oo<>> {
struct : method {
auto operator()(auto self) const {
return TyList__oo<>{}(self->_v);
}
auto operator()(auto self) const { return TyList__oo<>{}(self->_v); }
} static constexpr copy{};
struct : method {
......@@ -71,17 +69,17 @@ struct TyList__oo : classtype<_Base0, TyList__oo<>> {
} static constexpr oo__len__oo{};
// getitem
struct : method {
auto operator()(auto self, auto i) const {
if (i < 0) {
i += self->_v->size();
}
if (i < 0 || i >= self->_v->size()) {
throw std::out_of_range("list index out of range");
}
return self->_v->operator[](i);
}
} static constexpr oo__getitem__oo{};
struct : method {
auto operator()(auto self, auto i) const {
if (i < 0) {
i += TyInt(self->_v->size());
}
if (i < 0 || i >= self->_v->size()) {
throw std::out_of_range("list index out of range");
}
return self->_v->operator[](i);
}
} static constexpr oo__getitem__oo{};
template <typename T> struct Obj : instance<TyList__oo<>, Obj<T>> {
using value_type = T;
......
......@@ -174,8 +174,8 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> {
} static constexpr oo__repr__oo{};
struct : method {
auto operator()(auto self) const { return (self->value.size()); }
} static constexpr oo__len__oo{};
auto operator()(auto self) const { return (self->value.size()); }
} static constexpr oo__len__oo{};
struct Obj : value<TyStr__oo<>, Obj> {
std::string value;
......
......@@ -1052,10 +1052,17 @@ namespace meta {
concept Right##DUNDER##able = requires(Left left, Right right) { \
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> \
concept DUNDER##able = \
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> \
requires meta::DUNDER \
......@@ -1069,6 +1076,16 @@ namespace meta {
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) \
......@@ -1127,8 +1144,8 @@ LR_OP(%, mod)
LR_OP(<<, lshift)
LR_OP(>>, rshift)
LR_OP(&, and)
LR_OP(|, or)
LR_OP(^, xor)
LR_OP(|, or)
// TODO: iadd...
......@@ -1139,23 +1156,21 @@ SIMPLE_OP(!=, ne)
SIMPLE_OP(>, gt)
SIMPLE_OP(>=, ge)
namespace meta {
template <typename Left, typename Right>
concept DUNDERgetitemable = requires(Left left, Right right) {
left->oo__getitem__oo(left, right);
};
template <typename Left, typename Right>
concept DUNDERsetitemable = requires(Left left, Right right) {
left->oo__setitem__oo(left, right);
};
namespace meta {
template <typename Left, typename Right>
concept DUNDERgetitemable =
requires(Left left, Right right) { left->oo__getitem__oo(left, right); };
template <typename Left, typename Right>
concept DUNDERsetitemable =
requires(Left left, Right right) { left->oo__setitem__oo(left, right); };
}
/* template <meta::object Left, meta::object Right>
requires meta::DUNDERgetitemable<Left, Right> auto operator [](Left &&left, Right &&right) {
return dot(std::forward<Left>(left),
oo__getitem__oo)(std::forward<Right>(right));
}*/
// todo: setitem?
} // namespace meta
/* template <meta::object Left, meta::object Right>
requires meta::DUNDERgetitemable<Left, Right> auto operator [](Left &&left,
Right &&right) { return dot(std::forward<Left>(left),
oo__getitem__oo)(std::forward<Right>(right));
}*/
// todo: setitem?
} // namespace referencemodel
......
......@@ -27,7 +27,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
template <typename _Base0 = object>
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>> {
Obj(int fd = -1) : fd(fd) {}
......@@ -37,7 +37,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
};
struct : referencemodel::method {
template<typename T=void>
template <typename T = void>
auto operator()(auto self) -> typon::Task<
std::tuple<decltype(rc(Obj<T>{-1})), decltype(""_ps)>> const {
int connfd = co_await typon::io::accept(self->fd, NULL, NULL);
......@@ -49,7 +49,6 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
}
} static constexpr accept{};
struct : referencemodel::method {
auto operator()(auto self, int level, int optname, int optval) const {
if (::setsockopt(self->fd, level, optname, &optval, sizeof(int)) < 0) {
......@@ -138,8 +137,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
}
})*/
template<typename T=void>
auto operator()(int family, int type_) const {
template <typename T = void> auto operator()(int family, int type_) const {
if (int fd = ::socket(family, type_, 0); fd >= 0) {
return rc(Obj<T>{fd});
} else {
......
......@@ -47,11 +47,12 @@ def server_loop(sockfd, filepath):
if __name__ == "__main__":
PORT = 8000
if len(sys.argv) > 2:
print("Usage: webserver [ filepath ]")
sys.exit(1)
# if len(sys.argv) > 2:
# print("Usage: webserver [ filepath ]")
# 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"
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):
def fix_name(self, name: str) -> str:
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)
def visit_BaseType(self, node: BaseType) -> Iterable[str]:
......
......@@ -10,6 +10,7 @@ import colorful as cf
from transpiler import error_display
from transpiler.phases.desugar_compare import DesugarCompare
from transpiler.phases.desugar_op import DesugarOp
from transpiler.phases.desugar_subscript import DesugarSubscript
from transpiler.phases.desugar_with import DesugarWith
from transpiler.phases.emit_cpp.module import emit_module
from transpiler.phases.if_main import IfMainVisitor
......@@ -36,6 +37,7 @@ def transpile(source, name: str, path: Path):
node = DesugarWith().visit(node)
node = DesugarCompare().visit(node)
node = DesugarOp().visit(node)
node = DesugarSubscript().visit(node)
return node
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