nurpax / c64jasm

C64 6502 assembler in TypeScript
51 stars 14 forks source link

[bug] in case a label and a variable have the same name, no error is thrown #78

Closed shazz closed 3 years ago

shazz commented 3 years ago

Looks like variables prevail:

!let DEBUG = 1
!let data = 0

!if (DEBUG == 0) {
    data = $2010
}

!if (DEBUG == 1) {
    * = $2000
    data = $1234
    data: !byte 12
}

    * = $2050
    lda data 

Result:

2050: AD 34 12                    LDA $1234
nurpax commented 3 years ago

This is definitely a bug. EDIT no wait, it isn't. But perhaps the assembler could warn about symbol shadowing.

This type of thing is legal in c64jasm and can sometimes be even useful:

!let data = 0

!if (1) {
    lda #data ; `data` from outer scope
!let data = 4 ; `data` from current scope
    lda #data
}
lda #data

Produces:

0801: A9 00                       LDA #$00
0803: A9 04                       LDA #$04
0805: A9 00                       LDA #$00

It's similar to how this is valid C:

#include <stdio.h>

int main() {
  int a = 0;
  if (1) {
    int a = 1;
    printf("a=%d\n", a);
  }
  printf("a=%d\n", a);
}

The following is an error in c64jasm:

!let data = 0
data: !byte 0  ; Symbol 'data' already defined