Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tom Niget
typon
Commits
208daf4e
Commit
208daf4e
authored
Aug 13, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add object type
parent
c5aae2f9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
7 deletions
+43
-7
trans/stdlib/__init__.py
trans/stdlib/__init__.py
+9
-0
trans/transpiler/phases/typing/__init__.py
trans/transpiler/phases/typing/__init__.py
+3
-2
trans/transpiler/phases/typing/expr.py
trans/transpiler/phases/typing/expr.py
+8
-1
trans/transpiler/phases/typing/types.py
trans/transpiler/phases/typing/types.py
+23
-4
No files found.
trans/stdlib/__init__.py
View file @
208daf4e
from
typing
import
Self
,
TypeVar
,
Generic
,
Protocol
class
object
:
def
__eq__
(
self
,
other
:
Self
)
->
bool
:
...
def
__ne__
(
self
,
other
:
Self
)
->
bool
:
...
class
int
:
def
__add__
(
self
,
other
:
Self
)
->
Self
:
...
...
...
@@ -11,6 +15,8 @@ class int:
def
__neg__
(
self
)
->
Self
:
...
def
__init__
(
self
,
x
:
str
)
->
None
:
...
def
__lt__
(
self
,
other
:
Self
)
->
bool
:
...
def
__gt__
(
self
,
other
:
Self
)
->
bool
:
...
assert
int
.
__add__
...
...
@@ -64,6 +70,7 @@ class list(Generic[U]):
def
__iter__
(
self
)
->
Iterator
[
U
]:
...
def
__len__
(
self
)
->
int
:
...
def
append
(
self
,
value
:
U
)
->
None
:
...
def
__contains__
(
self
,
item
:
U
)
->
bool
:
...
assert
[
1
,
2
].
__iter__
()
assert
list
[
int
].
__iter__
...
...
@@ -82,6 +89,8 @@ assert [1, 2, 3][1]
def
iter
(
x
:
Iterable
[
U
])
->
Iterator
[
U
]:
...
assert
iter
def
next
(
it
:
Iterator
[
U
],
default
:
None
)
->
U
:
...
# what happens with multiple functions
...
...
trans/transpiler/phases/typing/__init__.py
View file @
208daf4e
...
...
@@ -5,7 +5,7 @@ from transpiler.phases.typing.scope import VarKind, VarDecl, ScopeKind, Scope
from
transpiler.phases.typing.stdlib
import
PRELUDE
,
StdlibVisitor
from
transpiler.phases.typing.types
import
TY_TYPE
,
TY_INT
,
TY_STR
,
TY_BOOL
,
TY_COMPLEX
,
TY_NONE
,
FunctionType
,
\
TypeVariable
,
CppType
,
PyList
,
TypeType
,
Forked
,
Task
,
Future
,
PyIterator
,
TupleType
,
TypeOperator
,
BaseType
,
\
ModuleType
,
TY_BYTES
,
TY_FLOAT
,
PyDict
,
TY_SLICE
ModuleType
,
TY_BYTES
,
TY_FLOAT
,
PyDict
,
TY_SLICE
,
TY_OBJECT
PRELUDE
.
vars
.
update
({
# "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT),
...
...
@@ -34,7 +34,8 @@ PRELUDE.vars.update({
"Future"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
Future
)),
"Iterator"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
PyIterator
)),
"tuple"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TupleType
)),
"slice"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_SLICE
))
"slice"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_SLICE
)),
"object"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_OBJECT
)),
})
typon_std
=
Path
(
__file__
).
parent
.
parent
.
parent
.
parent
/
"stdlib"
...
...
trans/transpiler/phases/typing/expr.py
View file @
208daf4e
...
...
@@ -157,7 +157,7 @@ class ScoperExprVisitor(ScoperVisitor):
ltype
=
self
.
visit
(
node
.
value
)
return
self
.
visit_getattr
(
ltype
,
node
.
attr
)
def
visit_getattr
(
self
,
ltype
:
BaseType
,
name
:
str
):
def
visit_getattr
(
self
,
ltype
:
BaseType
,
name
:
str
)
->
BaseType
:
bound
=
True
if
isinstance
(
ltype
,
TypeType
):
ltype
=
ltype
.
type_object
...
...
@@ -179,6 +179,13 @@ class ScoperExprVisitor(ScoperVisitor):
else
:
return
meth
from
transpiler.phases.typing.exceptions
import
MissingAttributeError
parents
=
ltype
.
iter_hierarchy_recursive
()
next
(
parents
)
for
p
in
parents
:
try
:
return
self
.
visit_getattr
(
p
,
name
)
except
MissingAttributeError
as
e
:
pass
raise
MissingAttributeError
(
ltype
,
name
)
def
visit_List
(
self
,
node
:
ast
.
List
)
->
BaseType
:
...
...
trans/transpiler/phases/typing/types.py
View file @
208daf4e
...
...
@@ -8,17 +8,38 @@ from typing import Dict, Optional, List, ClassVar, Callable
from
transpiler.utils
import
highlight
def
get_default_parents
():
if
obj
:
=
globals
().
get
(
"TY_OBJECT"
):
return
[
obj
]
return
[]
@
dataclass
(
eq
=
False
)
class
BaseType
(
ABC
):
members
:
Dict
[
str
,
"BaseType"
]
=
field
(
default_factory
=
dict
,
init
=
False
)
methods
:
Dict
[
str
,
"FunctionType"
]
=
field
(
default_factory
=
dict
,
init
=
False
)
parents
:
List
[
"BaseType"
]
=
field
(
default_factory
=
list
,
init
=
False
)
parents
:
List
[
"BaseType"
]
=
field
(
default_factory
=
get_default_parents
,
init
=
False
)
typevars
:
List
[
"TypeVariable"
]
=
field
(
default_factory
=
list
,
init
=
False
)
def
get_parents
(
self
)
->
List
[
"BaseType"
]:
return
self
.
parents
def
iter_hierarchy_recursive
(
self
)
->
typing
.
Iterator
[
"BaseType"
]:
cache
=
set
()
from
queue
import
Queue
queue
=
Queue
()
queue
.
put
(
self
)
while
not
queue
.
empty
():
cur
=
queue
.
get
()
yield
cur
if
cur
in
cache
:
continue
cache
.
add
(
cur
)
if
cur
==
TY_OBJECT
:
continue
for
p
in
cur
.
get_parents
():
queue
.
put
(
p
)
def
resolve
(
self
)
->
"BaseType"
:
return
self
...
...
@@ -343,6 +364,7 @@ class TypeType(TypeOperator):
self
.
args
[
0
]
=
value
TY_OBJECT
=
TypeOperator
.
make_type
(
"object"
)
TY_SELF
=
TypeOperator
.
make_type
(
"Self"
)
def
self_gen_sub
(
this
,
typevars
,
_
):
if
this
is
not
None
:
...
...
@@ -350,9 +372,6 @@ def self_gen_sub(this, typevars, _):
return
TY_SELF
TY_SELF
.
gen_sub
=
self_gen_sub
TY_BOOL
=
TypeOperator
.
make_type
(
"bool"
)
DEFAULT_EQ
=
FunctionType
([
TY_SELF
,
TY_SELF
],
TY_BOOL
)
TY_BOOL
.
_add_default_eq
()
TY_TYPE
=
TypeOperator
.
make_type
(
"type"
)
TY_INT
=
TypeOperator
.
make_type
(
"int"
)
TY_FLOAT
=
TypeOperator
.
make_type
(
"float"
)
...
...
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