forked from ToxicFrog/doom-mods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zspp
executable file
·73 lines (63 loc) · 2.16 KB
/
zspp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env luajit
--[[
usage: zsc infile.zs outfile.zsc
A simple ZScript preprocessor.
It supports the following features:
#namespace foo::bar
all code after this directive will be considered to be in the "foo::bar" namespace
::baz
expands to "foo::bar::baz"
the double colons are converted into single underscores at compile time.
#include "./bar"
the leading . is converted into the dirname of the file as passed on the command line,
on the assumption that you are running this tool from the wadroot and passing it
relative paths.
DEBUG(...)
if the ZSC_DEBUG environment variable is defined, expands to console.printf(...);
otherwise is completely elided.
]]
local src,dst = ...
local dirname = src:gsub('[^/]+$', '')
local lines = {}
local ns = nil
local debug = false;
for line in io.lines(src) do
if line:match('^#namespace') then
ns = assert(line:match('^#namespace (%S+);'), "malformed namespace directive: "..line)
table.insert(lines, '// '..line)
elseif line:match('^#debug on') then
-- TODO: allow enabling debug on ranges of lines rather than per-file
debug = true
table.insert(lines, '// '..line)
elseif line:match('^#debug off') then
debug = false
table.insert(lines, '// '..line)
elseif line:match('^#include "%./(.*)"') then
path = line:match('^#include "%./(.*)"')
line = ('#include "%s%s"'):format(dirname, path)
table.insert(lines, line)
elseif line:match('::') and ns then
line = (' '..line)
:gsub('(%W)::', '%1'..ns..'_')
:gsub('(%w)::', '%1_')
:sub(2)
table.insert(lines, line)
else
table.insert(lines, line)
end
end
local function replace_tag(argv)
argv = argv:sub(2,-2)
return string.format('(%s?%s.GetTag():"null")', argv, argv)
end
table.insert(lines, '')
local buf = table.concat(lines, '\n')
if debug or os.getenv('ZSPP_FORCE_DEBUG') then
buf = buf:gsub('DEBUG(%b())', function(debugargs)
-- TODO: add line number
return string.format('console.printf("%s: "..%s)', src, debugargs:sub(2,-2):gsub('TAG(%b())', replace_tag))
end)
else
buf = buf:gsub('%s*DEBUG%b();', function(s) return (s:gsub('\n', '\n//')) end)
end
dst = assert(io.open(dst, 'wb')):write(buf)