Mercurial > hg > index.cgi
comparison lwasm/pass1.c @ 399:6153cb49403c
Initial commit of pragma newsource
pragma newsource enables a source code variant as follows:
1. no line numbers
2. no implied comments at the end of lines
3. all comments must be introduced by a comment character
4. spaces are allowed in operands
(4) is not quite complete. This commit handles "operandless" instructions
(anything where the parser calls skip_operand()) and expression parsing.
author | William Astle <lost@l-w.ca> |
---|---|
date | Tue, 13 Oct 2015 23:38:02 -0600 |
parents | 4fd16faa4d93 |
children | b1adf549d181 |
comparison
equal
deleted
inserted
replaced
398:4cf907aa634c | 399:6153cb49403c |
---|---|
36 int add_macro_line(asmstate_t *as, char *optr); | 36 int add_macro_line(asmstate_t *as, char *optr); |
37 | 37 |
38 /* | 38 /* |
39 pass 1: parse the lines | 39 pass 1: parse the lines |
40 | 40 |
41 line format: | 41 line format if PRAGMA_NEWSOURCE is not in force: |
42 | 42 |
43 [<symbol>] <opcode> <operand>[ <comment>] | 43 [<symbol>] <opcode> <operand>[ <comment>] |
44 | 44 |
45 If <symbol> is followed by a :, whitespace may precede the symbol | 45 If <symbol> is followed by a :, whitespace may precede the symbol |
46 | 46 |
47 A line may optionally start with a number which must not be preceded by | 47 A line may optionally start with a number which must not be preceded by |
48 white space and must be followed by a single whitespace character. After | 48 white space and must be followed by a single whitespace character. After |
49 that whitespace character, the line is parsed as if it had no line number. | 49 that whitespace character, the line is parsed as if it had no line number. |
50 | |
51 Also, no spaces are permitted within <operand>. | |
52 | |
53 With PRAGMA_NEWSOURCE in effect, line numbers are not allowed and there | |
54 is no automatic comment at the end of each line. All comments must be | |
55 introduced with the comment character. This allows the parser to handle | |
56 spaces in operands unambiguously so in this mode, spaces are permitted | |
57 within operands. | |
50 | 58 |
51 */ | 59 */ |
52 void do_pass1(asmstate_t *as) | 60 void do_pass1(asmstate_t *as) |
53 { | 61 { |
54 char *line; | 62 char *line; |
172 as -> context = lwasm_next_context(as); | 180 as -> context = lwasm_next_context(as); |
173 goto nextline; | 181 goto nextline; |
174 } | 182 } |
175 | 183 |
176 // skip comments | 184 // skip comments |
177 // commends do not create a context break | 185 // comments do not create a context break |
178 if (*line == '*' || *line == ';' || *line == '#') | 186 if (*line == '*' || *line == ';' || *line == '#') |
179 goto nextline; | 187 goto nextline; |
180 | 188 |
181 p1 = line; | 189 p1 = line; |
182 if (isdigit(*p1)) | 190 if (isdigit(*p1) && !CURPRAGMA(cl, PRAGMA_NEWSOURCE)) |
183 { | 191 { |
184 // skip line number | 192 // skip line number |
185 while (*p1 && isdigit(*p1)) | 193 while (*p1 && isdigit(*p1)) |
186 p1++; | 194 p1++; |
187 if (!*p1 && !isspace(*p1)) | 195 if (!*p1 && !isspace(*p1)) |
210 stspace = 1; | 218 stspace = 1; |
211 } | 219 } |
212 else | 220 else |
213 stspace = 0; | 221 stspace = 0; |
214 | 222 |
215 // if (*p1 == '*' || *p1 == ';' || *p1 == '#') | |
216 // goto nextline; | |
217 if (!*p1) | 223 if (!*p1) |
218 { | 224 { |
219 // nothing but whitespace - context break | 225 // nothing but whitespace - context break |
220 as -> context = lwasm_next_context(as); | 226 as -> context = lwasm_next_context(as); |
221 goto nextline; | 227 goto nextline; |
233 sym = lw_strndup(tok, p1 - tok); | 239 sym = lw_strndup(tok, p1 - tok); |
234 if (*p1 == ':') | 240 if (*p1 == ':') |
235 p1++; | 241 p1++; |
236 for (; *p1 && isspace(*p1); p1++) | 242 for (; *p1 && isspace(*p1); p1++) |
237 /* do nothing */ ; | 243 /* do nothing */ ; |
238 | 244 |
239 if (*p1 == '=') | 245 if (*p1 == '=') |
240 { | 246 { |
241 tok = p1++; | 247 tok = p1++; |
242 } | 248 } |
243 else | 249 else |
369 if (cl -> len == 0) | 375 if (cl -> len == 0) |
370 cl -> len = cl -> dlen; | 376 cl -> len = cl -> dlen; |
371 else | 377 else |
372 cl -> dlen = cl -> len; | 378 cl -> dlen = cl -> len; |
373 } | 379 } |
374 if (*p1 && !isspace(*p1) && !(cl -> err)) | 380 if (!CURPRAGMA(cl, PRAGMA_NEWSOURCE)) |
375 { | 381 { |
376 // flag bad operand error | 382 if (*p1 && !isspace(*p1) && !(cl -> err)) |
377 lwasm_register_error2(as, cl, E_OPERAND_BAD, "(%s)", p1); | 383 { |
384 // flag bad operand error | |
385 lwasm_register_error2(as, cl, E_OPERAND_BAD, "(%s)", p1); | |
386 } | |
387 } | |
388 else | |
389 { | |
390 lwasm_skip_to_next_token(cl, &p1); | |
391 /* if we did not hit the end of the line and we aren't at a comment character, error out */ | |
392 if (*p1 && *p1 != ';' && *p1 != '#' && *p1 != ';') | |
393 { | |
394 // flag bad operand error | |
395 lwasm_register_error2(as, cl, E_OPERAND_BAD, "%s", p1); | |
396 } | |
378 } | 397 } |
379 | 398 |
380 /* do a reduction on the line expressions to avoid carrying excessive expression baggage if not needed */ | 399 /* do a reduction on the line expressions to avoid carrying excessive expression baggage if not needed */ |
381 lwasm_reduce_line_exprs(cl); | 400 lwasm_reduce_line_exprs(cl); |
382 } | 401 } |