1 #include "PathInterpreter.h"
2
3 // CPathScriptLoader
4 void CPathScriptLoader::LoadScript( const char *szFile )
5 {
6 HANDLE fp = CreateFile(
7 szFile,//这里输入需要复制的文件 src
8 GENERIC_READ | GENERIC_WRITE,
9 FILE_SHARE_READ,
10 NULL,
11 OPEN_EXISTING,
12 FILE_FLAG_SEQUENTIAL_SCAN,
13 NULL);
14 DWORD dwSize = GetFileSize( fp, 0 );
15
16 HANDLE hFileMapping = CreateFileMapping(
17 fp,
18 NULL,
19 PAGE_READWRITE,
20 0,//(DWORD)(dwBytesInBlock >> 16),
21 dwSize,//(DWORD)(dwBytesInBlock & 0x0000FFFF),
22 NULL);
23
24 LPVOID pbFile = (LPVOID)MapViewOfFile(
25 hFileMapping,
26 FILE_MAP_ALL_ACCESS,
27 0,
28 0,
29 dwSize);
30
31 Context=(LPTSTR)pbFile;
32 UnmapViewOfFile(pbFile);
33 CloseHandle(hFileMapping);
34 }
35
36 // ContexScan
37 ContexScan::ContexScan()
38 {
39
40 }
41
42 ContexScan::~ContexScan()
43 {
44
45 }
46
47 void ContexScan::SetExpression( const char *szExpr )
48 {
49 m_szExpr = (char *)szExpr;
50 m_pLastChar = (char *)m_szExpr;
51 m_pCurrChar = (char *)m_pLastChar;
52
53 if ( *m_pLastChar == ' ' || *m_pLastChar == '\t' || *m_pLastChar == 13 )
54 m_eState = NOPRINT;
55 else
56 m_eState = WORD;
57 }
58
59 bool ContexScan::SkipWord( const char *szWord )
60 {
61 if(NextWord())
62 {
63 if(strcmp(m_szCurrWord,szWord)==0)
64 return true;
65 printf( "Lexme ERROR: %s Excepted, but %s finded!\n", szWord, m_szCurrWord );
66 return false;
67 }
68 return false;
69 }
70
71 char * ContexScan::GetNextWord()
72 {
73 if(NextWord())
74 return m_szCurrWord;
75 return NULL;
76 }
77
78 bool ContexScan::NextWord()
79 {
80 while(1)
81 {
82 if ( *m_pCurrChar == '\0' )
83 return false;
84
85 ++m_pCurrChar;
86
87 if ( !isalpha(*m_pCurrChar) && !isdigit(*m_pCurrChar) && *m_pCurrChar != '.' && *m_pCurrChar != '-' )
88 {
89 if ( m_eState == WORD )
90 {
91 strncpy( m_szCurrWord, m_pLastChar, m_pCurrChar - m_pLastChar + 1 );
92 m_szCurrWord[ m_pCurrChar - m_pLastChar ] = '\0';
93 m_eState = NOPRINT;
94 break;
95 }
96 else
97 {
98 m_eState = NOPRINT;
99 }
100 }
101 else
102 {
103 if ( m_eState == NOPRINT )
104 {
105 m_pLastChar = m_pCurrChar;
106 m_eState = WORD;
107 }
108 }
109 }
110
111 return true;
112 }
113
114 void ContexScan::RebackWordByCount( int n )
115 {
116 if ( *m_pLastChar == ' ' || *m_pLastChar == '\t' || *m_pLastChar == 13 )
117 m_eState = NOPRINT;
118 else
119 m_eState = WORD;
120
121 for(int i=0;i<n;++i)
122 {
123 while(1)
124 {
125 --m_pCurrChar;
126 if ( !isalpha(*m_pCurrChar) && !isdigit(*m_pCurrChar) && *m_pCurrChar != '.' && *m_pCurrChar != '-' )
127 {
128 if ( m_eState == WORD )
129 {
130 m_pLastChar = m_pCurrChar + 1;
131 m_eState = NOPRINT;
132 break;
133 }
134 else
135 {
136 m_eState = NOPRINT;
137 }
138 }
139 else
140 {
141 m_eState = WORD;
142 }
143 }
144 }
145 }
146
147 void ContexScan::RebackNearestWordByName( const char *szWord )
148 {
149 if ( *m_pLastChar == ' ' || *m_pLastChar == '\t' || *m_pLastChar == 13 )
150 m_eState = NOPRINT;
151 else
152 m_eState = WORD;
153
154 while(1)
155 {
156 --m_pCurrChar;
157 if ( !isalpha(*m_pCurrChar) && !isdigit(*m_pCurrChar) && *m_pCurrChar != '.' && *m_pCurrChar != '-' )
158 {
159 if ( m_eState == WORD )
160 {
161 m_pLastChar = m_pCurrChar + 1;
162 m_eState = NOPRINT;
163 if(strncmp(m_pLastChar,szWord,strlen(szWord))==0)
164 break;
165 }
166 else
167 {
168 m_eState = NOPRINT;
169 }
170 }
171 else
172 {
173 m_eState = WORD;
174 }
175 }
176 }
177
178 // AbstractNode
179 AbstractNode::AbstractNode( ContexScan& contex ) : m_RefContexScan(contex)
180 {
181 }
182
183 AbstractNode::~AbstractNode()
184 {
185 }
186
187 // ProgramNode
188 ProgramNode::ProgramNode( ContexScan& contex ) : AbstractNode(contex)
189 {
190 }
191
192 ProgramNode::~ProgramNode()
193 {
194 }
195
196 void ProgramNode::Parse()
197 {
198 m_RefContexScan.SkipWord( "GO" );
199 if(CPathInterpret::m_Mode==CPathInterpret::DEBUG)
200 printf("ProgramNode Node!\n");
201 AbstractNode *pCmdListNode = new CommandListNode( m_RefContexScan );
202 pCmdListNode->Parse();
203 delete pCmdListNode;
204 }
205
206 // CommandListNode
207 CommandListNode::CommandListNode( ContexScan& contex ) : AbstractNode(contex)
208 {
209 }
210
211 CommandListNode::~CommandListNode()
212 {
213 }
214
215 void CommandListNode::Parse()
216 {
217 if(CPathInterpret::m_Mode==CPathInterpret::DEBUG)
218 printf("CommandListNode Node!\n");
219 while(1)
220 {
221 char *szNext=m_RefContexScan.GetNextWord();
222 if(strcmp(szNext,"END")==0)
223 break;
224 m_RefContexScan.RebackWordByCount(1);
225 AbstractNode *pCmdNode = new CommandNode( m_RefContexScan );
226 pCmdNode->Parse();
227 delete pCmdNode;
228 }
229 }
230
231 // CommandNode
232 CommandNode::CommandNode( ContexScan& contex ) : AbstractNode(contex)
233 {
234 }
235
236 CommandNode::~CommandNode()
237 {
238 }
239
240 void CommandNode::Parse()
241 {
242 if(CPathInterpret::m_Mode==CPathInterpret::DEBUG)
243 printf("CommandNode Node!\n");
244 char *szWord = m_RefContexScan.GetNextWord();
245 if(strcmp(szWord,"BEGIN")==0) // BEGIN --- END
246 {
247 AbstractNode *pBlockNode = new BlockNode( m_RefContexScan );
248 pBlockNode->Parse();
249 delete pBlockNode;
250 }
251 else if(strcmp(szWord,"REPEAT")==0) // REPEAT NUMBER
252 {
253 AbstractNode *pRepeatNode = new RepeatNode( m_RefContexScan );
254 pRepeatNode->Parse();
255 delete pRepeatNode;
256 }
257 else
258 {
259 printf( "Syntax ERROR!\n" );
260 }
261 }
262
263 // RepeatNode
264 RepeatNode::RepeatNode( ContexScan& contex ) : AbstractNode(contex)
265 {
266 }
267
268 RepeatNode::~RepeatNode()
269 {
270 }
271
272 void RepeatNode::Parse()
273 {
274 if(CPathInterpret::m_Mode==CPathInterpret::DEBUG)
275 printf("RepeatNode Node!\n");
276 int times = atoi( m_RefContexScan.GetNextWord() );
277 for(int i=0;i<times;++i)
278 {
279 AbstractNode *pCmdListNode = new CommandListNode( m_RefContexScan );
280 pCmdListNode->Parse();
281 delete pCmdListNode;
282
283 if(i==times-1)
284 break;
285
286 m_RefContexScan.RebackNearestWordByName( "REPEAT" );
287 m_RefContexScan.NextWord();
288 m_RefContexScan.NextWord();
289 }
290 }
291
292 // BlockNode
293 BlockNode::BlockNode( ContexScan& contex ) : AbstractNode(contex)
294 {
295 }
296
297 BlockNode::~BlockNode()
298 {
299 }
300
301 void BlockNode::Parse()
302 {
303 if(CPathInterpret::m_Mode==CPathInterpret::DEBUG)
304 printf("BlockNode Node!\n");
305 AbstractNode *pPrimNode = new PrimitiveNode( m_RefContexScan );
306 pPrimNode->Parse();
307 delete pPrimNode;
308 m_RefContexScan.SkipWord( "END" );
309 }
310
311 // PrimitiveNode
312 PrimitiveNode::PrimitiveNode( ContexScan& contex ) : AbstractNode(contex)
313 {
314 }
315
316 PrimitiveNode::~PrimitiveNode()
317 {
318 }
319
320 void PrimitiveNode::Parse()
321 {
322 if(CPathInterpret::m_Mode==CPathInterpret::DEBUG)
323 printf("PrimitiveNode Node!\n");
324 printf("执行一个运动关键点\n");
325 printf("当前速度X分量为: %s\n",m_RefContexScan.GetNextWord());
326 printf("当前速度Y分量为: %s\n",m_RefContexScan.GetNextWord());
327 printf("持续时间为: %s毫秒\n\n",m_RefContexScan.GetNextWord());
328 }
329
330 // CPathInterpret
331 CPathInterpret::InterpretMode CPathInterpret::m_Mode = NDEBUG;
332
333 void CPathInterpret::LoadScript( const char *szFile )
334 {
335 m_ScriptLoader.LoadScript( szFile );
336 m_ContexScaner.SetExpression( m_ScriptLoader.Context.c_str() );
337 }
338
339 void CPathInterpret::Interpret( InterpretMode mode )
340 {
341 m_Mode = mode;
342
343 AbstractNode *pRoot = new ProgramNode( m_ContexScaner );
344 pRoot->Parse();
345 delete pRoot;
346 }
347
348