Skip to content

Commit

Permalink
Fix: unary+
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdidehghandev committed Dec 10, 2024
1 parent b541673 commit d8dab26
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 2 additions & 0 deletions evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def _apply_binary_op(op, a, b):
def _apply_single_op(op, a):
if op == "unary-":
return -a
elif op == "unary+":
return a
elif op == "sin":
return math.sin(a)
elif op == "cos":
Expand Down
5 changes: 1 addition & 4 deletions lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def tokenize(self):
self.is_single_line_comment()
elif char == '{':
self.is_multi_line_comment()
# elif char in self.operators:

elif self.symbol_table.is_operator(char):
self.is_operator()
elif char.isdigit():
Expand Down Expand Up @@ -191,15 +191,12 @@ def is_identifier(self):
state = 2
case 2:
lexeme = self.text[self.beginning:self.forward].lower()
# if self.symbol_table.is_not_reserved(lexeme) and not self.symbol_table.is_id_existence(lexeme):
# self.symbol_table.add_id(lexeme)
if self.symbol_table.is_operator(lexeme):
self.create_token("OPERATOR")
self.beginning = self.forward
return
elif self.symbol_table.is_function(lexeme):

# if lexeme in ['sin', 'cos', 'tan', 'cot', 'arcsin', 'arccos', 'arctan', 'arccot', 'log', 'sqrt', 'sqr', 'exp']:
self.create_token("FUNCTION")
self.beginning = self.forward
return
Expand Down
11 changes: 6 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ def main(file_name):
parser = Parser(symbol_table, tokens)
parser.parse()

print("Parsed postfix expression:")
for i in parser.postfix:
print(i.value, end=" ")
print("\n")



while True:
Expand All @@ -108,6 +105,10 @@ def main(file_name):
choice = input("--> ")

if choice == "1":
print("Parsed postfix expression:")
for i in parser.postfix:
print(i.value, end=" ")
print("\n")
evaluate = Evaluate(parser.postfix,symbol_table)
print(evaluate.evaluate())

Expand All @@ -130,4 +131,4 @@ def main(file_name):


if __name__ == "__main__":
main('input1.txt')
main('test.txt')
7 changes: 6 additions & 1 deletion parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def factor(self):
token.value = 'unary-'
self.factor()
self.postfix.append(token)
elif self.lookahead.value == '+':
token = self.lookahead
self.match('+')
token.value = 'unary+'
self.factor()
self.postfix.append(token)

elif self.lookahead.type == "NUM":
self.number()
Expand All @@ -139,7 +145,6 @@ def factor(self):
self.expr()
self.match(')')

# elif self.lookahead.value.lower() in ['sin', 'cos', 'tan', 'cot', 'arcsin', 'arccos', 'arctan', 'arccot', 'log', 'sqrt', 'sqr', 'exp']:
elif self.symbol_table.is_function(self.lookahead.value.lower()):
token = self.lookahead
math_func = self.lookahead.value
Expand Down
1 change: 1 addition & 0 deletions symbol_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self):
'mod': {'type': 'OPERATOR', 'args': 2, "is_reserved": True},
'^': {'type': 'OPERATOR', 'args': 2, "is_reserved": None},
'unary-': {'type': 'OPERATOR', 'args': 1, "is_reserved": None},
'unary+': {'type': 'OPERATOR', 'args': 1, "is_reserved": None},
'(': {'type': 'DELIMITER', "is_reserved": None},
')': {'type': 'DELIMITER', "is_reserved": None},
'e': {'type': 'IDENTIFIER', 'value': 2.71, "is_reserved": True},
Expand Down

0 comments on commit d8dab26

Please sign in to comment.