8000 Reorder module fields · atg/pseudo-python@bd0bb8c · GitHub
[go: up one dir, main page]

Skip to content

Commit bd0bb8c

Browse files
committed
Reorder module fields
1 parent 6a7c316 commit bd0bb8c

File tree

5 files changed

+42
-82
lines changed

5 files changed

+42
-82
lines changed

example/f.pseudo.yaml

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,11 @@
11
constants: []
2-
definitions:
3-
- block:
4-
- pseudo_type: Int
5-
type: implicit_return
6-
value:
7-
args:
8-
- {pseudo_type: Int, type: int, value: 2}
9-
function:
10-
name: s
11-
pseudo_type: [Function, Int, Int]
12-
type: local
13-
pseudo_type: Int
14-
type: call
15-
name: f
16-
params: [s]
17-
pseudo_type:
18-
- Function
19-
- [Function, Int, Int]
20-
- Int
21-
return_type: Int
22-
type: function_definition
23-
- attrs: []
24-
base: null
25-
constructor: null
26-
methods:
27-
- block:
28-
- pseudo_type: B
29-
type: implicit_return
30-
value:
31-
class: {name: B, type: typename}
32-
params:
33-
- {name: a, pseudo_type: Int, type: local}
34-
pseudo_type: B
35-
type: new_instance
36-
is_public: true
37-
name: expand
38-
params: [a]
39-
pseudo_type: [Function, Int, B]
40-
return_type: B
41-
this: {name: A, type: typename}
42-
type: method_definition
43-
name: A
44-
type: class_definition
45-
- attrs:
46-
- {is_public: false, name: a, pseudo_type: Int, type: class_attr}
47-
base: null
48-
constructor:
49-
block:
50-
- pseudo_type: Void
51-
target: {name: a, pseudo_type: Int, type: instance_variable}
52-
type: assignment
53-
value: {name: a, pseudo_type: Int, type: local}
54-
name: __init__
55-
params: [a]
56-
pseudo_type: [Function, Int, B]
57-
return_type: null
58-
this: {name: B, type: typename}
59-
type: constructor
60-
methods: []
61-
name: B
62-
type: class_definition
2+
custom_exceptions:
3+
- {base: null, name: ExError, type: custom_exception}
4+
definitions: []
635
dependencies: []
64-
main: []
6+
main:
7+
- exception: ExError
8+
pseudo_type: Void
9+
type: throw_statement
10+
value: {pseudo_type: String, type: string, value: s}
6511
type: module

example/f.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
from typing import Dict, List, Tuple, Callable
2-
3-
def f(s: Callable[[int], int]) -> int:
4-
return s(2)
5-
6-
class A:
7-
def expand(self, a: int) -> 'B':
8-
return B(a)
9-
10-
class B:
11-
def __init__(self, a):
12-
self.a = a
13-
14-
# f(2)
1+
class ExError(Exception):
2+
pass
153

4+
raise ExError("s")

example/typing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Dict, List, Tuple, Callable
2+
3+
def f(s: Callable[[int], int]) -> int:
4+
return s(2)
5+
6+
class A:
7+
def expand(self, a: int) -> 'B':
8+
return B(a)
9+
10+
class B:
11+
def __init__(self, a):
12+
self.a = a
13+
14+
# f(2)
15+

pseudo_python/ast_translator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def translate(self):
7777
self.constants = []
7878
self.main = []
7979
self._exceptions = {'Exception'}
80+
self.custom_exceptions = []
8081
self._hierarchy = {}
8182
self._translated = {'functions': set()}
8283
self._attr_index = {}
@@ -91,8 +92,7 @@ def translate(self):
9192
self._translate_pure_functions()
9293
main = self._translate_main()
9394
definitions = self._translate_definitions()
94-
95-
return {'type': 'module', 'dependencies': self.dependencies, 'constants': self.constants, 'definitions': definitions, 'main': main}
95+
return {'type': 'module', 'dependencies': self.dependencies, 'custom_exceptions': self.custom_exceptions, 'constants': self.constants, 'definitions': definitions, 'main': main}
9696

9797
def _translate_definitions(self):
9898
definitions = []
@@ -160,7 +160,7 @@ def _translate_top_level(self, node):
160160
self._hierarchy[n.name] = (None, set())
161161
if n.bases:
162162
if len(n.bases) == 1 and isinstance(n.bases[0], ast.Name) and n.bases[0].id in self._exceptions:
163-
self.main.append({
163+
self.custom_exceptions.append({
164164
'type': 'custom_exception',
165165
'name': n.name,
166166
'base': None if n.bases[0].id == 'Exception' else n.bases[0].id
@@ -372,8 +372,8 @@ def _translate_init(self, name, params, location):
372372

373373
return {
374374
'type': 'new_instance',
375-
'class': {'type': 'typename', 'name': name},
376-
'params': params,
375+
'class_name': {'type': 'typename', 'name': name},
376+
'args': params,
377377
'pseudo_type': name
378378
}
379379

@@ -922,7 +922,7 @@ def _translate_tuple(self, elts, ctx, location):
922922
element_nodes, accidentaly_homogeneous, element_type = self._translate_elements(elts, 'tuple', homogeneous=False)
923923

924924
return {
925-
'type': 'tuple',
925+
'type': 'array' if accidentaly_homogeneous else 'tuple',
926926
'pseudo_type': ['Array', element_type, len(elts)] if accidentaly_homogeneous else ['Tuple'] + element_type,
927927
'elements': element_nodes
928928
}

pseudo_python/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ def serialize_type(l):
66
else:
77
return str(l)
88

9+
def safe_serialize_type(l):
10+
'''serialize only with letters, numbers and _'''
11+
12+
if isinstance(l, str):
13+
return l
14+
elif isinstance(l, list):
15+
return '%s_%s_' % (l[0], ''.join(map(serialize_type, l[1:])))
16+
else:
17+
return str(l)
18+
919
def prepare_table(types):
1020
names, args, returns = [], [], []
1121
max_name, max_arg, max_return = 0, 0, 0

0 commit comments

Comments
 (0)
0