Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/*
__pycache__
__pycache__
/venv/
mathterpreter.egg-info
14 changes: 9 additions & 5 deletions mathterpreter/_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mathterpreter.nodes import *
from mathterpreter.tokens import TokenType, Token
from mathterpreter.tokens import TokenType, Token, FORMATS
from mathterpreter.exceptions import MathSyntaxError
from typing import List

Expand All @@ -11,6 +11,7 @@ def __init__(self, tokens: List[Token]):
self._token = None
self._index = -1
self._iterate_token()
print(tokens)

def _iterate_token(self):
try:
Expand All @@ -31,20 +32,20 @@ def _addition_subtraction_base(self):
if self._token is None:
output = ''.join([z.__str__() for z in self.token_list])
raise MathSyntaxError("Expression expected", f"{output}\n{'^^^'.rjust(len(output) + 3)}")
result = self._multiplication_division()
result = self._multiplication_division_modulo()
while self._token is not None:
if self._token.type == TokenType.ADDITION_OPERATOR:
self._iterate_token()
result = AdditionNode(result, self._multiplication_division())
result = AdditionNode(result, self._multiplication_division_modulo())
elif self._token.type == TokenType.SUBTRACTION_OPERATOR:
self._iterate_token()
result = SubtractionNode(result, self._multiplication_division())
result = SubtractionNode(result, self._multiplication_division_modulo())
else:
break

return result

def _multiplication_division(self):
def _multiplication_division_modulo(self):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have renamed this function but not renamed its usages.

result = self._exponentiation_root()

while self._token is not None:
Expand All @@ -54,6 +55,9 @@ def _multiplication_division(self):
elif self._token.type == TokenType.DIVISION_OPERATOR:
self._iterate_token()
result = DivisionNode(result, self._exponentiation_root())
elif self._token.type == TokenType.MODULO_OPERATOR:
self._iterate_token()
result = ModuloNode(result, self._exponentiation_root())
elif self._token.type == TokenType.OPENING_BRACKET:
result = MultiplicationNode(result, self._literal_polarity())
else:
Expand Down
2 changes: 2 additions & 0 deletions mathterpreter/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def expand(compact_dict):
"^": lambda: TokenType.POWER_OPERATOR,
"(": lambda: TokenType.OPENING_BRACKET,
")": lambda: TokenType.CLOSING_BRACKET,
"!": lambda: TokenType.FACTORIAL_OPERATOR,
"%": lambda: TokenType.MODULO_OPERATOR,
}
CONSTANTS = {
("pi", "π"): lambda: math.pi,
Expand Down
11 changes: 11 additions & 0 deletions mathterpreter/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,14 @@ def __repr__(self):

def evaluate(self) -> float:
return self.first_operand.evaluate() ** self.second_operand.evaluate()

@dataclass
class ModuloNode(OperationNode):
def __repr__(self):
return f"({self.first_operand}%{self.second_operand})"

def evaluate(self) -> float:
return self.first_operand.evaluate() % self.second_operand.evaluate()



4 changes: 3 additions & 1 deletion mathterpreter/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TokenType:
POWER_OPERATOR = 7
OPENING_BRACKET = 8
CLOSING_BRACKET = 9
MODULO_OPERATOR = 10


FORMATS = {
Expand All @@ -27,7 +28,8 @@ class TokenType:
TokenType.SQRT_OPERATOR: "√",
TokenType.POWER_OPERATOR: "^",
TokenType.OPENING_BRACKET: "(",
TokenType.CLOSING_BRACKET: ")"
TokenType.CLOSING_BRACKET: ")",
TokenType.MODULO_OPERATOR: "%",
}


Expand Down