Add a sequence of examples for the new transformations.
[matthijs/master-project/report.git] / pret-lam.lua
1 -- filename : type-lam.lua
2 -- comment  : Pretty printing of (extended) lambda calculus
3 -- author   : Matthijs Kooijman, Universiteit Twente, NL
4 -- copyright: Matthijs Kooijman
5 -- license  : None
6
7 local utf = unicode.utf8
8
9 if not buffers                 then buffers                 = { } end
10 if not buffers.visualizers     then buffers.visualizers     = { } end
11 if not buffers.visualizers.lam then buffers.visualizers.lam = { } end
12
13 buffers.visualizers.lam.colors = {
14     "prettytwo",
15     "prettyone",
16     "prettythree",
17     "prettyfour"
18 }
19
20 buffers.visualizers.lam.tokens = {
21     [' '] = {repr = '\\obs '},
22     ['->'] = {repr = '\\rightarrow'},
23     ['case'] = {style = 'bold'},
24     ['of'] = {style = 'bold'},
25     ['let'] = {style = 'bold'},
26 }
27
28 function buffers.visualizers.lam.flush_line(str,nested)
29     local result, state = { }, 0
30     local finish, change = buffers.finish_state, buffers.change_state
31     -- Set the colorscheme, which is used by finish_state and change_state
32     buffers.currentcolors = buffers.visualizers.lam.colors
33
34     while str ~= "" do
35         found = false
36         for tok,props in pairs(buffers.visualizers.lam.tokens) do
37             -- Try to remove tok from the start of str 
38             str, count = utf.gsub(str, "^" .. tok, "")
39             if count ~= 0 then
40                 -- Replace the token with the repr, or just itself
41                 result[#result+1] = props.repr or tok
42                 -- Enclose the token in {\style .. }
43                 if props.style then
44                     result[#result] = "{\\" .. props.style ..
45                                         " " .. result[#result] ..
46                                         "}"
47                 end
48                 -- We found a token, now start over
49                 found = true
50                 break
51             end
52         end
53         if not found then
54             result[#result+1] = utf.sub(str, 1, 1)
55             str = utf.sub(str, 2)
56         end
57     end
58     state = finish(state, result)
59     buffers.flush_result(result,nested)
60 end
61
62 -- vim: set sw=4 sts=4 expandtab ai: