Do a better job of emitting whitespace

This commit is contained in:
XANTRONIX Development 2023-09-13 00:43:08 -04:00
parent 5bc8a00f02
commit bff09eb8bb

56
main.c
View file

@ -48,6 +48,14 @@ typedef struct _zx_basic_line {
uint16_t num, len;
} zx_basic_line;
enum zx_basic_token_type {
ZX_BASIC_TOKEN_UNKNOWN,
ZX_BASIC_TOKEN_ALNUM,
ZX_BASIC_TOKEN_QUOTE,
ZX_BASIC_TOKEN_SYMBOL,
ZX_BASIC_TOKEN_WORD,
};
#define ZX_BASIC_STATE_SIZE 116
#define ZX_BASIC_LINE_LAST 0x7676
@ -249,6 +257,33 @@ static inline int zx_print(uint8_t c, int tty) {
return 0;
}
static inline enum zx_basic_token_type zx_basic_token_type_get(uint8_t b) {
if (ZX_CHAR_LOW(b)) {
uint32_t codepoint = zx_charset[b];
if ((codepoint >= 'A' && codepoint <= 'Z')
|| (codepoint >= '0' && codepoint <= '9')) {
return ZX_BASIC_TOKEN_ALNUM;
} else {
return ZX_BASIC_TOKEN_SYMBOL;
}
} else if (ZX_CHAR_INVERSE(b)) {
return zx_basic_token_type_get(b - ZX_CHAR_INVERSE_START);
} else if (ZX_CHAR_TOKEN_LOW(b)) {
return ZX_BASIC_TOKEN_WORD;
} else if (ZX_CHAR_TOKEN_HIGH(b)) {
char *token = zx_tokens[b-ZX_CHAR_TOKEN_HIGH_START];
if (token[0] >= 'A' && token[0] <= 'Z') {
return ZX_BASIC_TOKEN_WORD;
} else {
return ZX_BASIC_TOKEN_SYMBOL;
}
}
return ZX_BASIC_TOKEN_UNKNOWN;
}
static ssize_t zx_dump_basic(int fd) {
void *buf;
ssize_t total = 0;
@ -294,17 +329,28 @@ static ssize_t zx_dump_basic(int fd) {
for (i=0; i<len; i++) {
uint8_t c = ((uint8_t *)buf)[i];
int token = ZX_CHAR_TOKEN(c),
token_last = ZX_CHAR_TOKEN(last);
if (token || token_last) {
if (putchar(' ') < 0) {
goto error_io;
enum zx_basic_token_type type = zx_basic_token_type_get(c),
type_last = zx_basic_token_type_get(last);
int space = 0;
if (type == ZX_BASIC_TOKEN_ALNUM) {
if (type_last == ZX_BASIC_TOKEN_WORD) {
space = 1;
}
} else if (type == ZX_BASIC_TOKEN_SYMBOL) {
space = 0;
} else if (type == ZX_BASIC_TOKEN_WORD) {
space = 1;
} else if (ZX_CHAR_TOKEN_INTEGRAL(c) || ZX_CHAR_TOKEN_FLOAT(c)) {
i += 5;
}
if (space && putchar(' ') < 0) {
goto error_io;
}
if (zx_print(c, tty) < 0) {
goto error_io;
}