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
3b3d5c13
Commit
3b3d5c13
authored
Jun 14, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix support for generic parent classes
parent
ba47aacb
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
10 deletions
+42
-10
trans/stdlib/__init__.py
trans/stdlib/__init__.py
+12
-4
trans/tests/builtins_test.py
trans/tests/builtins_test.py
+4
-1
trans/transpiler/phases/typing/stdlib.py
trans/transpiler/phases/typing/stdlib.py
+17
-4
trans/transpiler/phases/typing/types.py
trans/transpiler/phases/typing/types.py
+9
-1
No files found.
trans/stdlib/__init__.py
View file @
3b3d5c13
...
...
@@ -22,6 +22,14 @@ class HasLen:
def
len
(
x
:
HasLen
)
->
int
:
...
class
Iterator
(
Generic
[
U
]):
def
__iter__
(
self
)
->
Self
:
...
def
__next__
(
self
)
->
U
:
...
class
Iterable
(
Generic
[
U
]):
def
__iter__
(
self
)
->
Iterator
[
U
]:
...
class
str
(
HasLen
):
def
find
(
self
,
sub
:
Self
)
->
int
:
...
def
format
(
self
,
*
args
)
->
Self
:
...
...
...
@@ -30,7 +38,7 @@ class str(HasLen):
class
bytes
(
HasLen
):
def
decode
(
self
,
encoding
:
str
)
->
str
:
...
class
list
(
Generic
[
U
],
HasLen
):
class
list
(
Generic
[
U
],
HasLen
,
Iterable
[
U
]
):
def
__add__
(
self
,
other
:
Self
)
->
Self
:
...
...
...
@@ -40,16 +48,16 @@ class list(Generic[U], HasLen):
def
pop
(
self
,
index
:
int
=
-
1
)
->
U
:
...
assert
[
1
,
2
].
__iter__
()
assert
list
[
int
].
__iter__
assert
(
len
([
"a"
]))
assert
[
1
,
2
,
3
][
1
]
class
Iterator
(
Generic
[
U
]):
def
__iter__
(
self
)
->
Self
:
...
def
__next__
(
self
)
->
U
:
...
def
next
(
it
:
Iterator
[
U
],
default
:
None
)
->
U
:
...
...
...
trans/tests/builtins_test.py
View file @
3b3d5c13
...
...
@@ -40,7 +40,10 @@ if __name__ == "__main__":
print
(
is_cpp
)
# TODO: doesn't compile under G++ 12.2, fixed in trunk on March 15
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98056
sum
=
0
for
i
in
range
(
15
):
sum
+=
i
print
(
"C++ "
if
is_cpp
()
else
"Python"
,
"res="
,
5
,
"."
,
True
,
[
4
,
5
,
6
],
{
7
,
8
,
9
},
[
1
,
2
]
+
[
3
,
4
],
[
5
,
6
]
*
3
,
{
1
:
7
,
9
:
3
},
0x55
&
7
==
5
,
3j
)
3j
,
sum
)
print
()
trans/transpiler/phases/typing/stdlib.py
View file @
3b3d5c13
...
...
@@ -42,15 +42,26 @@ class StdlibVisitor(NodeVisitorSeq):
if
existing
:
=
self
.
scope
.
get
(
node
.
name
):
ty
=
existing
.
type
else
:
ty
=
TypeType
(
TypeOperator
([],
node
.
name
))
class
TheType
(
TypeOperator
):
def
__init__
(
self
,
*
args
):
super
().
__init__
(
args
,
node
.
name
)
ty
=
TypeType
(
TheType
)
self
.
scope
.
vars
[
node
.
name
]
=
VarDecl
(
VarKind
.
LOCAL
,
ty
)
typevars
=
[]
for
b
in
node
.
bases
:
if
isinstance
(
b
,
ast
.
Subscript
)
and
isinstance
(
b
.
value
,
ast
.
Name
)
and
b
.
value
.
id
==
"Generic"
:
if
isinstance
(
b
,
ast
.
Subscript
):
if
isinstance
(
b
.
slice
,
ast
.
Name
):
typevars
=
[
b
.
slice
.
id
]
sliceval
=
[
b
.
slice
.
id
]
elif
isinstance
(
b
.
slice
,
ast
.
Tuple
):
typevars
=
[
n
.
id
for
n
in
b
.
slice
.
value
.
elts
]
sliceval
=
[
n
.
id
for
n
in
b
.
slice
.
value
.
elts
]
if
isinstance
(
b
.
value
,
ast
.
Name
)
and
b
.
value
.
id
==
"Generic"
:
typevars
=
sliceval
else
:
idxs
=
[
typevars
.
index
(
v
)
for
v
in
sliceval
]
parent
=
self
.
visit
(
b
.
value
)
assert
isinstance
(
parent
,
TypeType
)
assert
isinstance
(
ty
.
type_object
,
ABCMeta
)
ty
.
type_object
.
gen_parents
.
append
(
lambda
selfvars
:
parent
.
type_object
(
*
[
selfvars
[
i
]
for
i
in
idxs
]))
else
:
parent
=
self
.
visit
(
b
)
assert
isinstance
(
parent
,
TypeType
)
...
...
@@ -58,6 +69,8 @@ class StdlibVisitor(NodeVisitorSeq):
ty
.
type_object
.
gen_parents
.
append
(
parent
.
type_object
)
else
:
ty
.
type_object
.
parents
.
append
(
parent
.
type_object
)
if
not
typevars
and
not
existing
:
ty
.
type_object
=
ty
.
type_object
()
cl_scope
=
self
.
scope
.
child
(
ScopeKind
.
CLASS
)
visitor
=
StdlibVisitor
(
cl_scope
,
ty
)
for
var
in
typevars
:
...
...
trans/transpiler/phases/typing/types.py
View file @
3b3d5c13
...
...
@@ -139,7 +139,11 @@ class TypeOperator(BaseType, ABC):
self
.
name
=
self
.
__class__
.
__name__
for
name
,
factory
in
self
.
gen_methods
.
items
():
self
.
methods
[
name
]
=
factory
(
self
)
self
.
parents
.
extend
(
self
.
gen_parents
)
for
gp
in
self
.
gen_parents
:
if
not
isinstance
(
gp
,
BaseType
):
gp
=
gp
(
self
.
args
)
self
.
parents
.
append
(
gp
)
self
.
methods
=
{
**
gp
.
methods
,
**
self
.
methods
}
def
unify_internal
(
self
,
other
:
BaseType
):
if
not
isinstance
(
other
,
TypeOperator
):
...
...
@@ -300,6 +304,10 @@ class TypeType(TypeOperator):
def
type_object
(
self
)
->
BaseType
:
return
self
.
args
[
0
]
@
type_object
.
setter
def
type_object
(
self
,
value
:
BaseType
):
self
.
args
[
0
]
=
value
TY_TYPE
=
TypeOperator
.
make_type
(
"type"
)
TY_INT
=
TypeOperator
.
make_type
(
"int"
)
...
...
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