1 -- filename : type-lam.lua
2 -- comment : Pretty printing of (extended) lambda calculus
3 -- author : Matthijs Kooijman, Universiteit Twente, NL
4 -- copyright: Matthijs Kooijman
7 local utf = unicode.utf8
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
13 buffers.visualizers.lam.colors = {
20 buffers.visualizers.lam.symbols = {
21 [' '] = {repr = '\\obs '},
22 ['_'] = {repr = '\\_'},
23 ['->'] = {repr = '\\rightarrow'},
24 -- The default * sits very high above the baseline, \ast (u+2217) looks
26 ['*'] = {repr = '\\ast'},
29 buffers.visualizers.lam.keywords = {
37 function buffers.visualizers.lam.take_symbol(str)
38 -- See if str starts with a symbol, and return the remaining
39 -- string and that symbol. If no symbol from the table is matched,
40 -- just returns the first character.
41 -- 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 for symbol,props in pairs(buffers.visualizers.lam.symbols) do
44 -- Try to remove symbol from the start of str
45 symbol, newstr = utf.match(str, "^(" .. symbol .. ")(.*)")
47 -- Return this tokens repr, or just the token if it has no
49 res = props.repr or symbol
50 -- Enclose the token in {\style .. }
52 res = "{\\" .. props.style .. " " .. res .. "}"
57 -- No symbol found, just return the first character
58 return utf.match(str, "^(.)(.*)")
61 -- Take a single word from str, if posible. Returns the rest of the string and
63 function buffers.visualizers.lam.take_word(str)
64 res, newstr = utf.match(str, "^(%a[%a%d_]+)(.*)")
65 return res, newstr or str
68 -- Tries to match each of the patterns and returns the captures of the first
69 -- matching pattern (up to 5 captures are supported). Returns nil when nothing
71 function buffers.visualizers.lam.match_mul(str, patterns)
72 for i, pat in ipairs(patterns) do
73 a, b, c, d, e = utf.match(str, pat)
81 function buffers.visualizers.lam.flush_line(str,nested)
82 local result, state = { }, 0
83 local finish, change = buffers.finish_state, buffers.change_state
84 local take_symbol = buffers.visualizers.lam.take_symbol
85 local take_word = buffers.visualizers.lam.take_word
86 local match_mul = buffers.visualizers.lam.match_mul
87 -- Set the colorscheme, which is used by finish_state and change_state
88 buffers.currentcolors = buffers.visualizers.lam.colors
93 -- See if the next token is a word
94 word, str = take_word(str)
96 if buffers.visualizers.lam.keywords[res] then
97 -- Make all keywords bold
98 word = "{\\bold " .. word .. "}"
100 -- Allow subscripts using _ or just appending a number.
101 base, sub = match_mul(res, {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d*)$"})
103 word = base .. "\\low{" .. sub .. "}"
107 -- The next token is not a word, it must be a symbol
108 symbol, str = take_symbol(str)
111 -- Append the resulting token
112 result[#result+1] = word or symbol
115 state = finish(state, result)
116 buffers.flush_result(result,nested)
119 -- vim: set sw=4 sts=4 expandtab ai: