• R/O
  • SSH

Joypy: Commit

Main interpreter and library.


Commit MetaInfo

Revisione553e795709aeb3e68ec579f4614e93a65393660 (tree)
Time2018-08-03 06:22:42
AuthorSimon Forman <sforman@hush...>
CommiterSimon Forman

Log Message

Add binary functions.

Change Summary

Incremental Difference

diff -r 3ec39c7d8ad3 -r e553e795709a joy/library.py
--- a/joy/library.py Wed Aug 01 12:26:50 2018 -0700
+++ b/joy/library.py Thu Aug 02 14:22:42 2018 -0700
@@ -426,6 +426,16 @@
426426
427427
428428 @inscribe
429+@SimpleFunctionWrapper
430+def infer_(stack):
431+ '''Attempt to infer the stack effect of a Joy expression.'''
432+ E, stack = stack
433+ effects = infer_expression(E)
434+ e = list_to_stack([(fi, (fo, ())) for fi, fo in effects])
435+ return e, stack
436+
437+
438+@inscribe
429439 @sec2
430440 @SimpleFunctionWrapper
431441 def getitem(stack):
diff -r 3ec39c7d8ad3 -r e553e795709a joy/utils/compiler.py
--- a/joy/utils/compiler.py Wed Aug 01 12:26:50 2018 -0700
+++ b/joy/utils/compiler.py Thu Aug 02 14:22:42 2018 -0700
@@ -1,3 +1,15 @@
1+'''
2+A crude compiler for a subset of Joy functions.
3+
4+I think I'm going about this the wrong way.
5+
6+The inference algorithm can "collapse" Yin function sequences into
7+single stack effects which can then be written out as Python functions.
8+Why not keep track of the new variables introduced as results of Yang
9+functions, during inference? Could I write out better code that way?
10+
11+In any event, I am proceeding with this sort of ad hoc way for now.
12+'''
113 from joy.parser import text_to_expression, Symbol
214 from joy.utils.stack import concat, iter_stack, list_to_stack
315 from joy.library import SimpleFunctionWrapper, YIN_STACK_EFFECTS
@@ -8,18 +20,22 @@
820 return locals()
921
1022
11-def _names():
12- n = 0
13- while True:
14- yield Symbol('a' + str(n))
15- n += 1
16-
17-
1823 class InfiniteStack(tuple):
1924
20- names = _names().next
25+ def _names():
26+ n = 0
27+ while True:
28+ m = yield Symbol('a' + str(n))
29+ n = n + 1 if m is None else m
30+
31+ _NAMES = _names()
32+ _NAMES.next()
33+
34+ names = _NAMES.next
35+ reset = lambda _, _n=_NAMES: _n.send(-1)
2136
2237 def __init__(self, code):
38+ self.reset()
2339 self.code = code
2440
2541 def __iter__(self):
@@ -29,18 +45,6 @@
2945 return iter((new_var, self))
3046
3147
32-class Foo(object):
33-
34- def __init__(self, name):
35- self.name = name
36-
37- def __call__(self, stack, expression, code):
38- in1, (in0, stack) = stack
39- out = InfiniteStack.names()
40- code.append(('call', out, self.name, (in0, in1)))
41- return (out, stack), expression, code
42-
43-
4448 def I(expression):
4549 code = []
4650 stack = InfiniteStack(code)
@@ -53,8 +57,7 @@
5357 else:
5458 stack = term, stack
5559
56- s = list(iter_stack(stack))
57- if s: code.append(tuple(['ret'] + s))
60+ code.append(tuple(['ret'] + list(iter_stack(stack))))
5861 return code
5962
6063
@@ -79,7 +82,8 @@
7982
8083 def coalesce_pops(code):
8184 code.sort(key=lambda p: p[0] != 'pop') # All pops to the front.
82- index = (i for i, t in enumerate(code) if t[0] != 'pop').next()
85+ try: index = (i for i, t in enumerate(code) if t[0] != 'pop').next()
86+ except StopIteration: return
8387 code[:index] = [tuple(['pop'] + [t for _, t in code[:index][::-1]])]
8488
8589
@@ -119,32 +123,114 @@
119123 return stack, map_
120124
121125
122-def first_two(stack, expression, code):
123- in_, out = YIN_STACK_EFFECTS['first_two']
124- stack, map_ = remap_inputs(in_, stack, code)
125- out = type_vars_to_labels(out, map_)
126- return concat(out, stack), expression, code
126+class BinaryBuiltin(object):
127+
128+ def __init__(self, name):
129+ self.name = name
130+
131+ def __call__(self, stack, expression, code):
132+ in1, (in0, stack) = stack
133+ out = InfiniteStack.names()
134+ code.append(('call', out, self.name, (in0, in1)))
135+ return (out, stack), expression, code
127136
128137
129138 YIN = import_yin()
130139
131140
132141 D = {
133- name: SimpleFunctionWrapper(func)
134- for name, func in YIN.iteritems()
142+ name: SimpleFunctionWrapper(YIN[name])
143+ for name in '''
144+ ccons
145+ cons
146+ dup
147+ dupd
148+ dupdd
149+ over
150+ pop
151+ popd
152+ popdd
153+ popop
154+ popopd
155+ popopdd
156+ rolldown
157+ rollup
158+ swap
159+ swons
160+ tuck
161+ unit
162+ '''.split()
135163 }
136164
137165
138-D['mul'] = Foo('mul')
139-D['sub'] = Foo('sub')
140-D['first_two'] = first_two
166+for name in '''
167+ first
168+ first_two
169+ fourth
170+ rest
171+ rrest
172+ second
173+ third
174+ uncons
175+ unswons
176+ '''.split():
177+
178+ def foo(stack, expression, code, name=name):
179+ in_, out = YIN_STACK_EFFECTS[name]
180+ stack, map_ = remap_inputs(in_, stack, code)
181+ out = type_vars_to_labels(out, map_)
182+ return concat(out, stack), expression, code
183+
184+ foo.__name__ = name
185+ D[name] = foo
186+
187+
188+for name in '''
189+ eq
190+ ge
191+ gt
192+ le
193+ lt
194+ ne
195+ xor
196+ lshift
197+ rshift
198+ and_
199+ or_
200+ add
201+ floordiv
202+ mod
203+ mul
204+ pow
205+ sub
206+ truediv
207+ '''.split():
208+ D[name.rstrip('-')] = BinaryBuiltin(name)
209+
210+
211+'''
212+ stack
213+ stuncons
214+ stununcons
215+ swaack
216+'''
217+
218+for name in sorted(D):
219+ print name,
220+## print compile_yinyang(name, name)
221+print '-' * 100
222+
141223
142224 print compile_yinyang('mul_', 'mul')
225+print compile_yinyang('pop', 'pop')
226+print compile_yinyang('ppm', 'popop mul')
143227 print compile_yinyang('sqr', 'dup mul')
144228 print compile_yinyang('foo', 'dup 23 sub mul')
145-print compile_yinyang('bar', 'mul mul mul mul')
229+print compile_yinyang('four_mul', 'mul mul mul mul')
146230 print compile_yinyang('baz', 'mul dup sub dup')
147231 print compile_yinyang('to_the_fifth_power', 'dup dup mul dup mul mul')
148-print compile_yinyang('hey', 'dup dup dup')
149-print compile_yinyang('hey', 'dup first_two mul')
150-
232+print compile_yinyang('dup3', 'dup dup dup')
233+print compile_yinyang('df2m', 'dup first_two mul')
234+print compile_yinyang('sqr_first', 'uncons swap dup mul swons')
235+print compile_yinyang('0BAD', 'uncons dup mul')
236+print compile_yinyang('uncons', 'uncons')
Show on old repository browser