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
e2d96f04
Commit
e2d96f04
authored
Mar 22, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mutexes work!
parent
0cff3aae
Changes
16
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
150 additions
and
29 deletions
+150
-29
typon/include/python/builtins.hpp
typon/include/python/builtins.hpp
+7
-2
typon/include/python/builtins/mutex.hpp
typon/include/python/builtins/mutex.hpp
+23
-13
typon/include/python/builtins/print.hpp
typon/include/python/builtins/print.hpp
+2
-2
typon/include/python/builtins/slice.hpp
typon/include/python/builtins/slice.hpp
+25
-1
typon/include/python/builtins/str.hpp
typon/include/python/builtins/str.hpp
+10
-0
typon/include/python/referencemodel.hpp
typon/include/python/referencemodel.hpp
+2
-0
typon/include/python/socket.hpp
typon/include/python/socket.hpp
+2
-2
typon/include/python/time.hpp
typon/include/python/time.hpp
+26
-0
typon/trans/stdlib/builtins_.py
typon/trans/stdlib/builtins_.py
+1
-1
typon/trans/stdlib/time_.py
typon/trans/stdlib/time_.py
+2
-0
typon/trans/tests/actors.py
typon/trans/tests/actors.py
+2
-3
typon/trans/tests/mutex_inc.py
typon/trans/tests/mutex_inc.py
+38
-0
typon/trans/transpiler/phases/emit_cpp/class_.py
typon/trans/transpiler/phases/emit_cpp/class_.py
+7
-2
typon/trans/transpiler/phases/emit_cpp/expr.py
typon/trans/transpiler/phases/emit_cpp/expr.py
+1
-1
typon/trans/transpiler/phases/emit_cpp/function.py
typon/trans/transpiler/phases/emit_cpp/function.py
+1
-1
typon/trans/transpiler/phases/typing/types.py
typon/trans/transpiler/phases/typing/types.py
+1
-1
No files found.
typon/include/python/builtins.hpp
View file @
e2d96f04
...
@@ -62,10 +62,16 @@ template <PySmartPtr T> struct RealType<T> {
...
@@ -62,10 +62,16 @@ template <PySmartPtr T> struct RealType<T> {
namespace
typon
{
namespace
typon
{
class
TyNone
{};
//class TyNone {};
using
TyNone
=
std
::
nullopt_t
;
auto
None
=
std
::
nullopt
;
}
// namespace typon
}
// namespace typon
static
constexpr
auto
None
=
typon
::
None
;
// typon_len
// typon_len
template
<
typename
T
>
template
<
typename
T
>
...
@@ -121,7 +127,6 @@ std::ostream &operator<<(std::ostream &os, std::optional<T> const &opt) {
...
@@ -121,7 +127,6 @@ std::ostream &operator<<(std::ostream &os, std::optional<T> const &opt) {
return
opt
?
os
<<
opt
.
value
()
:
os
<<
"None"
;
return
opt
?
os
<<
opt
.
value
()
:
os
<<
"None"
;
}
}
static
constexpr
auto
PyNone
=
std
::
nullopt
;
#define system_error(err, message) \
#define system_error(err, message) \
do { \
do { \
...
...
typon/include/python/builtins/mutex.hpp
View file @
e2d96f04
...
@@ -11,6 +11,24 @@ namespace view = std::views;
...
@@ -11,6 +11,24 @@ namespace view = std::views;
namespace
typon
{
namespace
typon
{
using
namespace
referencemodel
;
using
namespace
referencemodel
;
template
<
typename
_Base0
=
object
>
struct
TyCell__oo
:
classtype
<
_Base0
,
TyCell__oo
<>>
{
static
constexpr
std
::
string_view
name
=
"Cell"
;
template
<
typename
T
>
struct
Obj
:
instance
<
TyCell__oo
<>
,
Obj
<
T
>>
{
T
val
;
Obj
()
=
default
;
Obj
(
Obj
const
&
)
=
delete
;
};
template
<
typename
T
>
auto
operator
()(
T
val
)
const
{
return
Obj
<
T
>
{
val
};
}
};
template
<
typename
_Base0
=
object
>
template
<
typename
_Base0
=
object
>
struct
TyMutex__oo
:
classtype
<
_Base0
,
TyMutex__oo
<>>
{
struct
TyMutex__oo
:
classtype
<
_Base0
,
TyMutex__oo
<>>
{
static
constexpr
std
::
string_view
name
=
"Mutex"
;
static
constexpr
std
::
string_view
name
=
"Mutex"
;
...
@@ -18,30 +36,22 @@ struct TyMutex__oo : classtype<_Base0, TyMutex__oo<>> {
...
@@ -18,30 +36,22 @@ struct TyMutex__oo : classtype<_Base0, TyMutex__oo<>> {
template
<
typename
T
>
template
<
typename
T
>
struct
Obj
:
instance
<
TyMutex__oo
<>
,
Obj
<
T
>>
{
struct
Obj
:
instance
<
TyMutex__oo
<>
,
Obj
<
T
>>
{
typon
::
Mutex
mutex
;
typon
::
Mutex
mutex
;
T
val
;
TyCell__oo
<>::
Obj
<
T
>
cell
;
Obj
()
{
print
(
"Obj()"
_ps
);
}
};
};
struct
:
method
{
struct
:
method
{
auto
operator
()(
auto
self
,
auto
callback
)
->
typon
::
Task
<
TyNone
>
const
{
auto
operator
()(
auto
self
,
auto
callback
)
->
typon
::
Task
<
TyNone
>
const
{
print
(
"a"
_ps
);
auto
lock
=
dot
(
self
,
mutex
).
lock
();
auto
lock
=
dot
(
self
,
mutex
).
lock
();
print
(
"b"
_ps
);
co_await
lock
;
co_await
lock
;
print
(
"c"
_ps
);
co_await
callback
(
ref
(
dot
(
self
,
cell
)));
co_await
callback
(
dot
(
self
,
val
));
co_return
None
;
print
(
"d"
_ps
);
co_return
{};
}
}
}
static
constexpr
when
{};
}
static
constexpr
when
{};
template
<
typename
T
>
template
<
typename
T
>
auto
operator
()(
T
val
)
const
{
auto
operator
()(
T
val
)
const
{
auto
obj
=
referencemodel
::
meta
::
arc
<
Obj
<
T
>>
();
auto
obj
=
referencemodel
::
meta
::
arc
<
Obj
<
T
>>
(
std
::
in_place
);
//dot(obj, val)
= val;
dot
(
obj
,
cell
).
val
=
val
;
return
obj
;
return
obj
;
}
}
};
};
...
...
typon/include/python/builtins/print.hpp
View file @
e2d96f04
...
@@ -89,7 +89,7 @@ typon::Task<void> print(T const &head, Args const &...args) {
...
@@ -89,7 +89,7 @@ typon::Task<void> print(T const &head, Args const &...args) {
struct
{
struct
{
typon
::
TyNone
operator
()()
{
typon
::
TyNone
operator
()()
{
std
::
cout
<<
'\n'
;
std
::
cout
<<
'\n'
;
return
{}
;
return
typon
::
None
;
}
}
template
<
typename
T
,
typename
...
Args
>
template
<
typename
T
,
typename
...
Args
>
...
@@ -97,7 +97,7 @@ struct {
...
@@ -97,7 +97,7 @@ struct {
std
::
cout
<<
str
(
head
)
->
value
;
std
::
cout
<<
str
(
head
)
->
value
;
(((
std
::
cout
<<
' '
),
(
std
::
cout
<<
str
(
args
)
->
value
)),
...);
(((
std
::
cout
<<
' '
),
(
std
::
cout
<<
str
(
args
)
->
value
)),
...);
std
::
cout
<<
'\n'
;
std
::
cout
<<
'\n'
;
return
{}
;
return
typon
::
None
;
}
}
}
print
;
}
print
;
// typon::Task<void> print() { std::cout << '\n'; co_return; }
// typon::Task<void> print() { std::cout << '\n'; co_return; }
...
...
typon/include/python/builtins/slice.hpp
View file @
e2d96f04
...
@@ -17,7 +17,31 @@ template <typename _Base0 = object>
...
@@ -17,7 +17,31 @@ template <typename _Base0 = object>
struct
TySlice__oo
:
classtype
<
_Base0
,
TySlice__oo
<>>
{
struct
TySlice__oo
:
classtype
<
_Base0
,
TySlice__oo
<>>
{
static
constexpr
std
::
string_view
name
=
"TySlice"
;
static
constexpr
std
::
string_view
name
=
"TySlice"
;
auto
operator
()()
const
{}
template
<
typename
Start
,
typename
Stop
,
typename
Step
>
struct
Obj
:
value
<
TySlice__oo
<>
,
Obj
<
Start
,
Stop
,
Step
>>
{
Start
start
;
Stop
stop
;
Step
step
;
Obj
(
Start
start
,
Stop
stop
,
Step
step
)
:
start
(
start
),
stop
(
stop
),
step
(
step
)
{}
};
template
<
typename
Stop
>
auto
operator
()(
Stop
stop
)
const
{
return
Obj
<
TyNone
,
Stop
,
TyNone
>
{
None
,
stop
,
None
};
}
template
<
typename
Start
,
typename
Stop
>
auto
operator
()(
Start
start
,
Stop
stop
)
const
{
return
Obj
<
Start
,
Stop
,
TyNone
>
{
start
,
stop
,
None
};
}
template
<
typename
Start
,
typename
Stop
,
typename
Step
>
auto
operator
()(
Start
start
,
Stop
stop
,
Step
step
)
const
{
return
Obj
<
Start
,
Stop
,
Step
>
{
start
,
stop
,
step
};
}
};
};
static
constexpr
TySlice__oo
<>
TySlice
{};
static
constexpr
TySlice__oo
<>
TySlice
{};
...
...
typon/include/python/builtins/str.hpp
View file @
e2d96f04
...
@@ -176,6 +176,16 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> {
...
@@ -176,6 +176,16 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> {
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
{};
// getitem
struct
:
method
{
auto
operator
()(
auto
self
,
auto
index
)
const
{
if
(
index
<
0
)
{
index
+=
self
->
value
.
size
();
}
return
Obj
(
self
->
value
[
index
]);
}
}
static
constexpr
oo__getitem__oo
{};
/*struct : method {
/*struct : method {
auto operator()(auto self, auto other) const {
auto operator()(auto self, auto other) const {
auto pos = self->value.find(other->value);
auto pos = self->value.find(other->value);
...
...
typon/include/python/referencemodel.hpp
View file @
e2d96f04
...
@@ -550,6 +550,8 @@ template <typename T> struct Arc {
...
@@ -550,6 +550,8 @@ template <typename T> struct Arc {
Arc
(
std
::
nullptr_t
)
:
ptr
(
nullptr
)
{}
Arc
(
std
::
nullptr_t
)
:
ptr
(
nullptr
)
{}
Arc
(
std
::
in_place_t
)
:
ptr
(
new
Pack
<
T
>
())
{}
Arc
(
T
&&
t
)
:
ptr
(
new
Pack
<
T
>
(
std
::
move
(
t
)))
{}
Arc
(
T
&&
t
)
:
ptr
(
new
Pack
<
T
>
(
std
::
move
(
t
)))
{}
Arc
(
Arc
&&
arc
)
:
ptr
(
std
::
exchange
(
arc
.
ptr
,
nullptr
))
{}
Arc
(
Arc
&&
arc
)
:
ptr
(
std
::
exchange
(
arc
.
ptr
,
nullptr
))
{}
...
...
typon/include/python/socket.hpp
View file @
e2d96f04
...
@@ -54,7 +54,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
...
@@ -54,7 +54,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
if
(
::
setsockopt
(
self
->
fd
,
level
,
optname
,
&
optval
,
sizeof
(
int
))
<
0
)
{
if
(
::
setsockopt
(
self
->
fd
,
level
,
optname
,
&
optval
,
sizeof
(
int
))
<
0
)
{
system_error
(
errno
,
"setsockopt()"
);
system_error
(
errno
,
"setsockopt()"
);
}
}
return
typon
::
TyNone
{}
;
return
None
;
}
}
}
static
constexpr
setsockopt
{};
}
static
constexpr
setsockopt
{};
...
@@ -70,7 +70,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
...
@@ -70,7 +70,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
if
(
::
bind
(
self
->
fd
,
(
const
sockaddr
*
)
&
addr
,
sizeof
(
addr
))
<
0
)
{
if
(
::
bind
(
self
->
fd
,
(
const
sockaddr
*
)
&
addr
,
sizeof
(
addr
))
<
0
)
{
system_error
(
errno
,
"bind()"
);
system_error
(
errno
,
"bind()"
);
}
}
return
typon
::
TyNone
{}
;
return
None
;
}
}
}
static
constexpr
bind
{};
}
static
constexpr
bind
{};
...
...
typon/include/python/time.hpp
0 → 100644
View file @
e2d96f04
//
// Created by Tom on 09/03/2023.
//
#ifndef TYPON_TIME_HPP
#define TYPON_TIME_HPP
#include "builtins.hpp"
#include <iostream>
namespace
py_time
{
template
<
typename
_Unused
=
void
>
struct
time__oo
:
referencemodel
::
moduletype
<
time__oo
<>>
{
/*FUNCTION(void, exit, (int code), { std::exit(code); })*/
struct
:
referencemodel
::
function
{
typon
::
Task
<
typon
::
TyNone
>
operator
()(
auto
duration
)
const
{
co_await
typon
::
io
::
sleep
(
std
::
chrono
::
seconds
(
duration
));
co_return
None
;
}
}
static
constexpr
sleep
{};
};
time__oo
<>
all
;
}
// namespace py_time
#endif // TYPON_TIME_HPP
typon/trans/stdlib/builtins_.py
View file @
e2d96f04
...
@@ -28,7 +28,7 @@ class float:
...
@@ -28,7 +28,7 @@ class float:
assert
int
.
__add__
assert
int
.
__add__
assert
(
5
).
__add__
assert
(
5
).
__add__
class
slice
:
class
slice
[
Start
,
Stop
,
Step
]
:
pass
pass
...
...
typon/trans/stdlib/time_.py
0 → 100644
View file @
e2d96f04
def
sleep
(
duration
:
int
)
->
None
:
...
\ No newline at end of file
typon/trans/tests/actors.py
View file @
e2d96f04
...
@@ -32,10 +32,9 @@ def thing(x):
...
@@ -32,10 +32,9 @@ def thing(x):
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
print
(
"j"
)
print
(
"j"
)
# act = Actor(123)
act
=
Actor
(
123
)
m
=
Mutex
(
123
)
print
(
"k"
)
print
(
"k"
)
#
act.mutex.when(thing)
act
.
mutex
.
when
(
thing
)
# class Actor[T]:
# class Actor[T]:
# def __init__(self):
# def __init__(self):
...
...
typon/trans/tests/mutex_inc.py
0 → 100644
View file @
e2d96f04
from
time
import
sleep
def
inc
(
cell
):
x
=
cell
.
val
sleep
(
1
)
cell
.
val
=
x
+
1
# todo: why doesnt it crash with a wrong field name ???
print
(
"current:"
,
cell
.
val
)
class
Thing
:
x
:
int
def
__init__
(
self
,
x
:
int
):
self
.
x
=
x
def
inc
(
self
):
x
=
self
.
x
sleep
(
1
)
self
.
x
=
x
+
1
print
(
"current:"
,
self
.
x
)
def
truc
():
m
=
Mutex
(
0
)
for
_
in
range
(
10
):
fork
(
lambda
:
m
.
when
(
inc
))
sync
()
def
nomutex
():
t
=
Thing
(
0
)
for
_
in
range
(
10
):
fork
(
lambda
:
t
.
inc
())
sync
()
if
__name__
==
"__main__"
:
print
(
"Mutex:"
)
truc
()
print
(
"No mutex:"
)
nomutex
()
typon/trans/transpiler/phases/emit_cpp/class_.py
View file @
e2d96f04
...
@@ -62,8 +62,13 @@ def emit_class(name: str, node: ConcreteType) -> Iterable[str]:
...
@@ -62,8 +62,13 @@ def emit_class(name: str, node: ConcreteType) -> Iterable[str]:
yield
from
emit_function
(
mname
,
ty
,
"method"
)
yield
from
emit_function
(
mname
,
ty
,
"method"
)
yield
"template <"
yield
"template <"
yield
from
join
(
","
,
(
f"typename
{
name
}
"
for
name
in
template_params
()))
if
node
.
generic_parent
.
parameters
:
yield
from
join
(
","
,
(
f"typename
{
name
}
"
for
name
in
node
.
generic_parent
.
parameters
))
yield
", typename... $T"
yield
", typename... $T"
else
:
yield
"typename... $T, typename _Void = void"
yield
">"
yield
">"
def
obj_params
():
def
obj_params
():
yield
from
join
(
","
,
template_params
())
yield
from
join
(
","
,
template_params
())
...
...
typon/trans/transpiler/phases/emit_cpp/expr.py
View file @
e2d96f04
...
@@ -68,7 +68,7 @@ class ExpressionVisitor(NodeVisitor):
...
@@ -68,7 +68,7 @@ class ExpressionVisitor(NodeVisitor):
elif
isinstance
(
node
.
value
,
complex
):
elif
isinstance
(
node
.
value
,
complex
):
yield
f"TyComplex(
{
node
.
value
.
real
}
,
{
node
.
value
.
imag
}
)"
yield
f"TyComplex(
{
node
.
value
.
real
}
,
{
node
.
value
.
imag
}
)"
elif
node
.
value
is
None
:
elif
node
.
value
is
None
:
yield
"
Py
None"
yield
"None"
else
:
else
:
raise
NotImplementedError
(
node
,
type
(
node
))
raise
NotImplementedError
(
node
,
type
(
node
))
...
...
typon/trans/transpiler/phases/emit_cpp/function.py
View file @
e2d96f04
...
@@ -323,7 +323,7 @@ class BlockVisitor(NodeVisitor):
...
@@ -323,7 +323,7 @@ class BlockVisitor(NodeVisitor):
if
node
.
value
:
if
node
.
value
:
yield
from
self
.
expr
().
visit
(
node
.
value
)
yield
from
self
.
expr
().
visit
(
node
.
value
)
else
:
else
:
yield
"
typon::TyNone{}
"
yield
"
None
"
yield
";"
yield
";"
typon/trans/transpiler/phases/typing/types.py
View file @
e2d96f04
...
@@ -406,7 +406,6 @@ TY_STR = create_builtin_type("str")
...
@@ -406,7 +406,6 @@ TY_STR = create_builtin_type("str")
TY_BYTES
=
create_builtin_type
(
"bytes"
)
TY_BYTES
=
create_builtin_type
(
"bytes"
)
TY_COMPLEX
=
create_builtin_type
(
"complex"
)
TY_COMPLEX
=
create_builtin_type
(
"complex"
)
TY_NONE
=
create_builtin_type
(
"NoneType"
)
TY_NONE
=
create_builtin_type
(
"NoneType"
)
TY_SLICE
=
create_builtin_type
(
"slice"
)
def
unimpl
(
*
args
,
**
kwargs
):
def
unimpl
(
*
args
,
**
kwargs
):
...
@@ -430,6 +429,7 @@ TY_LIST = create_builtin_generic_type("list")
...
@@ -430,6 +429,7 @@ TY_LIST = create_builtin_generic_type("list")
TY_SET
=
create_builtin_generic_type
(
"set"
)
TY_SET
=
create_builtin_generic_type
(
"set"
)
TY_DICT
=
create_builtin_generic_type
(
"dict"
)
TY_DICT
=
create_builtin_generic_type
(
"dict"
)
TY_TUPLE
=
create_builtin_generic_type
(
"tuple"
)
TY_TUPLE
=
create_builtin_generic_type
(
"tuple"
)
TY_SLICE
=
create_builtin_generic_type
(
"slice"
)
TY_MUTEX
=
create_builtin_generic_type
(
"Mutex"
)
TY_MUTEX
=
create_builtin_generic_type
(
"Mutex"
)
...
...
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