• R/O
  • SSH

Joypy: Commit

Main interpreter and library.


Commit MetaInfo

Revision3ec39c7d8ad3870e05c96902fe32383a3abe111b (tree)
Time2018-08-02 04:26:50
AuthorSimon Forman <sforman@hush...>
CommiterSimon Forman

Log Message

In progress..

Change Summary

Incremental Difference

diff -r b24f0d4a1bd9 -r 3ec39c7d8ad3 joy/utils/compiler.py
--- a/joy/utils/compiler.py Wed Aug 01 10:59:07 2018 -0700
+++ b/joy/utils/compiler.py Wed Aug 01 12:26:50 2018 -0700
@@ -1,17 +1,12 @@
11 from joy.parser import text_to_expression, Symbol
2-from joy.utils.stack import iter_stack, list_to_stack
3-from joy.library import SimpleFunctionWrapper
2+from joy.utils.stack import concat, iter_stack, list_to_stack
3+from joy.library import SimpleFunctionWrapper, YIN_STACK_EFFECTS
44
55
66 def import_yin():
77 from joy.utils.generated_library import *
88 return locals()
99
10-D = {
11- name: SimpleFunctionWrapper(func)
12- for name, func in import_yin().iteritems()
13- }
14-
1510
1611 def _names():
1712 n = 0
@@ -68,6 +63,7 @@
6863
6964
7065 def code_gen(code):
66+ #for p in code: print p
7167 coalesce_pops(code)
7268 lines = []
7369 emit = lines.append
@@ -75,7 +71,7 @@
7571 tag, rest = t[0], t[1:]
7672 if tag == 'pop': emit(strstk(rest) + ' = stack')
7773 elif tag == 'call': emit('%s = %s%s' % rest)
78- elif tag == 'ret': emit('return ' + strstk(rest))
74+ elif tag == 'ret': emit('return ' + strstk(rest[::-1]))
7975 else:
8076 raise ValueError(tag)
8177 return '\n'.join(' ' + line for line in lines)
@@ -94,14 +90,61 @@
9490 ''' % (name, code_gen(I(text_to_expression(text))))
9591
9692
93+def q():
94+ memo = {}
95+ def bar(type_var):
96+ try:
97+ res = memo[type_var]
98+ except KeyError:
99+ res = memo[type_var] = InfiniteStack.names()
100+ return res
101+ return bar
102+
103+
104+def type_vars_to_labels(thing, map_):
105+ if not thing:
106+ return thing
107+ if not isinstance(thing, tuple):
108+ return map_(thing)
109+ return tuple(type_vars_to_labels(inner, map_) for inner in thing)
110+
111+
112+def remap_inputs(in_, stack, code):
113+ map_ = q()
114+ while in_:
115+ term, in_ = in_
116+ arg0, stack = stack
117+ term = type_vars_to_labels(term, map_)
118+ code.append(('call', term, '', arg0))
119+ return stack, map_
120+
121+
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
127+
128+
129+YIN = import_yin()
130+
131+
132+D = {
133+ name: SimpleFunctionWrapper(func)
134+ for name, func in YIN.iteritems()
135+ }
136+
137+
97138 D['mul'] = Foo('mul')
98139 D['sub'] = Foo('sub')
140+D['first_two'] = first_two
99141
100-##print compile_yinyang('mul_', 'mul')
101-##print compile_yinyang('sqr', 'dup mul')
102-##print compile_yinyang('foo', 'dup 23 sub mul')
103-##print compile_yinyang('bar', 'mul mul mul mul')
104-##print compile_yinyang('baz', 'mul dup sub dup')
105-##print compile_yinyang('to_the_fifth_power', 'dup dup mul dup mul mul')
106-##print compile_yinyang('hey', 'dup dup dup')
107-print compile_yinyang('hey', 'first_two')
142+print compile_yinyang('mul_', 'mul')
143+print compile_yinyang('sqr', 'dup mul')
144+print compile_yinyang('foo', 'dup 23 sub mul')
145+print compile_yinyang('bar', 'mul mul mul mul')
146+print compile_yinyang('baz', 'mul dup sub dup')
147+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+
Show on old repository browser