]> git.lizzy.rs Git - metalua.git/blob - src/lib/metalua/extension/log.mlua
Merge remote branch 'origin/master'
[metalua.git] / src / lib / metalua / extension / log.mlua
1 require 'metalua.dollar'
2
3 -{ extension 'match' }
4
5 function dollar.log(...)
6    local args   = {...}
7    local ti     = table.insert
8    local code   = { }
9    local nohash = false
10    local width  = 80
11
12    local i=1
13    if args[i].tag=='String' then
14       ti(code, +{print(" [LOG] "..-{args[1]})})
15       i += 1
16    end
17
18    local xtra_args, names, vals = { }, { }, { }
19    for i=i, #args do
20       match args[i] with
21       | +{ 'nohash' } -> nohash = true
22       | `Number{ w }  -> width = w
23       --| `String{...} | `Number{...} -> ti (xtra_args, args[i])
24       | `Id{n} -> ti (names, n); ti (vals, args[i])
25       | x      -> ti (names, table.tostring(x, 'nohash')); ti (vals, x)
26       end
27    end
28
29    for i=1, #names do
30       local msg = string.format(" [LOG] %s = ", names[i])
31       local printer = `Call{ +{table.tostring},
32                               vals[i],
33                               `Number{ width },
34                               `Number{ #msg  } }
35       if nohash then ti(printer, +{'nohash'}) end
36       ti (code, `Call{ +{printf}, +{"%s%s"}, `String{ msg }, printer })
37    end
38    return code
39 end