-
Notifications
You must be signed in to change notification settings - Fork 1
/
APLExecutor.py
66 lines (57 loc) · 2.3 KB
/
APLExecutor.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
import json
import requests
class APLExecutor:
def __init__(self):
self.url = "https://tryapl.org/Exec"
self.headers = {'Content-Type': 'application/json; charset=utf-8'}
self.state = ''
self.size = 0
self.hash = ''
self.user_defs = {'functions': {}, 'variables': {}}
def exec(self, code):
data = ['', 0, '', code]
response = requests.post(self.url, headers=self.headers, data=json.dumps(data))
data = response.json()
return data
def exec_stateful(self, code):
data = [self.state, self.size, self.hash, code]
response = requests.post(self.url, headers=self.headers, data=json.dumps(data))
data = response.json()
self.state, self.size, self.hash = data[:3]
return data
def fn(self, code):
def wrapped_function(*args):
apl_code = code + ' ' + ' '.join(map(str, args))
response = self.exec_stateful(apl_code)
result = response[3][0] if response[3] else None
return result
return wrapped_function
def clear(self):
self.state = ''
self.size = 0
self.hash = ''
self.user_defs['functions'].clear()
self.user_defs['variables'].clear()
def store_definition(self, code):
try:
# Split the code at the assignment arrow
name, definition = code.split('←', 1)
name = name.strip()
definition = definition.strip()
comment = ''
if '⍝' in definition:
definition, comment = definition.split('⍝', 1)
definition = definition.strip()
comment = comment.strip()
if definition.startswith('{') and definition.endswith('}'):
self.user_defs['functions'][name] = {
'source': definition.strip(),
'comment': comment or definition if len(definition) <= 50 else 'User Defined Function'
}
else:
self.user_defs['variables'][name] = {
'source': definition.strip(),
'comment': comment or definition if len(definition) <= 50 else 'User Defined Variable'
}
except Exception as e:
print(f"An error occurred: {e}")