Skip to content

Commit 364563b

Browse files
committed
refactor preprocessor
1 parent 59bacf5 commit 364563b

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

source/app.d

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ int main(string[] args) {
117117
compiler.backend.org = org;
118118
compiler.backend.orgSet = orgSet;
119119
compiler.includeDirs = includeDirs;
120-
121-
nodes = Preprocessor(nodes, includeDirs, included, compiler.backend.GetVersions());
120+
121+
auto preproc = new Preprocessor();
122+
preproc.includeDirs = includeDirs;
123+
preproc.versions = compiler.backend.GetVersions();
124+
nodes = preproc.Run(nodes);
122125

123126
if (optimise) {
124127
auto optimiser = new Optimiser();

source/preprocessor.d

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,62 @@ import callisto.error;
1010
import callisto.parser;
1111
import callisto.language;
1212

13-
Node[] Preprocessor(
14-
Node[] nodes, string[] includeDirs, ref string[] included, string[] versions
15-
) {
16-
Node[] ret;
13+
class Preprocessor {
14+
string[] includeDirs;
15+
string[] included;
16+
string[] versions;
1717

18-
foreach (ref inode ; nodes) {
19-
switch (inode.type) {
20-
case NodeType.Include: {
21-
auto node = cast(IncludeNode) inode;
22-
auto path = format("%s/%s", dirName(node.error.file), node.path);
18+
Node[] Run(Node[] nodes) {
19+
Node[] ret;
2320

24-
if (!exists(path)) {
25-
bool found;
26-
27-
foreach (ref ipath ; includeDirs) {
28-
path = format("%s/%s", ipath, node.path);
21+
foreach (ref inode ; nodes) {
22+
switch (inode.type) {
23+
case NodeType.Include: {
24+
auto node = cast(IncludeNode) inode;
25+
auto path = format("%s/%s", dirName(node.error.file), node.path);
2926

30-
if (exists(path)) {
31-
found = true;
32-
break;
27+
if (!exists(path)) {
28+
bool found;
29+
30+
foreach (ref ipath ; includeDirs) {
31+
path = format("%s/%s", ipath, node.path);
32+
33+
if (exists(path)) {
34+
found = true;
35+
break;
36+
}
3337
}
34-
}
3538

36-
if (!found) {
37-
ErrorBegin(node.error);
38-
stderr.writefln("Can't find file '%s'", node.path);
39-
exit(1);
39+
if (!found) {
40+
ErrorBegin(node.error);
41+
stderr.writefln("Can't find file '%s'", node.path);
42+
exit(1);
43+
}
4044
}
41-
}
4245

43-
if (included.canFind(path)) {
44-
continue;
45-
}
46+
if (included.canFind(path)) {
47+
continue;
48+
}
4649

47-
included ~= path;
50+
included ~= path;
4851

49-
ret ~= Preprocessor(ParseFile(path), includeDirs, included, versions);
50-
break;
51-
}
52-
case NodeType.Version: {
53-
auto node = cast(VersionNode) inode;
52+
ret ~= Run(ParseFile(path));
53+
break;
54+
}
55+
case NodeType.Version: {
56+
auto node = cast(VersionNode) inode;
5457

55-
if (versions.canFind(node.ver)) {
56-
ret ~= node.block;
58+
if (versions.canFind(node.ver)) {
59+
ret ~= node.block;
60+
}
61+
break;
62+
}
63+
default: {
64+
ret ~= inode;
5765
}
58-
break;
59-
}
60-
default: {
61-
ret ~= inode;
6266
}
6367
}
64-
}
6568

66-
return ret;
69+
return ret;
70+
}
6771
}

0 commit comments

Comments
 (0)