lispy.py now runs in Python 3; dropped Python 2 support

This commit is contained in:
Luciano Ramalho 2021-06-08 14:01:19 -03:00
parent 60168bce8c
commit a7295ffc18

View File

@ -4,9 +4,7 @@
################ Symbol, Procedure, classes ################ Symbol, Procedure, classes
from __future__ import division import re, sys, io
from __future__ import print_function
import re, sys, StringIO
class Symbol(str): pass class Symbol(str): pass
@ -33,7 +31,7 @@ class Procedure(object):
def parse(inport): def parse(inport):
"Parse a program: read and expand/error-check it." "Parse a program: read and expand/error-check it."
# Backwards compatibility: given a str, convert it to an InPort # Backwards compatibility: given a str, convert it to an InPort
if isinstance(inport, str): inport = InPort(StringIO.StringIO(inport)) if isinstance(inport, str): inport = InPort(io.StringIO(inport))
return expand(read(inport), toplevel=True) return expand(read(inport), toplevel=True)
eof_object = Symbol('#<eof-object>') # Note: uninterned; can't be read eof_object = Symbol('#<eof-object>') # Note: uninterned; can't be read
@ -83,7 +81,7 @@ def atom(token):
'Numbers become numbers; #t and #f are booleans; "..." string; otherwise Symbol.' 'Numbers become numbers; #t and #f are booleans; "..." string; otherwise Symbol.'
if token == '#t': return True if token == '#t': return True
elif token == '#f': return False elif token == '#f': return False
elif token[0] == '"': return token[1:-1].decode('string_escape') elif token[0] == '"': return token[1:-1]
try: return int(token) try: return int(token)
except ValueError: except ValueError:
try: return float(token) try: return float(token)
@ -97,7 +95,7 @@ def to_string(x):
if x is True: return "#t" if x is True: return "#t"
elif x is False: return "#f" elif x is False: return "#f"
elif isa(x, Symbol): return x elif isa(x, Symbol): return x
elif isa(x, str): return '"%s"' % x.encode('string_escape').replace('"',r'\"') elif isa(x, str): return repr(x)
elif isa(x, list): return '('+' '.join(map(to_string, x))+')' elif isa(x, list): return '('+' '.join(map(to_string, x))+')'
elif isa(x, complex): return str(x).replace('j', 'i') elif isa(x, complex): return str(x).replace('j', 'i')
else: return str(x) else: return str(x)
@ -158,7 +156,7 @@ def add_globals(self):
self.update(vars(math)) self.update(vars(math))
self.update(vars(cmath)) self.update(vars(cmath))
self.update({ self.update({
'+':op.add, '-':op.sub, '*':op.mul, '/':op.div, 'not':op.not_, '+':op.add, '-':op.sub, '*':op.mul, '/':op.truediv, 'not':op.not_,
'>':op.gt, '<':op.lt, '>=':op.ge, '<=':op.le, '=':op.eq, '>':op.gt, '<':op.lt, '>=':op.ge, '<=':op.le, '=':op.eq,
'equal?':op.eq, 'eq?':op.is_, 'length':len, 'cons':cons, 'equal?':op.eq, 'eq?':op.is_, 'length':len, 'cons':cons,
'car':lambda x:x[0], 'cdr':lambda x:x[1:], 'append':op.add, 'car':lambda x:x[0], 'cdr':lambda x:x[1:], 'append':op.add,