Commit 0cff3aae authored by Tom Niget's avatar Tom Niget

Proper type emission for annotated assign, fix socket

parent bbcef874
......@@ -93,6 +93,7 @@ struct TyInt__oo : classtype<_Base0, TyInt__oo<>> {
int value;
Obj(int value = 0) : value(value) {}
Obj(std::string_view value) : value(std::stoi(std::string(value))) {}
Obj(const Obj &other) : value(other.value) {}
operator int() const { return value; }
};
......
......@@ -19,21 +19,29 @@ struct TyMutex__oo : classtype<_Base0, TyMutex__oo<>> {
struct Obj : instance<TyMutex__oo<>, Obj<T>> {
typon::Mutex mutex;
T val;
Obj() {
print("Obj()"_ps);
}
};
struct : method {
auto operator()(auto self, auto callback) -> typon::Task<TyNone> const {
print("a"_ps);
auto lock = dot(self, mutex).lock();
print("b"_ps);
co_await lock;
print("c"_ps);
co_await callback(dot(self, val));
print("d"_ps);
co_return {};
}
} static constexpr when{};
template<typename T>
auto operator()(T val) const {
auto obj = arc(Obj<T>());
dot(obj, val) = val;
auto obj = referencemodel::meta::arc<Obj<T>>();
//dot(obj, val) = val;
return obj;
}
};
......
......@@ -10,7 +10,20 @@
#include <stdint.h>
#include <utility>
struct TySlice {
namespace typon {
using namespace referencemodel;
template <typename _Base0 = object>
struct TySlice__oo : classtype<_Base0, TySlice__oo<>> {
static constexpr std::string_view name = "TySlice";
auto operator()() const {}
};
static constexpr TySlice__oo<> TySlice{};
}
/*struct TySlice {
TySlice() = default;
TySlice(const TySlice &) = default;
TySlice(TySlice &&) = default;
......@@ -59,6 +72,6 @@ struct TySlice {
return {len, res};
}
};
};*/
#endif // TYPON_SLICE_HPP
......@@ -164,9 +164,41 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
system_error(errno, "socket()");
}
}
using ObjType = Obj<void>;
};
static constexpr socket_t__oo<> socket{};
struct : referencemodel::staticmethod {
auto operator()(std::string host, int port, int family = 0, int type_ = 0,
int proto = 0, int flags = 0) const {
addrinfo hints;
std::memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = type_;
hints.ai_protocol = proto;
hints.ai_flags = flags;
addrinfo *res;
// convert port to string
std::string port_str = std::to_string(port);
if (int err = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &res);
err != 0) {
system_error(err, "getaddrinfo()");
}
auto rlist = typon::TyList({
// make tuple (family, type, proto, canonname, sockaddr)
// (int, int, int, str, str)
std::make_tuple(
typon::TyInt(res->ai_family), typon::TyInt(res->ai_socktype),
typon::TyInt(res->ai_protocol),
typon::TyStr(res->ai_canonname ? res->ai_canonname : ""),
typon::TyStr(res->ai_addr ? res->ai_addr->sa_data : ""))
});
::freeaddrinfo(res);
return rlist;
}
} static constexpr getaddrinfo{};
/*FUNCTION(auto, getaddrinfo,
(std::string host, int port, int family = 0, int type_ = 0,
int proto = 0, int flags = 0),
......
......@@ -16,7 +16,7 @@ class int:
def __and__(self, other: Self) -> Self: ...
def __neg__(self) -> Self: ...
def __init__(self, x: object) -> None: ...
def __init__[T](self, x: T) -> None: ...
def __lt__(self, other: Self) -> bool: ...
def __gt__(self, other: Self) -> bool: ...
def __mod__(self, other: Self) -> Self: ...
......
......@@ -20,7 +20,9 @@ class Actor[T]:
mutex: Mutex[T]
def __init__(self, val: T):
print("l")
self.mutex = Mutex(val)
print("m")
# def when(self, f: Callable[[T], object]):
# return future(lambda: self.mutex.when(f))
......@@ -29,9 +31,11 @@ def thing(x):
print("hello", x)
if __name__ == "__main__":
act = Actor(123)
act.mutex.when(thing)
print("j")
# act = Actor(123)
m = Mutex(123)
print("k")
#act.mutex.when(thing)
# class Actor[T]:
# def __init__(self):
......
# norun
# nocompile
import sys
from socket import socket, getaddrinfo, AF_UNIX, SOCK_STREAM
......@@ -9,7 +8,9 @@ if __name__ == "__main__":
if len(sys.argv) == 3:
host = sys.argv[1]
port = sys.argv[2]
family, _, _, _, addr = getaddrinfo(host, int(port))[0]
addrinfo = getaddrinfo(host, int(port))[0]
family = addrinfo[0]
addr = addrinfo[4]
s = socket(family, SOCK_STREAM)
s.connect(addr)
elif len(sys.argv) == 2:
......
......@@ -5,7 +5,8 @@ from typing import Iterable
from transpiler.phases.emit_cpp.visitors import NodeVisitor, CoroutineMode, join
from transpiler.phases.typing.scope import Scope
from transpiler.phases.typing.types import ClassTypeType, TupleInstanceType, TY_FUTURE, ResolvedConcreteType, TY_FORKED
from transpiler.phases.typing.types import ClassTypeType, TupleInstanceType, TY_FUTURE, ResolvedConcreteType, TY_FORKED, \
GenericInstanceType
from transpiler.phases.utils import make_lnd
from transpiler.utils import linenodata
......@@ -182,11 +183,12 @@ class ExpressionVisitor(NodeVisitor):
if isinstance(node.func.type, ClassTypeType):
inner = node.func.type.inner_type
assert inner is node.type.generic_parent
yield ".template operator()"
yield "<"
yield from join(", ", (self.visit(arg) for arg in node.type.generic_args))
yield ">"
if isinstance(node.type, GenericInstanceType):
assert inner is node.type.generic_parent
yield ".template operator()"
yield "<"
yield from join(", ", (self.visit(arg) for arg in node.type.generic_args))
yield ">"
yield "("
......
......@@ -207,7 +207,7 @@ class BlockVisitor(NodeVisitor):
#
# yield "}"
#
def visit_lvalue(self, lvalue: ast.expr, declare: IsDeclare, allow_auto: bool = False) -> Iterable[str]:
def visit_lvalue(self, lvalue: ast.expr, declare: IsDeclare, allow_auto: bool = False, annotation: ast.Expr = None) -> Iterable[str]:
if isinstance(lvalue, ast.Tuple):
# for name, decl, ty in zip(lvalue.elts, declare, lvalue.type.args):
# if decl:
......@@ -230,10 +230,16 @@ class BlockVisitor(NodeVisitor):
if allow_auto:
yield "auto"
else:
yield "typename std::remove_reference<decltype("
yield from self.expr().visit(declare.initial_value)
yield ")>::type"
#yield from self.visit(lvalue.type)
if declare.initial_value:
yield "typename std::remove_reference<decltype("
yield from self.expr().visit(declare.initial_value)
yield ")>::type"
elif annotation is not None:
yield "typename std::remove_reference<decltype("
yield from self.expr().visit(annotation)
yield ")>::type::ObjType"
else:
yield from self.visit(lvalue.type)
yield name
elif isinstance(lvalue, ast.Subscript):
yield from self.expr().visit(lvalue)
......@@ -251,7 +257,7 @@ class BlockVisitor(NodeVisitor):
yield ";"
def visit_AnnAssign(self, node: ast.AnnAssign) -> Iterable[str]:
yield from self.visit_lvalue(node.target, node.is_declare, node.value is not None)
yield from self.visit_lvalue(node.target, node.is_declare, node.value is not None, node.annotation)
if node.value:
yield " = "
yield from self.expr().visit(node.value)
......
......@@ -9,7 +9,7 @@ from transpiler.phases.typing.types import BaseType
from transpiler.utils import UnsupportedNodeError, highlight
MAPPINGS = {
"int": "typon::TyInt"
}
class UniversalVisitor:
......
......@@ -2,7 +2,7 @@ from transpiler.phases.typing.common import PRELUDE
from transpiler.phases.typing.scope import VarKind, VarDecl
from transpiler.phases.typing.types import TY_TASK, TY_CALLABLE, TY_OPTIONAL, TY_CPP_TYPE, TY_BUILTIN_FEATURE, TY_TUPLE, \
TY_DICT, TY_SET, TY_LIST, TY_COMPLEX, TY_BYTES, TY_STR, TY_FLOAT, TY_INT, TY_BOOL, TY_OBJECT, TY_JOIN, TY_FUTURE, \
TY_FORKED, TY_GENERATOR, TY_MUTEX
TY_FORKED, TY_GENERATOR, TY_MUTEX, TY_SLICE
prelude_vars = {
"object": TY_OBJECT,
......@@ -16,6 +16,7 @@ prelude_vars = {
"set": TY_SET,
"dict": TY_DICT,
"tuple": TY_TUPLE,
"slice": TY_SLICE,
"BuiltinFeature": TY_BUILTIN_FEATURE,
"CppType": TY_CPP_TYPE,
"Task": TY_TASK,
......
......@@ -7,7 +7,7 @@ if TYPE_CHECKING:
from transpiler.utils import highlight
from transpiler.phases.typing.annotations import TypeAnnotationVisitor
from transpiler.phases.typing.scope import Scope, ScopeKind, VarDecl, VarKind
from transpiler.phases.typing.types import BaseType, TypeVariable, TY_NONE, BuiltinFeatureType
from transpiler.phases.typing.types import BaseType, TypeVariable, TY_NONE, BuiltinFeatureType, ClassTypeType
from transpiler.phases.utils import NodeVisitorSeq, AnnotationName
PRELUDE = Scope.make_global()
......@@ -27,7 +27,7 @@ class ScoperVisitor(NodeVisitorSeq):
def visit_annotation(self, expr: Optional[ast.expr]) -> BaseType:
res = self.anno().visit(expr) if expr else TypeVariable()
assert not isinstance(res, TypeType)
assert not isinstance(res, ClassTypeType)
return res
def annotate_arg(self, arg: ast.arg) -> BaseType:
......
......@@ -9,7 +9,7 @@ from transpiler.phases.typing.exceptions import ArgumentCountMismatchError, Type
from transpiler.phases.typing.types import BaseType, TY_STR, TY_BOOL, TY_INT, TY_COMPLEX, TY_FLOAT, TY_NONE, \
ClassTypeType, ResolvedConcreteType, GenericType, CallableInstanceType, TY_LIST, TY_SET, TY_DICT, RuntimeValue, \
TypeVariable, TY_LAMBDA, TypeListType, MethodType, TY_TUPLE, GenericInstanceType, PROMISES, TRANSPARENT_PROMISES, \
TY_FORKED, TY_JOIN, TypeTupleType, TupleInstanceType, TY_TYPE
TY_FORKED, TY_JOIN, TypeTupleType, TupleInstanceType, TY_TYPE, TY_SLICE
from transpiler.phases.typing.scope import ScopeKind, VarDecl, VarKind
from transpiler.utils import linenodata
......
......@@ -406,6 +406,7 @@ TY_STR = create_builtin_type("str")
TY_BYTES = create_builtin_type("bytes")
TY_COMPLEX = create_builtin_type("complex")
TY_NONE = create_builtin_type("NoneType")
TY_SLICE = create_builtin_type("slice")
def unimpl(*args, **kwargs):
......
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