-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_function.py
More file actions
57 lines (46 loc) · 1.71 KB
/
builtin_function.py
File metadata and controls
57 lines (46 loc) · 1.71 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
from exception import throw_exception
import arguments
class BuiltinFunction(object):
def __init__(self, name, args, action, supplied_env=None):
self.name = name
self.args = args
self.action = action
self.supplied_env = supplied_env
self.min_args, self.max_args = arguments.min_and_max_args(self.args)
def get_num_args(self):
return len(self.args)
def check_args_within_bounds(self, arg_values):
if self.args is None:
return True
else:
length = len(arg_values)
return length >= self.min_args and length <= self.max_args
def execute(self, arg_values, env=None):
if not self.check_args_within_bounds(arg_values):
throw_exception(
'IncorrectNumberOfArguments',
'Wrong number of arguments passed to built-in function\n' \
'Expected between {0} and {1}, but received {2}'.format(
self.min_args, self.max_args, len(arg_values)
)
)
return self.action(*arg_values)
def supply(self, env):
# A supplied environment has no meaning for a built-in function:
pass
def __repr__(self):
return '<function ' + self.name + '(' + str(self.args)[1:-1] + ')>'
def __str__(self):
return repr(self)
def int_div_action(a, b):
"""
Returns a/b, discarding any remainder.
"""
return int(a) / int(b)
def make_list_action(*args):
"""
Returns a list containing the given arguments.
"""
return list(args)
int_div = BuiltinFunction('intDiv', ['a', 'b'], int_div_action)
make_list = BuiltinFunction('makeList', None, make_list_action)