-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamalgam.lua
More file actions
71 lines (65 loc) · 1.95 KB
/
amalgam.lua
File metadata and controls
71 lines (65 loc) · 1.95 KB
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
-- Copyright 2013 Joseph Mucchiello
-- All Rights Reserved.
-- This file is not subject to the LICENSE shipped with the rest of the code.
--
-- tested only on LUA 5.1
--
-- Simple Amalgam Grammar
--
-- Skip all lines between OPEN_SKIP and CLOSE_SKIP
-- import text from #define REPLACE_unique_name "filename"
-- import text from #include "AMALGAM_PREFIX..file.h"
-- Replace REPLACE_STATIC at the start of any line with 'static'
--
-- Inside skip blocks, you place local #includes that you don't
-- need repeated throughout the output file.
--
local OPEN_SKIP = '#define REPLACE_SKIP';
local CLOSE_SKIP = '#undef REPLACE_SKIP';
local REPLACE_FILE = '#define REPLACE_';
local REPLACE_STATIC = 'REPLACE_STATIC';
local INCLUDE = '#include "';
local MONGOOSE_FILE = 'mongoose.c';
local AMALGAM_ROOT = 'mg_core.c';
local AMALGAM_PREFIX = 'mg_';
function process_file(filename,out,depth)
local skip = false; -- replace skip blocks
local chew = false; -- chew blank lines
local f = io.open(filename,"r");
print(string.rep(" ", depth*2).."processing "..filename);
for line in f:lines() do
if chew and line:match("[^%s]") then
chew = false;
end;
if skip and line:sub(1,#CLOSE_SKIP) == CLOSE_SKIP then
skip = false;
chew = true;
end;
if chew or skip then
-- do nothing
elseif line:sub(1,#OPEN_SKIP) == OPEN_SKIP then
skip = true;
elseif line:sub(1,#REPLACE_FILE) == REPLACE_FILE then
process_file(line:match('#define REPLACE_.*"(.*)"'), out, depth+1);
chew = true;
elseif line:sub(1,#INCLUDE) == INCLUDE then
fn = line:match('#include "(.*)"');
if fn:sub(1,#AMALGAM_PREFIX) ~= AMALGAM_PREFIX then
out:write(line,'\n');
else
process_file(fn, out, depth+1);
chew = true;
end;
else
if line:sub(1,#REPLACE_STATIC) == REPLACE_STATIC then
line = 'static'..line:sub(#REPLACE_STATIC+1);
end
out:write(line,'\n');
end
end
-- f:close();
end
--main
out = io.open(MONGOOSE_FILE,"w+");
process_file(AMALGAM_ROOT, out, 0);
out:close();