Skip to content

Commit

Permalink
Removed Process to clean up new rule evaluation implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrubin29 committed Oct 11, 2017
1 parent 3f7e035 commit 7360f45
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 44 deletions.
10 changes: 4 additions & 6 deletions ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 21 additions & 38 deletions rules.py
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,
}

0 comments on commit 7360f45

Please sign in to comment.