diff --git a/ast.py b/ast.py index 6a18915..fbd02bc 100644 --- a/ast.py +++ b/ast.py @@ -5,7 +5,7 @@ from typing import Dict from common import RuleMatch, remove, left_assoc, Token -from rules import rule_process_map, rule_process_value_map +from rules import rule_value_map, rule_value_operation_map class Ast: @@ -76,13 +76,11 @@ def _evaluate(self, node, vrs: Dict[str, RuleMatch]): if node.matched[0].name == 'IDT': return self._evaluate(copy.deepcopy(vrs[node.matched[0].value]), vrs) - elif node.name in rule_process_value_map: - process = rule_process_value_map[node.name](values, tokens) + elif node.name in rule_value_map: + return rule_value_map[node.name](values, tokens) else: - process = rule_process_map[node.name](values, tokens[0] if len(tokens) > 0 else None) # This extra rule is part of the num hotfix. - - return process.value + return rule_value_operation_map[node.name](values, tokens[0] if len(tokens) > 0 else None) # This extra rule is part of the num hotfix. def infix(self) -> str: # TODO: Add parentheses and missing tokens. diff --git a/common.py b/common.py index 0b5700f..5a6dead 100644 --- a/common.py +++ b/common.py @@ -8,6 +8,7 @@ Token = namedtuple('Token', ('name', 'value')) Value = namedtuple('Value', ('type', 'value')) + class RuleMatch: def __init__(self, name: str, matched: List[Token]): self.name = name diff --git a/rules.py b/rules.py index dbeff1e..942e4e2 100644 --- a/rules.py +++ b/rules.py @@ -1,5 +1,5 @@ """ -This file contains methods to handle processing RuleMatches +This file contains methods to handle Valueing RuleMatches """ from typing import List @@ -7,60 +7,44 @@ 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, @@ -68,11 +52,10 @@ def mat(operands: List[Value], operator: Token) -> Process: } # 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, }