-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.py
79 lines (51 loc) · 1.74 KB
/
rules.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
"""
This file contains methods to handle Valueing RuleMatches
"""
from typing import List
from common import Token
from vartypes import VariableValue, NumberValue, MatrixRowValue, MatrixValue, Value, TupleValue
def flatten(l):
lst = []
for elem in l:
if isinstance(elem, list):
lst.append(elem[0])
else:
lst.append(elem)
return lst
def var(_, tokens: List[Token]) -> VariableValue:
return VariableValue(tokens)
def num(_, tokens: List[Token]) -> NumberValue:
return NumberValue(tokens)
def mrw(values: List[Value], _) -> MatrixRowValue:
return MatrixRowValue(values)
def mbd(values: List[Value], _) -> MatrixValue:
return MatrixValue(values)
def opb(values: List[Value], _) -> TupleValue:
return TupleValue(values)
def add(operands: List[Value], operator: Token) -> Value:
return {'+': operands[0].add, '-': operands[0].sub}[operator.value](*operands[1:])
def mul(operands: List[Value], operator: Token) -> Value:
return {'*': operands[0].mul, '/': operands[0].div, '%': operands[0].mod}[operator.value](*operands[1:])
def pow(operands: List[Value], _) -> Value:
return operands[0].pow(*operands[1:])
def opr(operands: List[Value], operator: Token) -> Value:
args = operands[0].value
return getattr(args[0], operator.value)(*args[1:])
def neg(operands: List[Value], operator: Token) -> Value:
return {'+': operands[0].pos, '-': operands[0].neg}[operator.value](*operands[1:])
# The mapping for num, mrw, mbd.
rule_value_map = {
'var': var,
'num': num,
'mrw': mrw,
'mbd': mbd,
'opb': opb,
}
# The mapping for all other rules.
rule_value_operation_map = {
'add': add,
'mul': mul,
'pow': pow,
'opr': opr,
'neg': neg,
}