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
0cecacfe
Commit
0cecacfe
authored
Jul 30, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Continue work on errors
parent
d36c7e8f
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
93 additions
and
41 deletions
+93
-41
trans/main.py
trans/main.py
+10
-1
trans/requirements.txt
trans/requirements.txt
+2
-0
trans/tests/a_a_a_errtest.py
trans/tests/a_a_a_errtest.py
+3
-4
trans/tests/a_a_call_py_1.py
trans/tests/a_a_call_py_1.py
+11
-0
trans/tests/a_a_check_socket_listening.py
trans/tests/a_a_check_socket_listening.py
+42
-0
trans/transpiler/__init__.py
trans/transpiler/__init__.py
+9
-3
trans/transpiler/phases/typing/block.py
trans/transpiler/phases/typing/block.py
+6
-8
trans/transpiler/phases/typing/exceptions.py
trans/transpiler/phases/typing/exceptions.py
+9
-24
trans/transpiler/utils.py
trans/transpiler/utils.py
+1
-1
No files found.
trans/main.py
View file @
0cecacfe
print
(
"Main"
)
\ No newline at end of file
print
(
"Main"
)
# TODO
"""
webserver => investiguer
scanfs => fork
promesse => faire
self/this => bind/dot
stocker smart ptr dans closures
"""
\ No newline at end of file
trans/requirements.txt
View file @
0cecacfe
...
...
@@ -2,6 +2,8 @@ clang-format==15.0.7
pytype~=2023.4.11
dataclasses~=0.6
python-dotenv~=1.0.0
nanobind~=1.4.0
colorama~=0.4.6
numpy~=1.25.1
pygments~=2.15.1
colorful~=0.5.5
\ No newline at end of file
trans/tests/a_a_a_errtest.py
View file @
0cecacfe
import
sys
import
math
;
x
=
(
math
.
abcd
)
def
f
(
x
:
int
):
return
x
import
math
x
=
5
x
:
str
=
"str"
if
__name__
==
"__main__"
:
...
...
trans/tests/a_a_call_py_1.py
0 → 100644
View file @
0cecacfe
# test numpy interop
from
numpy
import
square
import
math
if
__name__
==
"__main__"
:
x
=
[
1
,
2
,
3
,
4
]
y
:
list
[
int
]
=
square
(
x
)
print
(
x
,
y
)
f
:
int
=
math
.
factorial
(
5
)
print
(
"5! ="
,
f
)
\ No newline at end of file
trans/tests/a_a_check_socket_listening.py
0 → 100644
View file @
0cecacfe
"""
def sense(self):
host = self.getConfig('host')
port = self.getConfig('port')
path = self.getConfig('pathname')
abstract = self.getConfig('abstract')
if host:
if path or abstract or not port:
self.logger.error(ADDRESS_USAGE)
return
# type of port must be int or str, unicode is not accepted.
family, _, _, _, addr = socket.getaddrinfo(host, int(port))[0]
else:
if bool(path) == bool(abstract):
self.logger.error(ADDRESS_USAGE)
return
family = socket.AF_UNIX
addr = path or '
\
0
' + abstract
s = socket.socket(family, socket.SOCK_STREAM)
try:
s.connect(addr)
except socket.error as e:
self.logger.error('%s: %s', type(e).__name__, e)
else:
self.logger.info("socket connection OK %r", addr)
finally:
s.close()
"""
import
sys
# prog host port
# prog path
# prog @abstract
if
__name__
==
"__main__"
:
pass
\ No newline at end of file
trans/transpiler/__init__.py
View file @
0cecacfe
...
...
@@ -60,9 +60,14 @@ def exception_hook(exc_type, exc_value, tb):
if
last_node
is
not
None
:
print
()
print
(
f"In file
\
"
{
cf
.
white
(
last_file
)
}\
"
, line
{
last_node
.
lineno
}
"
)
print
(
f"From
{
last_node
.
lineno
}
:
{
last_node
.
col_offset
}
to
{
last_node
.
end_lineno
}
:
{
last_node
.
end_col_offset
}
"
)
with
open
(
last_file
,
"r"
,
encoding
=
"utf-8"
)
as
f
:
code
=
f
.
read
()
hg
=
str
(
highlight
(
code
,
True
)).
replace
(
"
\
x1b
[04m"
,
""
).
replace
(
"
\
x1b
[24m"
,
""
).
splitlines
()
hg
=
(
str
(
highlight
(
code
,
True
))
.
replace
(
"
\
x1b
[04m"
,
""
)
.
replace
(
"
\
x1b
[24m"
,
""
)
.
replace
(
"
\
x1b
[39;24m"
,
"
\
x1b
[39m"
)
.
splitlines
())
if
last_node
.
lineno
==
last_node
.
end_lineno
:
old
=
hg
[
last_node
.
lineno
-
1
]
start
,
end
=
find_indices
(
old
,
[
last_node
.
col_offset
,
last_node
.
end_col_offset
])
...
...
@@ -81,8 +86,9 @@ def exception_hook(exc_type, exc_value, tb):
for
i
,
line
in
enumerate
(
hg
[
start
:
last_node
.
end_lineno
+
CONTEXT_SIZE
]):
erroneous
=
last_node
.
lineno
<=
offset
+
i
<=
last_node
.
end_lineno
indicator
=
cf
.
white
(
"**>"
)
if
erroneous
else
" "
print
(
f"
\
x1b
[24m
{
indicator
}{
cf
.
white
}{
(
offset
+
i
):
>
4
}{
cf
.
red
if
erroneous
else
cf
.
reset
}
|
{
cf
.
reset
}{
line
}\
x1b
[24m"
)
disp
=
f"
\
x1b
[24m
{
indicator
}{
cf
.
white
}{
(
offset
+
i
):
>
4
}{
cf
.
red
if
erroneous
else
cf
.
reset
}
|
{
cf
.
reset
}
{
line
}\
x1b
[24m"
print
(
disp
)
# print(repr(disp))
print
()
print
(
cf
.
red
(
"Error:"
),
exc_value
)
if
isinstance
(
exc_value
,
CompileError
):
...
...
trans/transpiler/phases/typing/block.py
View file @
0cecacfe
...
...
@@ -3,6 +3,7 @@ import dataclasses
import
importlib
from
dataclasses
import
dataclass
from
transpiler.utils
import
highlight
from
transpiler.phases.typing
import
make_mod_decl
from
transpiler.phases.typing.common
import
ScoperVisitor
from
transpiler.phases.typing.expr
import
ScoperExprVisitor
...
...
@@ -93,22 +94,19 @@ class ScoperBlockVisitor(ScoperVisitor):
if
not
isinstance
(
node
.
target
,
ast
.
Name
):
raise
NotImplementedError
(
node
)
ty
=
self
.
visit_annotation
(
node
.
annotation
)
try
:
node
.
is_declare
=
self
.
visit_assign_target
(
node
.
target
,
ty
)
except
IncompatibleTypesError
as
e
:
raise
IncompatibleTypesError
(
f"`
{
ast
.
unparse
(
node
)
}
:
{
e
}
"
)
node
.
is_declare
=
self
.
visit_assign_target
(
node
.
target
,
ty
)
ty_val
=
self
.
get_type
(
node
.
value
)
try
:
ty
.
unify
(
ty_val
)
except
IncompatibleTypesError
as
e
:
raise
IncompatibleTypesError
(
f"`
{
ast
.
unparse
(
node
)
}
:
{
e
}
"
)
TB
=
f"unifying annotation
{
highlight
(
node
.
annotation
)
}
with value
{
highlight
(
node
.
value
)
}
of type
{
highlight
(
ty_val
)
}
"
ty
.
unify
(
ty_val
)
def
visit_assign_target
(
self
,
target
,
decl_val
:
BaseType
)
->
bool
:
TB
=
f"analyzing assignment target
{
highlight
(
target
)
}
with value
{
highlight
(
decl_val
)
}
"
if
isinstance
(
target
,
ast
.
Name
):
if
target
.
id
==
"_"
:
return
False
target
.
type
=
decl_val
if
vdecl
:
=
self
.
scope
.
get
(
target
.
id
):
TB
=
f"unifying existing variable
{
highlight
(
target
.
id
)
}
of type
{
highlight
(
vdecl
.
type
)
}
with assigned value
{
highlight
(
decl_val
)
}
"
vdecl
.
type
.
unify
(
decl_val
)
return
False
else
:
...
...
trans/transpiler/phases/typing/exceptions.py
View file @
0cecacfe
...
...
@@ -56,30 +56,11 @@ class RecursiveTypeUnificationError(CompileError):
"""
@
dataclass
class
InvalidCallError
(
CompileError
):
callee
:
BaseType
args
:
list
[
BaseType
]
def
__str__
(
self
)
->
str
:
return
f"Invalid call:
{
highlight
(
self
.
callee
)
}
with arguments
{
highlight
(
self
.
args
)
}
"
def
detail
(
self
,
last_node
:
ast
.
AST
=
None
)
->
str
:
return
f"""
This generally indicates a type error in a function call.
For example:
{
highlight
(
'def f(x: int): pass'
)
}
{
highlight
(
'f("hello")'
)
}
In the current case,
{
highlight
(
self
.
callee
)
}
was called with arguments
{
highlight
(
self
.
args
)
}
, but the function
expects arguments of type
{
highlight
(
self
.
callee
.
args
)
}
.
"""
class
TypeMismatchKind
(
enum
.
Enum
):
NO_COMMON_PARENT
=
enum
.
auto
()
DIFFERENT_TYPE
=
enum
.
auto
()
@
dataclass
class
TypeMismatchError
(
CompileError
):
expected
:
BaseType
...
...
@@ -94,7 +75,7 @@ class TypeMismatchError(CompileError):
This generally indicates a type error.
For example:
{
highlight
(
'def f(x: int):
pass
'
)
}
{
highlight
(
'def f(x: int):
...
'
)
}
{
highlight
(
'f("hello")'
)
}
In the current case, the compiler expected an expression of type
{
highlight
(
self
.
expected
)
}
, but instead got
...
...
@@ -124,7 +105,7 @@ class ArgumentCountMismatchError(CompileError):
The called or instantiated signature was
{
highlight
(
self
.
func
)
}
.
Other examples:
{
highlight
(
'def f(x: int):
pass
'
)
}
{
highlight
(
'def f(x: int):
...
'
)
}
{
highlight
(
'f(1, 2)'
)
}
Here, the function
{
highlight
(
'f'
)
}
expects one argument, but was called with two.
...
...
@@ -147,13 +128,14 @@ class ProtocolMismatchError(CompileError):
This generally indicates a type error.
For example:
{
highlight
(
'def f(x: Iterable[int]):
pass
'
)
}
{
highlight
(
'def f(x: Iterable[int]):
...
'
)
}
{
highlight
(
'f("hello")'
)
}
In the current case, the compiler expected an expression whose type implements
{
highlight
(
self
.
protocol
)
}
, but
instead got an expression of type
{
highlight
(
self
.
value
)
}
.
"""
@
dataclass
class
NotCallableError
(
CompileError
):
value
:
BaseType
...
...
@@ -170,6 +152,7 @@ class NotCallableError(CompileError):
{
highlight
(
'x()'
)
}
"""
@
dataclass
class
MissingAttributeError
(
CompileError
):
value
:
BaseType
...
...
@@ -187,6 +170,7 @@ class MissingAttributeError(CompileError):
{
highlight
(
'print(x.y)'
)
}
"""
@
dataclass
class
UnknownNameError
(
CompileError
):
name
:
str
...
...
@@ -202,6 +186,7 @@ class UnknownNameError(CompileError):
{
highlight
(
'print(abcd)'
)
}
"""
@
dataclass
class
UnknownModuleMemberError
(
CompileError
):
module
:
str
...
...
@@ -216,4 +201,4 @@ class UnknownModuleMemberError(CompileError):
For example:
{
highlight
(
'from math import abcd'
)
}
"""
\ No newline at end of file
"""
trans/transpiler/utils.py
View file @
0cecacfe
...
...
@@ -44,7 +44,7 @@ def highlight(code, full=False):
res
=
items
[
0
]
if
len
(
items
)
>
1
:
res
+=
cf
.
white
(
" [...]"
)
return
cf
.
on_gray25
(
res
)
return
f"
\
x1b
[39;49m
{
cf
.
on_gray25
(
res
)
}
"
def
compare_ast
(
node1
:
Union
[
ast
.
expr
,
list
[
ast
.
expr
]],
node2
:
Union
[
ast
.
expr
,
list
[
ast
.
expr
]])
->
bool
:
...
...
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