• R/O
  • SSH

Joypy: Commit

Main interpreter and library.


Commit MetaInfo

Revisionf60f46a50473ba9b926ea23797208207cf405575 (tree)
Time2018-07-18 04:43:24
AuthorSimon Forman <sforman@hush...>
CommiterSimon Forman

Log Message

So fra, so good...

Change Summary

Incremental Difference

diff -r 178c94b1fc59 -r f60f46a50473 joy/library.py
--- a/joy/library.py Tue Jul 17 10:49:09 2018 -0700
+++ b/joy/library.py Tue Jul 17 12:43:24 2018 -0700
@@ -34,6 +34,8 @@
3434
3535 from .utils import generated_library as genlib
3636 from .utils.types import (
37+ compose,
38+ ef,
3739 stack_effect,
3840 AnyJoyType,
3941 BooleanJoyType,
@@ -42,6 +44,7 @@
4244 FloatJoyType,
4345 IntJoyType,
4446 TextJoyType,
47+ _functions,
4548 )
4649
4750
@@ -55,6 +58,17 @@
5558 T = t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 = map(TextJoyType, _R)
5659
5760
61+sec0 = stack_effect(t1)()
62+sec1 = stack_effect(s0, i1)(s1)
63+sec2 = stack_effect(s0, i1)(a1)
64+sec_binary_cmp = stack_effect(n1, n2)(b1)
65+sec_binary_ints = stack_effect(i1, i2)(i3)
66+sec_binary_logic = stack_effect(b1, b2)(b3)
67+sec_binary_math = stack_effect(n1, n2)(n3)
68+sec_unary_logic = stack_effect(a1)(b1)
69+sec_unary_math = stack_effect(n1)(n2)
70+
71+
5872 _dictionary = {}
5973
6074
@@ -74,6 +88,8 @@
7488 ('and', ['&']),
7589 ('bool', ['truthy']),
7690 ('mul', ['*']),
91+ ('floordiv', ['/floor', '//']),
92+ ('floor', ['round']),
7793 ('truediv', ['/']),
7894 ('mod', ['%', 'rem', 'remainder', 'modulus']),
7995 ('eq', ['=']),
@@ -111,6 +127,58 @@
111127 D[alias] = F
112128
113129
130+def yin_functions():
131+ '''
132+ Return a dict of named stack effects.
133+
134+ "Yin" functions are those that only rearrange items in stacks and
135+ can be defined completely by their stack effects. This means they
136+ can be auto-compiled.
137+ '''
138+ cons = ef(a1, s0)((a1, s0))
139+ ccons = compose(cons, cons)
140+ dup = ef(a1)(a1, a1)
141+ dupd = ef(a2, a1)(a2, a2, a1)
142+ dupdd = ef(a3, a2, a1)(a3, a3, a2, a1)
143+ first = ef((a1, s1),)(a1,)
144+ over = ef(a2, a1)(a2, a1, a2)
145+ pop = ef(a1)()
146+ popd = ef(a2, a1,)(a1)
147+ popdd = ef(a3, a2, a1,)(a2, a1,)
148+ popop = ef(a2, a1,)()
149+ popopd = ef(a3, a2, a1,)(a1)
150+ popopdd = ef(a4, a3, a2, a1,)(a2, a1)
151+ rest = ef((a1, s0),)(s0,)
152+ rolldown = ef(a1, a2, a3)(a2, a3, a1)
153+ rollup = ef(a1, a2, a3)(a3, a1, a2)
154+ rrest = compose(rest, rest)
155+ second = compose(rest, first)
156+ stack = s0, (s0, s0)
157+ swaack = (s1, s0), (s0, s1)
158+ swap = ef(a1, a2)(a2, a1)
159+ swons = compose(swap, cons)
160+ third = compose(rest, second)
161+ tuck = ef(a2, a1)(a1, a2, a1)
162+ uncons = ef((a1, s0),)(a1, s0)
163+ unswons = compose(uncons, swap)
164+ stuncons = compose(stack, uncons)
165+ stununcons = compose(stack, uncons, uncons)
166+ unit = ef(a1)((a1, ()))
167+
168+ first_two = compose(uncons, uncons, pop)
169+ fourth = compose(rest, third)
170+
171+ _Tree_add_Ee = compose(pop, swap, rolldown, rrest, ccons)
172+ _Tree_get_E = compose(popop, second)
173+ _Tree_delete_clear_stuff = compose(rollup, popop, rest)
174+ _Tree_delete_R0 = compose(over, first, swap, dup)
175+
176+ return {
177+ name.rstrip('_'): stack_effect
178+ for name, stack_effect in locals().iteritems()
179+ }
180+
181+
114182 definitions = ('''\
115183 of == swap at
116184 product == 1 swap [*] step
@@ -289,32 +357,40 @@
289357 return (line.strip() for line in text.splitlines() if '==' in line)
290358
291359
360+
361+## eh = compose(dup, bool_)
362+## sqr = compose(dup, mul)
363+## of = compose(swap, at)
364+
365+
292366 #
293367 # Functions
294368 #
295369
296370
297371 # Load the auto-generated primitives into the dictionary.
372+_functions.update(yin_functions())
298373 for name, primitive in getmembers(genlib, isfunction):
299374 inscribe(SimpleFunctionWrapper(primitive))
300375
376+
301377 @inscribe
378+@sec0
302379 @FunctionWrapper
303-@stack_effect(t1)()
304380 def inscribe_(stack, expression, dictionary):
305- '''
306- Create a new Joy function definition in the Joy dictionary. A
307- definition is given as a string with a name followed by a double
308- equal sign then one or more Joy functions, the body. for example:
381+ '''
382+ Create a new Joy function definition in the Joy dictionary. A
383+ definition is given as a string with a name followed by a double
384+ equal sign then one or more Joy functions, the body. for example:
309385
310- sqr == dup mul
386+ sqr == dup mul
311387
312- If you want the definition to persist over restarts, enter it into
313- the definitions.txt resource.
314- '''
315- definition, stack = stack
316- DefinitionWrapper.add_def(definition, dictionary)
317- return stack, expression, dictionary
388+ If you want the definition to persist over restarts, enter it into
389+ the definitions.txt resource.
390+ '''
391+ definition, stack = stack
392+ DefinitionWrapper.add_def(definition, dictionary)
393+ return stack, expression, dictionary
318394
319395
320396 @inscribe
@@ -327,6 +403,7 @@
327403
328404
329405 @inscribe
406+@sec2
330407 @SimpleFunctionWrapper
331408 def getitem(stack):
332409 '''
@@ -348,6 +425,7 @@
348425
349426
350427 @inscribe
428+@sec1
351429 @SimpleFunctionWrapper
352430 def drop(stack):
353431 '''
@@ -375,6 +453,7 @@
375453
376454
377455 @inscribe
456+@sec1
378457 @SimpleFunctionWrapper
379458 def take(stack):
380459 '''
@@ -512,6 +591,7 @@
512591 return list_to_stack(sorted(iter_stack(tos))), stack
513592
514593
594+_functions['clear'] = s0, s1
515595 @inscribe
516596 @SimpleFunctionWrapper
517597 def clear(stack):
@@ -1319,32 +1399,39 @@
13191399
13201400
13211401 for F in (
1322- BinaryBuiltinWrapper(operator.add),
1323- BinaryBuiltinWrapper(operator.and_),
1324- BinaryBuiltinWrapper(operator.div),
1325- BinaryBuiltinWrapper(operator.eq),
1326- BinaryBuiltinWrapper(operator.floordiv),
1327- BinaryBuiltinWrapper(operator.ge),
1328- BinaryBuiltinWrapper(operator.gt),
1329- BinaryBuiltinWrapper(operator.le),
1330- BinaryBuiltinWrapper(operator.lshift),
1331- BinaryBuiltinWrapper(operator.lt),
1332- BinaryBuiltinWrapper(operator.mod),
1333- BinaryBuiltinWrapper(operator.mul),
1334- BinaryBuiltinWrapper(operator.ne),
1335- BinaryBuiltinWrapper(operator.or_),
1336- BinaryBuiltinWrapper(operator.pow),
1337- BinaryBuiltinWrapper(operator.rshift),
1338- BinaryBuiltinWrapper(operator.sub),
1339- BinaryBuiltinWrapper(operator.truediv),
1340- BinaryBuiltinWrapper(operator.xor),
13411402
1342- UnaryBuiltinWrapper(abs),
1343- UnaryBuiltinWrapper(bool),
1344- UnaryBuiltinWrapper(floor),
1345- UnaryBuiltinWrapper(operator.neg),
1346- UnaryBuiltinWrapper(operator.not_),
1347- UnaryBuiltinWrapper(sqrt),
1403+ #divmod_ = pm = __(n2, n1), __(n4, n3)
1404+
1405+ sec_binary_cmp(BinaryBuiltinWrapper(operator.eq)),
1406+ sec_binary_cmp(BinaryBuiltinWrapper(operator.ge)),
1407+ sec_binary_cmp(BinaryBuiltinWrapper(operator.gt)),
1408+ sec_binary_cmp(BinaryBuiltinWrapper(operator.le)),
1409+ sec_binary_cmp(BinaryBuiltinWrapper(operator.lt)),
1410+ sec_binary_cmp(BinaryBuiltinWrapper(operator.ne)),
1411+
1412+ sec_binary_ints(BinaryBuiltinWrapper(operator.xor)),
1413+ sec_binary_ints(BinaryBuiltinWrapper(operator.lshift)),
1414+ sec_binary_ints(BinaryBuiltinWrapper(operator.rshift)),
1415+
1416+ sec_binary_logic(BinaryBuiltinWrapper(operator.and_)),
1417+ sec_binary_logic(BinaryBuiltinWrapper(operator.or_)),
1418+
1419+ sec_binary_math(BinaryBuiltinWrapper(operator.add)),
1420+ sec_binary_math(BinaryBuiltinWrapper(operator.floordiv)),
1421+ sec_binary_math(BinaryBuiltinWrapper(operator.mod)),
1422+ sec_binary_math(BinaryBuiltinWrapper(operator.mul)),
1423+ sec_binary_math(BinaryBuiltinWrapper(operator.pow)),
1424+ sec_binary_math(BinaryBuiltinWrapper(operator.sub)),
1425+ sec_binary_math(BinaryBuiltinWrapper(operator.truediv)),
1426+
1427+ sec_unary_logic(UnaryBuiltinWrapper(bool)),
1428+ sec_unary_logic(UnaryBuiltinWrapper(operator.not_)),
1429+
1430+ sec_unary_math(UnaryBuiltinWrapper(abs)),
1431+ sec_unary_math(UnaryBuiltinWrapper(operator.neg)),
1432+ sec_unary_math(UnaryBuiltinWrapper(sqrt)),
1433+
1434+ stack_effect(n1)(i1)(UnaryBuiltinWrapper(floor)),
13481435 ):
13491436 inscribe(F)
13501437 del F # Otherwise Sphinx autodoc will pick it up.
diff -r 178c94b1fc59 -r f60f46a50473 joy/utils/types.py
--- a/joy/utils/types.py Tue Jul 17 10:49:09 2018 -0700
+++ b/joy/utils/types.py Tue Jul 17 12:43:24 2018 -0700
@@ -275,108 +275,41 @@
275275 return %s''' % (name, doc, i, o)
276276
277277
278+_functions = {}
279+
280+
278281 def __(*seq):
279282 stack = StackJoyType(23)
280283 for item in seq: stack = item, stack
281284 return stack
282285
283286
284-
285287 def stack_effect(*inputs):
286-
287288 def _stack_effect(*outputs):
288- pass
289-
290-return _stack_effect
291-
292-
293-
294-_R = range(10)
295-A = a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 = map(AnyJoyType, _R)
296-B = b0, b1, b2, b3, b4, b5, b6, b7, b8, b9 = map(BooleanJoyType, _R)
297-N = n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = map(NumberJoyType, _R)
298-S = s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 = map(StackJoyType, _R)
299-F = f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = map(FloatJoyType, _R)
300-I = i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 = map(IntJoyType, _R)
301-T = t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 = map(TextJoyType, _R)
289+ def _apply_to(function):
290+ i, o = _functions[function.name] = __(*inputs), __(*outputs)
291+ function.__doc__ += (
292+ '\nStack effect::\n\n ' # '::' for Sphinx docs.
293+ + doc_from_stack_effect(i, o)
294+ )
295+ return function
296+ return _apply_to
297+ return _stack_effect
302298
303299
304-def defs():
305- '''
306- Return a dict of named stack effects.
307- '''
308- at = __(s0, i1), __(a1)
309- drop = take = __(s0, i1), __(s1)
310- cons = __(a1, s0), __((a1, s0),)
311- ccons = compose(cons, cons)
312- dup = __(a1,), __(a1, a1)
313- dupd = __(a2, a1), __(a2, a2, a1)
314- dupdd = __(a3, a2, a1), __(a3, a3, a2, a1)
315- first = __((a1, s1),), __(a1,)
316- inscribe = __(t1), __()
317- over = __(a2, a1), __(a2, a1, a2)
318- pop = __(a1), __()
319- popd = __(a2, a1,), __(a1)
320- popdd = __(a3, a2, a1,), __(a2, a1,)
321- popop = __(a2, a1,), __()
322- popopd = __(a3, a2, a1,), __(a1)
323- popopdd = __(a4, a3, a2, a1,), __(a2, a1)
324- rest = __((a1, s0),), __(s0,)
325- rolldown = __(a1, a2, a3), __(a2, a3, a1)
326- rollup = __(a1, a2, a3), __(a3, a1, a2)
327- rrest = compose(rest, rest)
328- second = compose(rest, first)
329- stack = s0, (s0, s0)
330- swaack = (s1, s0), (s0, s1)
331- swap = __(a1, a2), __(a2, a1)
332- swons = compose(swap, cons)
333- third = compose(rest, second)
334- tuck = __(a2, a1), __(a1, a2, a1)
335- uncons = __((a1, s0),), __(a1, s0)
336- unswons = compose(uncons, swap)
337- stuncons = compose(stack, uncons)
338- stununcons = compose(stack, uncons, uncons)
339- unit = __(a1), __((a1, ()))
340- of = compose(swap, at)
341- clear = s0, s1
342-
343- eq = ge = gt = le = lt = ne = __(n1, n2), __(b1)
344-
345- and_ = __(b1, b2), __(b3)
346- bool_ = not_ = __(a1), __(b1)
347- eh = compose(dup, bool_)
348-
349- add = div = floordiv = mod = mul = pow_ = sub = truediv = \
350- lshift = rshift = __(n1, n2), __(n3,)
351- sqr = compose(dup, mul)
352- abs_ = floor = sqrt = succ = pred = neg = __(n1,), __(n2,)
353- divmod_ = pm = __(n2, n1), __(n4, n3)
354-
355- first_two = compose(uncons, uncons, pop)
356- fourth = compose(rest, third)
357- of = compose(swap, at)
358-
359- _Tree_add_Ee = compose(pop, swap, rolldown, rrest, ccons)
360- _Tree_get_E = compose(popop, second)
361- _Tree_delete_clear_stuff = compose(rollup, popop, rest)
362- _Tree_delete_R0 = compose(over, first, swap, dup)
363-
364- return {
365- name.rstrip('_'): stack_effect
366- for name, stack_effect in locals().iteritems()
367- }
300+def ef(*inputs):
301+ def _ef(*outputs):
302+ return __(*inputs), __(*outputs)
303+ return _ef
368304
369305
370-DEFS = defs()
371-
372-
373-def show():
306+def show(DEFS):
374307 for name, stack_effect_comment in sorted(DEFS.iteritems()):
375308 t = ' *'[compilable(stack_effect_comment)]
376309 print name, '=', doc_from_stack_effect(*stack_effect_comment), t
377310
378311
379-def generate_library_code(f=None):
312+def generate_library_code(DEFS, f=None):
380313 if f is None:
381314 import sys
382315 f = sys.stdout
@@ -389,5 +322,5 @@
389322 print >> f
390323
391324
392-if __name__ == '__main__':
393- show()
325+##if __name__ == '__main__':
326+## show()
Show on old repository browser