Mercurial > lbo > hg > pcombinators
changeset 46:d1c1e4b60e63 draft
arith_test: More performance, 3. No whitespace, no worries.
3x better than at the start!
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Thu, 23 May 2019 21:48:25 +0200 |
parents | 2bce673577a2 |
children | c7658385dc5f |
files | pcombinators/arith_test.py |
diffstat | 1 files changed, 16 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/pcombinators/arith_test.py Thu May 23 21:44:04 2019 +0200 +++ b/pcombinators/arith_test.py Thu May 23 21:48:25 2019 +0200 @@ -10,8 +10,6 @@ from pcombinators.combinators import * from pcombinators.primitives import * -skip_whitespace = Skip(Whitespace()) - def Parens(): """Parentheses contain a term.""" return (Operator('(') + Term() + Operator(')')) >> (lambda l: l[1]) @@ -22,7 +20,9 @@ def Atom(): """An atom is a variable or a float or a parentheses term.""" - return Skip(Whitespace()).then((Variable() | Parens() | Float())) + return (Variable() | Parens() | Float()) + +atom = Atom() def Operator(set): """An operator or parenthesis.""" @@ -38,26 +38,34 @@ raise Exception("Parse failed: Missing operand") class Power(): + ops = Operator('^') def parse(self, st): - p = OptimisticSequence(Atom(), Operator('^') + Power()) >> operator_result_to_tuple + p = OptimisticSequence(atom, self.ops + power) >> operator_result_to_tuple return p.parse(st) +power = Power() + class Product(Parser): + ops = Operator('*/') def parse(self, st): # Try to parse an atom, a product operator, and another product. - p = OptimisticSequence(Power(), skip_whitespace, Operator('*/') + skip_whitespace + Product()) >> operator_result_to_tuple + p = OptimisticSequence(power, self.ops + product) >> operator_result_to_tuple return p.parse(st) +product = Product() + class Term(Parser): - + ops = Operator('+-') def parse(self, st): # Try to parse a product, then a sum operator, then another term. # OptimisticSequence will just return a product if there is no sum operator. - p = OptimisticSequence(Product(), skip_whitespace, Operator('+-') + skip_whitespace + Term()) >> operator_result_to_tuple + p = OptimisticSequence(product, self.ops + term) >> operator_result_to_tuple return p.parse(st) +term = Term() + def pretty_print(tpl): # tpl is a (left, op, right) tuple or a scalar. if not isinstance(tpl, tuple): @@ -67,7 +75,7 @@ def parse_and_print(expr): """Parse an expression string and return a string of the parsing result.""" - parsed, st = Term().parse(ParseState(expr)) + parsed, st = Term().parse(ParseState(expr.replace(' ', ''))) if parsed is None: print('Parse error :(', st) return