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
04557080
Commit
04557080
authored
Mar 15, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update
parent
2355cf46
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
86 additions
and
48 deletions
+86
-48
typon/include/python/builtins.hpp
typon/include/python/builtins.hpp
+1
-4
typon/include/python/builtins/int.hpp
typon/include/python/builtins/int.hpp
+2
-1
typon/include/python/builtins/list.hpp
typon/include/python/builtins/list.hpp
+12
-14
typon/include/python/builtins/str.hpp
typon/include/python/builtins/str.hpp
+2
-2
typon/include/python/referencemodel.hpp
typon/include/python/referencemodel.hpp
+32
-17
typon/include/python/socket.hpp
typon/include/python/socket.hpp
+3
-5
typon/trans/tests/webserver.py
typon/trans/tests/webserver.py
+5
-4
typon/trans/transpiler/phases/desugar_subscript/__init__.py
typon/trans/transpiler/phases/desugar_subscript/__init__.py
+25
-0
typon/trans/transpiler/phases/emit_cpp/visitors.py
typon/trans/transpiler/phases/emit_cpp/visitors.py
+2
-1
typon/trans/transpiler/transpiler.py
typon/trans/transpiler/transpiler.py
+2
-0
No files found.
typon/include/python/builtins.hpp
View file @
04557080
...
...
@@ -98,10 +98,7 @@ auto len(const T &t) {
template <PyLen T> size_t len(const T &t) { return t.py_len(); }*/
template
<
typename
T
>
auto
len
(
const
T
&
t
)
{
return
dot
(
t
,
oo__len__oo
)();
}
template
<
typename
T
>
auto
len
(
T
&&
t
)
{
return
dot
(
std
::
forward
<
T
>
(
t
),
oo__len__oo
)();
}
template
<
typename
T
>
concept
PyNext
=
requires
(
T
t
)
{
...
...
typon/include/python/builtins/int.hpp
View file @
04557080
...
...
@@ -93,10 +93,11 @@ struct TyInt__oo : classtype<_Base0, TyInt__oo<>> {
int
value
;
Obj
(
int
value
=
0
)
:
value
(
value
)
{}
Obj
(
const
Obj
&
other
)
:
value
(
other
.
value
)
{}
operator
int
()
const
{
return
value
;
}
};
auto
operator
()(
int
value
)
const
{
return
Obj
(
value
);
}
auto
operator
()(
auto
value
)
const
{
return
Obj
(
value
);
}
};
static
constexpr
TyInt__oo
<>
TyInt
{};
...
...
typon/include/python/builtins/list.hpp
View file @
04557080
...
...
@@ -17,9 +17,7 @@ template <typename _Base0 = object>
struct
TyList__oo
:
classtype
<
_Base0
,
TyList__oo
<>>
{
struct
:
method
{
auto
operator
()(
auto
self
)
const
{
return
TyList__oo
<>
{}(
self
->
_v
);
}
auto
operator
()(
auto
self
)
const
{
return
TyList__oo
<>
{}(
self
->
_v
);
}
}
static
constexpr
copy
{};
struct
:
method
{
...
...
@@ -74,7 +72,7 @@ struct TyList__oo : classtype<_Base0, TyList__oo<>> {
struct
:
method
{
auto
operator
()(
auto
self
,
auto
i
)
const
{
if
(
i
<
0
)
{
i
+=
self
->
_v
->
size
(
);
i
+=
TyInt
(
self
->
_v
->
size
()
);
}
if
(
i
<
0
||
i
>=
self
->
_v
->
size
())
{
throw
std
::
out_of_range
(
"list index out of range"
);
...
...
typon/include/python/builtins/str.hpp
View file @
04557080
typon/include/python/referencemodel.hpp
View file @
04557080
...
...
@@ -1051,11 +1051,18 @@ namespace meta {
template <typename Left, typename Right> \
concept Right##DUNDER##able = requires(Left left, Right right) { \
right->oo__r##DUNDER##__oo(right, left); \
}; \
template <typename Left, typename Right> \
concept Aug##DUNDER##able = requires(Left left, Right right) { \
left->oo__i##DUNDER##__oo(left, right); \
}; \
\
template <typename Left, typename Right> \
concept DUNDER##able = \
Left##DUNDER##able<Left, Right> || Right##DUNDER##able<Left, Right>; \
template <typename Left, typename Right> \
concept NormalOrAug##DUNDER##able = \
DUNDER##able<Left, Right> || Aug##DUNDER##able<Left, Right>; \
} \
template <meta::object Left, meta::object Right> \
requires meta::DUNDER \
...
...
@@ -1069,6 +1076,16 @@ namespace meta {
oo__r##DUNDER##__oo)(std::forward<Left>(left)); \
} \
} \
} \
template <meta::object Left, meta::object Right> \
requires meta::NormalOrAug \
##DUNDER##able<Left, Right> auto operator OP##=(Left &&left, Right &&right) { \
if constexpr (meta::Aug##DUNDER##able<Left, Right>) { \
return dot(std::forward<Left>(left), \
oo__i##DUNDER##__oo)(std::forward<Right>(right)); \
} else { \
return (left = (left OP right)); \
} \
}
#define SIMPLE_OP(OP, DUNDER) \
...
...
@@ -1127,8 +1144,8 @@ LR_OP(%, mod)
LR_OP
(
<<
,
lshift
)
LR_OP
(
>>
,
rshift
)
LR_OP
(
&
,
and
)
LR_OP
(
|
,
or
)
LR_OP
(
^
,
xor
)
LR_OP
(
|
,
or
)
// TODO: iadd...
...
...
@@ -1139,23 +1156,21 @@ SIMPLE_OP(!=, ne)
SIMPLE_OP
(
>
,
gt
)
SIMPLE_OP
(
>=
,
ge
)
namespace
meta
{
template
<
typename
Left
,
typename
Right
>
concept
DUNDERgetitemable
=
requires
(
Left
left
,
Right
right
)
{
left
->
oo__getitem__oo
(
left
,
right
);
};
template
<
typename
Left
,
typename
Right
>
concept
DUNDERsetitemable
=
requires
(
Left
left
,
Right
right
)
{
left
->
oo__setitem__oo
(
left
,
right
);
};
namespace
meta
{
template
<
typename
Left
,
typename
Right
>
concept
DUNDERgetitemable
=
requires
(
Left
left
,
Right
right
)
{
left
->
oo__getitem__oo
(
left
,
right
);
};
template
<
typename
Left
,
typename
Right
>
concept
DUNDERsetitemable
=
requires
(
Left
left
,
Right
right
)
{
left
->
oo__setitem__oo
(
left
,
right
);
};
}
}
// namespace meta
/* template <meta::object Left, meta::object Right>
requires meta::DUNDERgetitemable<Left, Right> auto operator [](Left &&left, Right &&right) {
return dot(std::forward<Left>(left),
requires meta::DUNDERgetitemable<Left, Right> auto operator [](Left &&left,
Right &&right) {
return dot(std::forward<Left>(left),
oo__getitem__oo)(std::forward<Right>(right));
}*/
// todo: setitem?
// todo: setitem?
}
// namespace referencemodel
...
...
typon/include/python/socket.hpp
View file @
04557080
...
...
@@ -27,7 +27,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
template
<
typename
_Base0
=
object
>
struct
socket_t__oo
:
referencemodel
::
classtype
<
_Base0
,
socket_t__oo
<>>
{
template
<
typename
T
>
template
<
typename
T
>
struct
Obj
:
referencemodel
::
instance
<
socket_t__oo
<>
,
Obj
<
T
>>
{
Obj
(
int
fd
=
-
1
)
:
fd
(
fd
)
{}
...
...
@@ -37,7 +37,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
};
struct
:
referencemodel
::
method
{
template
<
typename
T
=
void
>
template
<
typename
T
=
void
>
auto
operator
()(
auto
self
)
->
typon
::
Task
<
std
::
tuple
<
decltype
(
rc
(
Obj
<
T
>
{
-
1
})),
decltype
(
""
_ps
)
>>
const
{
int
connfd
=
co_await
typon
::
io
::
accept
(
self
->
fd
,
NULL
,
NULL
);
...
...
@@ -49,7 +49,6 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
}
}
static
constexpr
accept
{};
struct
:
referencemodel
::
method
{
auto
operator
()(
auto
self
,
int
level
,
int
optname
,
int
optval
)
const
{
if
(
::
setsockopt
(
self
->
fd
,
level
,
optname
,
&
optval
,
sizeof
(
int
))
<
0
)
{
...
...
@@ -138,8 +137,7 @@ struct socket__oo : referencemodel::moduletype<socket__oo<>> {
}
})*/
template
<
typename
T
=
void
>
auto
operator
()(
int
family
,
int
type_
)
const
{
template
<
typename
T
=
void
>
auto
operator
()(
int
family
,
int
type_
)
const
{
if
(
int
fd
=
::
socket
(
family
,
type_
,
0
);
fd
>=
0
)
{
return
rc
(
Obj
<
T
>
{
fd
});
}
else
{
...
...
typon/trans/tests/webserver.py
View file @
04557080
...
...
@@ -47,11 +47,12 @@ def server_loop(sockfd, filepath):
if
__name__
==
"__main__"
:
PORT
=
8000
if
len
(
sys
.
argv
)
>
2
:
print
(
"Usage: webserver [ filepath ]"
)
sys
.
exit
(
1
)
#
if len(sys.argv) > 2:
#
print("Usage: webserver [ filepath ]")
#
sys.exit(1)
# filepath = sys.argv[1] if len(sys.argv) == 2 else "requirements.txt"
l
=
len
(
sys
.
argv
)
filepath
=
sys
.
argv
[
1
]
if
l
==
2
else
"requirements.txt"
filepath
=
"requirements.txt"
print
(
"Serving"
,
filepath
,
"on port"
,
PORT
)
...
...
typon/trans/transpiler/phases/desugar_subscript/__init__.py
0 → 100644
View file @
04557080
# coding: utf-8
import
ast
from
transpiler.phases.utils
import
make_lnd
from
transpiler.utils
import
linenodata
class
DesugarSubscript
(
ast
.
NodeTransformer
):
def
visit_Subscript
(
self
,
node
:
ast
.
Subscript
):
match
node
.
ctx
:
case
ast
.
Load
():
return
ast
.
Call
(
func
=
ast
.
Attribute
(
value
=
node
.
value
,
attr
=
"__getitem__"
,
ctx
=
ast
.
Load
(),
),
args
=
[
node
.
slice
],
keywords
=
[],
**
linenodata
(
node
)
)
case
ast
.
Store
(),
ast
.
Del
():
raise
NotImplementedError
(
"Subscript assignment and deletion not supported"
)
case
_
:
raise
ValueError
(
f"Unexpected context
{
node
.
ctx
!
r
}
"
,
linenodata
(
node
))
\ No newline at end of file
typon/trans/transpiler/phases/emit_cpp/visitors.py
View file @
04557080
...
...
@@ -53,7 +53,8 @@ class NodeVisitor(UniversalVisitor):
def
fix_name
(
self
,
name
:
str
)
->
str
:
if
name
.
startswith
(
"__"
)
and
name
.
endswith
(
"__"
):
return
f"py_
{
name
[
2
:
-
2
]
}
"
# return f"py_{name[2:-2]}"
return
f"oo
{
name
}
oo"
return
MAPPINGS
.
get
(
name
,
name
)
def
visit_BaseType
(
self
,
node
:
BaseType
)
->
Iterable
[
str
]:
...
...
typon/trans/transpiler/transpiler.py
View file @
04557080
...
...
@@ -10,6 +10,7 @@ import colorful as cf
from
transpiler
import
error_display
from
transpiler.phases.desugar_compare
import
DesugarCompare
from
transpiler.phases.desugar_op
import
DesugarOp
from
transpiler.phases.desugar_subscript
import
DesugarSubscript
from
transpiler.phases.desugar_with
import
DesugarWith
from
transpiler.phases.emit_cpp.module
import
emit_module
from
transpiler.phases.if_main
import
IfMainVisitor
...
...
@@ -36,6 +37,7 @@ def transpile(source, name: str, path: Path):
node
=
DesugarWith
().
visit
(
node
)
node
=
DesugarCompare
().
visit
(
node
)
node
=
DesugarOp
().
visit
(
node
)
node
=
DesugarSubscript
().
visit
(
node
)
return
node
module
=
parse_module
(
path
.
stem
,
[
path
.
parent
,
TYPON_STD
],
preprocess
=
preprocess
)
...
...
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