source: dev/trunk/jenga/src/smoothie/LexicalAnalysis.cpp@ 297

Last change on this file since 297 was 211, checked in by dai_9181, 17 years ago
File size: 7.5 KB
Line 
1#include <jenga/include/smoothie/SmoothieException.h>
2#include <jenga/include/smoothie/LexicalAnalysis.h>
3#include <jenga/include/smoothie/BasicFixed.h>
4
5#include <windows.h>
6
7bool IsVariableTopChar(char c){
8 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||c=='_')
9 {
10 return true;
11 }
12 return false;
13}
14bool IsVariableChar(char c){
15 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||
16 c=='%'||c=='!'||c=='#'||c=='$'||
17 c=='_'||c=='.')
18 {
19 return true;
20 }
21 return false;
22}
23bool IsBlank(char c){
24 if(c==' '||c=='\t') return 1;
25 return 0;
26}
27int GetOneParameter(const char *Parameter,int pos,char *retAns){
28 int i,i2,i3,IsStr;
29 for(i=pos,i2=0,IsStr=0;;i++,i2++){
30 if(Parameter[i]=='\"') IsStr^=1;
31 else if(Parameter[i]=='('&&IsStr==0){
32 i3=GetStringInPare(retAns+i2,Parameter+i);
33 i+=i3-1;
34 i2+=i3-1;
35 continue;
36 }
37 else if(Parameter[i]=='['&&IsStr==0){
38 i3=GetStringInBracket(retAns+i2,Parameter+i);
39 i+=i3-1;
40 i2+=i3-1;
41 continue;
42 }
43 else if(Parameter[i]==','&&IsStr==0){
44 retAns[i2]=0;
45 i++;
46 break;
47 }
48
49 if(IsCommandDelimitation(Parameter[i])&&IsStr==0
50 || Parameter[i] == ')' && IsStr == 0 ){
51 retAns[i2]=0;
52 break;
53 }
54
55 retAns[i2]=Parameter[i];
56 }
57 return i;
58}
59int JumpOneParameter(char *Parameter,int i){
60 int i2,IsStr;
61 for(i2=0,IsStr=0;;i++,i2++){
62 if(Parameter[i]=='\"') IsStr^=1;
63 else if(Parameter[i]=='('&&IsStr==0){
64 i=JumpStringInPare(Parameter,i+1);
65 continue;
66 }
67 else if(Parameter[i]=='['&&IsStr==0){
68 i=JumpStringInBracket(Parameter,i+1);
69 continue;
70 }
71 else if(Parameter[i]==','&&IsStr==0){
72 i++;
73 break;
74 }
75 if(IsCommandDelimitation(Parameter[i])) break;
76 }
77 return i;
78}
79int GetStringInQuotation(char *buffer,char *ReadBuffer){
80 int i;
81
82 if(ReadBuffer[0]=='\"'){
83 buffer[0]=ReadBuffer[0];
84 i=1;
85 }
86 else i=0;
87
88 for(;;i++){
89 buffer[i]=ReadBuffer[i];
90 if(IsDBCSLeadByte(ReadBuffer[i])){
91 i++;
92 buffer[i]=ReadBuffer[i];
93 continue;
94 }
95 if(ReadBuffer[i]=='\"'){
96 i++;
97 buffer[i]=0;
98 break;
99 }
100 if(ReadBuffer[i]=='\0') return 0;
101 }
102 return i;
103}
104int GetStringInPare(char *buffer,const char *ReadBuffer){
105 int i,IsStr,PareNum;
106 for(i=0,IsStr=0,PareNum=0;;i++){
107 buffer[i]=ReadBuffer[i];
108 if(IsDBCSLeadByte(ReadBuffer[i])){
109 i++;
110 buffer[i]=ReadBuffer[i];
111 continue;
112 }
113 if(ReadBuffer[i]=='\"') IsStr^=1;
114 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
115 else if(ReadBuffer[i]==')'&&IsStr==0){
116 PareNum--;
117 if(PareNum==0){
118 i++;
119 buffer[i]=0;
120 break;
121 }
122 }
123 else if(ReadBuffer[i]=='\0') return 0;
124 }
125 return i;
126}
127int GetStringInBracket(char *buffer,const char *ReadBuffer){
128 int i,IsStr,PareNum;
129 for(i=0,IsStr=0,PareNum=0;;i++){
130 buffer[i]=ReadBuffer[i];
131 if(IsDBCSLeadByte(ReadBuffer[i])){
132 i++;
133 buffer[i]=ReadBuffer[i];
134 continue;
135 }
136 if(ReadBuffer[i]=='\"') IsStr^=1;
137 else if(ReadBuffer[i]=='['&&IsStr==0) PareNum++;
138 else if(ReadBuffer[i]==']'&&IsStr==0){
139 PareNum--;
140 if(PareNum==0){
141 i++;
142 buffer[i]=0;
143 break;
144 }
145 }
146 else if(ReadBuffer[i]=='\0') return 0;
147 }
148 return i;
149}
150int JumpStringInPare(const char *buffer,int pos){
151 int PareNum;
152 for(PareNum=1;;pos++){
153 if(buffer[pos]=='\"'){
154 for(pos++;;pos++){
155 if(buffer[pos]=='\"') break;
156 }
157 continue;
158 }
159 else if(buffer[pos]=='(') PareNum++;
160 else if(buffer[pos]==')'){
161 PareNum--;
162 if(PareNum==0) return pos;
163 }
164 else if(buffer[pos]=='\0') break;
165 }
166 return 0;
167}
168int JumpStringInBracket(const char *buffer,int pos){
169 int PareNum;
170 for(PareNum=1;;pos++){
171 if(buffer[pos]=='\"'){
172 for(pos++;;pos++){
173 if(buffer[pos]=='\"') break;
174 }
175 continue;
176 }
177 else if(buffer[pos]=='[') PareNum++;
178 else if(buffer[pos]==']'){
179 PareNum--;
180 if(PareNum==0) return pos;
181 }
182 else if(buffer[pos]=='\0') break;
183 }
184 return 0;
185}
186bool IsCommandDelimitation( char c ){
187 if( c == '\n' || c == ':' || c == '\0' ){
188 return true;
189 }
190
191 return false;
192}
193
194int GetStringInPare_RemovePare(char *buffer,const char *ReadBuffer){
195 int i,IsStr,PareNum;
196 for(i=0,IsStr=0,PareNum=1;;i++){
197 buffer[i]=ReadBuffer[i];
198 if(IsDBCSLeadByte(ReadBuffer[i])){
199 i++;
200 buffer[i]=ReadBuffer[i];
201 continue;
202 }
203 if(ReadBuffer[i]=='\"') IsStr^=1;
204 else if(ReadBuffer[i]=='('&&IsStr==0) PareNum++;
205 else if(ReadBuffer[i]==')'&&IsStr==0){
206 PareNum--;
207 if(PareNum==0){
208 buffer[i]=0;
209 break;
210 }
211 }
212 else if(ReadBuffer[i]=='\0') return 0;
213 }
214 return i;
215}
216
217bool SplitMemberName( const char *desc, char *object, char *member, ReferenceKind &refType ){
218 int lastIndex = -1;
219 for( int i=0; desc[i]; i++ ){
220 if( desc[i] == '(' ){
221 i=JumpStringInPare(desc,i+1);
222 continue;
223 }
224 else if( desc[i] == '[' ){
225 i=JumpStringInBracket(desc,i+1);
226 continue;
227 }
228 else if(desc[i]=='.'||(desc[i]==1&&desc[i+1]==ESC_PSMEM)){
229 lastIndex = i;
230 }
231 }
232 if( lastIndex == -1 ){
233 lstrcpy( member, desc );
234 return false;
235 }
236
237 if(desc[lastIndex]=='.'){
238 lstrcpy(member,desc+lastIndex+1);
239 refType = RefDot;
240 }
241 else{
242 lstrcpy(member,desc+lastIndex+2);
243 refType = RefPointer;
244 }
245
246 if( object ){
247 lstrcpy( object, desc );
248 object[lastIndex]=0;
249 }
250
251 return true;
252}
253bool SplitMemberName( const char *desc, char *object, char *member ){
254 ReferenceKind dummyRefType;
255 return SplitMemberName( desc, object, member, dummyRefType );
256}
257
258char *calcNames[255] = {
259 "xor",
260};
261void InitCalcNames()
262{
263 if( calcNames[CALC_XOR] )
264 {
265 return;
266 }
267
268 memset( calcNames, 0, 255 * sizeof(char *) );
269 calcNames[CALC_XOR] = "xor";
270 calcNames[CALC_OR] = "or";
271 calcNames[CALC_AND] = "and";
272 calcNames[CALC_NOT] = "Not";
273 calcNames[CALC_PE] = "<=";
274 calcNames[CALC_QE] = ">=";
275 calcNames[CALC_NOTEQUAL] = "<>";
276 calcNames[CALC_EQUAL] = "=(compare)";
277 calcNames[CALC_P] = "<";
278 calcNames[CALC_Q] = ">";
279 calcNames[CALC_SHL] = "<<";
280 calcNames[CALC_SHR] = ">>";
281 calcNames[CALC_ADDITION] = "+";
282 calcNames[CALC_SUBTRACTION] = "-";
283 calcNames[CALC_STRPLUS] = "&";
284 calcNames[CALC_MOD] = "mod";
285 calcNames[CALC_PRODUCT] = "*";
286 calcNames[CALC_QUOTIENT] = "/";
287 calcNames[CALC_INTQUOTIENT] = "\\";
288 calcNames[CALC_AS] = "As";
289 calcNames[CALC_BYVAL] = "ByVal";
290 calcNames[CALC_MINUSMARK] = "-(mark)";
291 calcNames[CALC_POWER] = "^";
292 calcNames[CALC_SUBSITUATION] = "=";
293 calcNames[CALC_ARRAY_GET] = "[]";
294 calcNames[CALC_ARRAY_SET] = "[]=";
295}
296void GetCalcName(int idCalc,char *name){
297 InitCalcNames();
298
299 if( calcNames[idCalc] == NULL )
300 {
301 SmoothieException::Throw();
302 }
303 lstrcpy( name, calcNames[idCalc] );
304}
305BYTE ToCalcId( const char *name )
306{
307 InitCalcNames();
308
309 for( int i=0; i<255; i++ )
310 {
311 if( calcNames[i] )
312 {
313 if( lstrcmp( name, calcNames[i] ) == 0 )
314 {
315 return i;
316 }
317 }
318 }
319 SmoothieException::Throw();
320 return 0;
321}
322
323std::string Operator_NaturalStringToCalcMarkString( const std::string &name )
324{
325 if( name[0] == 1 && name[1] == ESC_OPERATOR )
326 {
327 BYTE calcId = ToCalcId( name.c_str()+2 );
328 char temporary[255];
329 temporary[0] = name[0];
330 temporary[1] = name[1];
331 temporary[2] = calcId;
332 temporary[3] = 0;
333 return temporary;
334 }
335 return name;
336}
337std::string Operator_CalcMarkStringToNaturalString( const std::string &name )
338{
339 if( name[0] == 1 && name[1] == ESC_OPERATOR )
340 {
341 BYTE calcId = name[2];
342 char temporary[255], calcName[255];
343 GetCalcName( calcId, calcName );
344 temporary[0] = name[0];
345 temporary[1] = name[1];
346 lstrcpy( temporary+2, calcName );
347 return temporary;
348 }
349 return name;
350}
Note: See TracBrowser for help on using the repository browser.