From c00c6a4e32cb08231a483ae2d7188ad37962dac3 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 6 Apr 2021 11:28:30 -0700 Subject: [PATCH] Add a "quiet" mode. This brings the Python Joy interpreter into better congruence with the Nim interpreter for ease of automated testing. --- README | 6 ++++++ joy/__main__.py | 12 +++++++++--- joy/joy.py | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README b/README index c099933..0be7158 100644 --- a/README +++ b/README @@ -64,6 +64,12 @@ To start a crude REPL: python -m joy +There is a "quiet" mode for e.g. using joy from a shell script: + + python -m joy -q + +This supresses the initial banner output and the prompt text. + §.3 Documentation diff --git a/joy/__main__.py b/joy/__main__.py index 005473b..73b6ddc 100644 --- a/joy/__main__.py +++ b/joy/__main__.py @@ -17,13 +17,19 @@ # You should have received a copy of the GNU General Public License # along with Thun. If not see . # +import sys from .library import initialize, inscribe -from .joy import repl +from .joy import repl, interp from .utils.pretty_print import trace inscribe(trace) -print('''\ + +if '-q' in sys.argv: + j = interp +else: + j = repl + 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 @@ -31,4 +37,4 @@ 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()) +stack = j(dictionary=initialize()) diff --git a/joy/joy.py b/joy/joy.py index 0e755da..4ef38e8 100644 --- a/joy/joy.py +++ b/joy/joy.py @@ -107,3 +107,25 @@ def repl(stack=(), dictionary=None): print_exc() print() return stack + + +def interp(stack=(), dictionary=None): + ''' + Simple REPL with no extra output, suitable for use in scripts. + ''' + if dictionary is None: + dictionary = {} + try: + while True: + try: + text = input() + except (EOFError, KeyboardInterrupt): + break + try: + stack, _, dictionary = run(text, stack, dictionary) + except: + print_exc() + print(stack_to_string(stack)) + except: + print_exc() + return stack