Updated the tokenizer to use a deque object instead of a list. The old calc_engine should still work, not tested. New parser code works with this change.
This commit is contained in:
@@ -2,6 +2,7 @@ import ptoken as token
|
||||
import tokenizer
|
||||
import uuid
|
||||
import calc_engine
|
||||
from collections import deque
|
||||
|
||||
class Op:
|
||||
def __init__(self, left, op, right):
|
||||
@@ -22,14 +23,14 @@ class Num:
|
||||
class Parser:
|
||||
def __init__(self, tokens):
|
||||
self.tokens = tokens
|
||||
self.current_token = self.tokens.pop()
|
||||
self.current_token = self.tokens.popleft()
|
||||
|
||||
|
||||
def eat(self, token_type):
|
||||
if self.current_token.type != token_type:
|
||||
print("grammar")
|
||||
if len(self.tokens) > 0:
|
||||
self.current_token = self.tokens.pop()
|
||||
self.current_token = self.tokens.popleft()
|
||||
|
||||
def factor(self):
|
||||
node = self.current_token
|
||||
@@ -140,7 +141,7 @@ def calc(node):
|
||||
|
||||
return operate(left, right, node.op)
|
||||
|
||||
debug = True
|
||||
debug = False
|
||||
def operate(n1, n2, tokenType):
|
||||
# Utility function to handle dealing with the various mathmatical operations that the engine can process.
|
||||
# Returns the result of any one of five mathmatical operations, else throws an error for unrecognized operations.
|
||||
@@ -177,7 +178,7 @@ tokens = tokenizer.get_tokens_from_expression_string("5*2^(2+1)-(1+5)-2^5+5*(2/2
|
||||
#tokens = tokenizer.get_tokens_from_expression_string("200((1-(1.08)^(-12))/0.08)")
|
||||
#print(calc_engine.calculate_results(tokens))
|
||||
#tokens = tokenizer.get_tokens_from_expression_string("1+6 / 2")
|
||||
#n = Parser(tokens)
|
||||
#s = n.parse()
|
||||
n = Parser(tokens)
|
||||
s = n.parse()
|
||||
#Parser.p(s)
|
||||
#print(calc(s))
|
||||
print(calc(s))
|
||||
|
||||
Reference in New Issue
Block a user