Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon-concurrency
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
Xavier Thompson
typon-concurrency
Commits
667466a4
Commit
667466a4
authored
Jun 13, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement necessary socket features for web server
parent
c613c224
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
141 additions
and
63 deletions
+141
-63
rt/include/python/builtins.hpp
rt/include/python/builtins.hpp
+29
-13
rt/include/python/builtins/bytes.hpp
rt/include/python/builtins/bytes.hpp
+1
-0
rt/include/python/socket.hpp
rt/include/python/socket.hpp
+68
-33
rt/include/python/sys.hpp
rt/include/python/sys.hpp
+4
-0
trans/stdlib/socket.py
trans/stdlib/socket.py
+4
-4
trans/stdlib/sys.py
trans/stdlib/sys.py
+4
-1
trans/tests/a_a_a_webserver.py
trans/tests/a_a_a_webserver.py
+7
-5
trans/transpiler/phases/emit_cpp/block.py
trans/transpiler/phases/emit_cpp/block.py
+0
-4
trans/transpiler/phases/emit_cpp/expr.py
trans/transpiler/phases/emit_cpp/expr.py
+3
-0
trans/transpiler/phases/emit_cpp/file.py
trans/transpiler/phases/emit_cpp/file.py
+9
-2
trans/transpiler/phases/emit_cpp/function.py
trans/transpiler/phases/emit_cpp/function.py
+3
-0
trans/transpiler/phases/emit_cpp/module.py
trans/transpiler/phases/emit_cpp/module.py
+6
-1
trans/transpiler/phases/typing/block.py
trans/transpiler/phases/typing/block.py
+3
-0
No files found.
rt/include/python/builtins.hpp
View file @
667466a4
...
@@ -5,13 +5,14 @@
...
@@ -5,13 +5,14 @@
#ifndef TYPON_BUILTINS_HPP
#ifndef TYPON_BUILTINS_HPP
#define TYPON_BUILTINS_HPP
#define TYPON_BUILTINS_HPP
#include <fmt/format.h>
#include <iostream>
#include <iostream>
#include <optional>
#include <optional>
#include <ostream>
#include <ostream>
#include <string>
#include <string>
#include <typon/typon.hpp>
#include <python/basedef.hpp>
#include <python/basedef.hpp>
#include <typon/typon.hpp>
#ifdef __cpp_lib_unreachable
#ifdef __cpp_lib_unreachable
#include <utility>
#include <utility>
...
@@ -21,26 +22,35 @@
...
@@ -21,26 +22,35 @@
[[
noreturn
]]
inline
void
TYPON_UNREACHABLE
()
{
std
::
abort
();
}
[[
noreturn
]]
inline
void
TYPON_UNREACHABLE
()
{
std
::
abort
();
}
#endif
#endif
#define COMMA() ,
#define METHOD(ret, name, args, ...) \
struct
{
\
ret
operator
()
args
__VA_ARGS__
type
*
self
;
\
}
name
{
this
};
#define FUNCTION(ret, name, args, ...) \
struct
{
\
ret
operator
()
args
__VA_ARGS__
\
}
name
;
using
namespace
std
::
literals
;
using
namespace
std
::
literals
;
template
<
typename
T
>
template
<
typename
T
>
concept
PyUserType
=
requires
{
typename
T
::
type
;
};
concept
PyUserType
=
requires
{
typename
T
::
type
;
};
template
<
typename
T
>
template
<
typename
T
>
struct
RealType
{
struct
RealType
{
using
type
=
T
;
using
type
=
T
;
};
};
template
<
PyUserType
T
>
template
<
PyUserType
T
>
struct
RealType
<
T
>
{
struct
RealType
<
T
>
{
using
type
=
typename
T
::
type
;
using
type
=
typename
T
::
type
;
};
};
template
<
typename
T
>
using
PyObj
=
std
::
shared_ptr
<
typename
RealType
<
T
>::
type
>
;
template
<
typename
T
>
using
PyObj
=
std
::
shared_ptr
<
typename
RealType
<
T
>::
type
>
;
template
<
typename
T
,
typename
...
Args
>
template
<
typename
T
,
typename
...
Args
>
auto
pyobj
(
Args
&&
...
args
)
->
PyObj
<
T
>
{
auto
pyobj
(
Args
&&
...
args
)
->
PyObj
<
T
>
{
return
std
::
make_shared
<
typename
RealType
<
T
>::
type
>
(
return
std
::
make_shared
<
typename
RealType
<
T
>::
type
>
(
std
::
forward
<
Args
>
(
args
)...);
std
::
forward
<
Args
>
(
args
)...);
}
}
// typon_len
// typon_len
...
@@ -62,16 +72,16 @@ concept CppSize = requires(const T &t) {
...
@@ -62,16 +72,16 @@ concept CppSize = requires(const T &t) {
{
t
.
size
()
}
->
std
::
same_as
<
size_t
>
;
{
t
.
size
()
}
->
std
::
same_as
<
size_t
>
;
};
};
template
<
typename
T
>
template
<
typename
T
>
concept
PyLen
=
requires
(
const
T
&
t
)
{
concept
PyLen
=
requires
(
const
T
&
t
)
{
{
t
.
py_len
()
}
->
std
::
same_as
<
size_t
>
;
{
t
.
py_len
()
}
->
std
::
same_as
<
size_t
>
;
};
};
template
<
CppSize
T
>
template
<
CppSize
T
>
requires
(
!
PyLen
<
T
>
)
requires
(
!
PyLen
<
T
>
)
size_t
len
(
const
T
&
t
)
{
return
t
.
size
();
}
size_t
len
(
const
T
&
t
)
{
return
t
.
size
();
}
template
<
PyLen
T
>
size_t
len
(
const
T
&
t
)
{
return
t
.
py_len
();
}
template
<
PyLen
T
>
size_t
len
(
const
T
&
t
)
{
return
t
.
py_len
();
}
...
@@ -105,6 +115,12 @@ public:
...
@@ -105,6 +115,12 @@ public:
}
}
}
PyNone
{};
}
PyNone
{};
#define system_error(err, message) \
do
{
\
puts
(
message
);
\
throw
fmt
::
system_error
(
err
,
message
);
\
}
while
(
0
)
#include "builtins/bool.hpp"
#include "builtins/bool.hpp"
#include "builtins/complex.hpp"
#include "builtins/complex.hpp"
#include "builtins/dict.hpp"
#include "builtins/dict.hpp"
...
...
rt/include/python/builtins/bytes.hpp
View file @
667466a4
...
@@ -13,6 +13,7 @@ public:
...
@@ -13,6 +13,7 @@ public:
PyBytes
(
const
char
*
s
)
:
std
::
string
(
s
)
{}
PyBytes
(
const
char
*
s
)
:
std
::
string
(
s
)
{}
PyBytes
(
const
std
::
string
&
s
)
:
std
::
string
(
s
)
{}
PyBytes
(
const
std
::
string
&
s
)
:
std
::
string
(
s
)
{}
PyBytes
(
std
::
string
&&
s
)
:
std
::
string
(
std
::
move
(
s
))
{}
PyBytes
(
std
::
string
&&
s
)
:
std
::
string
(
std
::
move
(
s
))
{}
PyBytes
(
size_t
count
,
char
ch
)
:
std
::
string
(
count
,
ch
)
{}
template
<
class
InputIterator
>
template
<
class
InputIterator
>
PyBytes
(
InputIterator
first
,
InputIterator
last
)
:
std
::
string
(
first
,
last
)
{}
PyBytes
(
InputIterator
first
,
InputIterator
last
)
:
std
::
string
(
first
,
last
)
{}
...
...
rt/include/python/socket.hpp
View file @
667466a4
...
@@ -5,53 +5,88 @@
...
@@ -5,53 +5,88 @@
#ifndef TYPON_SOCKET_HPP
#ifndef TYPON_SOCKET_HPP
#define TYPON_SOCKET_HPP
#define TYPON_SOCKET_HPP
#undef SOCK_STREAM
#include "builtins.hpp"
#undef AF_INET6
#include <netinet/in.h>
#undef SOL_SOCKET
#undef SO_REUSEADDR
#include "builtins/bytes.hpp"
#include <tuple>
#include <tuple>
namespace
py_socket
{
namespace
py_socket
{
struct
socket_t
{
struct
socket_t
{
int
SOCK_STREAM
=
1
;
#undef SOCK_STREAM
int
AF_INET6
=
10
;
#undef AF_INET6
int
SOL_SOCKET
=
1
;
#undef SOL_SOCKET
int
SO_REUSEADDR
=
2
;
#undef SO_REUSEADDR
static
constexpr
int
SOCK_STREAM
=
1
;
static
constexpr
int
AF_INET6
=
10
;
static
constexpr
int
SOL_SOCKET
=
1
;
static
constexpr
int
SO_REUSEADDR
=
2
;
struct
{
struct
{
struct
type
{
struct
type
{
struct
{
METHOD
(
typon
::
Task
<
std
::
tuple
<
type
COMMA
()
std
::
string
>>
,
accept
,
(),
{
std
::
tuple
<
type
,
std
::
string
>
operator
()()
{
return
{};
}
int
connfd
=
co_await
typon
::
io
::
accept
(
self
->
fd
,
NULL
,
NULL
);
}
accept
;
co_return
std
::
make_tuple
(
type
(
connfd
),
std
::
string
(
""
));
// TODO
})
struct
{
METHOD
(
typon
::
Task
<
void
>
,
close
,
(),
void
operator
()()
{}
{
co_await
typon
::
io
::
close
(
self
->
fd
);
})
}
close
;
struct
{
METHOD
(
void
,
listen
,
(
int
backlog
),
{
void
operator
()(
int
backlog
)
{}
if
(
::
listen
(
self
->
fd
,
backlog
)
<
0
)
{
}
listen
;
self
->
close
();
system_error
(
errno
,
"listen()"
);
}
})
struct
{
METHOD
(
void
,
setsockopt
,
(
int
level
,
int
optname
,
int
optval
),
{
void
operator
()(
int
level
,
int
optname
,
int
optval
)
{}
if
(
::
setsockopt
(
self
->
fd
,
level
,
optname
,
&
optval
,
sizeof
(
int
))
<
0
)
{
}
setsockopt
;
system_error
(
errno
,
"setsockopt()"
);
}
})
struct
{
METHOD
(
void
,
bind
,
(
std
::
tuple
<
std
::
string
COMMA
()
int
>
address
),
{
void
operator
()(
std
::
tuple
<
std
::
string
,
int
>
address
)
{}
auto
[
host
,
port
]
=
address
;
}
bind
;
sockaddr_in6
addr
;
std
::
memset
(
&
addr
,
0
,
sizeof
(
addr
));
addr
.
sin6_family
=
AF_INET6
;
addr
.
sin6_port
=
htons
(
port
);
addr
.
sin6_addr
=
in6addr_any
;
if
(
::
bind
(
self
->
fd
,
(
const
sockaddr
*
)
&
addr
,
sizeof
(
addr
))
<
0
)
{
system_error
(
errno
,
"bind()"
);
}
})
struct
{
METHOD
(
typon
::
Task
<
PyBytes
>
,
recv
,
(
int
bufsize
),
{
PyBytes
operator
()(
int
bufsize
)
{
return
{};
}
PyBytes
buf
(
bufsize
,
'\0'
);
}
recv
;
co_await
typon
::
io
::
recv
(
self
->
fd
,
buf
.
data
(),
buf
.
size
(),
0
);
co_return
std
::
move
(
buf
);
})
struct
{
METHOD
(
typon
::
Task
<
void
>
,
send
,
(
PyBytes
data
),
{
void
operator
()(
PyBytes
data
)
{}
std
::
cout
<<
self
->
fd
<<
std
::
endl
;
}
send
;
if
(
int
sbytes
=
co_await
typon
::
io
::
send
(
self
->
fd
,
data
,
0
);
sbytes
<
0
)
{
co_await
self
->
close
();
system_error
(
-
sbytes
,
"send()"
);
}
})
type
(
int
fd
=
-
1
)
:
fd
(
fd
)
{
}
type
(
const
type
&
other
)
:
fd
(
other
.
fd
),
accept
(
this
),
close
(
this
),
listen
(
this
),
setsockopt
(
this
),
bind
(
this
),
recv
(
this
),
send
(
this
)
{
}
int
fd
;
};
};
type
operator
()(
int
family
,
int
type
)
{
return
{};
}
type
operator
()(
int
family
,
int
type_
)
{
if
(
int
fd
=
::
socket
(
family
,
type_
,
0
);
fd
>=
0
)
{
return
type
(
fd
);
}
else
{
system_error
(
errno
,
"socket()"
);
}
}
}
socket
;
}
socket
;
}
all
;
}
all
;
...
@@ -60,6 +95,6 @@ auto &get_all() { return all; }
...
@@ -60,6 +95,6 @@ auto &get_all() { return all; }
namespace
typon
{
namespace
typon
{
using
PySocket
=
decltype
(
py_socket
::
all
.
socket
)
::
type
;
using
PySocket
=
decltype
(
py_socket
::
all
.
socket
)
::
type
;
}
;
}
#endif // TYPON_SOCKET_HPP
#endif // TYPON_SOCKET_HPP
rt/include/python/sys.hpp
View file @
667466a4
...
@@ -14,6 +14,10 @@ struct sys_t {
...
@@ -14,6 +14,10 @@ struct sys_t {
static
constexpr
auto
&
stdout
=
std
::
cout
;
static
constexpr
auto
&
stdout
=
std
::
cout
;
static
constexpr
auto
&
stderr
=
std
::
cerr
;
static
constexpr
auto
&
stderr
=
std
::
cerr
;
PyList
<
PyStr
>
argv
;
PyList
<
PyStr
>
argv
;
FUNCTION
(
void
,
exit
,
(
int
code
),
{
std
::
exit
(
code
);
})
}
all
;
}
all
;
auto
&
get_all
()
{
auto
&
get_all
()
{
...
...
trans/stdlib/socket.py
View file @
667466a4
...
@@ -17,17 +17,17 @@ class socket:
...
@@ -17,17 +17,17 @@ class socket:
def
listen
(
self
,
backlog
:
int
)
->
None
:
def
listen
(
self
,
backlog
:
int
)
->
None
:
pass
pass
def
accept
(
self
)
->
tuple
[
Self
,
str
]:
def
accept
(
self
)
->
Task
[
tuple
[
Self
,
str
]
]:
pass
pass
def
recv
(
self
,
bufsize
:
int
)
->
bytes
:
def
recv
(
self
,
bufsize
:
int
)
->
Task
[
bytes
]
:
pass
pass
def
send
(
self
,
data
:
bytes
)
->
None
:
def
send
(
self
,
data
:
bytes
)
->
Task
[
None
]
:
pass
pass
def
__init__
(
self
,
family
:
int
,
type
:
int
)
->
Self
:
def
__init__
(
self
,
family
:
int
,
type
:
int
)
->
Self
:
pass
pass
def
close
(
self
)
->
None
:
def
close
(
self
)
->
Task
[
None
]
:
pass
pass
\ No newline at end of file
trans/stdlib/sys.py
View file @
667466a4
stdout
:
CppType
[
"auto&"
]
stdout
:
CppType
[
"auto&"
]
argv
:
list
[
str
]
argv
:
list
[
str
]
def
exit
(
code
:
int
)
->
None
:
...
\ No newline at end of file
trans/tests/a_a_a_webserver.py
View file @
667466a4
# coding: utf-8
# coding: utf-8
import
sys
import
sys
from
socket
import
socket
as
pysocket
,
SOCK_STREAM
,
AF_INET6
,
SOL_SOCKET
,
SO_REUSEADDR
from
socket
import
socket
,
SOCK_STREAM
,
AF_INET6
,
SOL_SOCKET
,
SO_REUSEADDR
from
typon
import
fork
from
typon
import
fork
BACKLOG
=
1024
BACKLOG
=
1024
...
@@ -15,13 +15,13 @@ response_fmt = \
...
@@ -15,13 +15,13 @@ response_fmt = \
"{}"
"{}"
def
create_listening_socket
(
port
):
def
create_listening_socket
(
port
):
sockfd
=
py
socket
(
AF_INET6
,
SOCK_STREAM
)
sockfd
=
socket
(
AF_INET6
,
SOCK_STREAM
)
sockfd
.
setsockopt
(
SOL_SOCKET
,
SO_REUSEADDR
,
1
)
sockfd
.
setsockopt
(
SOL_SOCKET
,
SO_REUSEADDR
,
1
)
sockfd
.
bind
((
""
,
port
))
sockfd
.
bind
((
""
,
port
))
sockfd
.
listen
(
BACKLOG
)
sockfd
.
listen
(
BACKLOG
)
return
sockfd
return
sockfd
def
handle_connection
(
connfd
:
py
socket
,
filepath
):
def
handle_connection
(
connfd
:
socket
,
filepath
):
buf
=
connfd
.
recv
(
1024
).
decode
(
"utf-8"
)
buf
=
connfd
.
recv
(
1024
).
decode
(
"utf-8"
)
length
=
buf
.
find
(
"
\
r
\
n
\
r
\
n
"
)
length
=
buf
.
find
(
"
\
r
\
n
\
r
\
n
"
)
content
=
"Hello world"
content
=
"Hello world"
...
@@ -29,19 +29,21 @@ def handle_connection(connfd: pysocket, filepath):
...
@@ -29,19 +29,21 @@ def handle_connection(connfd: pysocket, filepath):
connfd
.
send
(
response
.
encode
(
"utf-8"
))
connfd
.
send
(
response
.
encode
(
"utf-8"
))
connfd
.
close
()
connfd
.
close
()
def
server_loop
(
sockfd
:
py
socket
,
filepath
):
def
server_loop
(
sockfd
:
socket
,
filepath
):
while
True
:
while
True
:
connfd
,
_
=
sockfd
.
accept
()
connfd
,
_
=
sockfd
.
accept
()
fork
(
lambda
:
handle_connection
(
connfd
,
filepath
))
fork
(
lambda
:
handle_connection
(
connfd
,
filepath
))
break
def
server_loops
(
sockfd
,
filepath
):
def
server_loops
(
sockfd
,
filepath
):
for
i
in
range
(
20
):
for
i
in
range
(
1
):
fork
(
lambda
:
server_loop
(
sockfd
,
filepath
))
fork
(
lambda
:
server_loop
(
sockfd
,
filepath
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
if
len
(
sys
.
argv
)
>
2
:
if
len
(
sys
.
argv
)
>
2
:
print
(
"Usage: webserver [ filepath ]"
)
print
(
"Usage: webserver [ filepath ]"
)
sys
.
exit
(
1
)
filepath
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
==
2
else
"webserver.cpp"
filepath
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
==
2
else
"webserver.cpp"
print
(
"Serving"
,
filepath
,
"on port"
,
PORT
)
print
(
"Serving"
,
filepath
,
"on port"
,
PORT
)
...
...
trans/transpiler/phases/emit_cpp/block.py
View file @
667466a4
...
@@ -42,10 +42,6 @@ class BlockVisitor(NodeVisitor):
...
@@ -42,10 +42,6 @@ class BlockVisitor(NodeVisitor):
from
transpiler.phases.emit_cpp.function
import
FunctionVisitor
from
transpiler.phases.emit_cpp.function
import
FunctionVisitor
yield
from
FunctionVisitor
(
self
.
scope
,
CoroutineMode
.
TASK
).
emit_block
(
node
.
scope
,
block
())
yield
from
FunctionVisitor
(
self
.
scope
,
CoroutineMode
.
TASK
).
emit_block
(
node
.
scope
,
block
())
yield
"int main(int argc, char* argv[]) {"
yield
"py_sys::all.argv = PyList<PyStr>(std::vector<PyStr>(argv, argv + argc));"
yield
"root().call();"
yield
"}"
return
return
yield
"struct {"
yield
"struct {"
...
...
trans/transpiler/phases/emit_cpp/expr.py
View file @
667466a4
...
@@ -128,6 +128,7 @@ class ExpressionVisitor(NodeVisitor):
...
@@ -128,6 +128,7 @@ class ExpressionVisitor(NodeVisitor):
return
return
# TODO: precedence needed?
# TODO: precedence needed?
if
CoroutineMode
.
ASYNC
in
self
.
generator
and
node
.
is_await
:
if
CoroutineMode
.
ASYNC
in
self
.
generator
and
node
.
is_await
:
yield
"("
# TODO: temporary
yield
"co_await "
yield
"co_await "
node
.
in_await
=
True
node
.
in_await
=
True
elif
CoroutineMode
.
FAKE
in
self
.
generator
:
elif
CoroutineMode
.
FAKE
in
self
.
generator
:
...
@@ -136,6 +137,8 @@ class ExpressionVisitor(NodeVisitor):
...
@@ -136,6 +137,8 @@ class ExpressionVisitor(NodeVisitor):
yield
"("
yield
"("
yield
from
join
(
", "
,
map
(
self
.
reset
().
visit
,
node
.
args
))
yield
from
join
(
", "
,
map
(
self
.
reset
().
visit
,
node
.
args
))
yield
")"
yield
")"
if
CoroutineMode
.
ASYNC
in
self
.
generator
and
node
.
is_await
:
yield
")"
def
visit_Lambda
(
self
,
node
:
ast
.
Lambda
)
->
Iterable
[
str
]:
def
visit_Lambda
(
self
,
node
:
ast
.
Lambda
)
->
Iterable
[
str
]:
yield
"[]"
yield
"[]"
...
...
trans/transpiler/phases/emit_cpp/file.py
View file @
667466a4
...
@@ -12,5 +12,12 @@ class FileVisitor(BlockVisitor):
...
@@ -12,5 +12,12 @@ class FileVisitor(BlockVisitor):
stmt
:
ast
.
AST
stmt
:
ast
.
AST
yield
"#include <python/builtins.hpp>"
yield
"#include <python/builtins.hpp>"
visitor
=
ModuleVisitor
(
self
.
scope
)
visitor
=
ModuleVisitor
(
self
.
scope
)
for
stmt
in
node
.
body
:
code
=
[
line
for
stmt
in
node
.
body
for
line
in
visitor
.
visit
(
stmt
)]
yield
from
visitor
.
visit
(
stmt
)
yield
from
visitor
.
includes
yield
"namespace PROGRAMNS {"
yield
from
code
yield
"}"
yield
"int main(int argc, char* argv[]) {"
yield
"py_sys::all.argv = PyList<PyStr>(std::vector<PyStr>(argv, argv + argc));"
yield
"PROGRAMNS::root().call();"
yield
"}"
trans/transpiler/phases/emit_cpp/function.py
View file @
667466a4
...
@@ -78,3 +78,6 @@ class FunctionVisitor(BlockVisitor):
...
@@ -78,3 +78,6 @@ class FunctionVisitor(BlockVisitor):
for
child
in
items
:
for
child
in
items
:
yield
from
FunctionVisitor
(
scope
,
self
.
generator
).
visit
(
child
)
yield
from
FunctionVisitor
(
scope
,
self
.
generator
).
visit
(
child
)
yield
"}"
yield
"}"
def
visit_Break
(
self
,
node
:
ast
.
Break
)
->
Iterable
[
str
]:
yield
"break;"
trans/transpiler/phases/emit_cpp/module.py
View file @
667466a4
...
@@ -2,6 +2,8 @@
...
@@ -2,6 +2,8 @@
import
ast
import
ast
from
typing
import
Iterable
from
typing
import
Iterable
from
dataclasses
import
dataclass
,
field
from
transpiler.phases.emit_cpp
import
CoroutineMode
from
transpiler.phases.emit_cpp
import
CoroutineMode
from
transpiler.phases.emit_cpp.block
import
BlockVisitor
from
transpiler.phases.emit_cpp.block
import
BlockVisitor
from
transpiler.phases.emit_cpp.class_
import
ClassVisitor
from
transpiler.phases.emit_cpp.class_
import
ClassVisitor
...
@@ -10,7 +12,9 @@ from transpiler.utils import compare_ast
...
@@ -10,7 +12,9 @@ from transpiler.utils import compare_ast
# noinspection PyPep8Naming
# noinspection PyPep8Naming
@
dataclass
class
ModuleVisitor
(
BlockVisitor
):
class
ModuleVisitor
(
BlockVisitor
):
includes
:
list
[
str
]
=
field
(
default_factory
=
list
)
def
visit_Import
(
self
,
node
:
ast
.
Import
)
->
Iterable
[
str
]:
def
visit_Import
(
self
,
node
:
ast
.
Import
)
->
Iterable
[
str
]:
for
alias
in
node
.
names
:
for
alias
in
node
.
names
:
if
alias
.
name
==
"typon"
:
if
alias
.
name
==
"typon"
:
...
@@ -20,7 +24,8 @@ class ModuleVisitor(BlockVisitor):
...
@@ -20,7 +24,8 @@ class ModuleVisitor(BlockVisitor):
yield
f'auto&
{
alias
.
asname
or
alias
.
name
}
= py_
{
alias
.
name
}
::get_all();'
yield
f'auto&
{
alias
.
asname
or
alias
.
name
}
= py_
{
alias
.
name
}
::get_all();'
def
import_module
(
self
,
name
:
str
)
->
Iterable
[
str
]:
def
import_module
(
self
,
name
:
str
)
->
Iterable
[
str
]:
yield
f'#include "python/
{
name
}
.hpp"'
self
.
includes
.
append
(
f'#include "python/
{
name
}
.hpp"'
)
yield
""
def
visit_ImportFrom
(
self
,
node
:
ast
.
ImportFrom
)
->
Iterable
[
str
]:
def
visit_ImportFrom
(
self
,
node
:
ast
.
ImportFrom
)
->
Iterable
[
str
]:
if
node
.
module
==
"typon"
:
if
node
.
module
==
"typon"
:
...
...
trans/transpiler/phases/typing/block.py
View file @
667466a4
...
@@ -179,3 +179,6 @@ class ScoperBlockVisitor(ScoperVisitor):
...
@@ -179,3 +179,6 @@ class ScoperBlockVisitor(ScoperVisitor):
if
isinstance
(
node
,
ast
.
AST
):
if
isinstance
(
node
,
ast
.
AST
):
super
().
visit
(
node
)
super
().
visit
(
node
)
node
.
scope
=
self
.
scope
node
.
scope
=
self
.
scope
def
visit_Break
(
self
,
node
:
ast
.
Break
):
pass
# TODO: check in loop
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