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
29 from .parser import text_to_expression, ParseError, Symbol
30 from .utils.stack import stack_to_string
33 def joy(stack, expression, dictionary, viewer=None):
34 '''Evaluate a Joy expression on a stack.
36 This function iterates through a sequence of terms which are either
37 literals (strings, numbers, sequences of terms) or function symbols.
38 Literals are put onto the stack and functions are looked up in the
39 disctionary and executed.
41 The viewer is a function that is called with the stack and expression
42 on every iteration, its return value is ignored.
44 :param stack stack: The stack.
45 :param stack expression: The expression to evaluate.
46 :param dict dictionary: A ``dict`` mapping names to Joy functions.
47 :param function viewer: Optional viewer function.
48 :rtype: (stack, (), dictionary)
53 if viewer: viewer(stack, expression)
55 term, expression = expression
56 if isinstance(term, Symbol):
57 term = dictionary[term]
58 stack, expression, dictionary = term(stack, expression, dictionary)
62 if viewer: viewer(stack, expression)
63 return stack, expression, dictionary
66 def run(text, stack, dictionary, viewer=None):
68 Return the stack resulting from running the Joy code text on the stack.
70 :param str text: Joy code.
71 :param stack stack: The stack.
72 :param dict dictionary: A ``dict`` mapping names to Joy functions.
73 :param function viewer: Optional viewer function.
74 :rtype: (stack, (), dictionary)
77 expression = text_to_expression(text)
78 return joy(stack, expression, dictionary, viewer)
81 def repl(stack=(), dictionary=None):
83 Read-Evaluate-Print Loop
85 Accept input and run it on the stack, loop.
87 :param stack stack: The stack.
88 :param dict dictionary: A ``dict`` mapping names to Joy functions.
92 if dictionary is None:
97 print(stack_to_string(stack), '<-top')
100 text = input('joy? ')
101 except (EOFError, KeyboardInterrupt):
104 stack, _, dictionary = run(text, stack, dictionary)