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
54759ed9
Commit
54759ed9
authored
Feb 13, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename Py to Ty
parent
4b427fb6
Changes
22
Hide whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
169 additions
and
169 deletions
+169
-169
typon/include/python/builtins.hpp
typon/include/python/builtins.hpp
+30
-30
typon/include/python/builtins/bytes.hpp
typon/include/python/builtins/bytes.hpp
+10
-10
typon/include/python/builtins/complex.hpp
typon/include/python/builtins/complex.hpp
+4
-4
typon/include/python/builtins/dict.hpp
typon/include/python/builtins/dict.hpp
+11
-11
typon/include/python/builtins/exception.hpp
typon/include/python/builtins/exception.hpp
+4
-4
typon/include/python/builtins/list.hpp
typon/include/python/builtins/list.hpp
+14
-14
typon/include/python/builtins/print.hpp
typon/include/python/builtins/print.hpp
+2
-2
typon/include/python/builtins/set.hpp
typon/include/python/builtins/set.hpp
+5
-5
typon/include/python/builtins/slice.hpp
typon/include/python/builtins/slice.hpp
+8
-8
typon/include/python/builtins/str.hpp
typon/include/python/builtins/str.hpp
+30
-30
typon/include/python/hashlib.hpp
typon/include/python/hashlib.hpp
+11
-11
typon/include/python/os.hpp
typon/include/python/os.hpp
+15
-15
typon/include/python/socket.hpp
typon/include/python/socket.hpp
+7
-7
typon/include/python/sys.hpp
typon/include/python/sys.hpp
+1
-1
typon/trans/transpiler/phases/emit_cpp/expr.py
typon/trans/transpiler/phases/emit_cpp/expr.py
+5
-5
typon/trans/transpiler/phases/emit_cpp/visitors.py
typon/trans/transpiler/phases/emit_cpp/visitors.py
+1
-1
typon/trans/transpiler/phases/emit_cpp2/__init__.py
typon/trans/transpiler/phases/emit_cpp2/__init__.py
+2
-2
typon/trans/transpiler/phases/emit_cpp2/class_.py
typon/trans/transpiler/phases/emit_cpp2/class_.py
+1
-1
typon/trans/transpiler/phases/emit_cpp2/expr.py
typon/trans/transpiler/phases/emit_cpp2/expr.py
+5
-5
typon/trans/transpiler/phases/emit_cpp2/file.py
typon/trans/transpiler/phases/emit_cpp2/file.py
+1
-1
typon/trans/transpiler/phases/typing/expr.py
typon/trans/transpiler/phases/typing/expr.py
+1
-1
typon/trans/transpiler/transpiler.py
typon/trans/transpiler/transpiler.py
+1
-1
No files found.
typon/include/python/builtins.hpp
View file @
54759ed9
...
...
@@ -59,46 +59,46 @@ template <PySmartPtr T> struct RealType<T> {
using
type
=
typename
T
::
element_type
;
};
//template <typename T> using
P
yObj = std::shared_ptr<typename RealType<T>::type>;
//template <typename T> using
T
yObj = std::shared_ptr<typename RealType<T>::type>;
template
<
typename
T
>
class
P
yObj
:
public
std
::
shared_ptr
<
typename
RealType
<
T
>::
type
>
{
class
T
yObj
:
public
std
::
shared_ptr
<
typename
RealType
<
T
>::
type
>
{
public:
using
inner
=
typename
RealType
<
T
>::
type
;
/*template<typename... Args>
P
yObj(Args&&... args) : std::shared_ptr<inner>(std::make_shared<inner>(std::forward<Args>(args)...)) {}*/
P
yObj
()
:
std
::
shared_ptr
<
inner
>
()
{}
P
yObj
(
std
::
nullptr_t
)
:
std
::
shared_ptr
<
inner
>
(
nullptr
)
{}
P
yObj
(
inner
*
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
P
yObj
(
const
std
::
shared_ptr
<
inner
>
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
P
yObj
(
std
::
shared_ptr
<
inner
>
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
const
P
yObj
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
P
yObj
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
P
yObj
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
&
operator
=
(
const
P
yObj
&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
PyObj
&
operator
=
(
P
yObj
&&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
P
yObj
&
operator
=
(
std
::
nullptr_t
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
nullptr
);
return
*
this
;
}
P
yObj
&
operator
=
(
inner
*
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
P
yObj
&
operator
=
(
const
std
::
shared_ptr
<
inner
>
&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
T
yObj(Args&&... args) : std::shared_ptr<inner>(std::make_shared<inner>(std::forward<Args>(args)...)) {}*/
T
yObj
()
:
std
::
shared_ptr
<
inner
>
()
{}
T
yObj
(
std
::
nullptr_t
)
:
std
::
shared_ptr
<
inner
>
(
nullptr
)
{}
T
yObj
(
inner
*
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
T
yObj
(
const
std
::
shared_ptr
<
inner
>
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
T
yObj
(
std
::
shared_ptr
<
inner
>
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
TyObj
(
const
T
yObj
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
TyObj
(
T
yObj
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
TyObj
(
T
yObj
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
TyObj
&
operator
=
(
const
T
yObj
&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
TyObj
&
operator
=
(
T
yObj
&&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
T
yObj
&
operator
=
(
std
::
nullptr_t
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
nullptr
);
return
*
this
;
}
T
yObj
&
operator
=
(
inner
*
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
T
yObj
&
operator
=
(
const
std
::
shared_ptr
<
inner
>
&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
template
<
typename
U
>
PyObj
(
const
P
yObj
<
U
>
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
TyObj
(
const
T
yObj
<
U
>
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
//
PyObj(P
yObj<U> &&ptr) : std::shared_ptr<inner>(ptr) {}
//
TyObj(T
yObj<U> &&ptr) : std::shared_ptr<inner>(ptr) {}
// using make_shared
/*template<class U>
P
yObj(U&& other) : std::shared_ptr<inner>(std::make_shared<inner>(other)) {}*/
T
yObj(U&& other) : std::shared_ptr<inner>(std::make_shared<inner>(other)) {}*/
template
<
class
U
>
bool
operator
==
(
const
P
yObj
<
U
>
&
other
)
const
{
bool
operator
==
(
const
T
yObj
<
U
>
&
other
)
const
{
// check null
if
(
this
->
get
()
==
other
.
get
())
{
return
true
;
...
...
@@ -121,17 +121,17 @@ public:
}
template
<
class
U
>
bool
py_is
(
const
P
yObj
<
U
>
&
other
)
const
{
bool
py_is
(
const
T
yObj
<
U
>
&
other
)
const
{
return
this
->
get
()
==
other
.
get
();
}
};
template
<
typename
T
,
typename
...
Args
>
auto
pyobj
(
Args
&&
...
args
)
->
P
yObj
<
typename
RealType
<
T
>::
type
>
{
template
<
typename
T
,
typename
...
Args
>
auto
tyObj
(
Args
&&
...
args
)
->
T
yObj
<
typename
RealType
<
T
>::
type
>
{
return
std
::
make_shared
<
typename
RealType
<
T
>::
type
>
(
std
::
forward
<
Args
>
(
args
)...);
}
template
<
typename
T
,
typename
...
Args
>
auto
pyobj_agg
(
Args
&&
...
args
)
->
P
yObj
<
T
>
{
template
<
typename
T
,
typename
...
Args
>
auto
pyobj_agg
(
Args
&&
...
args
)
->
T
yObj
<
T
>
{
return
std
::
make_shared
<
typename
RealType
<
T
>::
type
>
((
typename
RealType
<
T
>::
type
)
{
std
::
forward
<
Args
>
(
args
)...
});
}
...
...
@@ -209,11 +209,11 @@ static constexpr auto PyNone = std::nullopt;
struct
file_s
{
struct
py_type
{
METHOD
(
typon
::
Task
<
P
yStr
>
,
read
,
(
Self
self
,
size_t
size
=
-
1
),
{
typon
::
Task
<
T
yStr
>
,
read
,
(
Self
self
,
size_t
size
=
-
1
),
{
if
(
size
==
-
1
)
{
size
=
self
->
len
;
}
P
yStr
buf
(
size
,
'\0'
);
T
yStr
buf
(
size
,
'\0'
);
int
nbytes
=
co_await
typon
::
io
::
read
(
self
->
fd
,
buf
.
data
(),
size
);
if
(
nbytes
<
0
)
{
system_error
(
-
nbytes
,
"read()"
);
...
...
@@ -256,10 +256,10 @@ struct file_s {
}
file
;
namespace
typon
{
using
PyFile
=
P
yObj
<
decltype
(
file
)
::
py_type
>
;
using
PyFile
=
T
yObj
<
decltype
(
file
)
::
py_type
>
;
}
typon
::
Task
<
typon
::
PyFile
>
open
(
const
P
yStr
&
path
,
std
::
string_view
mode
)
{
typon
::
Task
<
typon
::
PyFile
>
open
(
const
T
yStr
&
path
,
std
::
string_view
mode
)
{
const
char
*
path_c
=
path
.
c_str
();
size_t
len
=
0
;
struct
statx
statxbuf
;
...
...
@@ -307,7 +307,7 @@ typon::Task<typon::PyFile> open(const PyStr &path, std::string_view mode) {
std
::
cerr
<<
path
<<
","
<<
flags
<<
std
::
endl
;
system_error
(
-
fd
,
"openat()"
);
}
co_return
pyo
bj
<
typon
::
PyFile
>
(
fd
,
len
);
co_return
tyO
bj
<
typon
::
PyFile
>
(
fd
,
len
);
}
#include <typon/generator.hpp>
...
...
@@ -375,7 +375,7 @@ auto mapFilter(Map map, Seq seq, Filt filt = Filt()) {
std
::
vector
<
return_type
>
result
{};
for
(
auto
i
:
seq
|
std
::
views
::
filter
(
filt
)
|
std
::
views
::
transform
(
map
))
result
.
push_back
(
i
);
return
typon
::
P
yList
(
std
::
move
(
result
));
return
typon
::
T
yList
(
std
::
move
(
result
));
}
namespace
PYBIND11_NAMESPACE
{
...
...
typon/include/python/builtins/bytes.hpp
View file @
54759ed9
...
...
@@ -5,22 +5,22 @@
#ifndef TYPON_BYTES_H
#define TYPON_BYTES_H
class
P
yStr
;
class
T
yStr
;
class
P
yBytes
:
public
std
::
string
{
class
T
yBytes
:
public
std
::
string
{
public:
P
yBytes
()
:
std
::
string
()
{}
P
yBytes
(
const
char
*
s
)
:
std
::
string
(
s
)
{}
P
yBytes
(
const
std
::
string
&
s
)
:
std
::
string
(
s
)
{}
P
yBytes
(
std
::
string
&&
s
)
:
std
::
string
(
std
::
move
(
s
))
{}
P
yBytes
(
size_t
count
,
char
ch
)
:
std
::
string
(
count
,
ch
)
{}
T
yBytes
()
:
std
::
string
()
{}
T
yBytes
(
const
char
*
s
)
:
std
::
string
(
s
)
{}
T
yBytes
(
const
std
::
string
&
s
)
:
std
::
string
(
s
)
{}
T
yBytes
(
std
::
string
&&
s
)
:
std
::
string
(
std
::
move
(
s
))
{}
T
yBytes
(
size_t
count
,
char
ch
)
:
std
::
string
(
count
,
ch
)
{}
template
<
class
InputIterator
>
P
yBytes
(
InputIterator
first
,
InputIterator
last
)
:
std
::
string
(
first
,
last
)
{}
T
yBytes
(
InputIterator
first
,
InputIterator
last
)
:
std
::
string
(
first
,
last
)
{}
//
P
yStr decode_inner(const std::string &encoding = "utf-8") const;
//
T
yStr decode_inner(const std::string &encoding = "utf-8") const;
METHOD
(
P
yStr
,
decode
,
(
Self
self
,
const
std
::
string
&
encoding
=
"utf-8"
),
;)
METHOD
(
T
yStr
,
decode
,
(
Self
self
,
const
std
::
string
&
encoding
=
"utf-8"
),
;)
};
#endif // TYPON_BYTES_H
...
...
typon/include/python/builtins/complex.hpp
View file @
54759ed9
...
...
@@ -10,13 +10,13 @@
#include "print.hpp"
using
P
yComplex
=
std
::
complex
<
double
>
;
using
T
yComplex
=
std
::
complex
<
double
>
;
PyComplex
operator
+
(
int
a
,
const
PyComplex
&
b
)
{
return
P
yComplex
(
a
)
+
b
;
}
TyComplex
operator
+
(
int
a
,
const
TyComplex
&
b
)
{
return
T
yComplex
(
a
)
+
b
;
}
PyComplex
operator
-
(
int
a
,
const
PyComplex
&
b
)
{
return
P
yComplex
(
a
)
-
b
;
}
TyComplex
operator
-
(
int
a
,
const
TyComplex
&
b
)
{
return
T
yComplex
(
a
)
-
b
;
}
template
<
>
void
print_to
<
PyComplex
>
(
const
P
yComplex
&
x
,
std
::
ostream
&
s
)
{
template
<
>
void
print_to
<
TyComplex
>
(
const
T
yComplex
&
x
,
std
::
ostream
&
s
)
{
if
(
x
.
real
()
==
0
)
{
s
<<
x
.
imag
()
<<
"j"
;
}
else
{
...
...
typon/include/python/builtins/dict.hpp
View file @
54759ed9
...
...
@@ -10,12 +10,12 @@
/*
template <typename K, typename V>
class
P
yDict : public std::unordered_map<K, V> {
class
T
yDict : public std::unordered_map<K, V> {
public:
P
yDict(std::unordered_map<K, V> &&m)
T
yDict(std::unordered_map<K, V> &&m)
: std::unordered_map<K, V>(std::move(m)) {}
P
yDict(std::initializer_list<std::pair<const K, V>> m)
T
yDict(std::initializer_list<std::pair<const K, V>> m)
: std::unordered_map<K, V>(m) {}
operator std::unordered_map<K, V>() const {
...
...
@@ -83,22 +83,22 @@ public:
};
template <typename K, typename V>
PyDict(std::initializer_list<std::pair<K, V>>) -> P
yDict<K, V>;*/
TyDict(std::initializer_list<std::pair<K, V>>) -> T
yDict<K, V>;*/
namespace
typon
{
template
<
typename
K
,
typename
V
>
class
P
yDict
{
template
<
typename
K
,
typename
V
>
class
T
yDict
{
public:
P
yDict
(
std
::
shared_ptr
<
std
::
unordered_map
<
K
,
V
>>
&&
m
)
:
_m
(
std
::
move
(
m
))
{}
P
yDict
(
std
::
unordered_map
<
K
,
V
>
&&
m
)
T
yDict
(
std
::
shared_ptr
<
std
::
unordered_map
<
K
,
V
>>
&&
m
)
:
_m
(
std
::
move
(
m
))
{}
T
yDict
(
std
::
unordered_map
<
K
,
V
>
&&
m
)
:
_m
(
std
::
move
(
std
::
make_shared
<
std
::
unordered_map
<
K
,
V
>>
(
std
::
move
(
m
))))
{}
P
yDict
(
std
::
initializer_list
<
std
::
pair
<
const
K
,
V
>>
&&
m
)
T
yDict
(
std
::
initializer_list
<
std
::
pair
<
const
K
,
V
>>
&&
m
)
:
_m
(
std
::
make_shared
<
std
::
unordered_map
<
K
,
V
>>
(
std
::
move
(
m
)))
{}
P
yDict
()
:
_m
(
std
::
make_shared
<
std
::
unordered_map
<
K
,
V
>>
())
{}
T
yDict
()
:
_m
(
std
::
make_shared
<
std
::
unordered_map
<
K
,
V
>>
())
{}
template
<
typename
...
Args
>
P
yDict
(
Args
&&
...
args
)
T
yDict
(
Args
&&
...
args
)
:
_m
(
std
::
make_shared
<
std
::
unordered_map
<
K
,
V
>>
(
std
::
forward
<
Args
>
(
args
)...))
{}
...
...
@@ -139,7 +139,7 @@ private:
namespace
PYBIND11_NAMESPACE
{
namespace
detail
{
template
<
typename
K
,
typename
V
>
struct
type_caster
<
typon
::
PyDict
<
K
,
V
>>
:
map_caster
<
typon
::
P
yDict
<
K
,
V
>
,
K
,
V
>
{};
struct
type_caster
<
typon
::
TyDict
<
K
,
V
>>
:
map_caster
<
typon
::
T
yDict
<
K
,
V
>
,
K
,
V
>
{};
}}
#endif // TYPON_DICT_HPP
typon/include/python/builtins/exception.hpp
View file @
54759ed9
...
...
@@ -7,18 +7,18 @@
#include "str.hpp"
struct
P
yException_s
{
struct
T
yException_s
{
struct
py_type
{
P
yStr
message
;
T
yStr
message
;
};
auto
operator
()(
const
P
yStr
&
message
)
const
{
auto
operator
()(
const
T
yStr
&
message
)
const
{
return
py_type
{
message
};
}
};
namespace
typon
{
using
PyException
=
PyObj
<
P
yException_s
>
;
using
TyException
=
TyObj
<
T
yException_s
>
;
}
#endif // TYPON_EXCEPTION_HPP
typon/include/python/builtins/list.hpp
View file @
54759ed9
...
...
@@ -13,17 +13,17 @@
namespace
typon
{
template
<
typename
T
>
class
P
yList
{
template
<
typename
T
>
class
T
yList
{
public:
using
value_type
=
T
;
P
yList
(
std
::
shared_ptr
<
std
::
vector
<
T
>>
&&
v
)
:
_v
(
std
::
move
(
v
))
{}
P
yList
(
std
::
vector
<
T
>
&&
v
)
T
yList
(
std
::
shared_ptr
<
std
::
vector
<
T
>>
&&
v
)
:
_v
(
std
::
move
(
v
))
{}
T
yList
(
std
::
vector
<
T
>
&&
v
)
:
_v
(
std
::
move
(
std
::
make_shared
<
std
::
vector
<
T
>>
(
std
::
move
(
v
))))
{}
P
yList
(
std
::
initializer_list
<
T
>
&&
v
)
T
yList
(
std
::
initializer_list
<
T
>
&&
v
)
:
_v
(
std
::
make_shared
<
std
::
vector
<
T
>>
(
std
::
move
(
v
)))
{}
P
yList
()
:
_v
(
std
::
make_shared
<
std
::
vector
<
T
>>
())
{}
T
yList
()
:
_v
(
std
::
make_shared
<
std
::
vector
<
T
>>
())
{}
auto
begin
()
const
{
return
_v
->
begin
();
}
...
...
@@ -70,16 +70,16 @@ public:
void
py_print
(
std
::
ostream
&
s
)
const
{
py_repr
(
s
);
}
PyList
<
T
>
operator
+
(
const
P
yList
<
T
>
&
other
)
const
{
TyList
<
T
>
operator
+
(
const
T
yList
<
T
>
&
other
)
const
{
std
::
vector
<
T
>
v
;
v
.
reserve
(
_v
->
size
()
+
other
.
_v
->
size
());
v
.
insert
(
v
.
end
(),
_v
->
begin
(),
_v
->
end
());
v
.
insert
(
v
.
end
(),
other
.
_v
->
begin
(),
other
.
_v
->
end
());
return
P
yList
<
T
>
(
std
::
move
(
v
));
return
T
yList
<
T
>
(
std
::
move
(
v
));
}
P
yList
<
T
>
operator
*
(
size_t
n
)
const
{
P
yList
<
T
>
v
{};
T
yList
<
T
>
operator
*
(
size_t
n
)
const
{
T
yList
<
T
>
v
{};
v
.
_v
->
reserve
(
this
->
_v
->
size
()
*
n
);
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
v
.
_v
->
insert
(
v
.
_v
->
end
(),
this
->
_v
->
begin
(),
this
->
_v
->
end
());
...
...
@@ -97,21 +97,21 @@ private:
}
template
<
typename
T
>
typon
::
P
yList
<
T
>
list
(
std
::
initializer_list
<
T
>
&&
v
)
{
return
typon
::
P
yList
<
T
>
(
std
::
move
(
v
));
template
<
typename
T
>
typon
::
T
yList
<
T
>
list
(
std
::
initializer_list
<
T
>
&&
v
)
{
return
typon
::
T
yList
<
T
>
(
std
::
move
(
v
));
}
namespace
PYBIND11_NAMESPACE
{
namespace
detail
{
template
<
typename
Type
>
struct
type_caster
<
typon
::
PyList
<
Type
>>
:
list_caster
<
typon
::
P
yList
<
Type
>
,
Type
>
{};
struct
type_caster
<
typon
::
TyList
<
Type
>>
:
list_caster
<
typon
::
T
yList
<
Type
>
,
Type
>
{};
}}
/*NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)
template <typename Type> struct type_caster<
P
yList<Type>>
: list_caster<
P
yList<Type>, Type> { };
template <typename Type> struct type_caster<
T
yList<Type>>
: list_caster<
T
yList<Type>, Type> { };
NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE)
...
...
typon/include/python/builtins/print.hpp
View file @
54759ed9
...
...
@@ -28,11 +28,11 @@ template <PyPrint T> void print_to(const T &x, std::ostream &s) {
}
template
<
typename
T
>
concept
P
yRepr
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
concept
T
yRepr
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
x
.
py_repr
(
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
P
yRepr
T
>
void
repr_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
x
.
py_repr
(
s
);
}
template
<
T
yRepr
T
>
void
repr_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
x
.
py_repr
(
s
);
}
template
<
typename
T
>
concept
Printable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
...
...
typon/include/python/builtins/set.hpp
View file @
54759ed9
...
...
@@ -9,11 +9,11 @@
namespace
typon
{
template
<
typename
T
>
class
P
ySet
:
public
std
::
unordered_set
<
T
>
{
template
<
typename
T
>
class
T
ySet
:
public
std
::
unordered_set
<
T
>
{
public:
P
ySet
(
std
::
unordered_set
<
T
>
&&
s
)
:
std
::
unordered_set
<
T
>
(
std
::
move
(
s
))
{}
T
ySet
(
std
::
unordered_set
<
T
>
&&
s
)
:
std
::
unordered_set
<
T
>
(
std
::
move
(
s
))
{}
P
ySet
(
std
::
initializer_list
<
T
>
&&
s
)
:
std
::
unordered_set
<
T
>
(
std
::
move
(
s
))
{}
T
ySet
(
std
::
initializer_list
<
T
>
&&
s
)
:
std
::
unordered_set
<
T
>
(
std
::
move
(
s
))
{}
operator
std
::
unordered_set
<
T
>
()
const
{
return
std
::
unordered_set
<
T
>
(
this
->
begin
(),
this
->
end
());
...
...
@@ -73,8 +73,8 @@ public:
}
template
<
typename
T
>
typon
::
P
ySet
<
T
>
set
(
std
::
initializer_list
<
T
>
&&
s
)
{
return
typon
::
P
ySet
<
T
>
(
std
::
move
(
s
));
template
<
typename
T
>
typon
::
T
ySet
<
T
>
set
(
std
::
initializer_list
<
T
>
&&
s
)
{
return
typon
::
T
ySet
<
T
>
(
std
::
move
(
s
));
}
#endif // TYPON_SET_HPP
typon/include/python/builtins/slice.hpp
View file @
54759ed9
...
...
@@ -10,14 +10,14 @@
#include <Python.h>
#include <stdint.h>
struct
P
ySlice
{
P
ySlice
()
=
default
;
PySlice
(
const
P
ySlice
&
)
=
default
;
PySlice
(
P
ySlice
&&
)
=
default
;
PySlice
&
operator
=
(
const
P
ySlice
&
)
=
default
;
PySlice
&
operator
=
(
P
ySlice
&&
)
=
default
;
P
ySlice
(
std
::
optional
<
ssize_t
>
start
,
std
::
optional
<
ssize_t
>
stop
,
std
::
optional
<
ssize_t
>
step
)
:
start
(
start
),
stop
(
stop
),
step
(
step
)
{
struct
T
ySlice
{
T
ySlice
()
=
default
;
TySlice
(
const
T
ySlice
&
)
=
default
;
TySlice
(
T
ySlice
&&
)
=
default
;
TySlice
&
operator
=
(
const
T
ySlice
&
)
=
default
;
TySlice
&
operator
=
(
T
ySlice
&&
)
=
default
;
T
ySlice
(
std
::
optional
<
ssize_t
>
start
,
std
::
optional
<
ssize_t
>
stop
,
std
::
optional
<
ssize_t
>
step
)
:
start
(
start
),
stop
(
stop
),
step
(
step
)
{
if
(
step
==
0
)
{
throw
std
::
runtime_error
(
"slice step cannot be zero"
);
}
...
...
typon/include/python/builtins/str.hpp
View file @
54759ed9
...
...
@@ -18,26 +18,26 @@ using namespace std::literals;
#include <fmt/format.h>
#include <pybind11/cast.h>
class
P
yStr
:
public
std
::
string
{
class
T
yStr
:
public
std
::
string
{
public:
P
yStr
()
:
std
::
string
()
{}
P
yStr
(
const
std
::
string
&
s
)
:
std
::
string
(
s
)
{}
P
yStr
(
std
::
string
&&
s
)
:
std
::
string
(
std
::
move
(
s
))
{}
constexpr
P
yStr
(
const
char
*
s
,
size_t
count
)
:
std
::
string
(
s
,
count
)
{}
constexpr
P
yStr
(
size_t
count
,
char
ch
)
:
std
::
string
(
count
,
ch
)
{}
T
yStr
()
:
std
::
string
()
{}
T
yStr
(
const
std
::
string
&
s
)
:
std
::
string
(
s
)
{}
T
yStr
(
std
::
string
&&
s
)
:
std
::
string
(
std
::
move
(
s
))
{}
constexpr
T
yStr
(
const
char
*
s
,
size_t
count
)
:
std
::
string
(
s
,
count
)
{}
constexpr
T
yStr
(
size_t
count
,
char
ch
)
:
std
::
string
(
count
,
ch
)
{}
template
<
typename
...
Args
>
P
yStr
(
Args
&&
...
args
)
:
std
::
string
(
std
::
forward
<
Args
>
(
args
)...)
{}
T
yStr
(
Args
&&
...
args
)
:
std
::
string
(
std
::
forward
<
Args
>
(
args
)...)
{}
template
<
class
InputIterator
>
P
yStr
(
InputIterator
first
,
InputIterator
last
)
:
std
::
string
(
first
,
last
)
{}
T
yStr
(
InputIterator
first
,
InputIterator
last
)
:
std
::
string
(
first
,
last
)
{}
METHOD
(
P
yBytes
,
encode
,
(
Self
self
,
const
std
::
string
&
encoding
=
"utf-8"
),
{
return
P
yBytes
(
self
.
begin
(),
self
.
end
());
METHOD
(
T
yBytes
,
encode
,
(
Self
self
,
const
std
::
string
&
encoding
=
"utf-8"
),
{
return
T
yBytes
(
self
.
begin
(),
self
.
end
());
})
METHOD_GEN
((
typename
...
T
),
P
yStr
,
format
,
(
Self
self
,
T
&&
...
args
),
{
return
P
yStr
(
fmt
::
format
(
fmt
::
runtime
(
self
),
std
::
forward
<
T
>
(
args
)...));
METHOD_GEN
((
typename
...
T
),
T
yStr
,
format
,
(
Self
self
,
T
&&
...
args
),
{
return
T
yStr
(
fmt
::
format
(
fmt
::
runtime
(
self
),
std
::
forward
<
T
>
(
args
)...));
})
METHOD
(
bool
,
startswith
,
(
Self
self
,
const
std
::
string
&
s
),
{
...
...
@@ -57,10 +57,10 @@ public:
return
self
.
std
::
string
::
find
(
x
)
!=
std
::
string
::
npos
;
})
PyStr
operator
[](
P
ySlice
slice
)
const
{
TyStr
operator
[](
T
ySlice
slice
)
const
{
auto
[
len
,
new_slice
]
=
slice
.
adjust_indices
(
this
->
size
());
P
yStr
result
;
T
yStr
result
;
result
.
reserve
(
len
);
if
(
new_slice
.
start
<
new_slice
.
stop
)
{
...
...
@@ -82,12 +82,12 @@ public:
return
result
;
}
P
yStr
operator
[](
ssize_t
index
)
const
{
T
yStr
operator
[](
ssize_t
index
)
const
{
if
(
index
<
0
)
{
index
+=
this
->
size
();
}
return
P
yStr
(
1
,
std
::
string
::
operator
[](
index
));
return
T
yStr
(
1
,
std
::
string
::
operator
[](
index
));
}
char
char_at
(
ssize_t
index
)
const
{
...
...
@@ -103,29 +103,29 @@ public:
}
};
inline
constexpr
P
yStr
operator
""
_ps
(
const
char
*
s
,
size_t
len
)
noexcept
{
return
P
yStr
(
s
,
len
);
inline
constexpr
T
yStr
operator
""
_ps
(
const
char
*
s
,
size_t
len
)
noexcept
{
return
T
yStr
(
s
,
len
);
}
template
<
typename
Self
>
PyStr
P
yBytes
::
decode_s
::
operator
()(
Self
self
,
const
std
::
string
&
encoding
)
const
{
return
P
yStr
(
self
.
begin
(),
self
.
end
());
TyStr
T
yBytes
::
decode_s
::
operator
()(
Self
self
,
const
std
::
string
&
encoding
)
const
{
return
T
yStr
(
self
.
begin
(),
self
.
end
());
}
template
<
typename
T
>
P
yStr
str
(
const
T
&
x
)
{
template
<
typename
T
>
T
yStr
str
(
const
T
&
x
)
{
std
::
stringstream
s
;
print_to
(
x
,
s
);
return
s
.
str
();
}
template
<
typename
T
>
P
yStr
repr
(
const
T
&
x
)
{
template
<
typename
T
>
T
yStr
repr
(
const
T
&
x
)
{
std
::
stringstream
s
;
::
repr_to
(
x
,
s
);
return
s
.
str
();
}
template
<
>
struct
std
::
hash
<
P
yStr
>
{
std
::
size_t
operator
()(
const
P
yStr
&
s
)
const
noexcept
{
template
<
>
struct
std
::
hash
<
T
yStr
>
{
std
::
size_t
operator
()(
const
T
yStr
&
s
)
const
noexcept
{
return
std
::
hash
<
std
::
string
>
()(
s
);
}
};
...
...
@@ -133,20 +133,20 @@ template <> struct std::hash<PyStr> {
namespace
PYBIND11_NAMESPACE
{
namespace
detail
{
template
<
>
struct
type_caster
<
P
yStr
>
:
string_caster
<
P
yStr
>
{};
struct
type_caster
<
T
yStr
>
:
string_caster
<
T
yStr
>
{};
}}
template
<
>
void
repr_to
(
const
P
yStr
&
x
,
std
::
ostream
&
s
)
{
template
<
>
void
repr_to
(
const
T
yStr
&
x
,
std
::
ostream
&
s
)
{
s
<<
'"'
<<
x
<<
'"'
;
}
template
<
>
void
print_to
<
PyStr
>
(
const
P
yStr
&
x
,
std
::
ostream
&
s
)
{
s
<<
x
;
}
template
<
>
void
print_to
<
TyStr
>
(
const
T
yStr
&
x
,
std
::
ostream
&
s
)
{
s
<<
x
;
}
struct
{
PyStr
operator
()(
const
P
yStr
&
s
=
""
_ps
)
{
TyStr
operator
()(
const
T
yStr
&
s
=
""
_ps
)
{
std
::
cout
<<
s
;
P
yStr
input
;
T
yStr
input
;
std
::
getline
(
std
::
cin
,
input
);
return
input
;
}
...
...
typon/include/python/hashlib.hpp
View file @
54759ed9
...
...
@@ -22,7 +22,7 @@ struct hashlib_t {
struct
_Hash_s
{
struct
py_type
{
py_type
(
P
yObj
<
void
>
context
,
openssl_update
update
,
openssl_final
final
,
py_type
(
T
yObj
<
void
>
context
,
openssl_update
update
,
openssl_final
final
,
int
diglen
)
:
_context
(
context
),
_update
(
update
),
_final
(
final
),
_diglen
(
diglen
)
{
}
...
...
@@ -33,7 +33,7 @@ struct hashlib_t {
:
_context
(
other
.
_context
),
_update
(
other
.
_update
),
_final
(
other
.
_final
),
_diglen
(
other
.
_diglen
)
{}
P
yObj
<
void
>
_context
;
T
yObj
<
void
>
_context
;
openssl_update
_update
;
openssl_final
_final
;
int
_diglen
;
...
...
@@ -57,31 +57,31 @@ struct hashlib_t {
}
_Hash
;
FUNCTION
(
auto
,
md5
,
(),
{
auto
ctx
=
pyo
bj
<
MD5_CTX
>
();
auto
ctx
=
tyO
bj
<
MD5_CTX
>
();
MD5_Init
(
ctx
.
get
());
return
pyo
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
MD5_Update
,
return
tyO
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
MD5_Update
,
(
openssl_final
)
MD5_Final
,
MD5_DIGEST_LENGTH
);
})
FUNCTION
(
auto
,
sha1
,
(),
{
auto
ctx
=
pyo
bj
<
SHA_CTX
>
();
auto
ctx
=
tyO
bj
<
SHA_CTX
>
();
SHA1_Init
(
ctx
.
get
());
return
pyo
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
SHA1_Update
,
return
tyO
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
SHA1_Update
,
(
openssl_final
)
SHA1_Final
,
SHA_DIGEST_LENGTH
);
})
FUNCTION
(
auto
,
sha256
,
(),
{
auto
ctx
=
pyo
bj
<
SHA256_CTX
>
();
auto
ctx
=
tyO
bj
<
SHA256_CTX
>
();
SHA256_Init
(
ctx
.
get
());
return
pyo
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
SHA256_Update
,
return
tyO
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
SHA256_Update
,
(
openssl_final
)
SHA256_Final
,
SHA256_DIGEST_LENGTH
);
})
FUNCTION
(
auto
,
sha512
,
(),
{
auto
ctx
=
pyo
bj
<
SHA512_CTX
>
();
auto
ctx
=
tyO
bj
<
SHA512_CTX
>
();
SHA512_Init
(
ctx
.
get
());
return
pyo
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
SHA512_Update
,
return
tyO
bj
<
_Hash_s
>
(
ctx
,
(
openssl_update
)
SHA512_Update
,
(
openssl_final
)
SHA512_Final
,
SHA512_DIGEST_LENGTH
);
})
...
...
@@ -92,7 +92,7 @@ auto &get_all() { return all; }
}
// namespace py_hashlib
namespace
typon
{
using
Py_Hash
=
P
yObj
<
py_hashlib
::
hashlib_t
::
_Hash_s
>
;
using
Py_Hash
=
T
yObj
<
py_hashlib
::
hashlib_t
::
_Hash_s
>
;
}
#endif // TYPON_HASHLIB_HPP
typon/include/python/os.hpp
View file @
54759ed9
...
...
@@ -46,15 +46,15 @@ struct os_t {
struct
DirEntry_s
{
struct
py_type
{
P
yStr
name
;
P
yStr
path
;
T
yStr
name
;
T
yStr
path
;
};
}
DirEntry
;
struct
_Scandiriterator_s
{
struct
py_type
{
using
value_type
=
P
yObj
<
DirEntry_s
>
;
using
reference
=
P
yObj
<
DirEntry_s
>
;
using
value_type
=
T
yObj
<
DirEntry_s
>
;
using
reference
=
T
yObj
<
DirEntry_s
>
;
METHOD
(
auto
,
py_enter
,
(
Self
self
),
{
return
self
;
})
METHOD
(
void
,
py_exit
,
(
Self
self
),
{
...
...
@@ -68,11 +68,11 @@ struct os_t {
{
return
py_type
(
self
->
basepath
,
self
->
namelist
,
self
->
n
,
self
->
n
);
})
auto
operator
*
()
{
auto
name
=
P
yStr
(
this
->
namelist
[
this
->
current
]
->
d_name
);
auto
name
=
T
yStr
(
this
->
namelist
[
this
->
current
]
->
d_name
);
return
pyobj_agg
<
DirEntry_s
>
(
name
,
this
->
basepath
+
name
);
}
py_type
(
const
P
yStr
&
basepath
,
struct
dirent
**
namelist
,
int
n
,
py_type
(
const
T
yStr
&
basepath
,
struct
dirent
**
namelist
,
int
n
,
int
current
=
0
)
:
basepath
(
basepath
),
namelist
(
namelist
),
n
(
n
),
current
(
current
)
{
if
(
this
->
basepath
[
this
->
basepath
.
size
()
-
1
]
!=
'/'
)
{
...
...
@@ -88,14 +88,14 @@ struct os_t {
void
operator
++
()
{
this
->
current
++
;
}
P
yStr
basepath
;
T
yStr
basepath
;
struct
dirent
**
namelist
;
int
n
;
int
current
;
};
}
_Scandiriterator
;
FUNCTION
(
typon
::
Task
<
PyObj
<
Stat_Result_s
>>
,
stat
,
(
const
P
yStr
&
path
),
FUNCTION
(
typon
::
Task
<
TyObj
<
Stat_Result_s
>>
,
stat
,
(
const
T
yStr
&
path
),
{
const
char
*
path_c
=
path
.
c_str
();
struct
statx
statxbuf
;
...
...
@@ -103,7 +103,7 @@ struct os_t {
STATX_SIZE
,
&
statxbuf
))
{
system_error
(
-
err
,
"statx()"
);
}
co_return
P
yObj
<
Stat_Result_s
>
(
new
Stat_Result_s
::
py_type
{
co_return
T
yObj
<
Stat_Result_s
>
(
new
Stat_Result_s
::
py_type
{
statxbuf
.
stx_mode
,
statxbuf
.
stx_ino
,
makedev
(
statxbuf
.
stx_dev_major
,
statxbuf
.
stx_dev_minor
),
...
...
@@ -121,24 +121,24 @@ struct os_t {
statxbuf
.
stx_btime
.
tv_sec
+
statxbuf
.
stx_btime
.
tv_nsec
/
1e9
});
})
FUNCTION
(
PyObj
<
_Scandiriterator_s
>
,
scandir
,
(
const
P
yStr
&
path
),
{
FUNCTION
(
TyObj
<
_Scandiriterator_s
>
,
scandir
,
(
const
T
yStr
&
path
),
{
const
char
*
path_c
=
path
.
c_str
();
struct
dirent
**
namelist
;
int
n
=
::
scandir
(
path_c
,
&
namelist
,
no_special
,
alphasort
);
if
(
n
<
0
)
{
system_error
(
-
n
,
"scandir()"
);
}
return
pyo
bj
<
_Scandiriterator_s
>
(
path
,
namelist
,
n
);
return
tyO
bj
<
_Scandiriterator_s
>
(
path
,
namelist
,
n
);
});
FUNCTION
(
PyStr
,
readlink
,
(
const
P
yStr
&
path
),
{
FUNCTION
(
TyStr
,
readlink
,
(
const
T
yStr
&
path
),
{
const
char
*
path_c
=
path
.
c_str
();
char
buf
[
PATH_MAX
];
ssize_t
nbytes
=
::
readlink
(
path_c
,
buf
,
sizeof
(
buf
));
if
(
nbytes
<
0
)
{
system_error
(
-
nbytes
,
"readlink()"
);
}
return
P
yStr
(
buf
,
nbytes
);
return
T
yStr
(
buf
,
nbytes
);
})
}
all
;
...
...
@@ -146,8 +146,8 @@ auto &get_all() { return all; }
}
// namespace py_os
namespace
typon
{
using
PyStat_Result
=
P
yObj
<
py_os
::
os_t
::
Stat_Result_s
>
;
using
Py_Scandiriterator
=
P
yObj
<
py_os
::
os_t
::
_Scandiriterator_s
>
;
using
PyStat_Result
=
T
yObj
<
py_os
::
os_t
::
Stat_Result_s
>
;
using
Py_Scandiriterator
=
T
yObj
<
py_os
::
os_t
::
_Scandiriterator_s
>
;
}
#endif // TYPON_OS_HPP
typon/include/python/socket.hpp
View file @
54759ed9
...
...
@@ -25,12 +25,12 @@ struct socket_t {
struct
socket_s
{
struct
py_type
{
METHOD
(
typon
::
Task
<
std
::
tuple
<
P
yObj
<
py_type
>
COMMA
()
std
::
string
>>
,
accept
,
(
Self
self
),
{
METHOD
(
typon
::
Task
<
std
::
tuple
<
T
yObj
<
py_type
>
COMMA
()
std
::
string
>>
,
accept
,
(
Self
self
),
{
int
connfd
=
co_await
typon
::
io
::
accept
(
self
->
fd
,
NULL
,
NULL
);
if
(
connfd
<
0
)
{
system_error
(
-
connfd
,
"accept()"
);
}
co_return
std
::
make_tuple
(
pyo
bj
<
py_type
>
(
connfd
),
std
::
string
(
""
));
// TODO
co_return
std
::
make_tuple
(
tyO
bj
<
py_type
>
(
connfd
),
std
::
string
(
""
));
// TODO
})
METHOD
(
typon
::
Task
<
void
>
,
close
,
(
Self
self
),
...
...
@@ -61,13 +61,13 @@ struct socket_t {
}
})
METHOD
(
typon
::
Task
<
P
yBytes
>
,
recv
,
(
Self
self
,
int
bufsize
),
{
P
yBytes
buf
(
bufsize
,
'\0'
);
METHOD
(
typon
::
Task
<
T
yBytes
>
,
recv
,
(
Self
self
,
int
bufsize
),
{
T
yBytes
buf
(
bufsize
,
'\0'
);
co_await
typon
::
io
::
recv
(
self
->
fd
,
buf
.
data
(),
buf
.
size
(),
0
);
co_return
std
::
move
(
buf
);
})
METHOD
(
typon
::
Task
<
void
>
,
send
,
(
Self
self
,
P
yBytes
data
),
{
METHOD
(
typon
::
Task
<
void
>
,
send
,
(
Self
self
,
T
yBytes
data
),
{
if
(
int
sbytes
=
co_await
typon
::
io
::
send
(
self
->
fd
,
data
,
0
);
sbytes
<
0
)
{
co_await
dotp
(
self
,
close
)();
...
...
@@ -85,7 +85,7 @@ struct socket_t {
auto
operator
()(
int
family
,
int
type_
)
{
if
(
int
fd
=
::
socket
(
family
,
type_
,
0
);
fd
>=
0
)
{
return
pyo
bj
<
py_type
>
(
fd
);
return
tyO
bj
<
py_type
>
(
fd
);
}
else
{
system_error
(
errno
,
"socket()"
);
}
...
...
@@ -113,7 +113,7 @@ auto &get_all() { return all; }
}
// namespace py_socket
namespace
typon
{
using
PySocket
=
P
yObj
<
py_socket
::
socket_t
::
socket_s
>
;
using
PySocket
=
T
yObj
<
py_socket
::
socket_t
::
socket_s
>
;
}
#endif // TYPON_SOCKET_HPP
typon/include/python/sys.hpp
View file @
54759ed9
...
...
@@ -13,7 +13,7 @@ struct sys_t {
static
constexpr
auto
&
stdin
=
std
::
cin
;
static
constexpr
auto
&
stdout
=
std
::
cout
;
static
constexpr
auto
&
stderr
=
std
::
cerr
;
typon
::
PyList
<
P
yStr
>
argv
;
typon
::
TyList
<
T
yStr
>
argv
;
FUNCTION
(
void
,
exit
,
(
int
code
),
{
std
::
exit
(
code
);
...
...
typon/trans/transpiler/phases/emit_cpp/expr.py
View file @
54759ed9
...
...
@@ -37,14 +37,14 @@ class ExpressionVisitor(NodeVisitor):
elif
isinstance
(
node
.
value
,
float
):
yield
repr
(
node
.
value
)
elif
isinstance
(
node
.
value
,
complex
):
yield
f"
P
yComplex(
{
node
.
value
.
real
}
,
{
node
.
value
.
imag
}
)"
yield
f"
T
yComplex(
{
node
.
value
.
real
}
,
{
node
.
value
.
imag
}
)"
elif
node
.
value
is
None
:
yield
"PyNone"
else
:
raise
NotImplementedError
(
node
,
type
(
node
))
def
visit_Slice
(
self
,
node
:
ast
.
Slice
)
->
Iterable
[
str
]:
yield
"
P
ySlice("
yield
"
T
ySlice("
yield
from
join
(
", "
,
(
self
.
visit
(
x
or
ast
.
Constant
(
value
=
None
))
for
x
in
(
node
.
lower
,
node
.
upper
,
node
.
step
)))
yield
")"
...
...
@@ -193,7 +193,7 @@ class ExpressionVisitor(NodeVisitor):
def
visit_List
(
self
,
node
:
ast
.
List
)
->
Iterable
[
str
]:
if
node
.
elts
:
yield
"typon::
P
yList{"
yield
"typon::
T
yList{"
yield
from
join
(
", "
,
map
(
self
.
reset
().
visit
,
node
.
elts
))
yield
"}"
else
:
...
...
@@ -202,7 +202,7 @@ class ExpressionVisitor(NodeVisitor):
def
visit_Set
(
self
,
node
:
ast
.
Set
)
->
Iterable
[
str
]:
if
node
.
elts
:
yield
"typon::
P
ySet{"
yield
"typon::
T
ySet{"
yield
from
join
(
", "
,
map
(
self
.
reset
().
visit
,
node
.
elts
))
yield
"}"
else
:
...
...
@@ -293,4 +293,4 @@ class ExpressionVisitor(NodeVisitor):
# res_item_type = visitor.expr().visit(node.elt)
# for if_ in gen.ifs:
# visitor.expr().visit(if_)
# return PyList(res_item_type)
\ No newline at end of file
# return TyList(res_item_type)
\ No newline at end of file
typon/trans/transpiler/phases/emit_cpp/visitors.py
View file @
54759ed9
...
...
@@ -63,7 +63,7 @@ class NodeVisitor(UniversalVisitor):
case
types
.
TY_NONE
:
yield
"void"
case
types
.
TY_STR
:
yield
"
P
yStr"
yield
"
T
yStr"
case
types
.
TypeVariable
(
name
):
raise
UnresolvedTypeVariableError
(
node
)
case
_
:
...
...
typon/trans/transpiler/phases/emit_cpp2/__init__.py
View file @
54759ed9
...
...
@@ -65,10 +65,10 @@ class NodeVisitor(UniversalVisitor):
elif
node
is
TY_NONE
:
yield
"void"
elif
node
is
TY_STR
:
yield
"
P
yStr"
yield
"
T
yStr"
elif
isinstance
(
node
,
UserType
):
# if node.is_reference:
# yield "
P
yObj<"
# yield "
T
yObj<"
#yield "auto"
yield
f"referencemodel::Rc<__main____oo<>::
{
node
.
name
}
__oo<>::Obj>"
# if node.is_reference:
...
...
typon/trans/transpiler/phases/emit_cpp2/class_.py
View file @
54759ed9
...
...
@@ -54,7 +54,7 @@ class ClassVisitor(NodeVisitor):
yield
"};"
yield
"template<typename... T> auto operator()(T&&... args) {"
yield
"return
pyo
bj<py_type>(std::forward<T>(args)...);"
yield
"return
tyO
bj<py_type>(std::forward<T>(args)...);"
yield
"}"
# outer = ClassOuterVisitor(node.inner_scope)
...
...
typon/trans/transpiler/phases/emit_cpp2/expr.py
View file @
54759ed9
...
...
@@ -72,14 +72,14 @@ class ExpressionVisitor(NodeVisitor):
elif
isinstance
(
node
.
value
,
float
):
yield
repr
(
node
.
value
)
elif
isinstance
(
node
.
value
,
complex
):
yield
f"
P
yComplex(
{
node
.
value
.
real
}
,
{
node
.
value
.
imag
}
)"
yield
f"
T
yComplex(
{
node
.
value
.
real
}
,
{
node
.
value
.
imag
}
)"
elif
node
.
value
is
None
:
yield
"PyNone"
else
:
raise
NotImplementedError
(
node
,
type
(
node
))
def
visit_Slice
(
self
,
node
:
ast
.
Slice
)
->
Iterable
[
str
]:
yield
"
P
ySlice("
yield
"
T
ySlice("
yield
from
join
(
", "
,
(
self
.
visit
(
x
or
ast
.
Constant
(
value
=
None
))
for
x
in
(
node
.
lower
,
node
.
upper
,
node
.
step
)))
yield
")"
...
...
@@ -228,7 +228,7 @@ class ExpressionVisitor(NodeVisitor):
def
visit_List
(
self
,
node
:
ast
.
List
)
->
Iterable
[
str
]:
if
node
.
elts
:
yield
"typon::
P
yList{"
yield
"typon::
T
yList{"
yield
from
join
(
", "
,
map
(
self
.
reset
().
visit
,
node
.
elts
))
yield
"}"
else
:
...
...
@@ -237,7 +237,7 @@ class ExpressionVisitor(NodeVisitor):
def
visit_Set
(
self
,
node
:
ast
.
Set
)
->
Iterable
[
str
]:
if
node
.
elts
:
yield
"typon::
P
ySet{"
yield
"typon::
T
ySet{"
yield
from
join
(
", "
,
map
(
self
.
reset
().
visit
,
node
.
elts
))
yield
"}"
else
:
...
...
@@ -328,4 +328,4 @@ class ExpressionVisitor(NodeVisitor):
# res_item_type = visitor.expr().visit(node.elt)
# for if_ in gen.ifs:
# visitor.expr().visit(if_)
# return PyList(res_item_type)
\ No newline at end of file
# return TyList(res_item_type)
\ No newline at end of file
typon/trans/transpiler/phases/emit_cpp2/file.py
View file @
54759ed9
...
...
@@ -55,7 +55,7 @@ class FileVisitor(BlockVisitor):
yield
"}"
yield
"#else"
yield
"int main(int argc, char* argv[]) {"
yield
"py_sys::all.argv = typon::
PyList<PyStr>(std::vector<P
yStr>(argv, argv + argc));"
yield
"py_sys::all.argv = typon::
TyList<TyStr>(std::vector<T
yStr>(argv, argv + argc));"
yield
"PROGRAMNS::__main__.root().call();"
yield
"}"
yield
"#endif"
typon/trans/transpiler/phases/typing/expr.py
View file @
54759ed9
...
...
@@ -310,4 +310,4 @@ class ScoperExprVisitor(ScoperVisitor):
for
if_
in
gen
.
ifs
:
visitor
.
expr
().
visit
(
if_
)
gen
.
ifs_node
=
ast
.
BoolOp
(
ast
.
And
(),
gen
.
ifs
,
**
linenodata
(
node
))
return
PyList
(
node
.
item_type
)
\ No newline at end of file
return
TyList
(
node
.
item_type
)
\ No newline at end of file
typon/trans/transpiler/transpiler.py
View file @
54759ed9
...
...
@@ -58,7 +58,7 @@ def transpile(source, name: str, path: Path):
yield
f"co_await dot(PROGRAMNS::
{
module
.
name
()
}
, main)();"
yield
"}"
yield
"int main(int argc, char* argv[]) {"
yield
"py_sys::all.argv = typon::
PyList<PyStr>(std::vector<P
yStr>(argv, argv + argc));"
yield
"py_sys::all.argv = typon::
TyList<TyStr>(std::vector<T
yStr>(argv, argv + argc));"
yield
f"root().call();"
yield
"}"
yield
"#endif"
...
...
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