<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.luatex.org/index.php?action=history&amp;feed=atom&amp;title=T_wo_T_r%3Arepl.lua</id>
	<title>T wo T r:repl.lua - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.luatex.org/index.php?action=history&amp;feed=atom&amp;title=T_wo_T_r%3Arepl.lua"/>
	<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=T_wo_T_r:repl.lua&amp;action=history"/>
	<updated>2026-05-31T15:12:22Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=T_wo_T_r:repl.lua&amp;diff=3557&amp;oldid=prev</id>
		<title>Rkrug: Created page with &quot; &lt;nowiki&gt; --- Copyright (c) 2011-2015 Rob Hoelz &lt;rob@hoelz.ro&gt; --- --- Further hacked by others. --- --- Permission is hereby granted, free of charge, to any person --- obtain...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=T_wo_T_r:repl.lua&amp;diff=3557&amp;oldid=prev"/>
		<updated>2021-02-22T18:28:37Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; &amp;lt;nowiki&amp;gt; --- Copyright (c) 2011-2015 Rob Hoelz &amp;lt;rob@hoelz.ro&amp;gt; --- --- Further hacked by others. --- --- Permission is hereby granted, free of charge, to any person --- obtain...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt; &amp;lt;nowiki&amp;gt;&lt;br /&gt;
--- Copyright (c) 2011-2015 Rob Hoelz &amp;lt;rob@hoelz.ro&amp;gt;&lt;br /&gt;
---&lt;br /&gt;
--- Further hacked by others.&lt;br /&gt;
---&lt;br /&gt;
--- Permission is hereby granted, free of charge, to any person&lt;br /&gt;
--- obtaining a copy of this software and associated documentation&lt;br /&gt;
--- files (the &amp;quot;Software&amp;quot;), to deal in the Software without&lt;br /&gt;
--- restriction, including without limitation the rights to use, copy,&lt;br /&gt;
--- modify, merge, publish, distribute, sublicense, and/or sell copies&lt;br /&gt;
--- of the Software, and to permit persons to whom the Software is&lt;br /&gt;
--- furnished to do so, subject to the following conditions:&lt;br /&gt;
---&lt;br /&gt;
--- The above copyright notice and this permission notice shall be&lt;br /&gt;
--- included in all copies or substantial portions of the Software.&lt;br /&gt;
---&lt;br /&gt;
--- THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot;, WITHOUT WARRANTY OF ANY KIND,&lt;br /&gt;
--- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF&lt;br /&gt;
--- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND&lt;br /&gt;
--- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS&lt;br /&gt;
--- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN&lt;br /&gt;
--- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN&lt;br /&gt;
--- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE&lt;br /&gt;
--- SOFTWARE.&lt;br /&gt;
---&lt;br /&gt;
--- This module implements the core functionality of a REPL.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- repl() is the REPL.&lt;br /&gt;
&lt;br /&gt;
--- quit() or exit() will terminate the REPL.&lt;br /&gt;
&lt;br /&gt;
--- require(&amp;quot;utils&amp;quot;) or anything else you will want to use in the&lt;br /&gt;
--- REPL as a global, or they will not be visible.&lt;br /&gt;
&lt;br /&gt;
--- Note: The REPL is NOT Unicode aware!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local repl_buffer = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function gather_results(success, ...)&lt;br /&gt;
  local n = select(&amp;quot;#&amp;quot;, ...)&lt;br /&gt;
  return success, {n = n, ... }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function detectcontinue(err)&lt;br /&gt;
  -- Uses the compilation error to determine whether or not further input&lt;br /&gt;
  -- is pending after the last line. That is, is this a fraction of a&lt;br /&gt;
  -- statement.&lt;br /&gt;
  -- Rather crude, but this seems to work.&lt;br /&gt;
  return string.match(err, &amp;quot;&amp;#039;&amp;lt;eof&amp;gt;&amp;#039;$&amp;quot;) or string.match(err, &amp;quot;&amp;lt;eof&amp;gt;$&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function compilechunk(chunk)&lt;br /&gt;
  -- If this is an expression, rather than a statement, we should&lt;br /&gt;
  -- get a function, in f, to return the value of that expression.&lt;br /&gt;
  local f, err = load(&amp;quot;return &amp;quot; .. chunk, &amp;quot;REPL&amp;quot;)&lt;br /&gt;
  -- For statements (or fractions thereof).&lt;br /&gt;
  if not f then&lt;br /&gt;
    f, err = load(chunk, &amp;quot;REPL&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
  return f, err&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function displayresults(results)&lt;br /&gt;
  -- @param results The results to display. The results are a table,&lt;br /&gt;
  -- with the integer keys containing the results, and the &amp;quot;n&amp;quot; key&lt;br /&gt;
  -- containing the highest integer key.&lt;br /&gt;
  if results.n == 0 then return end&lt;br /&gt;
  print(table.unpack(results, 1, results.n))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function displayerror(err)&lt;br /&gt;
  print(err)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function handleline(line)&lt;br /&gt;
  -- Evaluates a line of input, and displays return value(s).&lt;br /&gt;
  local chunk  = repl_buffer .. line&lt;br /&gt;
  local f, err = compilechunk(chunk)&lt;br /&gt;
&lt;br /&gt;
  if f then&lt;br /&gt;
    -- We have a (presumed) function. Try to call it, and display the&lt;br /&gt;
    -- results, or error.&lt;br /&gt;
    repl_buffer = &amp;quot;&amp;quot;&lt;br /&gt;
    local success, results = gather_results(xpcall(f, function(...) return debug.traceback(...) end))&lt;br /&gt;
    if success then&lt;br /&gt;
      displayresults(results)&lt;br /&gt;
    else&lt;br /&gt;
      displayerror(results[1])&lt;br /&gt;
    end&lt;br /&gt;
  elseif detectcontinue(err) then&lt;br /&gt;
    -- This is a (presumed) fraction of a statement?&lt;br /&gt;
    repl_buffer = chunk .. &amp;quot;\n&amp;quot;&lt;br /&gt;
    return 2&lt;br /&gt;
  else&lt;br /&gt;
    -- An error. Clear the buffer, so this does not keep happening.&lt;br /&gt;
    repl_buffer = &amp;quot;&amp;quot;&lt;br /&gt;
    displayerror(err)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  return 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function prompt(level)&lt;br /&gt;
  local prompt&lt;br /&gt;
  if level == 1 then prompt=&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;quot; else prompt=&amp;quot;...&amp;quot; end&lt;br /&gt;
  io.write(prompt)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local function repl()&lt;br /&gt;
  -- Run a REPL loop in a synchronous fashion.&lt;br /&gt;
  print()&lt;br /&gt;
  prompt(1)&lt;br /&gt;
  for line in io.stdin:lines() do&lt;br /&gt;
    if line == &amp;quot;quit()&amp;quot; then&lt;br /&gt;
      break&lt;br /&gt;
    end&lt;br /&gt;
    if line == &amp;quot;exit()&amp;quot; then&lt;br /&gt;
      break&lt;br /&gt;
    end&lt;br /&gt;
    local level = handleline(line)&lt;br /&gt;
    prompt(level)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return {repl = repl}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rkrug</name></author>
		
	</entry>
</feed>