-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed Process to clean up new rule evaluation implementation.
- Loading branch information
Showing
3 changed files
with
26 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,61 @@ | ||
""" | ||
This file contains methods to handle processing RuleMatches | ||
This file contains methods to handle Valueing RuleMatches | ||
""" | ||
from typing import List | ||
|
||
from common import Token, Value | ||
from vartypes import Number, MatrixRow, Matrix, Variable | ||
|
||
|
||
class Process: | ||
def __init__(self, operation, operands: List, raw_args=False): | ||
self.operation = operation | ||
self.operands = operands | ||
def var(_, tokens: List[Token]) -> Value: | ||
return Variable.new(tokens) | ||
|
||
if raw_args: | ||
self.value = operation(operands) | ||
else: | ||
self.value = operation(*operands) | ||
|
||
def num(_, tokens: List[Token]) -> Value: | ||
return Number.new(tokens) | ||
|
||
def var(_, tokens: List[Token]) -> Process: | ||
return Process(Variable.new, tokens, raw_args=True) | ||
|
||
def mrw(values: List[Value], _) -> Value: | ||
return MatrixRow.new(values) | ||
|
||
def num(_, tokens: List[Token]) -> Process: | ||
return Process(Number.new, tokens, raw_args=True) | ||
|
||
def mbd(values: List[Value], _) -> Value: | ||
return Matrix.new(values) | ||
|
||
def mrw(values: List[Value], _) -> Process: | ||
return Process(MatrixRow.new, values, raw_args=True) | ||
|
||
def add(operands: List[Value], operator: Token) -> Value: | ||
return {'+': operands[0].type.add, '-': operands[0].type.sub}[operator.value](*operands) | ||
|
||
def mbd(values: List[Value], _) -> Process: | ||
return Process(Matrix.new, values, raw_args=True) | ||
|
||
def mul(operands: List[Value], operator: Token) -> Value: | ||
return {'*': operands[0].type.mul, '/': operands[0].type.div, '%': operands[0].type.mod}[operator.value](*operands) | ||
|
||
def add(operands: List[Value], operator: Token) -> Process: | ||
return Process({'+': operands[0].type.add, '-': operands[0].type.sub}[operator.value], operands) | ||
|
||
def pow(operands: List[Value], _) -> Value: | ||
return operands[0].type.pow(*operands) | ||
|
||
def mul(operands: List[Value], operator: Token) -> Process: | ||
return Process({'*': operands[0].type.mul, '/': operands[0].type.div, '%': operands[0].type.mod}[operator.value], operands) | ||
|
||
def opr(operands: List[Value], operator: Token) -> Value: | ||
return getattr(operands[0].type, operator.value)(*operands) | ||
|
||
def pow(operands: List[Value], operator: Token) -> Process: | ||
return Process(operands[0].type.pow, operands) | ||
|
||
|
||
def opr(operands: List[Value], operator: Token) -> Process: | ||
return Process(getattr(operands[0].type, operator.value), operands) | ||
|
||
|
||
def neg(operands: List[Value], operator: Token) -> Process: | ||
return Process({'+': operands[0].type.pos, '-': operands[0].type.neg}[operator.value], operands) | ||
|
||
|
||
def mat(operands: List[Value], operator: Token) -> Process: | ||
# Since mbd creates the matrix, we just want this to return the matrix. | ||
return Process(lambda x: x, operands) | ||
def neg(operands: List[Value], operator: Token) -> Value: | ||
return {'+': operands[0].type.pos, '-': operands[0].type.neg}[operator.value](*operands) | ||
|
||
|
||
# The mapping for num, mrw, mbd. | ||
rule_process_value_map = { | ||
rule_value_map = { | ||
'var': var, | ||
'num': num, | ||
'mrw': mrw, | ||
'mbd': mbd, | ||
} | ||
|
||
# The mapping for all other rules. | ||
rule_process_map = { | ||
rule_value_operation_map = { | ||
'add': add, | ||
'mul': mul, | ||
'pow': pow, | ||
'opr': opr, | ||
'neg': neg, | ||
'mat': mat, | ||
} |