forked from simtr/PowderToypp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
generator.py
207 lines (167 loc) · 4.5 KB
/
generator.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import re, os, shutil, string
def generateElements():
elementClasses = dict()
baseClasses = dict()
elementHeader = """#ifndef ELEMENTCLASSES_H
#define ELEMENTCLASSES_H
#include <vector>
#include "simulation/Element.h"
#include "simulation/elements/Element.h"
"""
directives = []
elementFiles = os.listdir("src/simulation/elements")
for elementFile in elementFiles:
f = open("src/simulation/elements/"+elementFile, "r")
fileData = f.read()
f.close()
directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)'
matcher = re.compile(directiveMatcher)
directiveMatches = matcher.findall(fileData)
for match in directiveMatches:
directives.append(match.split(" "))
classDirectives = []
for d in directives:
if d[0] == "ElementClass":
d[3] = string.atoi(d[3])
classDirectives.append(d)
elementIDs = sorted(classDirectives, key=lambda directive: directive[3])
for d in elementIDs:
tmpClass = d[1]
newClass = ""
baseClass = "Element"
if ':' in tmpClass:
classBits = tmpClass.split(':')
newClass = classBits[0]
baseClass = classBits[1]
else:
newClass = tmpClass
elementClasses[newClass] = []
baseClasses[newClass] = baseClass
elementHeader += "#define %s %s\n" % (d[2], d[3])
for d in directives:
if d[0] == "ElementHeader":
tmpClass = d[1]
newClass = ""
baseClass = "Element"
if ':' in tmpClass:
classBits = tmpClass.split(':')
newClass = classBits[0]
baseClass = classBits[1]
else:
newClass = tmpClass
elementClasses[newClass].append(string.join(d[2:], " ")+";")
#for className, classMembers in elementClasses.items():
for d in elementIDs:
tmpClass = d[1]
newClass = ""
baseClass = "Element"
if ':' in tmpClass:
classBits = tmpClass.split(':')
newClass = classBits[0]
baseClass = classBits[1]
else:
newClass = tmpClass
className = newClass
classMembers = elementClasses[newClass]
elementBase = baseClass
elementHeader += """
class {0}: public {1}
{{
public:
{0}();
virtual ~{0}();
{2}
}};
""".format(className, elementBase, string.join(classMembers, "\n\t"))
elementHeader += """
std::vector<Element> GetElements();
#endif
"""
elementContent = """#include "ElementClasses.h"
std::vector<Element> GetElements()
{
std::vector<Element> elements;
""";
for d in elementIDs:
tmpClass = d[1]
newClass = ""
baseClass = "Element"
if ':' in tmpClass:
classBits = tmpClass.split(':')
newClass = classBits[0]
baseClass = classBits[1]
else:
newClass = tmpClass
elementContent += """elements.push_back(%s());
""" % (newClass)
elementContent += """return elements;
}
""";
f = open("generated/ElementClasses.h", "w")
f.write(elementHeader)
f.close()
f = open("generated/ElementClasses.cpp", "w")
f.write(elementContent)
f.close()
def generateTools():
toolClasses = dict()
toolHeader = """#ifndef TOOLCLASSES_H
#define TOOLCLASSES_H
#include <vector>
#include "simulation/Tools.h"
#include "simulation/tools/SimTool.h"
"""
directives = []
toolFiles = os.listdir("src/simulation/tools")
for toolFile in toolFiles:
f = open("src/simulation/tools/"+toolFile, "r")
fileData = f.read()
f.close()
directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)'
matcher = re.compile(directiveMatcher)
directiveMatches = matcher.findall(fileData)
for match in directiveMatches:
directives.append(match.split(" "))
classDirectives = []
for d in directives:
if d[0] == "ToolClass":
toolClasses[d[1]] = []
toolHeader += "#define %s %s\n" % (d[2], d[3])
d[3] = string.atoi(d[3])
classDirectives.append(d)
for d in directives:
if d[0] == "ToolHeader":
toolClasses[d[1]].append(string.join(d[2:], " ")+";")
for className, classMembers in toolClasses.items():
toolHeader += """class {0}: public SimTool
{{
public:
{0}();
virtual ~{0}();
virtual int Perform(Simulation * sim, Particle * cpart, int x, int y, float strength);
{1}
}};
""".format(className, string.join(classMembers, "\n"))
toolHeader += """std::vector<SimTool*> GetTools();
#endif
"""
toolContent = """#include "ToolClasses.h"
std::vector<SimTool*> GetTools()
{
std::vector<SimTool*> tools;
""";
toolIDs = sorted(classDirectives, key=lambda directive: directive[3])
for d in toolIDs:
toolContent += """ tools.push_back(new %s());
""" % (d[1])
toolContent += """return tools;
}
""";
f = open("generated/ToolClasses.h", "w")
f.write(toolHeader)
f.close()
f = open("generated/ToolClasses.cpp", "w")
f.write(toolContent)
f.close()
generateElements()
generateTools()