-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlispy.diff
137 lines (120 loc) · 5.87 KB
/
lispy.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
--- lispy.py.orig 2014-05-26 01:32:18.034828600 -0400
+++ lispy.py 2014-06-20 08:26:27.982307000 -0400
@@ -4,8 +4,7 @@
################ Symbol, Procedure, classes
-from __future__ import division
-import re, sys, StringIO
+import re, sys, io
class Symbol(str): pass
@@ -14,11 +13,11 @@
if s not in symbol_table: symbol_table[s] = Symbol(s)
return symbol_table[s]
-_quote, _if, _set, _define, _lambda, _begin, _definemacro, = map(Sym,
-"quote if set! define lambda begin define-macro".split())
+_quote, _if, _set, _define, _lambda, _begin, _definemacro, = list(map(Sym,
+"quote if set! define lambda begin define-macro".split()))
-_quasiquote, _unquote, _unquotesplicing = map(Sym,
-"quasiquote unquote unquote-splicing".split())
+_quasiquote, _unquote, _unquotesplicing = list(map(Sym,
+"quasiquote unquote unquote-splicing".split()))
class Procedure(object):
"A user-defined Scheme procedure."
@@ -32,7 +31,7 @@
def parse(inport):
"Parse a program: read and expand/error-check it."
# 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)
eof_object = Symbol('#<eof-object>') # Note: uninterned; can't be read
@@ -82,7 +81,7 @@
'Numbers become numbers; #t and #f are booleans; "..." string; otherwise Symbol.'
if token == '#t': return True
elif token == '#f': return False
- elif token[0] == '"': return token[1:-1].decode('string_escape')
+ elif token[0] == '"': return token[1:-1].encode('utf_8').decode('unicode_escape')
try: return int(token)
except ValueError:
try: return float(token)
@@ -96,7 +95,7 @@
if x is True: return "#t"
elif x is False: return "#f"
elif isa(x, Symbol): return x
- elif isa(x, str): return '"%s"' % x.encode('string_escape').replace('"',r'\"')
+ elif isa(x, str): return '"%s"' % x.encode('unicode_escape').decode('utf_8').replace('"',r'\"')
elif isa(x, list): return '('+' '.join(map(to_string, x))+')'
elif isa(x, complex): return str(x).replace('j', 'i')
else: return str(x)
@@ -114,9 +113,9 @@
x = parse(inport)
if x is eof_object: return
val = eval(x)
- if val is not None and out: print >> out, to_string(val)
+ if out: print(to_string(val), file=out) if val is not None else print()
except Exception as e:
- print '%s: %s' % (type(e).__name__, e)
+ print('%s: %s' % (type(e).__name__, e))
################ Environment class
@@ -131,7 +130,7 @@
if len(args) != len(parms):
raise TypeError('expected %s, given %s, '
% (to_string(parms), to_string(args)))
- self.update(zip(parms,args))
+ self.update(list(zip(parms,args)))
def find(self, var):
"Find the innermost Env where var appears."
if var in self: return self
@@ -157,7 +156,7 @@
self.update(vars(math))
self.update(vars(cmath))
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,
'equal?':op.eq, 'eq?':op.is_, 'length':len, 'cons':cons,
'car':lambda x:x[0], 'cdr':lambda x:x[1:], 'append':op.add,
@@ -169,8 +168,8 @@
'open-input-file':open,'close-input-port':lambda p: p.file.close(),
'open-output-file':lambda f:open(f,'w'), 'close-output-port':lambda p: p.close(),
'eof-object?':lambda x:x is eof_object, 'read-char':readchar,
- 'read':read, 'write':lambda x,port=sys.stdout:port.write(to_string(x)),
- 'display':lambda x,port=sys.stdout:port.write(x if isa(x,str) else to_string(x))})
+ 'read':read, 'write':lambda x,port=sys.stdout:(port.write(to_string(x)), None)[1],
+ 'display':lambda x,port=sys.stdout:(port.write(x if isa(x,str) else to_string(x)), None)[1]})
return self
isa = isinstance
@@ -229,7 +228,7 @@
elif x[0] is _if:
if len(x)==3: x = x + [None] # (if t c) => (if t c None)
require(x, len(x)==4)
- return map(expand, x)
+ return list(map(expand, x))
elif x[0] is _set:
require(x, len(x)==3);
var = x[1] # (set! non-var exp) => Error
@@ -268,13 +267,13 @@
elif isa(x[0], Symbol) and x[0] in macro_table:
return expand(macro_table[x[0]](*x[1:]), toplevel) # (m arg...)
else: # => macroexpand if m isa macro
- return map(expand, x) # (f arg...) => expand each
+ return list(map(expand, x)) # (f arg...) => expand each
def require(x, predicate, msg="wrong length"):
"Signal a syntax error if predicate is false."
if not predicate: raise SyntaxError(to_string(x)+': '+msg)
-_append, _cons, _let = map(Sym, "append cons let".split())
+_append, _cons, _let = list(map(Sym, "append cons let".split()))
def expand_quasiquote(x):
"""Expand `x => 'x; `,x => x; `(,@x y) => (append x y) """
@@ -297,8 +296,8 @@
bindings, body = args[0], args[1:]
require(x, all(isa(b, list) and len(b)==2 and isa(b[0], Symbol)
for b in bindings), "illegal binding list")
- vars, vals = zip(*bindings)
- return [[_lambda, list(vars)]+map(expand, body)] + map(expand, vals)
+ vars, vals = list(zip(*bindings))
+ return [[_lambda, list(vars)]+list(map(expand, body))] + list(map(expand, vals))
macro_table = {_let:let} ## More macros can go here
@@ -315,4 +314,3 @@
if __name__ == '__main__':
repl()
-