mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
ShuntingYard: Decrease logging
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
da6c32255e
commit
751afbe93d
1 changed files with 30 additions and 20 deletions
|
|
@ -35,12 +35,20 @@ class Stack:
|
|||
class ShuntingYard(object): # export
|
||||
|
||||
def __init__(self, operators = None):
|
||||
self.debug = True
|
||||
self.do_debug = False
|
||||
self.__ops = {}
|
||||
if operators is not None:
|
||||
for k, v in operators.iteritems():
|
||||
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):
|
||||
r = ""
|
||||
for k, v in self.__ops.iteritems():
|
||||
|
|
@ -66,7 +74,7 @@ class ShuntingYard(object): # export
|
|||
|
||||
scanner = re.Scanner([
|
||||
(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.
|
||||
])
|
||||
|
||||
|
|
@ -74,7 +82,7 @@ class ShuntingYard(object): # export
|
|||
if len(remainder)>0:
|
||||
raise Exception("Failed to tokenize " + spec + ", remaining bit is ", remainder)
|
||||
|
||||
#print tokens
|
||||
#self.debug(tokens)
|
||||
return tokens
|
||||
r = []
|
||||
for e in tokens:
|
||||
|
|
@ -87,11 +95,11 @@ class ShuntingYard(object): # export
|
|||
|
||||
def infix_to_postfix(self, infix):
|
||||
tokenized = self.tokenize(infix)
|
||||
print "tokenized = ", tokenized
|
||||
self.debug("tokenized = ", tokenized)
|
||||
outq, stack = [], []
|
||||
table = ['TOKEN,ACTION,RPN OUTPUT,OP STACK,NOTES'.split(',')]
|
||||
for toktype, token in tokenized:
|
||||
print "Checking token", token
|
||||
self.debug("Checking token", token)
|
||||
note = action = ''
|
||||
if toktype == ARG:
|
||||
action = 'Add arg to output'
|
||||
|
|
@ -144,7 +152,7 @@ class ShuntingYard(object): # export
|
|||
outq.append(t2)
|
||||
table.append( (v, action, ' '.join(outq), ' '.join(s[0] for s in stack), note) )
|
||||
v = note = ''
|
||||
if self.debug:
|
||||
if self.do_debug:
|
||||
maxcolwidths = [len(max(x, key=len)) for x in zip(*table)]
|
||||
row = table[0]
|
||||
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:
|
||||
|
||||
print tokinfo
|
||||
self.debug(tokinfo)
|
||||
toktype, token = tokinfo[0], tokinfo[1]
|
||||
|
||||
print "Checking token ", token
|
||||
self.debug("Checking token ", token)
|
||||
|
||||
if token not in self.__ops.keys():
|
||||
r.append(token)
|
||||
|
|
@ -181,17 +189,17 @@ class ShuntingYard(object): # export
|
|||
continue
|
||||
|
||||
while (not s.isEmpty()) and (self.__ops[s.peek()].prec >= self.__ops[token].prec):
|
||||
#print token
|
||||
#self.debug(token)
|
||||
r.append(s.pop())
|
||||
#print r
|
||||
#self.debug(r)
|
||||
|
||||
s.push(token)
|
||||
print (s.peek())
|
||||
self.debug((s.peek()))
|
||||
|
||||
while not s.isEmpty():
|
||||
opToken = s.pop()
|
||||
r.append(opToken)
|
||||
#print r
|
||||
#self.debug(r)
|
||||
|
||||
return r
|
||||
#return " ".join(r)
|
||||
|
|
@ -202,27 +210,29 @@ class ShuntingYard(object): # export
|
|||
|
||||
for token in postfixexpr:
|
||||
|
||||
print "Checking token %s" % (token)
|
||||
self.debug("Checking token %s" % (token))
|
||||
if token not in self.__ops.keys():
|
||||
vals.push(token)
|
||||
continue
|
||||
|
||||
op = self.__ops[token]
|
||||
args = []
|
||||
print "Adding %d arguments" % (op.nargs)
|
||||
self.debug("Adding %d arguments" % (op.nargs))
|
||||
for i in range(0, op.nargs):
|
||||
print "Adding argument %d" % (i)
|
||||
self.debug("Adding argument %d" % (i))
|
||||
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))
|
||||
print "%s(%s) = %s" % (token, ', '.join(reversed(args)), val)
|
||||
self.debug("%s(%s) = %s" % (token, ', '.join(map(str, reversed(args))), val))
|
||||
vals.push(val)
|
||||
|
||||
return vals.pop()
|
||||
|
||||
def eval(self, infix):
|
||||
if not isinstance(infix, basestring):
|
||||
return infix
|
||||
postfix = self.infix_to_postfix(infix)
|
||||
print infix, "-->", postfix
|
||||
self.debug(infix, "-->", postfix)
|
||||
return self.eval_postfix(postfix)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
@ -262,7 +272,7 @@ if __name__ == '__main__':
|
|||
super(Calculator, self).__init__(operators)
|
||||
|
||||
rr = Calculator().eval("( 2 * 3 + 4 * 5 ) / ( 5 - 3 )")
|
||||
print "Result =", rr
|
||||
self.debug("Result =", rr)
|
||||
|
||||
# ------------- testbed match object
|
||||
|
||||
|
|
@ -309,5 +319,5 @@ if __name__ == '__main__':
|
|||
obj = Object("hans", "wurst")
|
||||
|
||||
r = Matcher(obj).eval("name=hans and (not label~=worst)")
|
||||
print "Result =", r
|
||||
self.debug("Result =", r)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue