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 -- 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
27 ['*'] = {repr = '\\ast'},
28 ['~'] = {repr = '\\sim'},
32 -- Keywords that should be bold
33 buffers.visualizers.lam.keywords = {
40 -- See if str starts with a symbol, and return the remaining string and that
41 -- symbol. If no symbol from the table is matched, just returns the first
42 -- character. We can do a lookup directly, since symbols can be different in
43 -- length, so we just loop over all symbols, trying them in turn.
44 function buffers.visualizers.lam.take_symbol(str)
45 for symbol,props in pairs(buffers.visualizers.lam.symbols) do
46 -- Try to remove symbol from the start of str
47 symbol, newstr = utf.match(str, "^(" .. symbol .. ")(.*)")
49 -- Return this tokens repr, or just the token if it has no
51 res = props.repr or symbol
52 -- Enclose the token in {\style .. }
54 res = "{\\" .. props.style .. " " .. res .. "}"
59 -- No symbol found, just return the first character
60 return utf.match(str, "^(.)(.*)")
63 -- Take a single word from str, if posible. Returns the rest of the string and
65 function buffers.visualizers.lam.take_word(str)
66 -- A word must always start with a-z (in particular, λ is not a valid
68 res, newstr = utf.match(str, "^([a-zA-Z][%a%d_]+)(.*)")
69 return res, newstr or str
72 -- Tries to match each of the patterns and returns the captures of the first
73 -- matching pattern (up to 5 captures are supported). Returns nil when nothing
75 function buffers.visualizers.lam.match_mul(str, patterns)
76 for i, pat in ipairs(patterns) do
77 a, b, c, d, e = utf.match(str, pat)
85 -- Find any subscripts in the given word and typeset them
86 function buffers.visualizers.lam.do_subscripts(word)
87 local match_mul = buffers.visualizers.lam.match_mul
88 base, sub = match_mul(res, submatches)
90 word = base .. "\\low{" .. sub .. "}"
91 -- After a word has been used as a base, allow subscripts
92 -- without _, even for non-numbers.
93 if not bases[base] then
94 -- Register that we've added this base
96 -- Add a pattern for this base
97 submatches[#submatches+1] = "^(" .. base .. ")([%a%d,]+)$"
103 function buffers.visualizers.lam.begin_of_buffer(type, name)
104 -- Initially allow subscripts using _ or just appending a number (later,
105 -- we will add extra patterns here.
106 submatches = {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d+)$"}
107 -- This stores all the bases we've encountered so far (to prevent
108 -- duplicates). For each of them there will be a pattern in submatches
113 function buffers.visualizers.lam.flush_line(str,nested)
114 local result, state = { }, 0
115 local finish, change = buffers.finish_state, buffers.change_state
116 local take_symbol = buffers.visualizers.lam.take_symbol
117 local take_word = buffers.visualizers.lam.take_word
118 local do_subscripts = buffers.visualizers.lam.do_subscripts
119 -- Set the colorscheme, which is used by finish_state and change_state
120 buffers.currentcolors = buffers.visualizers.lam.colors
124 -- See if the next token is a word
125 word, str = take_word(str)
127 if buffers.visualizers.lam.keywords[res] then
128 -- Make all keywords bold
129 word = "{\\bold " .. word .. "}"
131 -- Process any subscripts in the word
132 word = do_subscripts(word)
135 -- The next token is not a word, it must be a symbol
136 symbol, str = take_symbol(str)
139 -- Append the resulting token
140 result[#result+1] = word or symbol
143 state = finish(state, result)
144 buffers.flush_result(result,nested)
147 -- vim: set sw=4 sts=4 expandtab ai: