OSDN Git Service

Minor cleanup.
[joypy/Thun.git] / joy / joy.py
1 # -*- coding: utf-8 -*-
2 #
3 #    Copyright © 2014, 2015, 2017, 2018 Simon Forman
4 #
5 #    This file is part of Thun
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 '''
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.
24
25 '''
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
31
32
33 def joy(stack, expression, dictionary, viewer=None):
34         '''Evaluate a Joy expression on a stack.
35
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.
40
41         The viewer is a function that is called with the stack and expression
42         on every iteration, its return value is ignored.
43
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)
49
50         '''
51         while expression:
52
53                 if viewer: viewer(stack, expression)
54
55                 term, expression = expression
56                 if isinstance(term, Symbol):
57                         term = dictionary[term]
58                         stack, expression, dictionary = term(stack, expression, dictionary)
59                 else:
60                         stack = term, stack
61
62         if viewer: viewer(stack, expression)
63         return stack, expression, dictionary
64
65
66 def run(text, stack, dictionary, viewer=None):
67         '''
68         Return the stack resulting from running the Joy code text on the stack.
69
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)
75
76         '''
77         expression = text_to_expression(text)
78         return joy(stack, expression, dictionary, viewer)
79
80
81 def repl(stack=(), dictionary=None):
82         '''
83         Read-Evaluate-Print Loop
84
85         Accept input and run it on the stack, loop.
86
87         :param stack stack: The stack.
88         :param dict dictionary: A ``dict`` mapping names to Joy functions.
89         :rtype: stack
90
91         '''
92         if dictionary is None:
93                 dictionary = {}
94         try:
95                 while True:
96                         print()
97                         print(stack_to_string(stack), '<-top')
98                         print()
99                         try:
100                                 text = input('joy? ')
101                         except (EOFError, KeyboardInterrupt):
102                                 break
103                         try:
104                                 stack, _, dictionary = run(text, stack, dictionary)
105                         except:
106                                 print_exc()
107         except:
108                 print_exc()
109         print()
110         return stack