-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnatives.py
50 lines (48 loc) · 1012 Bytes
/
natives.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
from __future__ import print_function
import NP
def setup_natives(env):
natives = [
{
'name':'print',
'func':(lambda x='':print(stringify(x), end=''))
},
{
'name':'println',
'func':(lambda x='':print(stringify(x), end='\n'))
},
{
'name':'str',
'func':(lambda x='':{"type": "str", "value": str(stringify(x))})
},
{
'name':'int',
'func':(lambda x:{"type": "num", "value": int(stringify(x))})
},
{
'name':'float',
'func':(lambda x:float(x))
},
{
'name':'input',
'func':(lambda x='':raw_input(x))
}
]
for x in natives:
env.define(x['name'], x['func'])
def convert(value):
return {'string': value}
def stringify(value):
if(isinstance(value, (str))):
return value
if (value['type'] == 'list'):
a = []
for x in value['value']:
a.append(x['value'])
return a
elif value == None:
return 'null'
else:
try:
return (value['value'])
except:
return value