From b0df80f3e67127faabed60b36f82f31788562f18 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Thu, 23 Apr 2020 23:16:45 -0700 Subject: [PATCH] futurize stage1 core --- joy/__main__.py | 5 +++-- joy/library.py | 1 + joy/utils/compiler.py | 36 +++++++++++++++++++----------------- joy/utils/types.py | 12 +++++++----- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/joy/__main__.py b/joy/__main__.py index a6c682b..c461ef8 100644 --- a/joy/__main__.py +++ b/joy/__main__.py @@ -17,15 +17,16 @@ # You should have received a copy of the GNU General Public License # along with Thun. If not see . # +from __future__ import print_function from .library import initialize from .joy import repl -print '''\ +print('''\ Thun - Copyright © 2017 Simon Forman This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty". This is free software, and you are welcome to redistribute it under certain conditions; type "sharing" for details. Type "words" to see a list of all words, and "[] help" to print the docs for a word. -''' +''') stack = repl(dictionary=initialize()) diff --git a/joy/library.py b/joy/library.py index 2e57aad..eb44529 100644 --- a/joy/library.py +++ b/joy/library.py @@ -23,6 +23,7 @@ functions. Its main export is a Python function initialize() that returns a dictionary of Joy functions suitable for use with the joy() function. ''' +from __future__ import print_function from logging import getLogger _log = getLogger(__name__) diff --git a/joy/utils/compiler.py b/joy/utils/compiler.py index 7e72c4c..45ed27a 100644 --- a/joy/utils/compiler.py +++ b/joy/utils/compiler.py @@ -10,9 +10,11 @@ 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 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 +from functools import reduce def import_yin(): @@ -29,7 +31,7 @@ class InfiniteStack(tuple): n = n + 1 if m is None else m _NAMES = _names() - _NAMES.next() + next(_NAMES) names = _NAMES.next reset = lambda _, _n=_NAMES: _n.send(-1) @@ -82,7 +84,7 @@ def code_gen(code): def coalesce_pops(code): code.sort(key=lambda p: p[0] != 'pop') # All pops to the front. - try: index = (i for i, t in enumerate(code) if t[0] != 'pop').next() + try: index = next((i for i, t in enumerate(code) if t[0] != 'pop')) except StopIteration: return code[:index] = [tuple(['pop'] + [t for _, t in code[:index][::-1]])] @@ -216,21 +218,21 @@ for name in ''' ''' for name in sorted(D): - print name, + print(name, end=' ') ## print compile_yinyang(name, name) -print '-' * 100 +print('-' * 100) -print compile_yinyang('mul_', 'mul') -print compile_yinyang('pop', 'pop') -print compile_yinyang('ppm', 'popop mul') -print compile_yinyang('sqr', 'dup mul') -print compile_yinyang('foo', 'dup 23 sub mul') -print compile_yinyang('four_mul', 'mul mul mul mul') -print compile_yinyang('baz', 'mul dup sub dup') -print compile_yinyang('to_the_fifth_power', 'dup dup mul dup mul mul') -print compile_yinyang('dup3', 'dup dup dup') -print compile_yinyang('df2m', 'dup first_two mul') -print compile_yinyang('sqr_first', 'uncons swap dup mul swons') -print compile_yinyang('0BAD', 'uncons dup mul') -print compile_yinyang('uncons', 'uncons') +print(compile_yinyang('mul_', 'mul')) +print(compile_yinyang('pop', 'pop')) +print(compile_yinyang('ppm', 'popop mul')) +print(compile_yinyang('sqr', 'dup mul')) +print(compile_yinyang('foo', 'dup 23 sub mul')) +print(compile_yinyang('four_mul', 'mul mul mul mul')) +print(compile_yinyang('baz', 'mul dup sub dup')) +print(compile_yinyang('to_the_fifth_power', 'dup dup mul dup mul mul')) +print(compile_yinyang('dup3', 'dup dup dup')) +print(compile_yinyang('df2m', 'dup first_two mul')) +print(compile_yinyang('sqr_first', 'uncons swap dup mul swons')) +print(compile_yinyang('0BAD', 'uncons dup mul')) +print(compile_yinyang('uncons', 'uncons')) diff --git a/joy/utils/types.py b/joy/utils/types.py index 7c0ce83..8da4c6a 100644 --- a/joy/utils/types.py +++ b/joy/utils/types.py @@ -17,7 +17,9 @@ # You should have received a copy of the GNU General Public License # along with Thun. If not see . # +from __future__ import print_function from logging import getLogger, addLevelName +from functools import reduce _log = getLogger(__name__) addLevelName(15, 'hmm') @@ -721,20 +723,20 @@ def combinator_effect(number, *expect): def show(DEFS): for name, stack_effect_comment in sorted(DEFS.iteritems()): t = ' *'[compilable(stack_effect_comment)] - print name, '=', doc_from_stack_effect(*stack_effect_comment), t + print(name, '=', doc_from_stack_effect(*stack_effect_comment), t) def generate_library_code(DEFS, f=None): if f is None: import sys f = sys.stdout - print >> f, '# GENERATED FILE. DO NOT EDIT.\n' + print('# GENERATED FILE. DO NOT EDIT.\n', file=f) for name, stack_effect_comment in sorted(DEFS.iteritems()): if not compilable(stack_effect_comment): continue - print >> f - print >> f, compile_(name, stack_effect_comment) - print >> f + print(file=f) + print(compile_(name, stack_effect_comment), file=f) + print(file=f) def poly_combinator_effect(number, effect_funcs, *expect):