Skip to content

Commit c78ac40

Browse files
committed
started implementing transducers, fixed closure in closure bug
1 parent db4d4d3 commit c78ac40

7 files changed

Lines changed: 62 additions & 12 deletions

File tree

pixie/vm/code.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def get_fn(self, arity):
8080
f = self._arities.get(arity, None)
8181
if f is not None:
8282
return promote(f)
83-
if self.rest_fn is not None and arity >= self._rest_fn:
84-
return promote(self._rest_fn)
83+
#if self.rest_fn is not None and arity >= self._rest_fn:
84+
# return promote(self._rest_fn)
8585
raise AssertionError("Wrong number of args to fn")
8686

8787
def _invoke(self, args):

pixie/vm/compiler.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
rt.init()
1616

1717
class Context(object):
18-
def __init__(self, name, argc, parent_locals):
19-
locals = parent_locals.copy()
20-
for x in locals:
21-
locals[x] = Closure(locals[x])
18+
def __init__(self, name, argc, parent_ctx):
19+
if parent_ctx is not None:
20+
assert isinstance(parent_ctx, Context)
21+
locals = parent_ctx.locals[-1].copy()
22+
for x in locals:
23+
locals[x] = Closure(locals[x], parent_ctx)
24+
else:
25+
locals = {}
2226

2327
self.bytecode = []
2428
self.consts = []
@@ -75,7 +79,10 @@ def get_local(self, s_name):
7579
if x is local:
7680
break
7781
idx += 1
78-
self.closed_overs.append(local.local)
82+
if isinstance(local.local, Closure):
83+
self.closed_overs.append(local.ctx.get_local(s_name))
84+
else:
85+
self.closed_overs.append(local.local)
7986

8087
return ClosureCell(idx)
8188
return local
@@ -170,8 +177,9 @@ def emit(self, ctx):
170177
ctx.add_sp(1)
171178

172179
class Closure(LocalType):
173-
def __init__(self, local):
180+
def __init__(self, local, ctx):
174181
self.local = local
182+
self.ctx = ctx
175183

176184
class ClosureCell(LocalType):
177185
def __init__(self, idx):
@@ -300,7 +308,7 @@ def compile_fn(form, ctx):
300308

301309
def compile_fn_body(name, args, body, ctx):
302310
print args
303-
new_ctx = Context(name._str, rt.count(args).int_val(), ctx.locals[-1])
311+
new_ctx = Context(name._str, rt.count(args).int_val(), ctx)
304312
add_args(args, new_ctx)
305313
bc = 0
306314

@@ -520,7 +528,7 @@ def compile_cons(form, ctx):
520528

521529

522530
def compile(form):
523-
ctx = Context(u"main", 0, {})
531+
ctx = Context(u"main", 0, None)
524532
compile_form(form, ctx)
525533
ctx.bytecode.append(code.RETURN)
526534
return ctx.to_code()

pixie/vm/persistent_vector.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ def _nth(self, idx):
121121
def _conj(self, v):
122122
return self.conj(v)
123123

124+
@extend(proto._reduce, PersistentVector._type)
125+
def _reduce(self, f, init):
126+
assert isinstance(self, PersistentVector)
127+
i = 0
128+
while i < self._cnt:
129+
array = self.array_for(i)
130+
for j in range(len(array)):
131+
init = f.invoke([init, array[j]])
132+
step = len(array)
133+
i += step
134+
return init
135+
136+
124137
@as_var("vector")
125138
def vector__args(args):
126139
acc = EMPTY

pixie/vm/protocols.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
defprotocol("pixie.stdlib", "IObject", ["-hash", "-eq", "-str", "-repr"])
2121

22+
defprotocol("pixie.stdlib", "IReduce", ["-reduce"])
23+
2224
def default_str(x):
2325
from pixie.vm.string import String
2426

@@ -111,3 +113,18 @@ def _instance(o, proto):
111113
import pixie.vm.rt as rt
112114

113115

116+
@as_var("load_file")
117+
def load_file(filename):
118+
import pixie.vm.reader as reader
119+
import pixie.vm.compiler as compiler
120+
f = open(filename._str)
121+
data = f.read()
122+
f.close()
123+
rdr = reader.StringReader(unicode(data))
124+
result = nil
125+
while True:
126+
form = reader.read(rdr, False)
127+
if form is reader.eof:
128+
return result
129+
130+
result = compiler.compile(form).invoke([])

pixie/vm/reader.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,18 @@ def read(rdr, error_on_eof):
197197
if macro is not None:
198198
return macro.invoke(rdr, ch)
199199

200-
if is_digit(ch) or ch == u"-":
200+
if is_digit(ch):
201201
return read_number(rdr, ch)
202202

203+
if ch == u"-":
204+
ch2 = rdr.read()
205+
if is_digit(ch2):
206+
rdr.unread(ch2)
207+
return read_number(rdr, ch)
208+
else:
209+
rdr.unread(ch2)
210+
return read_symbol(rdr, ch)
211+
203212
return read_symbol(rdr, ch)
204213

205214

pixie/vm/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ def fmix(h1, length):
5656
h1 ^= h1 >> 13
5757
h1 *= 0xc2b2ae35
5858
h1 ^= h1 >> 16
59-
return h1
59+
return h1
60+
61+

target.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def entry_point(foo=None):
4545
from pixie.vm.keyword import keyword
4646
import pixie.vm.rt as rt
4747
from pixie.vm.string import String
48+
rt.load_file(String(u"pixie/stdlib.lisp"))
4849

4950
rdr = PromptReader()
5051
while True:

0 commit comments

Comments
 (0)