ShuntingYard: Decrease logging

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2017-07-27 17:53:55 +02:00
commit 751afbe93d

View file

@ -35,12 +35,20 @@ class Stack:
class ShuntingYard(object): # export class ShuntingYard(object): # export
def __init__(self, operators = None): def __init__(self, operators = None):
self.debug = True self.do_debug = False
self.__ops = {} self.__ops = {}
if operators is not None: if operators is not None:
for k, v in operators.iteritems(): for k, v in operators.iteritems():
self.add_operator(k, v.func, v.nargs, v.prec, v.assoc) self.add_operator(k, v.func, v.nargs, v.prec, v.assoc)
def debug(self, *args):
if self.do_debug:
msg = ""
for count, thing in enumerate(args):
msg += ' ' + str(thing)
if len(msg):
print msg[1:]
def token_string(self): def token_string(self):
r = "" r = ""
for k, v in self.__ops.iteritems(): for k, v in self.__ops.iteritems():
@ -66,7 +74,7 @@ class ShuntingYard(object): # export
scanner = re.Scanner([ scanner = re.Scanner([
(regex, lambda scanner,token:("kw", token)), (regex, lambda scanner,token:("kw", token)),
(r"\w+", lambda scanner,token:("arg", token)), (r"[^\s()]+", lambda scanner,token:("arg", token)),
(r"\s+", None), # None == skip token. (r"\s+", None), # None == skip token.
]) ])
@ -74,7 +82,7 @@ class ShuntingYard(object): # export
if len(remainder)>0: if len(remainder)>0:
raise Exception("Failed to tokenize " + spec + ", remaining bit is ", remainder) raise Exception("Failed to tokenize " + spec + ", remaining bit is ", remainder)
#print tokens #self.debug(tokens)
return tokens return tokens
r = [] r = []
for e in tokens: for e in tokens:
@ -87,11 +95,11 @@ class ShuntingYard(object): # export
def infix_to_postfix(self, infix): def infix_to_postfix(self, infix):
tokenized = self.tokenize(infix) tokenized = self.tokenize(infix)
print "tokenized = ", tokenized self.debug("tokenized = ", tokenized)
outq, stack = [], [] outq, stack = [], []
table = ['TOKEN,ACTION,RPN OUTPUT,OP STACK,NOTES'.split(',')] table = ['TOKEN,ACTION,RPN OUTPUT,OP STACK,NOTES'.split(',')]
for toktype, token in tokenized: for toktype, token in tokenized:
print "Checking token", token self.debug("Checking token", token)
note = action = '' note = action = ''
if toktype == ARG: if toktype == ARG:
action = 'Add arg to output' action = 'Add arg to output'
@ -144,7 +152,7 @@ class ShuntingYard(object): # export
outq.append(t2) outq.append(t2)
table.append( (v, action, ' '.join(outq), ' '.join(s[0] for s in stack), note) ) table.append( (v, action, ' '.join(outq), ' '.join(s[0] for s in stack), note) )
v = note = '' v = note = ''
if self.debug: if self.do_debug:
maxcolwidths = [len(max(x, key=len)) for x in zip(*table)] maxcolwidths = [len(max(x, key=len)) for x in zip(*table)]
row = table[0] row = table[0]
print( ' '.join('{cell:^{width}}'.format(width=width, cell=cell) for (width, cell) in zip(maxcolwidths, row))) print( ' '.join('{cell:^{width}}'.format(width=width, cell=cell) for (width, cell) in zip(maxcolwidths, row)))
@ -160,10 +168,10 @@ class ShuntingYard(object): # export
for tokinfo in tokens: for tokinfo in tokens:
print tokinfo self.debug(tokinfo)
toktype, token = tokinfo[0], tokinfo[1] toktype, token = tokinfo[0], tokinfo[1]
print "Checking token ", token self.debug("Checking token ", token)
if token not in self.__ops.keys(): if token not in self.__ops.keys():
r.append(token) r.append(token)
@ -181,17 +189,17 @@ class ShuntingYard(object): # export
continue continue
while (not s.isEmpty()) and (self.__ops[s.peek()].prec >= self.__ops[token].prec): while (not s.isEmpty()) and (self.__ops[s.peek()].prec >= self.__ops[token].prec):
#print token #self.debug(token)
r.append(s.pop()) r.append(s.pop())
#print r #self.debug(r)
s.push(token) s.push(token)
print (s.peek()) self.debug((s.peek()))
while not s.isEmpty(): while not s.isEmpty():
opToken = s.pop() opToken = s.pop()
r.append(opToken) r.append(opToken)
#print r #self.debug(r)
return r return r
#return " ".join(r) #return " ".join(r)
@ -202,27 +210,29 @@ class ShuntingYard(object): # export
for token in postfixexpr: for token in postfixexpr:
print "Checking token %s" % (token) self.debug("Checking token %s" % (token))
if token not in self.__ops.keys(): if token not in self.__ops.keys():
vals.push(token) vals.push(token)
continue continue
op = self.__ops[token] op = self.__ops[token]
args = [] args = []
print "Adding %d arguments" % (op.nargs) self.debug("Adding %d arguments" % (op.nargs))
for i in range(0, op.nargs): for i in range(0, op.nargs):
print "Adding argument %d" % (i) self.debug("Adding argument %d" % (i))
args.append(vals.pop()) args.append(vals.pop())
#print "running %s(%s)" % (token, ', '.join(reversed(args))) #self.debug("running %s(%s)" % (token, ', '.join(reversed(args))))
val = op.func(*reversed(args)) val = op.func(*reversed(args))
print "%s(%s) = %s" % (token, ', '.join(reversed(args)), val) self.debug("%s(%s) = %s" % (token, ', '.join(map(str, reversed(args))), val))
vals.push(val) vals.push(val)
return vals.pop() return vals.pop()
def eval(self, infix): def eval(self, infix):
if not isinstance(infix, basestring):
return infix
postfix = self.infix_to_postfix(infix) postfix = self.infix_to_postfix(infix)
print infix, "-->", postfix self.debug(infix, "-->", postfix)
return self.eval_postfix(postfix) return self.eval_postfix(postfix)
if __name__ == '__main__': if __name__ == '__main__':
@ -262,7 +272,7 @@ if __name__ == '__main__':
super(Calculator, self).__init__(operators) super(Calculator, self).__init__(operators)
rr = Calculator().eval("( 2 * 3 + 4 * 5 ) / ( 5 - 3 )") rr = Calculator().eval("( 2 * 3 + 4 * 5 ) / ( 5 - 3 )")
print "Result =", rr self.debug("Result =", rr)
# ------------- testbed match object # ------------- testbed match object
@ -309,5 +319,5 @@ if __name__ == '__main__':
obj = Object("hans", "wurst") obj = Object("hans", "wurst")
r = Matcher(obj).eval("name=hans and (not label~=worst)") r = Matcher(obj).eval("name=hans and (not label~=worst)")
print "Result =", r self.debug("Result =", r)