c2v icon indicating copy to clipboard operation
c2v copied to clipboard

Translating C to V fails to preserve case of variable names

Open struanb opened this issue 2 years ago • 1 comments

V version: V 0.3.1 085a09e OS: macos, macOS, 12.1, 21C52

What did you do?

$ cat >test.c 
#include <stdio.h>

int main() {
    int I = 1;
    int i = 2;
    printf("i=%d I=%d\n", i, I);
    return 0;
}

$ ./v/v translate test.c

What did you expect to see?

$ ./v/v translate test.c
C to V translator 0.3.1
  translating test.c         ... Reformatted file: /Users/struan/Downloads/test.v
 took   181 ms ; output .v file: test.v
Translated   1 files in   181 ms.

What did you see instead?

$ ./v/v translate test.c
C to V translator 0.3.1
  translating test.c          ... test.v:6:2: error: redefinition of `i`
    4 | fn main()  {
    5 |     i := 1
    6 |     i := 2
      |     ^
    7 |     C.printf(c'i=%d I=%d\n', i, i)
    8 |     return

Internal vfmt error while formatting file: test.v.
Encountered a total of: 1 errors.
 took   198 ms ; output .v file: test.v
Translated   1 files in   198 ms.

In short, although C variable names are case-sensitive, v translate doesn't respect the case of C variables when translating and migrates all C variable names to their lower-case equivalents. This breaks translation when the C code makes use of multiple variables differing only in case in the same scope.

struanb avatar Sep 17 '22 14:09 struanb

V can't preserve the case of the names, because V doesn't support mixed-case variable names. v translate does, however, need to provide a workaround for this issue.

JalonSolov avatar Sep 17 '22 15:09 JalonSolov