-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhinxed.winxed
64 lines (52 loc) · 1.48 KB
/
hinxed.winxed
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
// hinxed.winxed
// html-like templated winxed
function genblock(var out, string blockcontent)
{
out.print("{ print(\"");
out.print(escape(blockcontent));
out.print("\"); }\n");
}
function gencode(var out, string code)
{
out.print("\n");
out.print(code);
out.print("\n");
}
function main [main](var argv)
{
string filename = argv[1];
var file = open(filename);
file.encoding("utf8");
var templ = file.readall();
file.close();
string genfuncname = "hinxed_test";
const string TAG = "<?hinxed";
const string TAGEND = "?>";
var out = new ["StringHandle"];
out.encoding("utf8");
out.open("test", "w");
// Read template and generate winxed source.
out.print("function " + string(genfuncname) + "()\n{\n");
int p = 0, prev = 0;
while ((p = indexof(templ, TAG, prev)) >= 0) {
genblock(out, substr(templ, prev, p - prev));
p += length(TAG);
int pend = indexof(templ, TAGEND, p);
if (pend < 0)
throw "Unclosed hinxed tag";
gencode(out, substr(templ, p, pend - p));
prev = pend + length(TAGEND);
}
genblock(out, (substr(templ, prev)));
out.print("}\n");
out.close();
out.open("test");
string code = out.readall();
out.close();
// Compile winxed source to packfile
var winxed = load_language("winxed");
var object = winxed.compile(code, "pbc": [named("target")] );
// And run
var entry = object.all_subs()[0];
entry();
}