-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi_translator.py
187 lines (161 loc) · 6.26 KB
/
api_translator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from pseudo_python.builtin_typed_api import builtin_type_check
from pseudo_python.errors import PseudoPythonTypeCheckError
class Standard:
'''
Standard classes should respond to expand and to return
valid nodes on expand
'''
pass
class StandardCall(Standard):
'''
converts to a standard call of the given namespace and function
'''
def __init__(self, namespace, function, expander=None):
self.namespace = namespace
self.function = function
self.expander = expander
def expand(self, args):
if not self.expander:
q = builtin_type_check(self.namespace, self.function, None, args)[-1]
return {'type': 'standard_call', 'namespace': self.namespace, 'function': self.function, 'args': args, 'pseudo_type': q}
else:
return self.expander(self.namespace, self.function, args)
class StandardMethodCall(Standard):
'''
converts to a method call of the same class
'''
def __init__(self, type, message, default=None, expander=None):
self.type = type
self.message = message
self.default = default
self.expander = expander
def expand(self, args):
if self.default and len(args) - 1 in self.default:
args += self.default[len(args) - 1]
if not self.expander:
q = builtin_type_check(self.type, self.message, args[0], args[1:])[-1]
return {'type': 'standard_method_call', 'receiver': args[0], 'message': self.message, 'args': args[1:], 'pseudo_type': q}
else:
return self.expander(self.type, self.message, args)
class StandardRegex(Standard):
'''
converts re.compile(r'literal') to {type: regex} node
re.compile(variable) to re.compile(variable) standard call
'''
def expand(self, args):
if args[0]['type'] == 'String':
return {'type': 'regex', 'value': args[0]['value'], 'pseudo_type': 'Regexp'}
else:
return {'type': 'standard_call', 'namespace': 'regexp', 'function': 'compile', 'args': [args[0]], 'pseudo_type': 'Regexp'}
class StandardSwapper(Standard):
def __init__(self, type, message):
self.type = type
self.message = message
def expand(self, args):
if len(args) < 2:
raise PseudoPythonTypeCheckError('%s expects more args' % self.message)
q = builtin_type_check(self.type, self.message, args[1], [args[0]])[-1]
return {'type': 'standard_method_call', 'receiver': args[1], 'args': [args[0]], 'message': self.message, 'pseudo_type': q}
def to_int_expander(type, message, args):
return len_expander(type, message, args)
def len_expander(type, message, args):
receiver_type = args[0]['pseudo_type']
if isinstance(receiver_type, list):
a = receiver_type[0]
else:
a = receiver_type
# print(a, message, args[0], args[1:])
# input(0)
if message == 'length' and 'special' in args[0]: # len(sys.argv)
return {'type': 'standard_call', 'namespace': 'system', 'function': 'arg_count', 'args': [], 'pseudo_type': 'Int'}
else:
q = builtin_type_check(a, message, args[0], args[1:])
return {'type': 'standard_method_call', 'receiver': args[0], 'message': message, 'args': [], 'pseudo_type': q[-1]}
FUNCTION_API = {
'global': {
'input': StandardCall('io', 'read'),
'print': StandardCall('io', 'display'),
'str': StandardCall('global', 'to_string'),
'len': StandardMethodCall('List', 'length', expander=len_expander),
'int': StandardMethodCall('String', 'to_int', expander=to_int_expander)
},
'math': {
'log': {
1: StandardCall('math', 'ln'),
2: StandardCall('math', 'log')
},
'sin': StandardCall('math', 'sin'),
'cos': StandardCall('math', 'cos'),
'tan': StandardCall('math', 'tan'),
'pow': lambda left, right, pseudo_type: Node('binary_op',
op='**', left=left, right=right, pseudo_type=pseudo_type)
},
're': {
'match': StandardMethodCall('Regexp', 'match'),
'sub': StandardMethodCall('Regexp', 'replace'),
'compile': StandardRegex(),
'escape': StandardCall('regexp', 'escape')
}
}
METHOD_API = {
'String': {
'split': StandardMethodCall('String', 'split'),
'join': StandardSwapper('List', 'join'),
'upper': StandardMethodCall('String', 'upper'),
'lower': StandardMethodCall('String', 'lower'),
'title': StandardMethodCall('String', 'title'),
'center': StandardMethodCall('String', 'center', default={1: [{'type': 'string', 'value': ' ', 'pseudo_type': 'String'}]}),
'index': {
1: StandardMethodCall('String', 'find'),
2: StandardMethodCall('String', 'find_from')
}
},
'List': {
'append': StandardMethodCall('List', 'push'),
'pop': StandardMethodCall('List', 'pop'),
'insert': {
1: StandardMethodCall('List', 'insert'),
2: StandardMethodCall('List', 'insert_at')
},
'remove': StandardMethodCall('List', 'remove'),
'extend': StandardMethodCall('List', 'push_many'),
'map': StandardMethodCall('List', 'map'),
'filter': StandardMethodCall('List', 'filter')
},
'Dictionary': {
'keys': StandardMethodCall('Dictionary', 'keys'),
'values': StandardMethodCall('Dictionary', 'values'),
'[]': StandardMethodCall('Dictionary', 'getitem'),
'[]=': StandardMethodCall('Dictionary', 'setitem')
},
'Array': {
},
'Tuple': {
},
'Set': {
'|': StandardMethodCall('Set', 'union')
},
'Regexp': {
'match': StandardMethodCall('Regexp', 'match')
},
'RegexpMatch': {
'group': StandardMethodCall('RegexpMatch', 'group')
}
}
OPERATOR_API = {
'List': {
'+': 'concat',
'*': 'repeat'
},
'Set': {
'|': 'union',
'&': 'intersection',
'^': 'symmetric_diff',
'-': 'diff'
},
'String': {
'+': 'concat',
'*': 'repeat',
'%': 'c_format'
}
}