Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon-compiler
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
typon
typon-compiler
Commits
0cff3aae
Commit
0cff3aae
authored
Mar 22, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proper type emission for annotated assign, fix socket
parent
bbcef874
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
96 additions
and
27 deletions
+96
-27
typon/include/python/builtins/int.hpp
typon/include/python/builtins/int.hpp
+1
-0
typon/include/python/builtins/mutex.hpp
typon/include/python/builtins/mutex.hpp
+10
-2
typon/include/python/builtins/slice.hpp
typon/include/python/builtins/slice.hpp
+15
-2
typon/include/python/socket.hpp
typon/include/python/socket.hpp
+32
-0
typon/trans/stdlib/builtins_.py
typon/trans/stdlib/builtins_.py
+1
-1
typon/trans/tests/actors.py
typon/trans/tests/actors.py
+7
-3
typon/trans/tests/check_socket_listening.py
typon/trans/tests/check_socket_listening.py
+3
-2
typon/trans/transpiler/phases/emit_cpp/expr.py
typon/trans/transpiler/phases/emit_cpp/expr.py
+8
-6
typon/trans/transpiler/phases/emit_cpp/function.py
typon/trans/transpiler/phases/emit_cpp/function.py
+12
-6
typon/trans/transpiler/phases/emit_cpp/visitors.py
typon/trans/transpiler/phases/emit_cpp/visitors.py
+1
-1
typon/trans/transpiler/phases/typing/__init__.py
typon/trans/transpiler/phases/typing/__init__.py
+2
-1
typon/trans/transpiler/phases/typing/common.py
typon/trans/transpiler/phases/typing/common.py
+2
-2
typon/trans/transpiler/phases/typing/expr.py
typon/trans/transpiler/phases/typing/expr.py
+1
-1
typon/trans/transpiler/phases/typing/types.py
typon/trans/transpiler/phases/typing/types.py
+1
-0
No files found.
typon/include/python/builtins/int.hpp
View file @
0cff3aae
...
...
@@ -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
;
}
};
...
...
typon/include/python/builtins/mutex.hpp
View file @
0cff3aae
...
...
@@ -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
;
}
};
...
...
typon/include/python/builtins/slice.hpp
View file @
0cff3aae
...
...
@@ -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
typon/include/python/socket.hpp
View file @
0cff3aae
...
...
@@ -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),
...
...
typon/trans/stdlib/builtins_.py
View file @
0cff3aae
...
...
@@ -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
:
...
...
...
typon/trans/tests/actors.py
View file @
0cff3aae
...
...
@@ -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):
...
...
typon/trans/tests/check_socket_listening.py
View file @
0cff3aae
# 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
:
...
...
typon/trans/transpiler/phases/emit_cpp/expr.py
View file @
0cff3aae
...
...
@@ -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
"("
...
...
typon/trans/transpiler/phases/emit_cpp/function.py
View file @
0cff3aae
...
...
@@ -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
)
...
...
typon/trans/transpiler/phases/emit_cpp/visitors.py
View file @
0cff3aae
...
...
@@ -9,7 +9,7 @@ from transpiler.phases.typing.types import BaseType
from
transpiler.utils
import
UnsupportedNodeError
,
highlight
MAPPINGS
=
{
"int"
:
"typon::TyInt"
}
class
UniversalVisitor
:
...
...
typon/trans/transpiler/phases/typing/__init__.py
View file @
0cff3aae
...
...
@@ -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
,
...
...
typon/trans/transpiler/phases/typing/common.py
View file @
0cff3aae
...
...
@@ -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
,
Class
TypeType
)
return
res
def
annotate_arg
(
self
,
arg
:
ast
.
arg
)
->
BaseType
:
...
...
typon/trans/transpiler/phases/typing/expr.py
View file @
0cff3aae
...
...
@@ -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
...
...
typon/trans/transpiler/phases/typing/types.py
View file @
0cff3aae
...
...
@@ -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
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment