changeset 395:3b0c495e3008

Speed improvements
author Windel Bouwman
date Fri, 23 May 2014 14:28:03 +0200
parents 988f3fb861e4
children fb3c1f029b30
files python/pyyacc.py test/testx86asm.py
diffstat 2 files changed, 23 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/python/pyyacc.py	Thu May 22 08:14:12 2014 +0200
+++ b/python/pyyacc.py	Fri May 23 14:28:03 2014 +0200
@@ -302,37 +302,28 @@
         assert self.dotpos <= len(self.production.symbols)
         self.look_ahead = look_ahead
         self._is_shift = self.dotpos < len(self.production.symbols)
-
-    def getdata(self):
-        """ Gets the members as a tuple """
-        return (self.production, self.dotpos, self.look_ahead)
+        self.IsShift = self._is_shift
+        if self.IsShift:
+            self.Next = self.production.symbols[self.dotpos]
+        self._data = (self.production, self.dotpos, self.look_ahead)
+        self._hash = self._data.__hash__()
 
     def __eq__(self, other):
         if type(other) is type(self):
-            return self.getdata() == other.getdata()
+            return self._data == other._data
         return False
 
     def __hash__(self):
-        return self.getdata().__hash__()
+        return self._hash
 
     @property
     def IsReduce(self):
         """ Check if this item has the dot at the end """
         return not self._is_shift
 
-    @property
-    def IsShift(self):
-        """ Check if this item is a shift item, i.e. the dot can proceed """
-        return self._is_shift
-
-    @property
-    def Next(self):
-        """ Returns the symbol after the dot """
-        return self.production.symbols[self.dotpos]
-
     def can_shift_over(self, symbol):
         """ Determines if this item can shift over the given symbol """
-        return self.IsShift and self.Next == symbol
+        return self._is_shift and self.Next == symbol
 
     def shifted(self):
         """ Creates a new item that is shifted one position """
@@ -356,7 +347,9 @@
 
 
 class LRParser:
-    """ LR parser automata """
+    """ LR parser automata. This class takes goto and action table
+        and can then process a sequence of tokens.
+    """
     def __init__(self, action_table, goto_table, start_symbol):
         self.action_table = action_table
         self.goto_table = goto_table
--- a/test/testx86asm.py	Thu May 22 08:14:12 2014 +0200
+++ b/test/testx86asm.py	Fri May 23 14:28:03 2014 +0200
@@ -39,26 +39,27 @@
         # assert(assembler.call('rcx') == [0xff, 0xd1])
 
     def testXOR(self):
-      assert(assembler.xorreg64('rax', 'rax') == [0x48, 0x31, 0xc0])
-      assert(assembler.xorreg64('r9', 'r8') == [0x4d, 0x31, 0xc1])
-      assert(assembler.xorreg64('rbx', 'r11') == [0x4c, 0x31, 0xdb])
+        self.feed('xor rax, rax')
+        self.feed('xor r9, r8')
+        self.feed('xor rbx, r11')
+        self.check('48 31 c0 4d 31 c1 4c 31 db')
 
     def testINC(self):
-      assert(assembler.increg64('r11') == [0x49, 0xff, 0xc3])
-      assert(assembler.increg64('rcx') == [0x48, 0xff, 0xc1])
+        self.feed('inc r11')
+        self.feed('inc rcx')
+        self.check('49 ff c3 48 ff c1')
 
     def testPush(self):
-      assert(assembler.push('rbp') == [0x55])
-      assert(assembler.push('rbx') == [0x53])
-      assert(assembler.push('r12') == [0x41, 0x54])
+        self.feed('push rbp')
+        self.feed('push rbx')
+        self.feed('push r12')
+        self.check('55 53 41 54')
 
     def testPop(self):
         self.feed('pop rbx')
         self.feed('pop rbp')
         self.feed('pop r12')
-        assert(assembler.pop('rbx') == [0x5b])
-        assert(assembler.pop('rbp') == [0x5d])
-        assert(assembler.pop('r12') == [0x41, 0x5c])
+        self.check('5b 5d 41 5c')
 
     def testAsmLoads(self):
       # TODO constant add testcases