Add and improve some 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 -- Symbols that should have a different representation
21 buffers.visualizers.lam.symbols = {
22     [' '] = {repr = '\\obs '},
23     ['_'] = {repr = '\\_'},
24     ['->'] = {repr = '\\rightarrow'},
25     -- The default * sits very high above the baseline, \ast (u+2217) looks
26     -- better.
27     ['*'] = {repr = '\\ast'},
28 }
29
30
31 -- Keywords that should be bold
32 buffers.visualizers.lam.keywords = {
33     ['case'] = {},
34     ['of'] = {},
35     ['let'] = {},
36     ['in'] = {},
37 }
38
39 -- See if str starts with a symbol, and return the remaining string and that
40 -- symbol. If no symbol from the table is matched, just returns the first
41 -- character.  We can do a lookup directly, since symbols can be different in
42 -- length, so we just loop over all symbols, trying them in turn.
43 function buffers.visualizers.lam.take_symbol(str)
44     for symbol,props in pairs(buffers.visualizers.lam.symbols) do
45         -- Try to remove symbol from the start of str 
46         symbol, newstr = utf.match(str, "^(" .. symbol .. ")(.*)")
47         if symbol then
48             -- Return this tokens repr, or just the token if it has no
49             -- repr.
50             res = props.repr or symbol
51             -- Enclose the token in {\style .. }
52             if props.style then
53                 res = "{\\" .. props.style ..  " " .. res ..  "}"
54             end
55             return res, newstr
56         end
57     end
58     -- No symbol found, just return the first character
59     return utf.match(str, "^(.)(.*)")
60 end
61
62 -- Take a single word from str, if posible. Returns the rest of the string and
63 -- the word taken.
64 function buffers.visualizers.lam.take_word(str)
65         res, newstr = utf.match(str, "^(%a[%a%d_]+)(.*)")
66         return res, newstr or str
67 end
68
69 -- Tries to match each of the patterns and returns the captures of the first
70 -- matching pattern (up to 5 captures are supported). Returns nil when nothing
71 -- matches.
72 function buffers.visualizers.lam.match_mul(str, patterns)
73     for i, pat in ipairs(patterns) do
74         a, b, c, d, e = utf.match(str, pat)
75         if a then
76             return a, b, c, d, e
77         end
78     end
79     return nil
80 end
81
82 -- Find any subscripts in the given word and typeset them
83 function buffers.visualizers.lam.do_subscripts(word)
84     local match_mul = buffers.visualizers.lam.match_mul
85     base, sub = match_mul(res, submatches)
86     if sub then
87         word = base .. "\\low{" .. sub .. "}"
88         -- After a word has been used as a base, allow subscripts
89         -- without _, even for non-numbers.
90         if not bases[base] then
91             -- Register that we've added this base
92             bases[base] = true
93             -- Add a pattern for this base
94             submatches[#submatches+1] = "^(" .. base .. ")([%a%d,]+)$"
95         end
96     end
97     return word
98 end
99
100 function buffers.visualizers.lam.begin_of_buffer(type, name)
101     -- Initially allow subscripts using _ or just appending a number (later,
102     -- we will add extra patterns here.
103     submatches = {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d+)$"}
104     -- This stores all the bases we've encountered so far (to prevent
105     -- duplicates). For each of them there will be a pattern in submatches
106     -- above.
107     bases = {}
108 end
109
110 function buffers.visualizers.lam.flush_line(str,nested)
111     local result, state = { }, 0
112     local finish, change = buffers.finish_state, buffers.change_state
113     local take_symbol = buffers.visualizers.lam.take_symbol
114     local take_word = buffers.visualizers.lam.take_word
115     local do_subscripts = buffers.visualizers.lam.do_subscripts
116     -- Set the colorscheme, which is used by finish_state and change_state
117     buffers.currentcolors = buffers.visualizers.lam.colors
118     while str ~= "" do
119         local found = false
120         local word, symbol
121         -- See if the next token is a word
122         word, str = take_word(str)
123         if word then
124             if buffers.visualizers.lam.keywords[res] then
125                 -- Make all keywords bold
126                 word = "{\\bold " .. word ..  "}"
127             else
128                 -- Process any subscripts in the word
129                 word = do_subscripts(word)
130             end
131         else
132             -- The next token is not a word, it must be a symbol
133             symbol, str = take_symbol(str)
134         end
135
136         -- Append the resulting token
137         result[#result+1] = word or symbol
138     end
139
140     state = finish(state, result)
141     buffers.flush_result(result,nested)
142 end
143
144 -- vim: set sw=4 sts=4 expandtab ai: