-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operators.py
92 lines (83 loc) · 2.47 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
class PlusOperator:
symbol = '+'
minArgs = 2
maxArgs = -1
description = "{0}+{1}+{2}...+{n}"
def process(self, values, machine):
value = machine.parse(values[0])
for i in range(1,len(values)):
value += machine.parse(values[i])
return value
class SubtractOperator:
symbol = '-'
minArgs = 2
maxArgs = -1
description = "{0}-{1}-{2}...-{n}"
def process(self, values, machine):
value = machine.parse(values[0])
for i in range(1,len(values)):
value -= machine.parse(values[i])
return value
class MultiplyOperator:
symbol = '*'
minArgs = 2
maxArgs = -1
description = "{0}*{1}*{2}...*{n}"
def process(self, values, machine):
value = machine.parse(values[0])
for i in range(1,len(values)):
value *= machine.parse(values[i])
return value
class DivideOperator:
symbol = '/'
minArgs = 2
maxArgs = 2
description = "{0}/{1}"
def process(self, values, machine):
return machine.parse(values[0]) / machine.parse(values[1])
class LessThan:
symbol = "<"
minArgs = 4
maxArgs = 4
description = "if {0} < {1} then {2} else {3}"
def process(self, values, machine):
if machine.parse(values[0]) < machine.parse(values[1]):
return machine.parse( values[2] )
else:
return machine.parse( values[3] )
class If:
symbol='if'
minArgs = 3
maxArgs = 3
description = "if {0} si not zero then {1} else {2}"
def process(self, values, machine):
if machine.parse( values[0] ):
return machine.parse( values[1] )
else:
return machine.parse( values[2] )
class Print:
symbol='print'
minArgs = 1
maxArgs = 1
description = "prints 'print: {0}' and returns {0}"
def process(self, values, machine):
val = machine.parse( values[0] )
print('print: ' +str(val))
return val
class Describe:
symbol='desc'
minArgs = 1
maxArgs = 2
description = """Prints the description of an operator.
This has two forms:
desc(operator)
describes operator and returns 0.0.
desc(operatr, expression)
describes operator and then returns the result of expression."
"""
def process(self, values, machine):
machine.describeOperator( values[0] )
if len(values) == 2:
return machine.parse( values[1] )
else:
return 0.0