From abd6a3a5da3341d761c73daf17490ea8aee0ae79 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Tue, 28 Dec 2021 21:38:05 -0500 Subject: [PATCH] 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. --- src/uxnasm.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/uxnasm.c b/src/uxnasm.c index 7c0f6c5..d7b2229 100644 --- 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))