Fix the return value simplification transformation.
[matthijs/master-project/report.git] / pret-trans.lua
index 9c6847e9bf68e5a9fead39032f93f8d1003b8a14..a0ae9c2c73747ae99650e63d6f87f19b6d14c591 100644 (file)
@@ -9,21 +9,20 @@
 
 local utf = unicode.utf8
 
 
 local utf = unicode.utf8
 
-if not buffers                 then buffers                 = { } end
-if not buffers.visualizers     then buffers.visualizers     = { } end
-if not buffers.visualizers.trans then buffers.visualizers.trans = { } end
+local vis = buffers.newvisualizer("trans")
 
 local commands = {}
 -- A command to create a horizontal rule.
 
 local commands = {}
 -- A command to create a horizontal rule.
-commands.rule = "\\blackrule[height=0.5pt,depth=0pt,width=.45\\textwidth]"
--- Pretty printer to use for the stuff before and after the line
-commands.before_pret = "lam"
-commands.after_pret = "lam"
+commands.rule = "\\HLine[width=.40 * \\the\\textwidth]"
+-- Typing environment to use for the stuff before and after the line. Will be
+-- prefixed with \start and \stop automatically.
+commands.beforeenv = "unboxedlambda"
+commands.afterenv = "unboxedlambda"
 -- Frame commands to use for the left (before + line + after) and right
 -- (conditions) parts. Should include an opening {, which will be closed
 -- automatically.
 -- Frame commands to use for the left (before + line + after) and right
 -- (conditions) parts. Should include an opening {, which will be closed
 -- automatically.
-commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.5\\textwidth]{\\sans"
-commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.5\\textwidth]{"
+commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{"
+commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{"
 
 -- A table to keep the lines in this buffer, so we can process them all at
 -- once at the end.
 
 -- A table to keep the lines in this buffer, so we can process them all at
 -- once at the end.
@@ -40,52 +39,50 @@ end
 
 -- Insert n blank lines
 local function blanks(n)
 
 -- Insert n blank lines
 local function blanks(n)
-    print("Inserting " .. n .. " blanks")
     for i = 1,n do
     for i = 1,n do
-        buffers.visualizers.default.empty_line()
+        buffers.visualizers.handlers.default.empty_line()
     end
 end
 
 -- Prettyprint the given lines using the given pretty printer
 local function prettyprint(ppr, lines)
     -- Change the current visualizer
     end
 end
 
 -- Prettyprint the given lines using the given pretty printer
 local function prettyprint(ppr, lines)
     -- Change the current visualizer
-    oldvisualizer = buffers.currentvisualizer
-    buffers.currentvisualizer = 'lam'
+    buffers.setvisualizer('lam')
 
     -- Output the lines
 
     -- Output the lines
-    buffers.hooks.begin_of_buffer('buffer', 'pret-trans-internal')
+    buffers.hooks.begin_of_display()
     line = 0
     for i = 1,#lines do
         _, line = buffers.typeline(lines[i], i, #lines, line)
     end
     line = 0
     for i = 1,#lines do
         _, line = buffers.typeline(lines[i], i, #lines, line)
     end
-    buffers.hooks.end_of_buffer('buffer', 'pret-trans-internal')
+    buffers.hooks.end_of_display()
 
     -- Change the visualizer back
 
     -- Change the visualizer back
-    buffers.currentvisualizer = oldvisualizer
+    buffers.setvisualizer('trans')
 end
 
 -- Capture all lines, without generating any output
 end
 
 -- Capture all lines, without generating any output
-function buffers.visualizers.trans.begin_of_buffer(type, name)
+function vis.begin_of_display()
     lines = {}
 end
     lines = {}
 end
-function buffers.visualizers.trans.begin_of_line(n)
+function vis.begin_of_line(n)
     -- Don't generate output here
 end
     -- Don't generate output here
 end
-function buffers.visualizers.trans.flush_line(str, nested)
+function vis.flush_line(str, nested)
     table.insert(lines, str)
     -- Don't generate output here
 end
     table.insert(lines, str)
     -- Don't generate output here
 end
-function buffers.visualizers.trans.end_of_line(n)
+function vis.end_of_line(n)
     -- Don't generate output here
 end
     -- Don't generate output here
 end
-function buffers.visualizers.empty_line()
+function vis.empty_line()
     table.insert(lines, '')
     -- Don't generate output here
 end
 
 -- We do the actual work here. Process all the lines in the buffer and
 -- generate output for them.
     table.insert(lines, '')
     -- Don't generate output here
 end
 
 -- We do the actual work here. Process all the lines in the buffer and
 -- generate output for them.
-function buffers.visualizers.trans.end_of_buffer()
+function vis.end_of_display()
     -- Find the horizontal rule, and see how long it is.
     len = nil
     for i = 1,#lines do
     -- Find the horizontal rule, and see how long it is.
     len = nil
     for i = 1,#lines do
@@ -95,8 +92,11 @@ function buffers.visualizers.trans.end_of_buffer()
             break
         end
     end
             break
         end
     end
-    print("Rule length: " .. len)
-   
+  
+    if not len then
+        error("No horizontal separator found in:\n" .. table.concat(lines, "\n"))
+    end
+
     -- Split the input in three parts. Stuff before the line, stuff
     -- after the line, stuff to the right of the line.
     before, after, rights = {}, {}, {}
     -- Split the input in three parts. Stuff before the line, stuff
     -- after the line, stuff to the right of the line.
     before, after, rights = {}, {}, {}
@@ -109,7 +109,6 @@ function buffers.visualizers.trans.end_of_buffer()
         if utf.match(left, "^%-%-%-*") then
             found_line = true
         else
         if utf.match(left, "^%-%-%-*") then
             found_line = true
         else
-            print("Looking at " .. line .. "('" .. left .. "', '" .. right .. "')")
             if utf.len(left) > 0 then
                 if not found_line then
                     table.insert(before, left)
             if utf.len(left) > 0 then
                 if not found_line then
                     table.insert(before, left)
@@ -127,17 +126,29 @@ function buffers.visualizers.trans.end_of_buffer()
     -- Real output starts here
     --
 
     -- Real output starts here
     --
 
+    -- Let all the lambda pretty printing in this buffer use shared subscript
+    -- detection
+    tex.sprint("\\directlua{buffers.visualizers.handlers.lam.begin_of_block()}")
+
     -- Ensure the left and right frames end up next to each other.
     tex.sprint("\\dontleavehmode")
 
     -- Open and fill the left frame
     tex.sprint(commands.leftframe)
 
     -- Ensure the left and right frames end up next to each other.
     tex.sprint("\\dontleavehmode")
 
     -- Open and fill the left frame
     tex.sprint(commands.leftframe)
 
-    prettyprint('lam', before)
+    tex.sprint("\\start" .. commands.beforeenv)
+
+    tex.sprint(table.concat(before, "\n"))
+
+    tex.sprint("\\stop" .. commands.beforeenv)
 
     tex.sprint(commands.rule)
 
 
     tex.sprint(commands.rule)
 
-    prettyprint('lam', after)
+    tex.sprint("\\start" .. commands.afterenv)
+
+    tex.sprint(table.concat(after, "\n"))
+
+    tex.sprint("\\stop" .. commands.afterenv)
 
     -- Close the left frame
     tex.sprint("}")
 
     -- Close the left frame
     tex.sprint("}")
@@ -154,7 +165,7 @@ function buffers.visualizers.trans.end_of_buffer()
     -- Print the conditions
     for i = 1,#rights do
         tex.sprint(rights[i])
     -- Print the conditions
     for i = 1,#rights do
         tex.sprint(rights[i])
-        buffers.visualizers.default.end_of_line()
+        buffers.visualizers.handlers.default.end_of_line()
     end
 
     -- Fill up the remaining space with blanks
     end
 
     -- Fill up the remaining space with blanks
@@ -164,6 +175,8 @@ function buffers.visualizers.trans.end_of_buffer()
 
     -- Close the right frame 
     tex.sprint("}")
 
     -- Close the right frame 
     tex.sprint("}")
+
+    tex.sprint("\\directlua{buffers.visualizers.handlers.lam.end_of_block()}")
     
     -- Clean up
     lines = {}
     
     -- Clean up
     lines = {}