[Python-modules-commits] [ply] 01/08: Import ply_3.9.orig.tar.gz

Arnaud Fontaine arnau at moszumanska.debian.org
Fri Nov 18 06:50:31 UTC 2016


This is an automated email from the git hooks/post-receive script.

arnau pushed a commit to branch master
in repository ply.

commit 466362e0b5385640af2ec20d3b71f2d5b10ca85e
Author: Arnaud Fontaine <arnaud.fontaine at nexedi.com>
Date:   Fri Nov 18 15:30:46 2016 +0900

    Import ply_3.9.orig.tar.gz
---
 ANNOUNCE                               |   8 +-
 CHANGES                                |  36 ++
 PKG-INFO                               |   2 +-
 README.md                              |   4 +-
 doc/ply.html                           |  39 +-
 example/BASIC/basic.py                 |  46 +--
 example/BASIC/basiclex.py              |  71 ++--
 example/BASIC/basiclog.py              |  56 ++-
 example/BASIC/basinterp.py             | 719 ++++++++++++++++++---------------
 example/BASIC/basparse.py              | 290 +++++++------
 example/GardenSnake/GardenSnake.py     | 160 +++++---
 example/ansic/clex.py                  | 132 +++---
 example/ansic/cparse.py                | 208 +++++++++-
 example/calc/calc.py                   |  50 ++-
 example/calc/parser.out                | 395 ------------------
 example/calc/parsetab.py               |  39 --
 example/calcdebug/calc.py              |  52 ++-
 example/calceof/calc.py                |  51 ++-
 example/classcalc/calc.py              |  72 ++--
 example/closurecalc/calc.py            |  44 +-
 example/hedit/hedit.py                 |  22 +-
 example/newclasscalc/calc.py           |  73 ++--
 example/optcalc/calc.py                |  67 +--
 example/unicalc/calc.py                |  66 +--
 example/yply/ylex.py                   |  91 +++--
 example/yply/yparse.py                 | 153 ++++---
 example/yply/yply.py                   |  10 +-
 ply.egg-info/PKG-INFO                  |   2 +-
 ply.egg-info/SOURCES.txt               |  19 +-
 ply/__init__.py                        |   2 +-
 ply/cpp.py                             |  13 +-
 ply/lex.py                             |  13 +-
 ply/yacc.py                            |  75 ++--
 setup.cfg                              |   8 +-
 setup.py                               |   2 +-
 test/parser.out                        |   1 -
 test/pkg_test1/parsing/lextab.py       |  10 -
 test/pkg_test1/parsing/parser.out      | 395 ------------------
 test/pkg_test1/parsing/parsetab.py     |  39 --
 test/pkg_test2/parsing/calclextab.py   |  10 -
 test/pkg_test2/parsing/calcparsetab.py |  39 --
 test/pkg_test2/parsing/parser.out      | 395 ------------------
 test/pkg_test3/generated/lextab.py     |  10 -
 test/pkg_test3/generated/parser.out    | 395 ------------------
 test/pkg_test3/generated/parsetab.py   |  39 --
 test/pkg_test5/parsing/lextab.py       |  10 -
 test/pkg_test5/parsing/parser.out      | 395 ------------------
 test/pkg_test5/parsing/parsetab.py     |  39 --
 test/pkg_test6/parsing/lextab.py       |  10 -
 test/pkg_test6/parsing/parser.out      | 338 ----------------
 test/pkg_test6/parsing/parsetab.py     |  39 --
 test/testlex.py                        |  77 ++--
 52 files changed, 1655 insertions(+), 3676 deletions(-)

diff --git a/ANNOUNCE b/ANNOUNCE
index badc9b6..15fcb41 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,11 +1,11 @@
-August 25, 2015
+August 31, 2016
 
-                  Announcing :  PLY-3.7 (Python Lex-Yacc)
+                  Announcing :  PLY-3.9 (Python Lex-Yacc)
 
                         http://www.dabeaz.com/ply
 
-I'm pleased to announce PLY-3.7--a pure Python implementation of the
-common parsing tools lex and yacc.  PLY-3.7 is a minor bug fix
+I'm pleased to announce PLY-3.9--a pure Python implementation of the
+common parsing tools lex and yacc.  PLY-3.9 is a minor bug fix
 release.  It supports both Python 2 and Python 3.
 
 If you are new to PLY, here are a few highlights:
diff --git a/CHANGES b/CHANGES
index 6a50772..75cba86 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,39 @@
+Version 3.9
+---------------------
+08/30/16: beazley
+          Exposed the parser state number as the parser.state attribute
+          in productions and error functions. For example:
+
+          def p_somerule(p):
+              '''
+              rule : A B C
+              '''
+              print('State:', p.parser.state)
+
+          May address issue #65 (publish current state in error callback).
+
+08/30/16: beazley 
+          Fixed Issue #88. Python3 compatibility with ply/cpp.
+
+08/30/16: beazley
+          Fixed Issue #93. Ply can crash if SyntaxError is raised inside
+          a production.   Not actually sure if the original implementation
+          worked as documented at all.  Yacc has been modified to follow
+          the spec as outlined in the CHANGES noted for 11/27/07 below.
+
+08/30/16: beazley
+          Fixed Issue #97. Failure with code validation when the original
+          source files aren't present.   Validation step now ignores
+          the missing file.
+
+08/30/16: beazley
+          Minor fixes to version numbers.
+
+Version 3.8
+---------------------
+10/02/15: beazley
+          Fixed issues related to Python 3.5. Patch contributed by Barry Warsaw.
+
 Version 3.7
 ---------------------
 08/25/15: beazley
diff --git a/PKG-INFO b/PKG-INFO
index a332621..bbbb91c 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: ply
-Version: 3.7
+Version: 3.9
 Summary: Python Lex & Yacc
 Home-page: http://www.dabeaz.com/ply/
 Author: David Beazley
diff --git a/README.md b/README.md
index ec10aaf..402ffba 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
-PLY (Python Lex-Yacc)                   Version 3.7
+PLY (Python Lex-Yacc)                   Version 3.9
 
-Copyright (C) 2001-2015,
+Copyright (C) 2001-2016
 David M. Beazley (Dabeaz LLC)
 All rights reserved.
 
diff --git a/doc/ply.html b/doc/ply.html
index 808ed1d..70a4152 100644
--- a/doc/ply.html
+++ b/doc/ply.html
@@ -12,7 +12,7 @@ dave at dabeaz.com<br>
 </b>
 
 <p>
-<b>PLY Version: 3.6</b>
+<b>PLY Version: 3.9</b>
 <p>
 
 <!-- INDEX -->
@@ -84,6 +84,7 @@ dave at dabeaz.com<br>
 
 
 
+
 <H2><a name="ply_nn1"></a>1. Preface and Requirements</H2>
 
 
@@ -3167,6 +3168,42 @@ in the same source file.
 </li>
 </p>
 
+<p>
+<li>Decorators of production rules have to update the wrapped function's line number.  <tt>wrapper.co_firstlineno = func.__code__.co_firstlineno</tt>:
+
+<blockquote>
+<pre>
+from functools import wraps
+from nodes import Collection
+
+
+def strict(*types):
+    def decorate(func):
+        @wraps(func)
+        def wrapper(p):
+            func(p)
+            if not isinstance(p[0], types):
+                raise TypeError
+
+        wrapper.co_firstlineno = func.__code__.co_firstlineno
+        return wrapper
+
+    return decorate
+
+ at strict(Collection)
+def p_collection(p):
+    """
+    collection  : sequence
+                | map
+    """
+    p[0] = p[1]
+</pre>
+</blockquote>
+
+</li>
+</p>
+
+
 </ul>
 </p>
 
diff --git a/example/BASIC/basic.py b/example/BASIC/basic.py
index b14483d..70ac9e7 100644
--- a/example/BASIC/basic.py
+++ b/example/BASIC/basic.py
@@ -2,7 +2,7 @@
 #
 
 import sys
-sys.path.insert(0,"../..")
+sys.path.insert(0, "../..")
 
 if sys.version_info[0] >= 3:
     raw_input = input
@@ -17,7 +17,8 @@ import basinterp
 if len(sys.argv) == 2:
     data = open(sys.argv[1]).read()
     prog = basparse.parse(data)
-    if not prog: raise SystemExit
+    if not prog:
+        raise SystemExit
     b = basinterp.BasicInterpreter(prog)
     try:
         b.run()
@@ -39,33 +40,26 @@ while 1:
         line = raw_input("[BASIC] ")
     except EOFError:
         raise SystemExit
-    if not line: continue
+    if not line:
+        continue
     line += "\n"
     prog = basparse.parse(line)
-    if not prog: continue
+    if not prog:
+        continue
 
     keys = list(prog)
     if keys[0] > 0:
-         b.add_statements(prog)
+        b.add_statements(prog)
     else:
-         stat = prog[keys[0]]
-         if stat[0] == 'RUN':
-             try:
-                 b.run()
-             except RuntimeError:
-                 pass
-         elif stat[0] == 'LIST':
-             b.list()
-         elif stat[0] == 'BLANK':
-             b.del_line(stat[1])
-         elif stat[0] == 'NEW':
-             b.new()
-
-  
-            
-
-
-
-
-
-
+        stat = prog[keys[0]]
+        if stat[0] == 'RUN':
+            try:
+                b.run()
+            except RuntimeError:
+                pass
+        elif stat[0] == 'LIST':
+            b.list()
+        elif stat[0] == 'BLANK':
+            b.del_line(stat[1])
+        elif stat[0] == 'NEW':
+            b.new()
diff --git a/example/BASIC/basiclex.py b/example/BASIC/basiclex.py
index 3d27cde..4151f4c 100644
--- a/example/BASIC/basiclex.py
+++ b/example/BASIC/basiclex.py
@@ -3,72 +3,59 @@
 from ply import *
 
 keywords = (
-    'LET','READ','DATA','PRINT','GOTO','IF','THEN','FOR','NEXT','TO','STEP',
-    'END','STOP','DEF','GOSUB','DIM','REM','RETURN','RUN','LIST','NEW',
+    'LET', 'READ', 'DATA', 'PRINT', 'GOTO', 'IF', 'THEN', 'FOR', 'NEXT', 'TO', 'STEP',
+    'END', 'STOP', 'DEF', 'GOSUB', 'DIM', 'REM', 'RETURN', 'RUN', 'LIST', 'NEW',
 )
 
 tokens = keywords + (
-     'EQUALS','PLUS','MINUS','TIMES','DIVIDE','POWER',
-     'LPAREN','RPAREN','LT','LE','GT','GE','NE',
-     'COMMA','SEMI', 'INTEGER','FLOAT', 'STRING',
-     'ID','NEWLINE'
+    'EQUALS', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'POWER',
+    'LPAREN', 'RPAREN', 'LT', 'LE', 'GT', 'GE', 'NE',
+    'COMMA', 'SEMI', 'INTEGER', 'FLOAT', 'STRING',
+    'ID', 'NEWLINE'
 )
 
 t_ignore = ' \t'
 
+
 def t_REM(t):
     r'REM .*'
     return t
 
+
 def t_ID(t):
     r'[A-Z][A-Z0-9]*'
     if t.value in keywords:
         t.type = t.value
     return t
-    
-t_EQUALS  = r'='
-t_PLUS    = r'\+'
-t_MINUS   = r'-'
-t_TIMES   = r'\*'
-t_POWER   = r'\^'
-t_DIVIDE  = r'/'
-t_LPAREN  = r'\('
-t_RPAREN  = r'\)'
-t_LT      = r'<'
-t_LE      = r'<='
-t_GT      = r'>'
-t_GE      = r'>='
-t_NE      = r'<>'
-t_COMMA   = r'\,'
-t_SEMI    = r';'
-t_INTEGER = r'\d+'    
-t_FLOAT   = r'((\d*\.\d+)(E[\+-]?\d+)?|([1-9]\d*E[\+-]?\d+))'
-t_STRING  = r'\".*?\"'
+
+t_EQUALS = r'='
+t_PLUS = r'\+'
+t_MINUS = r'-'
+t_TIMES = r'\*'
+t_POWER = r'\^'
+t_DIVIDE = r'/'
+t_LPAREN = r'\('
+t_RPAREN = r'\)'
+t_LT = r'<'
+t_LE = r'<='
+t_GT = r'>'
+t_GE = r'>='
+t_NE = r'<>'
+t_COMMA = r'\,'
+t_SEMI = r';'
+t_INTEGER = r'\d+'
+t_FLOAT = r'((\d*\.\d+)(E[\+-]?\d+)?|([1-9]\d*E[\+-]?\d+))'
+t_STRING = r'\".*?\"'
+
 
 def t_NEWLINE(t):
     r'\n'
     t.lexer.lineno += 1
     return t
 
+
 def t_error(t):
     print("Illegal character %s" % t.value[0])
     t.lexer.skip(1)
 
 lex.lex(debug=0)
-
-
-
-
-
-
-
-       
-   
-  
-            
-
-
-
-
-
-
diff --git a/example/BASIC/basiclog.py b/example/BASIC/basiclog.py
index ccfd7b9..9dcc7fe 100644
--- a/example/BASIC/basiclog.py
+++ b/example/BASIC/basiclog.py
@@ -2,16 +2,16 @@
 #
 
 import sys
-sys.path.insert(0,"../..")
+sys.path.insert(0, "../..")
 
 if sys.version_info[0] >= 3:
     raw_input = input
 
 import logging
 logging.basicConfig(
-    level = logging.INFO,
-    filename = "parselog.txt",
-    filemode = "w"
+    level=logging.INFO,
+    filename="parselog.txt",
+    filemode="w"
 )
 log = logging.getLogger()
 
@@ -24,8 +24,9 @@ import basinterp
 # interactive mode below
 if len(sys.argv) == 2:
     data = open(sys.argv[1]).read()
-    prog = basparse.parse(data,debug=log)
-    if not prog: raise SystemExit
+    prog = basparse.parse(data, debug=log)
+    if not prog:
+        raise SystemExit
     b = basinterp.BasicInterpreter(prog)
     try:
         b.run()
@@ -47,33 +48,26 @@ while 1:
         line = raw_input("[BASIC] ")
     except EOFError:
         raise SystemExit
-    if not line: continue
+    if not line:
+        continue
     line += "\n"
-    prog = basparse.parse(line,debug=log)
-    if not prog: continue
+    prog = basparse.parse(line, debug=log)
+    if not prog:
+        continue
 
     keys = list(prog)
     if keys[0] > 0:
-         b.add_statements(prog)
+        b.add_statements(prog)
     else:
-         stat = prog[keys[0]]
-         if stat[0] == 'RUN':
-             try:
-                 b.run()
-             except RuntimeError:
-                 pass
-         elif stat[0] == 'LIST':
-             b.list()
-         elif stat[0] == 'BLANK':
-             b.del_line(stat[1])
-         elif stat[0] == 'NEW':
-             b.new()
-
-  
-            
-
-
-
-
-
-
+        stat = prog[keys[0]]
+        if stat[0] == 'RUN':
+            try:
+                b.run()
+            except RuntimeError:
+                pass
+        elif stat[0] == 'LIST':
+            b.list()
+        elif stat[0] == 'BLANK':
+            b.del_line(stat[1])
+        elif stat[0] == 'NEW':
+            b.new()
diff --git a/example/BASIC/basinterp.py b/example/BASIC/basinterp.py
index 3e8a777..67762c7 100644
--- a/example/BASIC/basinterp.py
+++ b/example/BASIC/basinterp.py
@@ -5,141 +5,167 @@ import sys
 import math
 import random
 
+
 class BasicInterpreter:
 
     # Initialize the interpreter. prog is a dictionary
     # containing (line,statement) mappings
-    def __init__(self,prog):
-         self.prog = prog
-
-         self.functions = {           # Built-in function table
-             'SIN' : lambda z: math.sin(self.eval(z)),
-             'COS' : lambda z: math.cos(self.eval(z)),
-             'TAN' : lambda z: math.tan(self.eval(z)),
-             'ATN' : lambda z: math.atan(self.eval(z)),
-             'EXP' : lambda z: math.exp(self.eval(z)),
-             'ABS' : lambda z: abs(self.eval(z)),
-             'LOG' : lambda z: math.log(self.eval(z)),
-             'SQR' : lambda z: math.sqrt(self.eval(z)),
-             'INT' : lambda z: int(self.eval(z)),
-             'RND' : lambda z: random.random()
-         }
+    def __init__(self, prog):
+        self.prog = prog
+
+        self.functions = {           # Built-in function table
+            'SIN': lambda z: math.sin(self.eval(z)),
+            'COS': lambda z: math.cos(self.eval(z)),
+            'TAN': lambda z: math.tan(self.eval(z)),
+            'ATN': lambda z: math.atan(self.eval(z)),
+            'EXP': lambda z: math.exp(self.eval(z)),
+            'ABS': lambda z: abs(self.eval(z)),
+            'LOG': lambda z: math.log(self.eval(z)),
+            'SQR': lambda z: math.sqrt(self.eval(z)),
+            'INT': lambda z: int(self.eval(z)),
+            'RND': lambda z: random.random()
+        }
 
     # Collect all data statements
     def collect_data(self):
-         self.data = []
-         for lineno in self.stat:
-              if self.prog[lineno][0] == 'DATA':
-                  self.data = self.data + self.prog[lineno][1]
-         self.dc = 0                  # Initialize the data counter
+        self.data = []
+        for lineno in self.stat:
+            if self.prog[lineno][0] == 'DATA':
+                self.data = self.data + self.prog[lineno][1]
+        self.dc = 0                  # Initialize the data counter
 
     # Check for end statements
     def check_end(self):
-         has_end = 0
-         for lineno in self.stat:
-             if self.prog[lineno][0] == 'END' and not has_end:
-                  has_end = lineno
-         if not has_end:
-             print("NO END INSTRUCTION")
-             self.error = 1
-             return
-         if has_end != lineno:
-             print("END IS NOT LAST")
-             self.error = 1
+        has_end = 0
+        for lineno in self.stat:
+            if self.prog[lineno][0] == 'END' and not has_end:
+                has_end = lineno
+        if not has_end:
+            print("NO END INSTRUCTION")
+            self.error = 1
+            return
+        if has_end != lineno:
+            print("END IS NOT LAST")
+            self.error = 1
 
     # Check loops
     def check_loops(self):
-         for pc in range(len(self.stat)):
-             lineno = self.stat[pc]
-             if self.prog[lineno][0] == 'FOR':
-                  forinst = self.prog[lineno]
-                  loopvar = forinst[1]
-                  for i in range(pc+1,len(self.stat)):
-                       if self.prog[self.stat[i]][0] == 'NEXT':
-                            nextvar = self.prog[self.stat[i]][1]
-                            if nextvar != loopvar: continue
-                            self.loopend[pc] = i
-                            break
-                  else:
-                       print("FOR WITHOUT NEXT AT LINE %s" % self.stat[pc])
-                       self.error = 1
-                  
+        for pc in range(len(self.stat)):
+            lineno = self.stat[pc]
+            if self.prog[lineno][0] == 'FOR':
+                forinst = self.prog[lineno]
+                loopvar = forinst[1]
+                for i in range(pc + 1, len(self.stat)):
+                    if self.prog[self.stat[i]][0] == 'NEXT':
+                        nextvar = self.prog[self.stat[i]][1]
+                        if nextvar != loopvar:
+                            continue
+                        self.loopend[pc] = i
+                        break
+                else:
+                    print("FOR WITHOUT NEXT AT LINE %s" % self.stat[pc])
+                    self.error = 1
+
     # Evaluate an expression
-    def eval(self,expr):
+    def eval(self, expr):
         etype = expr[0]
-        if etype == 'NUM': return expr[1]
-        elif etype == 'GROUP': return self.eval(expr[1])
+        if etype == 'NUM':
+            return expr[1]
+        elif etype == 'GROUP':
+            return self.eval(expr[1])
         elif etype == 'UNARY':
-             if expr[1] == '-': return -self.eval(expr[2])
+            if expr[1] == '-':
+                return -self.eval(expr[2])
         elif etype == 'BINOP':
-             if expr[1] == '+': return self.eval(expr[2])+self.eval(expr[3])
-             elif expr[1] == '-': return self.eval(expr[2])-self.eval(expr[3])
-             elif expr[1] == '*': return self.eval(expr[2])*self.eval(expr[3])
-             elif expr[1] == '/': return float(self.eval(expr[2]))/self.eval(expr[3])
-             elif expr[1] == '^': return abs(self.eval(expr[2]))**self.eval(expr[3])
+            if expr[1] == '+':
+                return self.eval(expr[2]) + self.eval(expr[3])
+            elif expr[1] == '-':
+                return self.eval(expr[2]) - self.eval(expr[3])
+            elif expr[1] == '*':
+                return self.eval(expr[2]) * self.eval(expr[3])
+            elif expr[1] == '/':
+                return float(self.eval(expr[2])) / self.eval(expr[3])
+            elif expr[1] == '^':
+                return abs(self.eval(expr[2]))**self.eval(expr[3])
         elif etype == 'VAR':
-             var,dim1,dim2 = expr[1]
-             if not dim1 and not dim2:
-                  if var in self.vars:
-                       return self.vars[var]
-                  else:
-                       print("UNDEFINED VARIABLE %s AT LINE %s" % (var, self.stat[self.pc]))
-                       raise RuntimeError
-             # May be a list lookup or a function evaluation
-             if dim1 and not dim2:
+            var, dim1, dim2 = expr[1]
+            if not dim1 and not dim2:
+                if var in self.vars:
+                    return self.vars[var]
+                else:
+                    print("UNDEFINED VARIABLE %s AT LINE %s" %
+                          (var, self.stat[self.pc]))
+                    raise RuntimeError
+            # May be a list lookup or a function evaluation
+            if dim1 and not dim2:
                 if var in self.functions:
-                      # A function
-                      return self.functions[var](dim1)
+                    # A function
+                    return self.functions[var](dim1)
                 else:
-                      # A list evaluation
-                      if var in self.lists:
-                            dim1val = self.eval(dim1)
-                            if dim1val < 1 or dim1val > len(self.lists[var]):
-                                 print("LIST INDEX OUT OF BOUNDS AT LINE %s" % self.stat[self.pc])
-                                 raise RuntimeError
-                            return self.lists[var][dim1val-1]
-             if dim1 and dim2:
-                 if var in self.tables:
-                      dim1val = self.eval(dim1)
-                      dim2val = self.eval(dim2)
-                      if dim1val < 1 or dim1val > len(self.tables[var]) or dim2val < 1 or dim2val > len(self.tables[var][0]):
-                           print("TABLE INDEX OUT OUT BOUNDS AT LINE %s" % self.stat[self.pc])
-                           raise RuntimeError
-                      return self.tables[var][dim1val-1][dim2val-1]
-             print("UNDEFINED VARIABLE %s AT LINE %s" % (var, self.stat[self.pc]))
-             raise RuntimeError
+                    # A list evaluation
+                    if var in self.lists:
+                        dim1val = self.eval(dim1)
+                        if dim1val < 1 or dim1val > len(self.lists[var]):
+                            print("LIST INDEX OUT OF BOUNDS AT LINE %s" %
+                                  self.stat[self.pc])
+                            raise RuntimeError
+                        return self.lists[var][dim1val - 1]
+            if dim1 and dim2:
+                if var in self.tables:
+                    dim1val = self.eval(dim1)
+                    dim2val = self.eval(dim2)
+                    if dim1val < 1 or dim1val > len(self.tables[var]) or dim2val < 1 or dim2val > len(self.tables[var][0]):
+                        print("TABLE INDEX OUT OUT BOUNDS AT LINE %s" %
+                              self.stat[self.pc])
+                        raise RuntimeError
+                    return self.tables[var][dim1val - 1][dim2val - 1]
+            print("UNDEFINED VARIABLE %s AT LINE %s" %
+                  (var, self.stat[self.pc]))
+            raise RuntimeError
 
     # Evaluate a relational expression
-    def releval(self,expr):
-         etype = expr[1]
-         lhs   = self.eval(expr[2])
-         rhs   = self.eval(expr[3])
-         if etype == '<':
-             if lhs < rhs: return 1
-             else: return 0
-
-         elif etype == '<=':
-             if lhs <= rhs: return 1
-             else: return 0
-
-         elif etype == '>':
-             if lhs > rhs: return 1
-             else: return 0
-
-         elif etype == '>=':
-             if lhs >= rhs: return 1
-             else: return 0
-
-         elif etype == '=':
-             if lhs == rhs: return 1
-             else: return 0
-
-         elif etype == '<>':
-             if lhs != rhs: return 1
-             else: return 0
+    def releval(self, expr):
+        etype = expr[1]
+        lhs = self.eval(expr[2])
+        rhs = self.eval(expr[3])
+        if etype == '<':
+            if lhs < rhs:
+                return 1
+            else:
+                return 0
+
+        elif etype == '<=':
+            if lhs <= rhs:
+                return 1
+            else:
+                return 0
+
+        elif etype == '>':
+            if lhs > rhs:
+                return 1
+            else:
+                return 0
+
+        elif etype == '>=':
+            if lhs >= rhs:
+                return 1
+            else:
+                return 0
+
+        elif etype == '=':
+            if lhs == rhs:
+                return 1
+            else:
+                return 0
+
+        elif etype == '<>':
+            if lhs != rhs:
+                return 1
+            else:
+                return 0
 
     # Assignment
-    def assign(self,target,value):
+    def assign(self, target, value):
         var, dim1, dim2 = target
         if not dim1 and not dim2:
             self.vars[var] = self.eval(value)
@@ -147,42 +173,44 @@ class BasicInterpreter:
             # List assignment
             dim1val = self.eval(dim1)
             if not var in self.lists:
-                 self.lists[var] = [0]*10
+                self.lists[var] = [0] * 10
 
             if dim1val > len(self.lists[var]):
-                 print ("DIMENSION TOO LARGE AT LINE %s" % self.stat[self.pc])
-                 raise RuntimeError
-            self.lists[var][dim1val-1] = self.eval(value)
+                print ("DIMENSION TOO LARGE AT LINE %s" % self.stat[self.pc])
+                raise RuntimeError
+            self.lists[var][dim1val - 1] = self.eval(value)
         elif dim1 and dim2:
             dim1val = self.eval(dim1)
             dim2val = self.eval(dim2)
             if not var in self.tables:
-                 temp = [0]*10
-                 v = []
-                 for i in range(10): v.append(temp[:])
-                 self.tables[var] = v
+                temp = [0] * 10
+                v = []
+                for i in range(10):
+                    v.append(temp[:])
+                self.tables[var] = v
             # Variable already exists
             if dim1val > len(self.tables[var]) or dim2val > len(self.tables[var][0]):
-                 print("DIMENSION TOO LARGE AT LINE %s" % self.stat[self.pc])
-                 raise RuntimeError
-            self.tables[var][dim1val-1][dim2val-1] = self.eval(value)
+                print("DIMENSION TOO LARGE AT LINE %s" % self.stat[self.pc])
+                raise RuntimeError
+            self.tables[var][dim1val - 1][dim2val - 1] = self.eval(value)
 
     # Change the current line number
-    def goto(self,linenum):
-         if not linenum in self.prog:
-              print("UNDEFINED LINE NUMBER %d AT LINE %d" % (linenum, self.stat[self.pc]))
-              raise RuntimeError
-         self.pc = self.stat.index(linenum)
+    def goto(self, linenum):
+        if not linenum in self.prog:
+            print("UNDEFINED LINE NUMBER %d AT LINE %d" %
+                  (linenum, self.stat[self.pc]))
+            raise RuntimeError
+        self.pc = self.stat.index(linenum)
 
     # Run it
     def run(self):
-        self.vars   = { }            # All variables
-        self.lists  = { }            # List variables
-        self.tables = { }            # Tables
-        self.loops  = [ ]            # Currently active loops
-        self.loopend= { }            # Mapping saying where loops end
-        self.gosub  = None           # Gosub return point (if any)
-        self.error  = 0              # Indicates program error
+        self.vars = {}            # All variables
+        self.lists = {}            # List variables
+        self.tables = {}            # Tables
+        self.loops = []            # Currently active loops
+        self.loopend = {}            # Mapping saying where loops end
+        self.gosub = None           # Gosub return point (if any)
+        self.error = 0              # Indicates program error
 
         self.stat = list(self.prog)  # Ordered list of all line numbers
         self.stat.sort()
@@ -194,248 +222,275 @@ class BasicInterpreter:
         self.check_end()
         self.check_loops()
 
-        if self.error: raise RuntimeError
+        if self.error:
+            raise RuntimeError
 
         while 1:
-            line  = self.stat[self.pc]
+            line = self.stat[self.pc]
             instr = self.prog[line]
-            
+
             op = instr[0]
 
             # END and STOP statements
             if op == 'END' or op == 'STOP':
-                 break           # We're done
+                break           # We're done
 
             # GOTO statement
             elif op == 'GOTO':
-                 newline = instr[1]
-                 self.goto(newline)
-                 continue
+                newline = instr[1]
+                self.goto(newline)
+                continue
 
             # PRINT statement
             elif op == 'PRINT':
-                 plist = instr[1]
-                 out = ""
-                 for label,val in plist:
-                     if out:
-                          out += ' '*(15 - (len(out) % 15))
-                     out += label
-                     if val:
-                          if label: out += " "
-                          eval = self.eval(val)
-                          out += str(eval)
-                 sys.stdout.write(out)
-                 end = instr[2]
-                 if not (end == ',' or end == ';'): 
-                     sys.stdout.write("\n")
-                 if end == ',': sys.stdout.write(" "*(15-(len(out) % 15)))
-                 if end == ';': sys.stdout.write(" "*(3-(len(out) % 3)))
-                     
+                plist = instr[1]
+                out = ""
+                for label, val in plist:
+                    if out:
+                        out += ' ' * (15 - (len(out) % 15))
+                    out += label
+                    if val:
+                        if label:
+                            out += " "
+                        eval = self.eval(val)
+                        out += str(eval)
+                sys.stdout.write(out)
+                end = instr[2]
+                if not (end == ',' or end == ';'):
+                    sys.stdout.write("\n")
+                if end == ',':
+                    sys.stdout.write(" " * (15 - (len(out) % 15)))
+                if end == ';':
+                    sys.stdout.write(" " * (3 - (len(out) % 3)))
+
             # LET statement
             elif op == 'LET':
-                 target = instr[1]
-                 value  = instr[2]
-                 self.assign(target,value)
+                target = instr[1]
+                value = instr[2]
+                self.assign(target, value)
 
             # READ statement
             elif op == 'READ':
-                 for target in instr[1]:
-                      if self.dc < len(self.data):
-                          value = ('NUM',self.data[self.dc])
-                          self.assign(target,value)
-                          self.dc += 1
-                      else:
-                          # No more data.  Program ends
-                          return
+                for target in instr[1]:
+                    if self.dc < len(self.data):
+                        value = ('NUM', self.data[self.dc])
+                        self.assign(target, value)
+                        self.dc += 1
+                    else:
+                        # No more data.  Program ends
+                        return
             elif op == 'IF':
-                 relop = instr[1]
-                 newline = instr[2]
-                 if (self.releval(relop)):
-                     self.goto(newline)
-                     continue
+                relop = instr[1]
+                newline = instr[2]
+                if (self.releval(relop)):
+                    self.goto(newline)
+                    continue
 
             elif op == 'FOR':
-                 loopvar = instr[1]
-                 initval = instr[2]
-                 finval  = instr[3]
-                 stepval = instr[4]
-              
-                 # Check to see if this is a new loop
-                 if not self.loops or self.loops[-1][0] != self.pc:
-                        # Looks like a new loop. Make the initial assignment
-                        newvalue = initval
-                        self.assign((loopvar,None,None),initval)
-                        if not stepval: stepval = ('NUM',1)
-                        stepval = self.eval(stepval)    # Evaluate step here
-                        self.loops.append((self.pc,stepval))
-                 else:
-                        # It's a repeat of the previous loop
-                        # Update the value of the loop variable according to the step
-                        stepval = ('NUM',self.loops[-1][1])
-                        newvalue = ('BINOP','+',('VAR',(loopvar,None,None)),stepval)
-
-                 if self.loops[-1][1] < 0: relop = '>='
-                 else: relop = '<='
-                 if not self.releval(('RELOP',relop,newvalue,finval)):
-                      # Loop is done. Jump to the NEXT
-                      self.pc = self.loopend[self.pc]
-                      self.loops.pop()
-                 else:
-                      self.assign((loopvar,None,None),newvalue)
+                loopvar = instr[1]
+                initval = instr[2]
+                finval = instr[3]
+                stepval = instr[4]
+
+                # Check to see if this is a new loop
+                if not self.loops or self.loops[-1][0] != self.pc:
+                    # Looks like a new loop. Make the initial assignment
+                    newvalue = initval
+                    self.assign((loopvar, None, None), initval)
+                    if not stepval:
+                        stepval = ('NUM', 1)
+                    stepval = self.eval(stepval)    # Evaluate step here
+                    self.loops.append((self.pc, stepval))
+                else:
+                    # It's a repeat of the previous loop
+                    # Update the value of the loop variable according to the
+                    # step
+                    stepval = ('NUM', self.loops[-1][1])
+                    newvalue = (
+                        'BINOP', '+', ('VAR', (loopvar, None, None)), stepval)
+
+                if self.loops[-1][1] < 0:
+                    relop = '>='
+                else:
+                    relop = '<='
+                if not self.releval(('RELOP', relop, newvalue, finval)):
+                    # Loop is done. Jump to the NEXT
+                    self.pc = self.loopend[self.pc]
+                    self.loops.pop()
+                else:
... 7674 lines suppressed ...

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/ply.git



More information about the Python-modules-commits mailing list