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
b761813b
Commit
b761813b
authored
Jul 27, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start adding proper error handling infrastructure
parent
50a42e46
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
68 additions
and
7 deletions
+68
-7
trans/test_runner.py
trans/test_runner.py
+1
-1
trans/transpiler/__init__.py
trans/transpiler/__init__.py
+23
-4
trans/transpiler/exceptions.py
trans/transpiler/exceptions.py
+7
-0
trans/transpiler/phases/emit_cpp/__init__.py
trans/transpiler/phases/emit_cpp/__init__.py
+2
-1
trans/transpiler/phases/typing/exceptions.py
trans/transpiler/phases/typing/exceptions.py
+32
-0
trans/transpiler/utils.py
trans/transpiler/utils.py
+3
-1
No files found.
trans/test_runner.py
View file @
b761813b
...
...
@@ -26,7 +26,7 @@ def run_tests():
with
open
(
path
,
"r"
,
encoding
=
"utf-8"
)
as
f
:
code
=
f
.
read
()
execute
=
"# norun"
not
in
code
res
=
format_code
(
transpile
(
code
))
res
=
format_code
(
transpile
(
code
,
path
.
name
,
path
))
#print(res)
name_cpp
=
path
.
with_suffix
(
'.cpp'
)
with
open
(
name_cpp
,
"w"
,
encoding
=
"utf-8"
)
as
fcpp
:
...
...
trans/transpiler/__init__.py
View file @
b761813b
# coding: utf-8
import
ast
import
builtins
import
inspect
from
transpiler.consts
import
MAPPINGS
from
transpiler.phases.desugar_with
import
DesugarWith
...
...
@@ -13,18 +15,30 @@ import sys
from
colorama
import
Fore
import
colorama
from
transpiler.utils
import
highlight
colorama
.
init
()
def
exception_hook
(
exc_type
,
exc_value
,
tb
):
print
=
lambda
*
args
,
**
kwargs
:
builtins
.
print
(
*
args
,
**
kwargs
,
file
=
sys
.
stderr
)
last_node
=
None
last_file
=
None
while
tb
:
local_vars
=
tb
.
tb_frame
.
f_locals
name
=
tb
.
tb_frame
.
f_code
.
co_name
if
name
==
"transpile"
:
last_file
=
local_vars
[
"path"
]
if
name
==
"visit"
and
(
node
:
=
local_vars
[
"node"
])
and
isinstance
(
node
,
ast
.
AST
):
last_node
=
node
if
local_vars
.
get
(
"TB_SKIP"
,
None
)
and
tb
.
tb_next
:
tb
=
tb
.
tb_next
continue
filename
=
tb
.
tb_frame
.
f_code
.
co_filename
name
=
tb
.
tb_frame
.
f_code
.
co_name
line_no
=
tb
.
tb_lineno
print
(
f"
{
Fore
.
RED
}
File
\
"
{
filename
}\
"
, line
{
line_no
}
, in
{
name
}
"
,
end
=
""
)
...
...
@@ -34,13 +48,18 @@ def exception_hook(exc_type, exc_value, tb):
print
()
tb
=
tb
.
tb_next
# Exception type and value
print
(
f"
{
exc_type
.
__name__
}
, Message:
{
exc_value
}
"
)
if
last_node
is
not
None
:
print
(
f"In file
\
"
{
Fore
.
RESET
}{
last_file
}{
Fore
.
RED
}\
"
, line
{
last_node
.
lineno
}
"
)
print
(
"
\
t
"
+
highlight
(
ast
.
unparse
(
last_node
)))
print
(
f"
{
Fore
.
RED
}
Error:
{
Fore
.
RESET
}
{
exc_value
}
"
)
print
(
inspect
.
cleandoc
(
exc_value
.
detail
(
last_node
)))
sys
.
excepthook
=
exception_hook
def
transpile
(
source
):
def
transpile
(
source
,
name
=
"<module>"
,
path
=
None
):
TB
=
f"transpiling module
{
Fore
.
RESET
}{
name
}
"
res
=
ast
.
parse
(
source
,
type_comments
=
True
)
#res = initial_pytype.run(source, res)
res
=
DesugarWith
().
visit
(
res
)
...
...
trans/transpiler/exceptions.py
0 → 100644
View file @
b761813b
import
ast
from
abc
import
ABC
class
CompileError
(
Exception
,
ABC
):
def
detail
(
self
,
last_node
:
ast
.
AST
=
None
)
->
str
:
return
""
trans/transpiler/phases/emit_cpp/__init__.py
View file @
b761813b
...
...
@@ -7,6 +7,7 @@ from typing import Iterable
from
transpiler.phases.emit_cpp.consts
import
MAPPINGS
from
transpiler.phases.typing
import
TypeVariable
from
transpiler.phases.typing.exceptions
import
UnresolvedTypeVariableError
from
transpiler.phases.typing.types
import
BaseType
,
TY_INT
,
TY_BOOL
,
TY_NONE
,
Promise
,
PromiseKind
,
TY_STR
,
UserType
,
\
TypeType
,
TypeOperator
,
TY_FLOAT
from
transpiler.utils
import
UnsupportedNodeError
,
highlight
...
...
@@ -88,7 +89,7 @@ class NodeVisitor(UniversalVisitor):
yield
">"
elif
isinstance
(
node
,
TypeVariable
):
#yield f"TYPEVAR_{node.name}";return
raise
NotImplementedError
(
f"Not unified type variable
{
node
}
"
)
raise
UnresolvedTypeVariableError
(
node
)
elif
isinstance
(
node
,
TypeOperator
):
yield
"Py"
+
node
.
name
.
title
()
if
node
.
args
:
...
...
trans/transpiler/phases/typing/exceptions.py
0 → 100644
View file @
b761813b
import
ast
from
dataclasses
import
dataclass
from
transpiler.utils
import
highlight
from
transpiler.exceptions
import
CompileError
from
transpiler.phases.typing
import
TypeVariable
@
dataclass
class
UnresolvedTypeVariableError
(
CompileError
):
variable
:
TypeVariable
def
__str__
(
self
)
->
str
:
return
f"Unresolved type variable:
{
self
.
variable
}
"
def
detail
(
self
,
last_node
:
ast
.
AST
=
None
)
->
str
:
if
isinstance
(
last_node
,
(
ast
.
Import
,
ast
.
ImportFrom
)):
return
f"""
This indicates the compiler was unable to infer the type of a function in a module.
Currently, Typon cannot determine the type of Python functions imported from other modules, except
for the standard library.
As such, you need to give enough information to the compiler to infer the type of the function.
For example:
vvv this tells the compiler that
{
highlight
(
'math.factorial'
)
}
returns an
{
highlight
(
'int'
)
}
{
highlight
(
'res: int = math.factorial(5)'
)
}
"""
return
"""
This generally indicates the compiler was unable to infer the type of a variable or expression.
A common fix is to add a type annotation to the variable or function.
"""
trans/transpiler/utils.py
View file @
b761813b
...
...
@@ -7,7 +7,7 @@ from typing import Union
from
colorama
import
Fore
def
highlight
(
code
):
def
highlight
(
code
,
full
=
False
):
"""
Syntax highlights code as Python using colorama
"""
...
...
@@ -21,6 +21,8 @@ def highlight(code):
from
pygments.formatters
import
TerminalFormatter
items
=
pyg_highlight
(
code
,
PythonLexer
(),
TerminalFormatter
()).
splitlines
()
if
full
:
return
Fore
.
RESET
+
"
\
n
"
.
join
(
items
)
res
=
items
[
0
]
if
len
(
items
)
>
1
:
res
+=
Fore
.
WHITE
+
" [...]"
...
...
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