From 29d510eb46d1bfc6a19823bd36aaf471fe5af4b6 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Thu, 23 Apr 2020 23:33:23 -0700 Subject: [PATCH] futurize stage2 core --- joy/utils/compiler.py | 5 ++++- joy/utils/infinite_stack.py | 3 ++- joy/utils/pretty_print.py | 1 + joy/utils/stack.py | 3 +-- joy/utils/types.py | 18 +++++++++++------- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/joy/utils/compiler.py b/joy/utils/compiler.py index 45ed27a..e74b40f 100644 --- a/joy/utils/compiler.py +++ b/joy/utils/compiler.py @@ -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): diff --git a/joy/utils/infinite_stack.py b/joy/utils/infinite_stack.py index 10367c8..f373835 100644 --- a/joy/utils/infinite_stack.py +++ b/joy/utils/infinite_stack.py @@ -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: diff --git a/joy/utils/pretty_print.py b/joy/utils/pretty_print.py index 5151ae1..6844c64 100644 --- a/joy/utils/pretty_print.py +++ b/joy/utils/pretty_print.py @@ -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 diff --git a/joy/utils/stack.py b/joy/utils/stack.py index afff8b6..f998e6c 100644 --- a/joy/utils/stack.py +++ b/joy/utils/stack.py @@ -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) ) diff --git a/joy/utils/types.py b/joy/utils/types.py index 8da4c6a..e30be43 100644 --- a/joy/utils/types.py +++ b/joy/utils/types.py @@ -18,6 +18,10 @@ # along with Thun. If not see . # 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)