comparison lwasm/pseudo.c @ 75:3d50022e16e7

Restored pragma cescapes functionality
author lost@l-w.ca
date Wed, 13 Apr 2011 19:01:53 -0600
parents 74b82202d355
children da74ccf4278c
comparison
equal deleted inserted replaced
74:e95eaf2f7fe0 75:3d50022e16e7
183 e = lwasm_fetch_expr(l, i); 183 e = lwasm_fetch_expr(l, i);
184 lwasm_emitexpr(l, e, 4); 184 lwasm_emitexpr(l, e, 4);
185 } 185 }
186 } 186 }
187 187
188 static int cstringlen(asmstate_t *as, line_t *ln, char **p, char delim)
189 {
190 int l = 0;
191 char *str = NULL;
192 int blen = 0;
193 int bsize = 0;
194
195 if (!(as -> pragmas & PRAGMA_CESCAPES))
196 {
197 for (; **p && **p != delim; (*p)++)
198 {
199 l++;
200 if (blen >= bsize)
201 {
202 str = lw_realloc(str, bsize + 32);
203 bsize++;
204 }
205 str[blen++] = **p;
206 }
207 }
208 else
209 {
210 while (**p && **p != delim)
211 {
212 int wch = **p;
213 if (**p == '\\')
214 {
215 /* escape sequence */
216
217 (*p)++;
218 if (!**p)
219 break;
220
221 switch (**p)
222 {
223 /* octal sequence or NUL */
224 /* skip the "0", then skip up to two more digits */
225 case '0':
226 case '1':
227 case '2':
228 case '3':
229 (*p)++;
230 wch -= 0x30;
231 if (**p >= '0' && **p <= '9')
232 {
233 wch *= 8;
234 wch += **p - 0x30;
235 (*p)++;
236 }
237 if (**p >= '0' && **p <= '9')
238 {
239 wch *= 8;
240 wch += **p -0x30;
241 }
242 break;
243
244 /* hexadecimal value */
245 case 'x':
246 (*p)++; // ignore "x"
247 wch = 0;
248 if (**p) // skip digit 1
249 {
250 wch = **p - 0x30;
251 if (wch > 9)
252 wch -= 7;
253 if (wch > 9)
254 wch -= 32;
255 (*p)++;
256 }
257 if (**p)
258 {
259 int i;
260 i = **p - 0x30;
261 if (i > 9)
262 i -= 7;
263 if (i > 9)
264 i -= 32;
265 wch = wch * 16 + i;
266 }
267 break;
268
269 case 'a':
270 wch = 7;
271 break;
272
273 case 'b':
274 wch = 8;
275 break;
276
277 case 't':
278 wch = 9;
279 break;
280
281 case 'n':
282 wch = 10;
283 break;
284
285 case 'v':
286 wch = 11;
287 break;
288
289 case 'f':
290 wch = 12;
291 break;
292
293 case 'r':
294 wch = 13;
295
296 /* everything else represents itself */
297 default:
298 break;
299 }
300 }
301 /* now "wch" is the character to write out */
302 l++;
303 (*p)++;
304 if (blen >= bsize)
305 {
306 str = lw_realloc(str, bsize + 32);
307 bsize += 32;
308 }
309 str[blen++] = wch;
310 }
311 }
312 /* do something with the string here */
313 /* l is the string length, str is the string itself */
314 ln -> lstr = str;
315 return l;
316 }
317
188 PARSEFUNC(pseudo_parse_fcc) 318 PARSEFUNC(pseudo_parse_fcc)
189 { 319 {
190 char delim; 320 char delim;
191 int i; 321 int i;
192 322
197 } 327 }
198 328
199 delim = **p; 329 delim = **p;
200 (*p)++; 330 (*p)++;
201 331
202 for (i = 0; (*p)[i] && (*p)[i] != delim; i++) 332
203 /* do nothing */ ; 333 i = cstringlen(as, l, p, delim);
204 334
205 if ((*p)[i] != delim) 335 if (**p != delim)
206 { 336 {
207 lwasm_register_error(as, l, "Bad operand"); 337 lwasm_register_error(as, l, "Bad operand");
208 return; 338 return;
209 } 339 }
210 340 (*p)++;
211 l -> lstr = lw_strndup(*p, i);
212 (*p) += i + 1;
213
214 l -> len = i; 341 l -> len = i;
215 } 342 }
216 343
217 EMITFUNC(pseudo_emit_fcc) 344 EMITFUNC(pseudo_emit_fcc)
218 { 345 {
234 } 361 }
235 362
236 delim = **p; 363 delim = **p;
237 (*p)++; 364 (*p)++;
238 365
239 for (i = 0; (*p)[i] && (*p)[i] != delim; i++) 366 i = cstringlen(as, l, p, delim);
240 /* do nothing */ ; 367
241 368 if (**p != delim)
242 if ((*p)[i] != delim)
243 { 369 {
244 lwasm_register_error(as, l, "Bad operand"); 370 lwasm_register_error(as, l, "Bad operand");
245 return; 371 return;
246 } 372 }
247 373 (*p)++;
248 l -> lstr = lw_strndup(*p, i);
249 (*p) += i + 1;
250
251 l -> len = i; 374 l -> len = i;
252 } 375 }
253 376
254 EMITFUNC(pseudo_emit_fcs) 377 EMITFUNC(pseudo_emit_fcs)
255 { 378 {
272 } 395 }
273 396
274 delim = **p; 397 delim = **p;
275 (*p)++; 398 (*p)++;
276 399
277 for (i = 0; (*p)[i] && (*p)[i] != delim; i++) 400 i = cstringlen(as, l, p, delim);
278 /* do nothing */ ; 401
279 402 if (**p != delim)
280 if ((*p)[i] != delim)
281 { 403 {
282 lwasm_register_error(as, l, "Bad operand"); 404 lwasm_register_error(as, l, "Bad operand");
283 return; 405 return;
284 } 406 }
285 407 (*p)++;
286 l -> lstr = lw_strndup(*p, i);
287 (*p) += i + 1;
288
289 l -> len = i + 1; 408 l -> len = i + 1;
290 } 409 }
291 410
292 EMITFUNC(pseudo_emit_fcn) 411 EMITFUNC(pseudo_emit_fcn)
293 { 412 {