-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript.cpp
242 lines (192 loc) · 6.24 KB
/
script.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "prism/script.h"
#include <string.h>
#include "prism/log.h"
#include "prism/system.h"
namespace prism {
Script loadScript(const char* tPath) {
Script ret;
ret.mBuffer = fileToBuffer(tPath);
appendTerminationSymbolToBuffer(&ret.mBuffer);
return ret;
}
void executeOnScriptRegion(const ScriptRegion& tRegion, ScriptExecuteCB tFunc, void* tCaller) {
ScriptPosition pos = getScriptRegionStart(tRegion);
while (pos.mPointer != NULL) {
pos = tFunc(tCaller, pos);
if (pos.mPointer == NULL) break;
pos = getNextScriptInstruction(pos);
}
}
static ScriptPosition updateScriptPositionValidity(const ScriptPosition& tPos) {
auto ret = tPos;
if ((ret.mPointer >= ret.mRegion.mEnd)) {
ret.mPointer = NULL;
}
return ret;
}
ScriptPosition getNextScriptString(const ScriptPosition& tPos, char* tDest) {
int positionsRead;
int items = sscanf(tPos.mPointer, "%99s%n", tDest, &positionsRead);
if (items != 1) {
logWarningFormat("Unable to parse next script string from: %s", (char*)tPos.mPointer);
}
auto ret = tPos;
ret.mPointer += positionsRead;
ret = updateScriptPositionValidity(ret);
return ret;
}
static ScriptPosition skipNextScriptString(const ScriptPosition& tPos) {
char buf[100];
return getNextScriptString(tPos, buf);
}
static ScriptPosition getNextScriptRawCharacter(const ScriptPosition& tPos, char* tDest) {
tDest[0] = *tPos.mPointer;
auto ret = tPos;
ret.mPointer++;
ret = updateScriptPositionValidity(ret);
return ret;
}
ScriptPosition getNextScriptDouble(const ScriptPosition& tPos, double* tDest) {
int positionsRead;
int items = sscanf(tPos.mPointer, "%lf%n", tDest, &positionsRead);
if (items != 1) {
logWarningFormat("Unable to parse next script double from: %s", (char*)tPos.mPointer);
}
auto ret = tPos;
ret.mPointer += positionsRead;
ret = updateScriptPositionValidity(ret);
return ret;
}
ScriptPosition getNextScriptInteger(const ScriptPosition& tPos, int* tDest) {
int positionsRead;
int items = sscanf(tPos.mPointer, "%d%n", tDest, &positionsRead);
if (items != 1) {
logWarningFormat("Unable to parse next script integer from: %s", (char*)tPos.mPointer);
}
auto ret = tPos;
ret.mPointer += positionsRead;
ret = updateScriptPositionValidity(ret);
return ret;
}
static ScriptRegion makeScriptRegion(const Script& tScript, char* tStart, char* tEnd) {
ScriptRegion ret;
ret.mScript = tScript;
ret.mStart = tStart;
ret.mEnd = tEnd;
return ret;
}
static ScriptRegion getWholeScriptRegion(Script tScript) {
return makeScriptRegion(tScript, (char*)tScript.mBuffer.mData, (char*)((uintptr_t)tScript.mBuffer.mData + tScript.mBuffer.mLength - 1));
}
static ScriptPosition findNextScriptOccurenceOnSameLevel(const ScriptPosition& tPos, const char* tWord) {
char w[100];
int isInside = 0;
auto ret = tPos;
while (ret.mPointer != NULL) {
ScriptPosition next;
next = getNextScriptString(ret, w);
if (!isInside && !strcmp(tWord, w)) {
return ret;
}
if (!strcmp("{", w)) isInside++;
if (!strcmp("}", w)) isInside--;
ret = next;
}
if (*tWord == '}') {
ret.mPointer = ret.mRegion.mEnd;
return ret;
}
logError("Unable to find word");
logString(ret.mPointer);
logString(tWord);
recoverFromError();
return ret;
}
static ScriptPosition findNextCharacterScriptOccurenceOnSameLevel(const ScriptPosition& tPos, char tChar) {
char w[100];
int isInside = 0;
auto ret = tPos;
while (ret.mPointer != NULL) {
ScriptPosition next;
next = getNextScriptRawCharacter(ret, w);
if (!isInside && tChar == *w) {
return ret;
}
if (!strcmp("{", w)) isInside++;
if (!strcmp("}", w)) isInside--;
ret = next;
}
logError("Unable to find char");
logString(ret.mPointer);
logInteger(tChar);
recoverFromError();
return ret;
}
static ScriptPosition findScriptRegionEnd(const ScriptPosition& tPos) {
return findNextScriptOccurenceOnSameLevel(tPos, "}");
}
static ScriptPosition findScriptRegionStart(const ScriptPosition& tPos, const char* tName) {
char w1[100];
char w2[100];
int isInside = 0;
auto ret = tPos;
while (ret.mPointer != NULL) {
ScriptPosition next;
ret = getNextScriptString(ret, w1);
next = getNextScriptString(ret, w2);
if (!isInside && !strcmp(tName, w1) && w2[0] == '{') {
if (next.mPointer == NULL) {
next.mPointer = (char*)((uintptr_t)ret.mRegion.mScript.mBuffer.mData + ret.mRegion.mScript.mBuffer.mLength - 1);
}
return next;
}
if (!strcmp("{", w1)) isInside++;
if (!strcmp("}", w1)) isInside--;
}
logError("Unable to find name");
logString(ret.mPointer);
logString(tName);
recoverFromError();
#if defined(DREAMCAST) || defined(__EMSCRIPTEN__)
return ret;
#endif
}
ScriptRegion getScriptRegion(const Script& tScript, const char* tName) {
ScriptRegion r = getWholeScriptRegion(tScript);
ScriptPosition p0 = getScriptRegionStart(r);
ScriptPosition p1 = findScriptRegionStart(p0, tName);
ScriptPosition p2 = findScriptRegionEnd(p1);
p2.mPointer--;
return makeScriptRegion(tScript, p1.mPointer, p2.mPointer);
}
static ScriptPosition makeScriptPosition(const ScriptRegion& tRegion, char* tPointer) {
ScriptPosition ret;
ret.mRegion = tRegion;
ret.mPointer = tPointer;
return ret;
}
ScriptPosition getScriptRegionStart(const ScriptRegion& tRegion) {
return makeScriptPosition(tRegion, tRegion.mStart);
}
ScriptPosition getPositionAfterScriptRegion(const ScriptRegion& tRegion, const ScriptRegion& tSkippedRegion) {
ScriptPosition ret = makeScriptPosition(tRegion, tSkippedRegion.mEnd);
ret = skipNextScriptString(ret);
return ret;
}
ScriptRegion getScriptRegionAtPosition(const ScriptPosition& tPos) {
ScriptPosition start = findNextScriptOccurenceOnSameLevel(tPos, "{");
start = skipNextScriptString(start);
ScriptPosition end = findNextScriptOccurenceOnSameLevel(start, "}");
return makeScriptRegion(tPos.mRegion.mScript, start.mPointer, end.mPointer);
}
int hasNextScriptWord(const ScriptPosition& tPos) {
return tPos.mPointer != NULL;
}
ScriptPosition getNextScriptInstruction(const ScriptPosition& tPos) {
if (tPos.mPointer == NULL) return tPos;
auto ret = findNextCharacterScriptOccurenceOnSameLevel(tPos, '\n');
ret.mPointer++;
ret = updateScriptPositionValidity(ret);
return ret;
}
}