-
Notifications
You must be signed in to change notification settings - Fork 1
/
operators.py
110 lines (76 loc) · 2.18 KB
/
operators.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
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
from functools import wraps, reduce
import operator
def type_check(check_type: type):
def wrapper(proceed: callable):
@wraps(proceed)
def checker(*args):
for idx, arg in enumerate(args):
if not type(arg) is check_type:
raise TypeError(f"{proceed.__name__}(): invalid type for argument {idx},"
f" expected {check_type.__name__}, but found {type(arg).__name__}")
return proceed(*args)
return checker
return wrapper
def argcount_check(rule: str):
def wrapper(proceed: callable):
@wraps(proceed)
def checker(*args):
allow_more = rule[-1] == '+'
arg_count = int(rule[:-1]) if allow_more else int(rule)
if (not allow_more and len(args) != arg_count) or (allow_more and len(args) < arg_count):
raise SyntaxError(f"{proceed.__name__}(): expected {rule} arguments, got {len(args)}")
return proceed(*args)
return checker
return wrapper
@argcount_check("1")
@type_check(bool)
def print_bool(x):
print("#t" if x else "#f")
@argcount_check("1")
@type_check(int)
def print_num(x):
print(x)
@argcount_check("2+")
@type_check(int)
def add(*args):
return reduce(operator.add, args)
@argcount_check("2")
@type_check(int)
def sub(*args):
return operator.sub(*args)
@argcount_check("2+")
@type_check(int)
def mul(*args):
return reduce(operator.mul, args)
@argcount_check("2")
@type_check(int)
def div(*args):
return operator.floordiv(*args)
@argcount_check("2")
@type_check(int)
def mod(*args):
return operator.mod(*args)
@argcount_check("2")
@type_check(int)
def lt(*args):
return operator.lt(*args)
@argcount_check("2")
@type_check(int)
def gt(*args):
return operator.gt(*args)
@argcount_check("2+")
@type_check(int)
def eq(*args):
return args.count(args[0]) == len(args)
@argcount_check("2+")
@type_check(bool)
def and_(*args):
return reduce(operator.and_, args)
@argcount_check("2+")
@type_check(bool)
def or_(*args):
return reduce(operator.or_, args)
@argcount_check("1")
@type_check(bool)
def not_(*args):
return operator.not_(*args)