From a197c24b2a55e16aa5cbe089468d2885b150cc72 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Tue, 26 Mar 2024 13:35:37 -0700 Subject: [PATCH] Abstracted walk-comment --- etc/hello.tal | 6 ++++-- src/uxnasm.c | 33 ++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/etc/hello.tal b/etc/hello.tal index a77fc39..2379bcc 100644 --- a/etc/hello.tal +++ b/etc/hello.tal @@ -1,5 +1,7 @@ ( init ) +%emit { #18 DEO } + |0100 @program #1234 SWP @@ -11,8 +13,8 @@ ;hello-word &while - ( send ) LDAk #18 DEO - ( loop ) INC2 LDAk ,&while JCN + ( send ) LDAk emit + ( loop ) INC2 LDAk ?&while POP2 #010f DEO diff --git a/src/uxnasm.c b/src/uxnasm.c index 388e596..8851fd0 100644 --- a/src/uxnasm.c +++ b/src/uxnasm.c @@ -312,25 +312,32 @@ doinclude(char *filename) return res; } +static int +walkcomment(char *w, FILE *f) +{ + int i = 1; + char word[0x40]; + if(slen(w) != 1) + fprintf(stderr, "-- Malformed comment: %s\n", w); + while(fscanf(f, "%63s", word) == 1) { + if(slen(word) != 1) + continue; + else if(word[0] == '(') + i++; + else if(word[0] == ')' && --i < 1) + break; + } + return 1; +} + static int parse(char *w, FILE *f) { int i; - char word[0x40], c; + char c; Macro *m; switch(w[0]) { - case '(': /* comment */ - 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 '(': return walkcomment(w, f); case '~': return !doinclude(w + 1) ? error_asm("Invalid include") : 1; case '%': return !makemacro(w + 1, f) ? error_asm("Invalid macro") : 1; case '@': return !makelabel(w + 1, 1) ? error_asm("Invalid label") : 1;