commit abd6a3a5da3341d761c73daf17490ea8aee0ae79
parent 55f448fb773205e5b4eeba5a130e812c7a0c1371
Author: Erik Osheim <d_m@plastic-idolatry.com>
Date: Tue, 28 Dec 2021 21:38:05 -0500
Support nested comments in uxnasm.
Previously, code like this would fail with an error
about an unrecognized ) token:
( this is a ( nested ) comment )
With this patch, the above code will now work.
Relatedly, it was previously possible to write code
that compiled but was confusing:
(open parenthesis should have a space )
( in this case the ADD2 will be ignored )ADD2
( this comment with ( would have been fine )
With this commit, the first example will emit a warning
but continue to work as intended. The second and third
examples will continue searching for a matching ) token,
which due to the new nested coment behavior will probably
mean the rest of the file gets commented out.
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/src/uxnasm.c b/src/uxnasm.c
@@ -243,8 +243,13 @@ parse(char *w, FILE *f)
return error("Invalid token", w);
switch(w[0]) {
case '(': /* comment */
- while(fscanf(f, "%63s", word) == 1)
- if(word[0] == ')') break;
+ if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
+ i = 1; /* track nested comment depth */
+ while(fscanf(f, "%63s", word) == 1) {
+ if(slen(word) != 1) continue;
+ else if(word[0] == '(') i++;
+ else if(word[0] == ')' && --i < 1) break;
+ }
break;
case '~': /* include */
if(!doinclude(w + 1))