changeset 62:f2515a9dc9ef draft

arith_test: 20% faster by caching OptimisticSequence objects
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 24 May 2019 01:19:01 +0200
parents e3abe28c5a3a
children 62c8c783da35
files pcombinators/arith_test.py
diffstat 1 files changed, 7 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/pcombinators/arith_test.py	Fri May 24 01:13:35 2019 +0200
+++ b/pcombinators/arith_test.py	Fri May 24 01:19:01 2019 +0200
@@ -37,32 +37,30 @@
         # Parse failed if not either 1 or 3.
         raise Exception("Parse failed: Missing operand")
 
-class Power():
+class Power(Parser):
     ops = Operator('^')
-
+    p = OptimisticSequence(atom, Power.ops + power) >> operator_result_to_tuple
     def parse(self, st):
-        p = OptimisticSequence(atom, self.ops + power) >> operator_result_to_tuple
-        return p.parse(st)
+        return self.p.parse(st)
 
 power = Power()
 
 class Product(Parser):
     ops = Operator('*/')
-
+    p = OptimisticSequence(power, Product.ops + product) >> operator_result_to_tuple
     def parse(self, st):
         # Try to parse an atom, a product operator, and another product.
-        p = OptimisticSequence(power, self.ops + product) >> operator_result_to_tuple
-        return p.parse(st)
+        return self.p.parse(st)
 
 product = Product()
 
 class Term(Parser):
     ops = Operator('+-')
+    p = OptimisticSequence(product, Term.ops + term) >> operator_result_to_tuple
     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, self.ops + term) >> operator_result_to_tuple
-        return p.parse(st)
+        return self.p.parse(st)
 
 term = Term()