futurize stage2 core

This commit is contained in:
Simon Forman 2020-04-23 23:33:23 -07:00
parent e758d7d2be
commit 29d510eb46
5 changed files with 19 additions and 11 deletions

View File

@ -11,6 +11,9 @@ functions, during inference? Could I write out better code that way?
In any event, I am proceeding with this sort of ad hoc way for now.
'''
from __future__ import print_function
from builtins import next
from builtins import str
from builtins import object
from joy.parser import text_to_expression, Symbol
from joy.utils.stack import concat, iter_stack, list_to_stack
from joy.library import SimpleFunctionWrapper, YIN_STACK_EFFECTS
@ -33,7 +36,7 @@ class InfiniteStack(tuple):
_NAMES = _names()
next(_NAMES)
names = _NAMES.next
names = _NAMES.__next__
reset = lambda _, _n=_NAMES: _n.send(-1)
def __init__(self, code):

View File

@ -1,3 +1,4 @@
from builtins import str
from joy.parser import Symbol
@ -10,7 +11,7 @@ def _names():
class InfiniteStack(tuple):
names = _names().next
names = _names().__next__
def __iter__(self):
if not self:

View File

@ -40,6 +40,7 @@ left.
# (Kinda clunky and hacky. This should be swapped out in favor of much
# smarter stuff.)
from __future__ import print_function
from builtins import object
from traceback import print_exc
from .stack import expression_to_string, stack_to_string

View File

@ -72,6 +72,7 @@ printed left-to-right. These functions are written to support :doc:`../pretty`.
'''
from builtins import map
def list_to_stack(el, stack=()):
'''Convert a Python list (or other sequence) to a Joy stack::
@ -129,7 +130,6 @@ def expression_to_string(expression):
def _to_string(stack, f):
if isinstance(stack, long): return str(stack).rstrip('L')
if not isinstance(stack, tuple): return repr(stack)
if not stack: return '' # shortcut
return ' '.join(map(_s, f(stack)))
@ -137,7 +137,6 @@ def _to_string(stack, f):
_s = lambda s: (
'[%s]' % expression_to_string(s) if isinstance(s, tuple)
else str(s).rstrip('L') if isinstance(s, long)
else repr(s)
)

View File

@ -18,6 +18,10 @@
# along with Thun. If not see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function
from builtins import str
from builtins import map
from past.builtins import basestring
from builtins import object
from logging import getLogger, addLevelName
from functools import reduce
@ -25,7 +29,7 @@ _log = getLogger(__name__)
addLevelName(15, 'hmm')
from collections import Counter
from itertools import imap, chain, product
from itertools import chain, product
from inspect import stack as inspect_stack
from joy.utils.stack import (
concat,
@ -41,7 +45,7 @@ class AnyJoyType(object):
Joy type variable. Represents any Joy value.
'''
accept = tuple, int, float, long, complex, str, unicode, bool, Symbol
accept = tuple, int, float, int, complex, str, bool, Symbol
prefix = 'a'
def __init__(self, number):
@ -81,7 +85,7 @@ class BooleanJoyType(AnyJoyType):
class NumberJoyType(AnyJoyType):
accept = bool, int, float, long, complex
accept = bool, int, float, complex
prefix = 'n'
@ -105,7 +109,7 @@ class StackJoyType(AnyJoyType):
accept = tuple
prefix = 's'
def __nonzero__(self):
def __bool__(self):
# Imitate () at the end of cons list.
return False
@ -453,7 +457,7 @@ def compilable(f):
Return True if a stack effect represents a function that can be
automatically compiled (to Python), False otherwise.
'''
return isinstance(f, tuple) and all(imap(compilable, f)) or _stacky(f)
return isinstance(f, tuple) and all(map(compilable, f)) or _stacky(f)
def doc_from_stack_effect(inputs, outputs=('??', ())):
@ -721,7 +725,7 @@ def combinator_effect(number, *expect):
def show(DEFS):
for name, stack_effect_comment in sorted(DEFS.iteritems()):
for name, stack_effect_comment in sorted(DEFS.items()):
t = ' *'[compilable(stack_effect_comment)]
print(name, '=', doc_from_stack_effect(*stack_effect_comment), t)
@ -731,7 +735,7 @@ def generate_library_code(DEFS, f=None):
import sys
f = sys.stdout
print('# GENERATED FILE. DO NOT EDIT.\n', file=f)
for name, stack_effect_comment in sorted(DEFS.iteritems()):
for name, stack_effect_comment in sorted(DEFS.items()):
if not compilable(stack_effect_comment):
continue
print(file=f)