/
interpreter.cpp
256 lines (226 loc) · 6.34 KB
/
interpreter.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
243
244
245
246
247
248
249
250
251
252
253
254
#include "interpreter.h"
using namespace std;
bool CScript::Load(char* fname)
{
ifstream f;
f.open(fname,ios::in|ios::ate); //open and set initial pos at eof
if(!f.is_open())
{
cerr << "Error: script file " << fname << " not found!" << endl;
return false;
}
long size = f.tellg(); //get current numerical location(file size)
f.seekg (0, ios::beg);
char *buffer = new char [size]; //make buffer of that size
f.read (buffer, size); //copy whole script into memory
f.close();
if(!init(buffer))
{
cerr << "Error: script file " << fname << " could not be parsed. Check your syntax." << endl;
return false;
}
if(children!=NULL) //safety check
{
}
else //if children was NULL then no script was actually loaded at all for that file
{
cerr << "No script found for at file " << fname << "." << endl;
return false;
}
cerr << "Script " << fname << " successfully loaded." << endl;
return true; //everything went through alright
}
char* cursePastBrackets(char* string)
{
char* tmp=string;
while(*tmp!='{') //make sure we are actually started at the bracket
{
tmp++;
if(*tmp=='\0')
return tmp;
}
int bracketCount=1;
tmp++;
if(*tmp=='\0') //standard NULL check on tmp doesnt work
return tmp; //will check against *tmp returning NULL would segfault
while(bracketCount>0)
{
if(*tmp=='{')
{
bracketCount++;
}
else if(*tmp=='}')
{
bracketCount--;
}
tmp++;
if(*tmp=='\0') //premature eos(probably missing bracket)
{
return tmp; //will check against *tmp returning NULL would segfault
}
}
return tmp;
}
char* safeGoto(char s, char* string)
{
char* tmp=string;
while(*tmp!=s)
{
tmp++;
if(*tmp=='\0') //no end to tag flag an error
return tmp;//will check against *tmp returning NULL would segfault
}
return tmp;
}
bool CScript::init(char* scr)
{
char* tmp = scr;
while(*tmp=='\n' || *tmp==' ' ||
*tmp=='\t' || *tmp==',') //whitespace trim
{
tmp++;
if(*tmp=='\0') //whitespace went straight to eos
return false;
}
if (*tmp!='<') //if we didn't get a tag we got an argument save as arg and exit
{
children=NULL;
value=scr; //get rid of | | for strings (DO LATER)
return true;
}
else //otherwise we parse tag and all it's arguments(passing downwards)
{
children = new LinkedList<CScript>();
tmp = safeGoto('>',tmp);
if(*tmp=='\0')
return false;
char* buffer = new char[tmp-scr+2];
memcpy(buffer,scr,tmp-scr+2);
buffer[tmp-scr+1]='\0';
value=buffer; //tag should now be <something>
//cerr << value << endl;
//get beginning { skip whitespace and other stuff
tmp = safeGoto('{',tmp);
if(*tmp=='\0')
return false;
tmp++; //go past {
if(*tmp=='\0') //nothing after { (segfault protection)
return false;
//start getting child arguments past '{' (if there are any)
bool notDone=true;
while(notDone)
{
while(*tmp=='\n' || *tmp==' ' ||
*tmp=='\t' || *tmp==',') //whitespace trim
{
tmp++;
if(*tmp=='\0') //whitespace went straight to eos
return false;
}
if (*tmp=='<') //make a new recursing child
{
char* startPosition = tmp;
tmp = safeGoto('>',tmp);
if(*tmp=='\0')
return false;
tmp = safeGoto('{',tmp);
if(*tmp=='\0') //never found { flag an error
return false;
tmp = cursePastBrackets(tmp);
if(*tmp=='\0')
return false;
char* buffer=new char[tmp-startPosition+2];
memcpy(buffer,startPosition,tmp-startPosition+2);
buffer[tmp-startPosition+1]='\0';
CScript *script = new CScript();
children->insert(script);
script->init(buffer); //new script will span out here
}//end of current child is recursive if block
else //if child wasn't recursive then it was just an argument
{
char* startPosition = tmp;
//will catch empty tags here(ie <destroy> { }
while(*tmp!='}' && *tmp!=',')
{
tmp++;
if(*tmp=='\0') //no end to argument flag error
return false;
}
char* buffer=new char[tmp-startPosition+1];
//no +1 because we don't want the } or ,
memcpy(buffer,startPosition,tmp-startPosition+1);
//but it does actually copy and the terminator doesn't so..
buffer[tmp-startPosition]='\0';//terminate it
CScript *script = new CScript();
children->insert(script); //add to list of children
//std::cerr << "\t" << buffer << std::endl;
script->init(buffer); //script will store argument
}//end of current child was non recursive handle block
while(*tmp=='\n' || *tmp==' ' ||
*tmp=='\t' || *tmp==',') //whitespace trim
{
tmp++;
if(*tmp=='\0') //whitespace went straight to eos
return false;
}
if(*tmp=='}') //found the end
notDone=false;
}//end of looping of argument parsing
return true;
}//end of main else block(handling a tag script)
return false; //should never be hit
}
CScript* CScript::findChild(char* childTag,bool reset)
{
if(children!=NULL) //safety check
{
if(reset)
children->beginning();
do
{
if((children->display())->value!=NULL)
{
if(strcmp((children->display())->value,childTag)==0) //see if config's first undertag is includes
{
return children->display();
}
}
}while(children->next()); //will drop through if nothing was found
}
return NULL; //failed
}
/*
This code was originally in the Load function but on retrospect it probably should be somewhere else
and rewritten differently keep it around though cause it shows the format for using findChild
children->beginning();
//test
CScript *ptr = findChild("<includes>"); //find include block
if(ptr!=NULL)
{
CScript *ptr = children->display(); //points to includes now
ptr->children->beginning();
CScript *ptrChild=NULL;
char* includeFname;
do
{
ptrChild = ptr->findChild("<include>");
if(ptrChild!=NULL)
{
includeFname = (ptrChild->children->display())->value;
cerr << "Found include file: " << includeFname
<< endl;
CScript *include = new CScript();
if(include->Load(includeFname))
{//if the load went through replace the filename with the actual script
CScript *oldPtr = ptrChild->children->replace(include);
delete oldPtr; //prevent mem leak
}
else //one of the scripts failed to load
{
cerr << "Scripts not loaded" << endl;
return false;
}
}
}while(ptrChild!=NULL && ptr->children->next());
}
*/