Move the State chapter into the Hardware Description chapter.
[matthijs/master-project/report.git] / pret-trans.lua
1 -- filename : type-trans.lua
2 -- comment  : Pretty printing of core transformations. Parses a specific
3 --            syntax, consisting of a before / after with a dashed lined in
4 --            between, and some extra conditions to the right of the line.
5 --            Recreates this layout using frames and line commands.
6 -- author   : Matthijs Kooijman, Universiteit Twente, NL
7 -- copyright: Matthijs Kooijman
8 -- license  : None
9
10 local utf = unicode.utf8
11
12 local vis = buffers.newvisualizer("trans")
13
14 local commands = {}
15 -- A command to create a horizontal rule.
16 commands.rule = "\\HLine[width=.40 * \\the\\textwidth]"
17 -- Pretty printer to use for the stuff before and after the line
18 commands.before_pret = "lam"
19 commands.after_pret = "lam"
20 -- Frame commands to use for the left (before + line + after) and right
21 -- (conditions) parts. Should include an opening {, which will be closed
22 -- automatically.
23 commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{\\sans"
24 commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.48\\textwidth]{"
25
26 -- A table to keep the lines in this buffer, so we can process them all at
27 -- once at the end.
28 local lines
29
30 -- Some helper functions
31 local function ltrim(s)
32     return (string.gsub(s, "^%s*", ""))
33 end
34
35 local function rtrim(s)
36     return (string.gsub(s, "%s*$", ""))
37 end
38
39 -- Insert n blank lines
40 local function blanks(n)
41     for i = 1,n do
42         buffers.visualizers.handlers.default.empty_line()
43     end
44 end
45
46 -- Prettyprint the given lines using the given pretty printer
47 local function prettyprint(ppr, lines)
48     -- Change the current visualizer
49     buffers.setvisualizer('lam')
50
51     -- Output the lines
52     buffers.hooks.begin_of_display()
53     line = 0
54     for i = 1,#lines do
55         _, line = buffers.typeline(lines[i], i, #lines, line)
56     end
57     buffers.hooks.end_of_display()
58
59     -- Change the visualizer back
60     buffers.setvisualizer('trans')
61 end
62
63 -- Capture all lines, without generating any output
64 function vis.begin_of_display()
65     lines = {}
66     -- Let all the lambda pretty printing in this buffer use shared subscript
67     -- detection
68     buffers.visualizers.handlers.lam.begin_of_block()
69 end
70 function vis.begin_of_line(n)
71     -- Don't generate output here
72 end
73 function vis.flush_line(str, nested)
74     table.insert(lines, str)
75     -- Don't generate output here
76 end
77 function vis.end_of_line(n)
78     -- Don't generate output here
79 end
80 function vis.empty_line()
81     table.insert(lines, '')
82     -- Don't generate output here
83 end
84
85 -- We do the actual work here. Process all the lines in the buffer and
86 -- generate output for them.
87 function vis.end_of_display()
88     -- Find the horizontal rule, and see how long it is.
89     len = nil
90     for i = 1,#lines do
91         match = utf.match(lines[i], "^%-%-%-*")
92         if match then
93             len = utf.len(match)
94             break
95         end
96     end
97   
98     if not len then
99         error("No horizontal separator found in:\n" .. table.concat(lines, "\n"))
100     end
101
102     -- Split the input in three parts. Stuff before the line, stuff
103     -- after the line, stuff to the right of the line.
104     before, after, rights = {}, {}, {}
105     found_line = false
106     for i = 1,#lines do
107         line = lines[i]
108         -- Split the line into a left and right part
109         left = rtrim(utf.sub(line, 1, len))
110         right = ltrim(utf.sub(line, len + 1))
111         if utf.match(left, "^%-%-%-*") then
112             found_line = true
113         else
114             if utf.len(left) > 0 then
115                 if not found_line then
116                     table.insert(before, left)
117                 else
118                     table.insert(after, left)
119                 end
120             end
121         end
122         if utf.len(right) > 0 then
123             table.insert(rights, right)
124         end
125     end
126
127     --
128     -- Real output starts here
129     --
130
131     -- Ensure the left and right frames end up next to each other.
132     tex.sprint("\\dontleavehmode")
133
134     -- Open and fill the left frame
135     tex.sprint(commands.leftframe)
136
137     prettyprint('lam', before)
138
139     tex.sprint(commands.rule)
140
141     prettyprint('lam', after)
142
143     -- Close the left frame
144     tex.sprint("}")
145
146     -- Open and fill the right frame
147     tex.sprint(commands.rightframe)
148
149     -- Insert spacer blank lines to align the middle condition with the
150     -- conclusion rule
151     n_blanks = #before - math.ceil((#rights - 1) / 2)
152     n_blanks = math.max(0, n_blanks)
153     blanks(n_blanks)
154
155     -- Print the conditions
156     for i = 1,#rights do
157         tex.sprint(rights[i])
158         buffers.visualizers.handlers.default.end_of_line()
159     end
160
161     -- Fill up the remaining space with blanks
162     n_blanks = (#before + 1 + #after) - (n_blanks + #rights)
163     n_blanks = math.max(0, n_blanks)
164     blanks(n_blanks)
165
166     -- Close the right frame 
167     tex.sprint("}")
168     
169     -- Clean up
170     lines = {}
171     buffers.visualizers.handlers.lam.end_of_block()
172 end
173
174 -- vim: set sw=4 sts=4 expandtab ai: