Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 379 lines (351 sloc) 7.777 kb
a9461cd initializing repo from version 0.10.2 minus the example files
Julian authored
1 // keyword.cpp
2 // Revision 7-feb-2005
3
4 #include "keyword.h"
5 #include "util.h"
6 #include "sysvar.h"
7 #include "trace.h"
8
9 #include <iostream>
10 #include <set>
11 #include <algorithm>
12 #include <iterator>
13 #include <functional>
14 #include <cctype>
15
16 // I don't understand why, but with this using the older version
17 // of C++ Builder fails.
18 #if __BORLANDC__ >= 0x0560
19 using std::toupper;
20 #endif
21
22 namespace sysvar= blassic::sysvar;
23 using util::dim_array;
24
25
26 namespace {
27
28 struct keycode {
29 std::string key;
30 BlCode code;
31 keycode (const char * str, BlCode c) :
32 key (str), code (c)
33 { }
34 };
35
36 // Can't declare const on Borland or fail at instantiate find_if,
37 // don't know why.
38 #ifdef __BORLANDC__
39 #define const_keycode keycode
40 #else
41 #define const_keycode const keycode
42 #endif
43
44 #define m_keycode(n) keycode (#n, key ## n)
45
46 #define m_keycode_s(n) keycode (#n "$", key ## n ## _S)
47
48 const_keycode table []= {
49 m_keycode (END),
50 m_keycode (LIST),
51 m_keycode (REM),
52 m_keycode (LOAD),
53 m_keycode (SAVE),
54 m_keycode (EXIT),
55 m_keycode (NEW),
56 m_keycode (RUN),
57 m_keycode (PRINT),
58 m_keycode (FOR),
59 m_keycode (NEXT),
60 m_keycode (TO),
61 m_keycode (STEP),
62 m_keycode (IF),
63 m_keycode (THEN),
64 m_keycode (ELSE),
65 m_keycode (TRON),
66 m_keycode (TROFF),
67 m_keycode (LET),
68 m_keycode (GOTO),
69 m_keycode (STOP),
70 m_keycode (CONT),
71 m_keycode (CLEAR),
72 m_keycode (GOSUB),
73 m_keycode (RETURN),
74 m_keycode (POKE),
75 m_keycode (DATA),
76 m_keycode (READ),
77 m_keycode (RESTORE),
78 m_keycode (INPUT),
79 m_keycode (LINE),
80 m_keycode (RANDOMIZE),
81 m_keycode (PLEASE),
82 m_keycode (AUTO),
83 m_keycode (DIM),
84 m_keycode (SYSTEM),
85 m_keycode (ON),
86 m_keycode (ERROR),
87 m_keycode (OPEN),
88 m_keycode (CLOSE),
89 m_keycode (OUTPUT),
90 m_keycode (AS),
91 m_keycode (LOCATE),
92 m_keycode (CLS),
93 m_keycode (APPEND),
94 m_keycode (WRITE),
95 m_keycode (MODE),
96 m_keycode (MOVE),
97 m_keycode (COLOR),
98 m_keycode (GET),
99 m_keycode (LABEL),
100 m_keycode (DELIMITER),
101 m_keycode (REPEAT),
102 m_keycode (UNTIL),
103 m_keycode (WHILE),
104 m_keycode (WEND),
105 m_keycode (PLOT),
106 m_keycode (POPEN),
107 m_keycode (RESUME),
108 m_keycode (DELETE),
109 m_keycode (LOCAL),
110 m_keycode (RANDOM),
111 m_keycode (PUT),
112 m_keycode (FIELD),
113 m_keycode (LSET),
114 m_keycode (RSET),
115 m_keycode (SOCKET),
116 m_keycode (DRAW),
117 m_keycode (DEF),
118 m_keycode (FN),
119 m_keycode (ERASE),
120 m_keycode (SWAP),
121 m_keycode (SYMBOL),
122 m_keycode (ZONE),
123 m_keycode (POP),
124 m_keycode (NAME),
125 m_keycode (KILL),
126 m_keycode (FILES),
127 m_keycode (PAPER),
128 m_keycode (PEN),
129 m_keycode (SHELL),
130 m_keycode (MERGE),
131 m_keycode (CHDIR),
132 m_keycode (MKDIR),
133 m_keycode (RMDIR),
134 m_keycode (BREAK),
135 m_keycode (SYNCHRONIZE),
136 m_keycode (PAUSE),
137 m_keycode (CHAIN),
138 m_keycode (STR),
139 m_keycode (REAL),
140 m_keycode (ENVIRON),
141 m_keycode (EDIT),
142 m_keycode (DRAWR),
143 m_keycode (PLOTR),
144 m_keycode (MOVER),
145 m_keycode (POKE16),
146 m_keycode (POKE32),
147 m_keycode (RENUM),
148 m_keycode (CIRCLE),
149 m_keycode (MASK),
150 m_keycode (WINDOW),
151 m_keycode (GRAPHICS),
152 m_keycode (AFTER),
153 m_keycode (BEEP),
154 m_keycode (DEFINT),
155 m_keycode (DEFSTR),
156 m_keycode (DEFREAL),
157 m_keycode (DEFSNG),
158 m_keycode (DEFDBL),
159 m_keycode (INK),
160 m_keycode (SET_TITLE),
161 m_keycode (TAG),
162 m_keycode (TAGOFF),
163 m_keycode (ORIGIN),
164 m_keycode (DEG),
165 m_keycode (RAD),
166 m_keycode (INVERSE),
167 m_keycode (IF_DEBUG),
168 m_keycode (LPRINT),
169 m_keycode (LLIST),
170 m_keycode (WIDTH),
171 m_keycode (BRIGHT),
172 m_keycode (BINARY),
173 m_keycode (DRAWARC),
174 m_keycode (PULL),
175 m_keycode (PAINT),
176 m_keycode (FREE_MEMORY),
177 m_keycode (SCROLL),
178 m_keycode (ZX_PLOT),
179 m_keycode (ZX_UNPLOT),
180
181 m_keycode_s (MID),
182 m_keycode_s (LEFT),
183 m_keycode_s (RIGHT),
184 m_keycode_s (CHR),
185 m_keycode_s (ENVIRON),
186 m_keycode_s (STRING),
187 m_keycode_s (OSFAMILY),
188 m_keycode_s (HEX),
189 m_keycode_s (SPACE),
190 m_keycode_s (UPPER),
191 m_keycode_s (LOWER),
192 m_keycode_s (STR),
193 m_keycode_s (OCT),
194 m_keycode_s (BIN),
195 m_keycode_s (INKEY),
196 m_keycode_s (PROGRAMARG),
197 m_keycode_s (DATE),
198 m_keycode_s (TIME),
199 m_keycode_s (INPUT),
200 m_keycode_s (MKI),
201 m_keycode_s (MKS),
202 m_keycode_s (MKD),
203 m_keycode_s (MKL),
204 m_keycode_s (TRIM),
205 m_keycode_s (LTRIM),
206 m_keycode_s (RTRIM),
207 m_keycode_s (OSNAME),
208 m_keycode_s (FINDFIRST),
209 m_keycode_s (FINDNEXT),
210 m_keycode_s (COPYCHR),
211 m_keycode_s (STRERR),
212 m_keycode_s (DEC),
213 m_keycode_s (VAL),
214 m_keycode_s (SCREEN),
215 m_keycode_s (MKSMBF),
216 m_keycode_s (MKDMBF),
217 m_keycode_s (REGEXP_REPLACE),
218 m_keycode_s (UCASE),
219 m_keycode_s (LCASE),
220
221 m_keycode (ASC),
222 m_keycode (LEN),
223 m_keycode (PEEK),
224 m_keycode (PROGRAMPTR),
225 m_keycode (RND),
226 m_keycode (INT),
227 m_keycode (SIN),
228 m_keycode (COS),
229 m_keycode (PI),
230 m_keycode (TAN),
231 m_keycode (SQR),
232 m_keycode (ASIN),
233 m_keycode (ACOS),
234 m_keycode (INSTR),
235 m_keycode (ATAN),
236 m_keycode (ABS),
237 m_keycode (USR),
238 m_keycode (VAL),
239 m_keycode (EOF),
240 m_keycode (VARPTR),
241 m_keycode (SYSVARPTR),
242 m_keycode (SGN),
243 m_keycode (LOG),
244 m_keycode (LOG10),
245 m_keycode (EXP),
246 m_keycode (TIME),
247 m_keycode (ERR),
248 m_keycode (ERL),
249 m_keycode (CVI),
250 m_keycode (CVS),
251 m_keycode (CVD),
252 m_keycode (CVL),
253 m_keycode (MIN),
254 m_keycode (MAX),
255 m_keycode (CINT),
256 m_keycode (FIX),
257 m_keycode (XMOUSE),
258 m_keycode (YMOUSE),
259 m_keycode (XPOS),
260 m_keycode (YPOS),
261 m_keycode (PEEK16),
262 m_keycode (PEEK32),
263 m_keycode (RINSTR),
264 m_keycode (FIND_FIRST_OF),
265 m_keycode (FIND_LAST_OF),
266 m_keycode (FIND_FIRST_NOT_OF),
267 m_keycode (FIND_LAST_NOT_OF),
268 m_keycode (SINH),
269 m_keycode (COSH),
270 m_keycode (TANH),
271 m_keycode (ASINH),
272 m_keycode (ACOSH),
273 m_keycode (ATANH),
274 m_keycode (ATAN2),
275 m_keycode (TEST),
276 m_keycode (TESTR),
277 m_keycode (POS),
278 m_keycode (VPOS),
279 m_keycode (LOF),
280 m_keycode (FREEFILE),
281 m_keycode (INKEY),
282 m_keycode (ROUND),
283 m_keycode (CVSMBF),
284 m_keycode (CVDMBF),
285 m_keycode (REGEXP_INSTR),
286 m_keycode (ALLOC_MEMORY),
287 m_keycode (LOC),
288
289 m_keycode (NOT),
290 m_keycode (OR),
291 m_keycode (AND),
292 m_keycode (TAB),
293 m_keycode (SPC),
294 m_keycode (AT),
295 m_keycode (XOR),
296 m_keycode (MOD),
297 m_keycode (USING),
298
299 keycode ("<>", keyDISTINCT),
300 keycode ("<=", keyMINOREQUAL),
301 keycode (">=", keyGREATEREQUAL),
302 keycode ("=<", keyEQUALMINOR),
303 keycode ("=>", keyEQUALGREATER),
304 keycode ("><", keyGREATERMINOR),
305
306 // table_end points here, then if find_if (table, table_end, ...)
307 // fails the result is:
308 keycode ("???", 0)
309 };
310
311 const_keycode * table_end= table + dim_array (table) - 1;
312
313 class key_is : public std::unary_function <keycode, bool> {
314 public:
315 key_is (const std::string & str) : str (str)
316 { }
317 bool operator () (const keycode & k) const
318 { return k.key == str; }
319 private:
320 const std::string & str;
321 };
322
323 class code_is : public std::unary_function <keycode, bool> {
324 public:
325 code_is (BlCode code) : code (code)
326 { }
327 bool operator () (const keycode & k) const
328 { return k.code == code; }
329 private:
330 BlCode code;
331 };
332
333 inline std::string stringupper (const std::string & str)
334 {
335 std::string u (str.size (), 0);
336 std::transform (str.begin (), str.end (), u.begin (), toupper);
337 return u;
338 }
339
340 std::set <std::string> exclude;
341
342 } // namespace
343
344 void excludekeyword (const std::string & str)
345 {
346 TRACEFUNC (tr, "excludekeyword");
347
348 std::string stru= stringupper (str);
349 if (find_if (table, table_end, key_is (stru) ) != table_end)
350 {
351 exclude.insert (stru);
352 TRMESSAGE (tr, std::string ("Excluding ") + stru);
353 }
354 }
355
356 BlCode keyword (const std::string & str)
357 {
358 std::string stru= stringupper (str);
359 BlCode code= std::find_if (table, table_end, key_is (stru) )->code;
360 if (code != 0)
361 if (exclude.find (stru) != exclude.end () )
362 return 0;
363 return code;
364 }
365
366 std::string decodekeyword (BlCode s)
367 {
368 if (s == keyGOTO || s == keyGOSUB)
369 {
370 if (sysvar::hasFlags2 (sysvar::SeparatedGoto) )
371 return (s == keyGOTO) ?
372 "GO TO" :
373 "GO SUB";
374 }
375 return std::find_if (table, table_end, code_is (s) )->key;
376 }
377
378 // Fin de keyword.cpp
Something went wrong with that request. Please try again.