6 <meta charset="utf-8" />
7 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
8 <title>joy.joy — Thun 0.4.1 documentation</title>
9 <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
10 <link rel="stylesheet" type="text/css" href="../../_static/alabaster.css" />
11 <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
12 <script src="../../_static/jquery.js"></script>
13 <script src="../../_static/underscore.js"></script>
14 <script src="../../_static/doctools.js"></script>
15 <link rel="index" title="Index" href="../../genindex.html" />
16 <link rel="search" title="Search" href="../../search.html" />
18 <link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
21 <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
26 <div class="document">
27 <div class="documentwrapper">
28 <div class="bodywrapper">
31 <div class="body" role="main">
33 <h1>Source code for joy.joy</h1><div class="highlight"><pre>
34 <span></span><span class="c1"># -*- coding: utf-8 -*-</span>
35 <span class="c1">#</span>
36 <span class="c1"># Copyright © 2014, 2015, 2017, 2018 Simon Forman</span>
37 <span class="c1">#</span>
38 <span class="c1"># This file is part of Thun</span>
39 <span class="c1">#</span>
40 <span class="c1"># Thun is free software: you can redistribute it and/or modify</span>
41 <span class="c1"># it under the terms of the GNU General Public License as published by</span>
42 <span class="c1"># the Free Software Foundation, either version 3 of the License, or</span>
43 <span class="c1"># (at your option) any later version.</span>
44 <span class="c1">#</span>
45 <span class="c1"># Thun is distributed in the hope that it will be useful,</span>
46 <span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
47 <span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
48 <span class="c1"># GNU General Public License for more details.</span>
49 <span class="c1">#</span>
50 <span class="c1"># You should have received a copy of the GNU General Public License</span>
51 <span class="c1"># along with Thun. If not see <http://www.gnu.org/licenses/>.</span>
52 <span class="c1">#</span>
53 <span class="sd">'''</span>
54 <span class="sd">This module implements an interpreter for a dialect of Joy that</span>
55 <span class="sd">attempts to stay very close to the spirit of Joy but does not precisely</span>
56 <span class="sd">match the behaviour of the original version(s) written in C.</span>
58 <span class="sd">'''</span>
59 <span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">input</span>
60 <span class="kn">from</span> <span class="nn">traceback</span> <span class="kn">import</span> <span class="n">print_exc</span>
61 <span class="kn">from</span> <span class="nn">.parser</span> <span class="kn">import</span> <span class="n">text_to_expression</span><span class="p">,</span> <span class="n">ParseError</span><span class="p">,</span> <span class="n">Symbol</span>
62 <span class="kn">from</span> <span class="nn">.utils.stack</span> <span class="kn">import</span> <span class="n">stack_to_string</span>
63 <span class="kn">from</span> <span class="nn">.utils.errors</span> <span class="kn">import</span> <span class="p">(</span>
64 <span class="n">NotAListError</span><span class="p">,</span>
65 <span class="n">NotAnIntError</span><span class="p">,</span>
66 <span class="n">StackUnderflowError</span><span class="p">,</span>
67 <span class="p">)</span>
70 <div class="viewcode-block" id="UnknownSymbolError"><a class="viewcode-back" href="../../joy.html#joy.joy.UnknownSymbolError">[docs]</a><span class="k">class</span> <span class="nc">UnknownSymbolError</span><span class="p">(</span><span class="ne">KeyError</span><span class="p">):</span> <span class="k">pass</span></div>
73 <div class="viewcode-block" id="joy"><a class="viewcode-back" href="../../joy.html#joy.joy.joy">[docs]</a><span class="k">def</span> <span class="nf">joy</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">,</span> <span class="n">viewer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
74 <span class="sd">'''Evaluate a Joy expression on a stack.</span>
76 <span class="sd"> This function iterates through a sequence of terms which are either</span>
77 <span class="sd"> literals (strings, numbers, sequences of terms) or function symbols.</span>
78 <span class="sd"> Literals are put onto the stack and functions are looked up in the</span>
79 <span class="sd"> dictionary and executed.</span>
81 <span class="sd"> The viewer is a function that is called with the stack and expression</span>
82 <span class="sd"> on every iteration, its return value is ignored.</span>
84 <span class="sd"> :param stack stack: The stack.</span>
85 <span class="sd"> :param stack expression: The expression to evaluate.</span>
86 <span class="sd"> :param dict dictionary: A ``dict`` mapping names to Joy functions.</span>
87 <span class="sd"> :param function viewer: Optional viewer function.</span>
88 <span class="sd"> :rtype: (stack, (), dictionary)</span>
90 <span class="sd"> '''</span>
91 <span class="k">while</span> <span class="n">expression</span><span class="p">:</span>
93 <span class="k">if</span> <span class="n">viewer</span><span class="p">:</span> <span class="n">viewer</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span>
95 <span class="n">term</span><span class="p">,</span> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span>
96 <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">term</span><span class="p">,</span> <span class="n">Symbol</span><span class="p">):</span>
97 <span class="k">try</span><span class="p">:</span>
98 <span class="n">term</span> <span class="o">=</span> <span class="n">dictionary</span><span class="p">[</span><span class="n">term</span><span class="p">]</span>
99 <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
100 <span class="k">raise</span> <span class="n">UnknownSymbolError</span><span class="p">(</span><span class="n">term</span><span class="p">)</span>
101 <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span> <span class="o">=</span> <span class="n">term</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">)</span>
102 <span class="k">else</span><span class="p">:</span>
103 <span class="n">stack</span> <span class="o">=</span> <span class="n">term</span><span class="p">,</span> <span class="n">stack</span>
105 <span class="k">if</span> <span class="n">viewer</span><span class="p">:</span> <span class="n">viewer</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span>
106 <span class="k">return</span> <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span></div>
109 <div class="viewcode-block" id="run"><a class="viewcode-back" href="../../joy.html#joy.joy.run">[docs]</a><span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">stack</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">,</span> <span class="n">viewer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
110 <span class="sd">'''</span>
111 <span class="sd"> Return the stack resulting from running the Joy code text on the stack.</span>
113 <span class="sd"> :param str text: Joy code.</span>
114 <span class="sd"> :param stack stack: The stack.</span>
115 <span class="sd"> :param dict dictionary: A ``dict`` mapping names to Joy functions.</span>
116 <span class="sd"> :param function viewer: Optional viewer function.</span>
117 <span class="sd"> :rtype: (stack, (), dictionary)</span>
119 <span class="sd"> '''</span>
120 <span class="n">expression</span> <span class="o">=</span> <span class="n">text_to_expression</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
121 <span class="k">return</span> <span class="n">joy</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">,</span> <span class="n">viewer</span><span class="p">)</span></div>
124 <div class="viewcode-block" id="repl"><a class="viewcode-back" href="../../joy.html#joy.joy.repl">[docs]</a><span class="k">def</span> <span class="nf">repl</span><span class="p">(</span><span class="n">stack</span><span class="o">=</span><span class="p">(),</span> <span class="n">dictionary</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
125 <span class="sd">'''</span>
126 <span class="sd"> Read-Evaluate-Print Loop</span>
128 <span class="sd"> Accept input and run it on the stack, loop.</span>
130 <span class="sd"> :param stack stack: The stack.</span>
131 <span class="sd"> :param dict dictionary: A ``dict`` mapping names to Joy functions.</span>
132 <span class="sd"> :rtype: stack</span>
134 <span class="sd"> '''</span>
135 <span class="k">if</span> <span class="n">dictionary</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
136 <span class="n">dictionary</span> <span class="o">=</span> <span class="p">{}</span>
137 <span class="k">try</span><span class="p">:</span>
138 <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
139 <span class="nb">print</span><span class="p">()</span>
140 <span class="nb">print</span><span class="p">(</span><span class="n">stack_to_string</span><span class="p">(</span><span class="n">stack</span><span class="p">),</span> <span class="s1">'<-top'</span><span class="p">)</span>
141 <span class="nb">print</span><span class="p">()</span>
142 <span class="k">try</span><span class="p">:</span>
143 <span class="n">text</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">'joy? '</span><span class="p">)</span>
144 <span class="k">except</span> <span class="p">(</span><span class="ne">EOFError</span><span class="p">,</span> <span class="ne">KeyboardInterrupt</span><span class="p">):</span>
145 <span class="k">break</span>
146 <span class="k">try</span><span class="p">:</span>
147 <span class="n">stack</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">dictionary</span> <span class="o">=</span> <span class="n">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">stack</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">)</span>
148 <span class="k">except</span><span class="p">:</span>
149 <span class="n">print_exc</span><span class="p">()</span>
150 <span class="k">except</span><span class="p">:</span>
151 <span class="n">print_exc</span><span class="p">()</span>
152 <span class="nb">print</span><span class="p">()</span>
153 <span class="k">return</span> <span class="n">stack</span></div>
156 <div class="viewcode-block" id="interp"><a class="viewcode-back" href="../../joy.html#joy.joy.interp">[docs]</a><span class="k">def</span> <span class="nf">interp</span><span class="p">(</span><span class="n">stack</span><span class="o">=</span><span class="p">(),</span> <span class="n">dictionary</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
157 <span class="sd">'''</span>
158 <span class="sd"> Simple REPL with no extra output, suitable for use in scripts.</span>
159 <span class="sd"> '''</span>
160 <span class="k">if</span> <span class="n">dictionary</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
161 <span class="n">dictionary</span> <span class="o">=</span> <span class="p">{}</span>
162 <span class="k">try</span><span class="p">:</span>
163 <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
164 <span class="k">try</span><span class="p">:</span>
165 <span class="n">text</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
166 <span class="k">except</span> <span class="p">(</span><span class="ne">EOFError</span><span class="p">,</span> <span class="ne">KeyboardInterrupt</span><span class="p">):</span>
167 <span class="k">break</span>
168 <span class="k">try</span><span class="p">:</span>
169 <span class="n">stack</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">dictionary</span> <span class="o">=</span> <span class="n">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">stack</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">)</span>
170 <span class="k">except</span> <span class="n">UnknownSymbolError</span> <span class="k">as</span> <span class="n">sym</span><span class="p">:</span>
171 <span class="nb">print</span><span class="p">(</span><span class="s1">'Unknown:'</span><span class="p">,</span> <span class="n">sym</span><span class="p">)</span>
172 <span class="k">except</span> <span class="n">StackUnderflowError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
173 <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="c1"># 'Not enough values on stack.'</span>
174 <span class="k">except</span> <span class="n">NotAnIntError</span><span class="p">:</span>
175 <span class="nb">print</span><span class="p">(</span><span class="s1">'Not an integer.'</span><span class="p">)</span>
176 <span class="k">except</span> <span class="n">NotAListError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
177 <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="c1"># 'Not a list.'</span>
178 <span class="k">except</span><span class="p">:</span>
179 <span class="n">print_exc</span><span class="p">()</span>
180 <span class="nb">print</span><span class="p">(</span><span class="n">stack_to_string</span><span class="p">(</span><span class="n">stack</span><span class="p">))</span>
181 <span class="k">except</span><span class="p">:</span>
182 <span class="n">print_exc</span><span class="p">()</span>
183 <span class="k">return</span> <span class="n">stack</span></div>
190 <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
191 <div class="sphinxsidebarwrapper">
192 <h1 class="logo"><a href="../../index.html">Thun</a></h1>
203 <li class="toctree-l1"><a class="reference internal" href="../../notebooks/Intro.html">Thun: Joy in Python</a></li>
204 <li class="toctree-l1"><a class="reference internal" href="../../joy.html">Joy Interpreter</a></li>
205 <li class="toctree-l1"><a class="reference internal" href="../../stack.html">Stack or Quote or Sequence or List…</a></li>
206 <li class="toctree-l1"><a class="reference internal" href="../../parser.html">Parsing Text into Joy Expressions</a></li>
207 <li class="toctree-l1"><a class="reference internal" href="../../pretty.html">Tracing Joy Execution</a></li>
208 <li class="toctree-l1"><a class="reference internal" href="../../library.html">Function Reference</a></li>
209 <li class="toctree-l1"><a class="reference internal" href="../../lib.html">Functions Grouped by, er, Function with Examples</a></li>
210 <li class="toctree-l1"><a class="reference internal" href="../../types.html">Type Inference of Joy Expressions</a></li>
211 <li class="toctree-l1"><a class="reference internal" href="../../notebooks/index.html">Essays about Programming in Joy</a></li>
214 <div class="relations">
215 <h3>Related Topics</h3>
217 <li><a href="../../index.html">Documentation overview</a><ul>
218 <li><a href="../index.html">Module code</a><ul>
223 <div id="searchbox" style="display: none" role="search">
224 <h3 id="searchlabel">Quick search</h3>
225 <div class="searchformwrapper">
226 <form class="search" action="../../search.html" method="get">
227 <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
228 <input type="submit" value="Go" />
232 <script>$('#searchbox').show(0);</script>
243 <div class="clearer"></div>
245 <div class="footer" role="contentinfo">
246 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
247 <img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
250 <span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Thun Documentation</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="https://joypy.osdn.io/" property="cc:attributionName" rel="cc:attributionURL">Simon Forman</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="https://osdn.net/projects/joypy/" rel="dct:source">https://osdn.net/projects/joypy/</a>.
251 Created using <a href="http://sphinx-doc.org/">Sphinx</a> 4.3.0.