diff options
-rw-r--r-- | listamsb.c | 110 |
1 files changed, 46 insertions, 64 deletions
@@ -594,10 +594,10 @@ int token_is_extended(int tok) { return tok > 0xff; } -int append_token(int tok, char *buf, int len) { +int append_token(int tok, unsigned char *buf, int len) { if(token_is_extended(tok)) { buf[len++] = 0xff; - buf[len++] = tok; + buf[len++] = tok & 0xff; } else { buf[len++] = tok; } @@ -612,47 +612,43 @@ void token2text(int tok, unsigned char *buf) { the tokenizer in AMSB requires spaces to separate keywords and variables/numbers. "IF A THEN 100" really needs the spaces before and after the A, for example. */ -int need_space_between(int ext1, int ext2, unsigned char t1, unsigned char t2) { - unsigned char tok1[10], tok2[10]; +int need_space_between(int t1, int t2) { + unsigned char text1[10], text2[10]; unsigned char t1last, t2first; if(!t1) return 0; /* start of line */ if(!t2) return 0; /* end of line */ if(t1 < 0x80 && t2 < 0x80) return 0; /* 2 ASCII chars */ - expand_token(ext1, t1, tok1); - expand_token(ext2, t2, tok2); - t1last = tok1[strlen((char *)tok1) - 1]; /* "PRINT" => "T" */ - t2first = tok2[0]; /* "PRINT" => "P" */ + token2text(t1, text1); + token2text(t2, text2); + t1last = text1[strlen((char *)text1) - 1]; /* "PRINT" => "T" */ + t2first = text2[0]; /* "PRINT" => "P" */ /* if we already have a space, don't need to add another */ if(t1last == ' ' || t2first == ' ') return 0; - if(!ext1) { - /* IF, THEN, and operators like AND/OR/NOT always get a - space after them, for readability. */ - switch(t1) { - case TOK_IF: - case TOK_THEN: - case TOK_ELSE: - case TOK_AND: - case TOK_OR: - case TOK_NOT: - return 1; - default: break; - } - } - - if(!ext2) { - /* these always get a space before them, for readability. */ - switch(t2) { - case TOK_THEN: - case TOK_AND: - case TOK_OR: - case TOK_NOT: - return 1; - default: break; - } + /* IF, THEN, and operators like AND/OR/NOT always get a + space after them, for readability. */ + switch(t1) { + case TOK_IF: + case TOK_THEN: + case TOK_ELSE: + case TOK_AND: + case TOK_OR: + case TOK_NOT: + return 1; + default: break; + } + + /* these always get a space before them, for readability. */ + switch(t2) { + case TOK_THEN: + case TOK_AND: + case TOK_OR: + case TOK_NOT: + return 1; + default: break; } if(t1 >= 0x80 && isalnum(t1last)) { @@ -680,8 +676,8 @@ int need_space_between(int ext1, int ext2, unsigned char t1, unsigned char t2) { } int decrunch_line(void) { - unsigned char code[MAX_LINE_LEN_HARD + 10], byte = 0, prev; - int lineno, ptr, codelen = 0, in_string = 0, in_comment = 0; + unsigned char code[MAX_LINE_LEN_HARD + 10]; + int lineno, tok = 0, prev, ptr, codelen = 0, in_string = 0, in_comment = 0; static int addr = 0x700; /* doesn't really matter where */ ptr = read_prog_word(); @@ -693,53 +689,39 @@ int decrunch_line(void) { while(1) { if(codelen >= MAX_LINE_LEN_HARD) die("line %d too long, decrunching failed", lineno); - prev = byte; - byte = read_prog_byte(); - - if(byte != 0) { - if(byte == ' ' || is_comment_start(byte)) spacecount++; + prev = tok; + tok = read_token(); + if(tok != 0) { if(in_string) { - if(byte == '|') + if(tok == '|') in_string = 0; } else if(in_comment) { /* NOP */ } else { - if(byte == '"') { + if(tok == ' ' || is_comment_start(tok)) { + verbose(2, "found space/comment at line %d", lineno); + spacecount++; + } + if(tok == '"') { in_string = 1; - /* XXX: if a " ever occurs immediately after an extended - token, this is wrong. I don't *think* the syntax - allows that... */ - if(need_space_between(0, 0, prev, byte)) + if(need_space_between(prev, tok)) code[codelen++] = ' '; - } else if(is_comment_start(byte)) { + } else if(is_comment_start(tok)) { in_comment = 1; - } else if(byte == 0xff) { - byte = read_prog_byte(); - if(need_space_between(0, 1, prev, byte)) - code[codelen++] = ' '; - code[codelen++] = 0xff; - code[codelen++] = byte; - prev = byte; - /* XXX: this next bit assumes there will never be two - extended tokens in a row. I *think* this is true, but - I'm not 100% certain. */ - byte = read_prog_byte(); - if(need_space_between(1, 0, prev, byte)) - code[codelen++] = ' '; - } else if(prev == ':' && byte == TOK_ELSE) { + } else if(prev == ':' && tok == TOK_ELSE) { /* special case, ELSE needs a space before the (invisible) colon that comes before it. */ code[codelen - 1] = ' '; code[codelen++] = prev; - } else if(need_space_between(0, 0, prev, byte)) { + } else if(need_space_between(prev, tok)) { code[codelen++] = ' '; } } } - code[codelen++] = byte; - if(byte == 0) break; + codelen = append_token(tok, code, codelen); + if(tok == 0) break; } codelen += 4; /* account for ptr and lineno */ |