From 7d235cfe8ad479692848763dd51620dae3eb2026 Mon Sep 17 00:00:00 2001
From: "B. Watson" <urchlay@slackware.uk>
Date: Mon, 10 Mar 2025 21:58:39 -0400
Subject: listamsb: more work on cleaning up -D.

---
 listamsb.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

(limited to 'listamsb.c')

diff --git a/listamsb.c b/listamsb.c
index 99007d4..74d01c3 100644
--- a/listamsb.c
+++ b/listamsb.c
@@ -559,31 +559,33 @@ int crunch_line(void) {
 }
 
 /* only called during decrunching. */
-void expand_token(int ext, unsigned char t, unsigned char *buf) {
+int expand_token(int t, unsigned char *buf) {
 	const char *result;
 
 	if(t < 0x80) {
 		buf[0] = t;
 		buf[1] = 0;
-		return;
+		return 1;
 	}
 
-	if(ext) {
+	if(t > 0xff) {
+		t &= 0xff;
 		if(t > MAX_EXT_TOK)
-			die("invalid token in program, can't decrunch");
+			return 0;
 		result = ext_tokens[t - MIN_EXT_TOK];
 	} else {
 		if(t > MAX_STD_TOK)
-			die("invalid token in program, can't decrunch");
+			return 0;
 		result = std_tokens[t - MIN_STD_TOK];
 	}
 
 	strcpy((char *)buf, result);
+	return 1;
 }
 
-int read_token(void) {
+int read_token(int literal) {
 	int tok = read_prog_byte();
-	if(tok == 0xff) {
+	if((!literal) && (tok == 0xff)) {
 		tok <<= 8;
 		tok |= read_prog_byte();
 	}
@@ -604,10 +606,6 @@ int append_token(int tok, unsigned char *buf, int len) {
 	return len;
 }
 
-void token2text(int tok, unsigned char *buf) {
-	return expand_token(token_is_extended(tok), tok & 0xff, buf);
-}
-
 /* only called during decrunching.
 	the tokenizer in AMSB requires spaces to separate keywords
    and variables/numbers. "IF A THEN 100" really needs the spaces
@@ -620,8 +618,9 @@ int need_space_between(int t1, int t2) {
 	if(!t2) return 0; /* end of line */
 	if(t1 < 0x80 && t2 < 0x80) return 0; /* 2 ASCII chars */
 
-	token2text(t1, text1);
-	token2text(t2, text2);
+	if( !(expand_token(t1, text1) && expand_token(t2, text2)) )
+		die("invalid token in program, decrunching failed");
+
 	t1last = text1[strlen((char *)text1) - 1]; /* "PRINT" => "T" */
 	t2first = text2[0];                        /* "PRINT" => "P" */
 
@@ -690,7 +689,7 @@ int decrunch_line(void) {
 		if(codelen >= MAX_LINE_LEN_HARD)
 			die("line %d too long, decrunching failed", lineno);
 		prev = tok;
-		tok = read_token();
+		tok = read_token(in_string || in_comment);
 
 		if(tok != 0) {
 			if(in_string) {
-- 
cgit v1.2.3