-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg.transcrypt.__runtime__.py
More file actions
285 lines (221 loc) · 9.82 KB
/
org.transcrypt.__runtime__.py
File metadata and controls
285 lines (221 loc) · 9.82 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Transcrypt runtime module
#__pragma__ ('js', 'export var __envir__ = {{}};\n{}', __include__ ('org/transcrypt/__envir__.js'))
#__pragma__ ('js', '{}', __include__ ('org/transcrypt/__core__.js'))
#__pragma__ ('js', '{}', __include__ ('org/transcrypt/__builtin__.js'))
#__pragma__ ('skip')
copy = Math = __typeof__ = __repr__ = document = console = window = 0
#__pragma__ ('noskip')
#__pragma__ ('notconv') # !!! tconv gives a problem with __terminal__, needs investigation
#__pragma__ ('nokwargs')
#__pragma__ ('noalias', 'sort')
class BaseException:
pass
class Exception (BaseException):
#__pragma__ ('kwargs')
def __init__ (self, *args, **kwargs):
self.__args__ = args
try:
self.stack = kwargs.error.stack # Integrate with JavaScript Error object
except:
self.stack = 'No stack trace available'
#__pragma__ ('nokwargs')
def __repr__ (self):
if len (self.__args__) > 1:
return '{}{}'.format (self.__class__.__name__, repr (tuple (self.__args__)))
elif len (self.__args__):
return '{}({})'.format (self.__class__.__name__, repr (self.__args__ [0]))
else:
return '{}()'.format (self.__class__.__name__)
def __str__ (self):
if len (self.__args__) > 1:
return str (tuple (self.__args__))
elif len (self.__args__):
return str (self.__args__ [0])
else:
return ''
class IterableError (Exception):
def __init__ (self, error):
Exception.__init__ (self, 'Can\'t iterate over non-iterable', error = error)
class StopIteration (Exception):
def __init__ (self, error):
Exception.__init__ (self, 'Iterator exhausted', error = error)
class ValueError (Exception):
def __init__ (self, message, error):
Exception.__init__ (self, message, error = error)
class KeyError (Exception):
def __init__ (self, message, error):
Exception.__init__ (self, message, error = error)
class AssertionError (Exception):
def __init__ (self, message, error):
if message:
Exception.__init__ (self, message, error = error)
else:
Exception.__init__ (self, error = error)
class NotImplementedError (Exception):
def __init__(self, message, error):
Exception.__init__(self, message, error = error)
class IndexError (Exception):
def __init__(self, message, error):
Exception.__init__(self, message, error = error)
class AttributeError (Exception):
def __init__(self, message, error):
Exception.__init__(self, message, error = error)
class TypeError (Exception):
def __init__(self, message, error):
Exception.__init__(self, message, error = error)
# Warnings Exceptions
# N.B. This is a limited subset of the warnings defined in
# the cpython implementation to keep things small for now.
class Warning (Exception):
''' Warning Base Class
'''
pass
class UserWarning (Warning):
pass
class DeprecationWarning (Warning):
pass
class RuntimeWarning (Warning):
pass
#__pragma__ ('kwargs')
def __sort__ (iterable, key = None, reverse = False): # Used by py_sort, can deal with kwargs
if key:
iterable.sort (lambda a, b: 1 if key (a) > key (b) else -1) # JavaScript sort, case '==' is irrelevant for sorting
else:
iterable.sort () # JavaScript sort
if reverse:
iterable.reverse ()
def sorted (iterable, key = None, reverse = False):
if type (iterable) == dict:
result = copy (iterable.keys ())
else:
result = copy (iterable)
__sort__ (result, key, reverse)
return result
#__pragma__ ('nokwargs')
def map (func, iterable):
return [func (item) for item in iterable]
def filter (func, iterable):
if func == None:
func = bool
return [item for item in iterable if func (item)]
def divmod (n, d):
return n // d, n % d
#__pragma__ ('ifdef', '__complex__')
class complex:
def __init__ (self, real, imag = None):
if imag == None:
if type (real) == complex:
self.real = real.real
self.imag = real.imag
else:
self.real = real
self.imag = 0
else:
self.real = real
self.imag = imag
def __neg__ (self):
return complex (-self.real, -self.imag)
def __exp__ (self):
modulus = Math.exp (self.real)
return complex (modulus * Math.cos (self.imag), modulus * Math.sin (self.imag))
def __log__ (self):
return complex (Math.log (Math.sqrt (self.real * self.real + self.imag * self.imag)), Math.atan2 (self.imag, self.real))
def __pow__ (self, other): # a ** b = exp (b log a)
return (self.__log__ () .__mul__ (other)) .__exp__ ()
def __rpow__ (self, real): # real ** comp -> comp.__rpow__ (real)
return self.__mul__ (Math.log (real)) .__exp__ ()
def __mul__ (self, other):
if __typeof__ (other) is 'number':
return complex (self.real * other, self.imag * other)
else:
return complex (self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real)
def __rmul__ (self, real): # real + comp -> comp.__rmul__ (real)
return complex (self.real * real, self.imag * real)
def __div__ (self, other):
if __typeof__ (other) is 'number':
return complex (self.real / other, self.imag / other)
else:
denom = other.real * other.real + other.imag * other.imag
return complex (
(self.real * other.real + self.imag * other.imag) / denom,
(self.imag * other.real - self.real * other.imag) / denom
)
def __rdiv__ (self, real): # real / comp -> comp.__rdiv__ (real)
denom = self.real * self.real
return complex (
(real * self.real) / denom,
(real * self.imag) / denom
)
def __add__ (self, other):
if __typeof__ (other) is 'number':
return complex (self.real + other, self.imag)
else: # Assume other is complex
return complex (self.real + other.real, self.imag + other.imag)
def __radd__ (self, real): # real + comp -> comp.__radd__ (real)
return complex (self.real + real, self.imag)
def __sub__ (self, other):
if __typeof__ (other) is 'number':
return complex (self.real - other, self.imag)
else:
return complex (self.real - other.real, self.imag - other.imag)
def __rsub__ (self, real): # real - comp -> comp.__rsub__ (real)
return complex (real - self.real, -self.imag)
def __repr__ (self):
return '({}{}{}j)'.format (self.real, '+' if self.imag >= 0 else '', self.imag)
def __str__ (self):
return __repr__ (self) [1 : -1]
def __eq__ (self, other):
if __typeof__ (other) is 'number':
return self.real == other
else:
return self.real == other.real and self.imag == other.imag
def __ne__ (self, other):
if __typeof__ (other) is 'number':
return self.real != other
else:
return self.real != other.real or self.imag != other.imag
def conjugate (self):
return complex (self.real, -self.imag)
def __conj__ (aNumber):
if isinstance (aNumber, complex):
return complex (aNumber.real, -aNumber.imag)
else:
return complex (aNumber, 0)
#__pragma__ ('endif')
class __Terminal__:
'''
Printing to either the console or to html happens async, but is blocked by calling window.prompt.
So while all input and print statements are encountered in normal order, the print's exit immediately without yet having actually printed
This means the next input takes control, blocking actual printing and so on indefinitely
The effect is that everything's only printed after all inputs are done
To prevent that, what's needed is to only execute the next window.prompt after actual printing has been done
Since we've no way to find out when that is, a timeout is used.
'''
def __init__ (self):
self.buffer = ''
try:
self.element = document.getElementById ('__terminal__')
except:
self.element = None
if self.element:
self.element.style.overflowX = 'auto'
self.element.style.boxSizing = 'border-box'
self.element.style.padding = '5px'
self.element.innerHTML = '_'
#__pragma__ ('kwargs')
def print (self, *args, sep = ' ', end = '\n'):
self.buffer = '{}{}{}'.format (self.buffer, sep.join ([str (arg) for arg in args]), end) [-4096 : ]
if self.element:
self.element.innerHTML = self.buffer.replace ('\n', '<br>') .replace (' ', ' ')
self.element.scrollTop = self.element.scrollHeight
else:
console.log (sep.join ([str (arg) for arg in args]))
def input (self, question):
self.print ('{}'.format (question), end = '')
answer = window.prompt ('\n'.join (self.buffer.split ('\n') [-8:]))
self.print (answer)
return answer
#__pragma__ ('nokwargs')
__terminal__ = __Terminal__ ()
print = __terminal__.print
input = __terminal__.input