1 # -*- coding: utf-8 -*-
3 # Copyright © 2014, 2015, 2017, 2018 Simon Forman
5 # This file is part of Thun
7 # Thun is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # Thun is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Thun. If not see <http://www.gnu.org/licenses/>.
21 This module implements an interpreter for a dialect of Joy that
22 attempts to stay very close to the spirit of Joy but does not precisely
23 match the behaviour of the original version(s) written in C.
26 from __future__ import print_function
27 from builtins import input
28 from traceback import print_exc, format_exc
29 from .parser import text_to_expression, ParseError, Symbol
30 from .utils.stack import stack_to_string
31 from .utils.pretty_print import TracePrinter
34 def joy(stack, expression, dictionary, viewer=None):
35 '''Evaluate a Joy expression on a stack.
37 This function iterates through a sequence of terms which are either
38 literals (strings, numbers, sequences of terms) or function symbols.
39 Literals are put onto the stack and functions are looked up in the
40 disctionary and executed.
42 The viewer is a function that is called with the stack and expression
43 on every iteration, its return value is ignored.
45 :param stack stack: The stack.
46 :param stack expression: The expression to evaluate.
47 :param dict dictionary: A ``dict`` mapping names to Joy functions.
48 :param function viewer: Optional viewer function.
49 :rtype: (stack, (), dictionary)
54 if viewer: viewer(stack, expression)
56 term, expression = expression
57 if isinstance(term, Symbol):
58 term = dictionary[term]
59 stack, expression, dictionary = term(stack, expression, dictionary)
63 if viewer: viewer(stack, expression)
64 return stack, expression, dictionary
67 def run(text, stack, dictionary, viewer=None):
69 Return the stack resulting from running the Joy code text on the stack.
71 :param str text: Joy code.
72 :param stack stack: The stack.
73 :param dict dictionary: A ``dict`` mapping names to Joy functions.
74 :param function viewer: Optional viewer function.
75 :rtype: (stack, (), dictionary)
78 expression = text_to_expression(text)
79 return joy(stack, expression, dictionary, viewer)
82 def repl(stack=(), dictionary=None):
84 Read-Evaluate-Print Loop
86 Accept input and run it on the stack, loop.
88 :param stack stack: The stack.
89 :param dict dictionary: A ``dict`` mapping names to Joy functions.
93 if dictionary is None:
98 print(stack_to_string(stack), '<-top')
101 text = input('joy? ')
102 except (EOFError, KeyboardInterrupt):
104 viewer = TracePrinter()
106 stack, _, dictionary = run(text, stack, dictionary, viewer.viewer)
108 exc = format_exc() # Capture the exception.
109 viewer.print_() # Print the Joy trace.
111 print(exc) # Print the original exception.