-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharguments.py
More file actions
94 lines (88 loc) · 3.84 KB
/
arguments.py
File metadata and controls
94 lines (88 loc) · 3.84 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
from exception import throw_exception
import execution
def is_grouping_argument(arg_name):
return arg_name.startswith('[') and arg_name.endswith(']')
def count_grouping_arguments(arg_names):
count = 0
for name in arg_names:
if is_grouping_argument(name):
count += 1
return count
def is_out_of_bounds(index, lst):
list_len = len(lst)
return index >= list_len or index < -list_len
def min_and_max_args(arg_names):
"""
Returns the minimum and maximum number of arguments,
for a built-in function, as a tuple.
"""
if arg_names is None:
return (None, None)
min_args = 0
max_args = 0
for name in arg_names:
if name.endswith('?'):
# TODO : if name contains '=' then it may be an argument
# with a default value
max_args += 1
elif is_grouping_argument(name):
max_args = float('inf')
else:
min_args += 1
max_args += 1
return (min_args, max_args)
def assign_arguments(arg_names, arg_values, env):
num_grouping_args = count_grouping_arguments(arg_names)
if num_grouping_args == 0:
if len(arg_values) > len(arg_names):
throw_exception('TooManyArgumentsException',
'Number of arguments exceeds number expected in function definition')
for i in xrange(len(arg_names)):
current_name = arg_names[i]
if '=' in current_name:
# This argument name has a default value given
# TODO : the following line does not account for operators
# such as ==, !=, <=, etc.
pieces = [piece.strip() for piece in current_name.split('=')]
if len(pieces) != 2:
throw_exception('DefaultArgumentException',
'Incorrect default argument syntax in {0}'.format(current_name))
var_name, var_expr = pieces
if is_out_of_bounds(i, arg_values):
env.assign(var_name, execution.eval_parentheses(var_expr, env))
else:
# Instead of the default value, use the value already given
env.assign(var_name, arg_values[i])
else:
if is_out_of_bounds(i, arg_values):
throw_exception('ArgValueException',
'Number of arguments does not match function definition')
else:
env.assign(current_name, arg_values[i])
elif num_grouping_args == 1:
for i in xrange(len(arg_names)):
if is_grouping_argument(arg_names[i]):
break
# Remove brackets around group name
group_name = arg_names[i][1:-1].strip()
num_left = i
num_right = len(arg_names) - num_left - 1
# TODO : account for default arguments
# (num_left + num_right) might be greater than the actual number of needed arguments
if len(arg_values) < num_left + num_right:
throw_exception('TooFewArgumentsException',
'Number of arguments is less than number expected in function definition')
else:
values_for_left = arg_values[0:i]
if num_right == 0:
values_for_right = []
values_for_middle = arg_values[i:]
else:
values_for_right = arg_values[-num_right:]
values_for_middle = arg_values[i:-num_right]
env.assign(group_name, values_for_middle)
other_arg_names = arg_names[0:i] + arg_names[i+1:]
assign_arguments(other_arg_names, values_for_left + values_for_right, env)
else:
throw_exception('GroupingArgumentException',
'Cannot have more than 1 grouping argument in function definition')