fusionlanguage / fut

Fusion programming language. Transpiling to C, C++, C#, D, Java, JavaScript, Python, Swift, TypeScript and OpenCL C.
https://fusion-lang.org
GNU General Public License v3.0
1.77k stars 55 forks source link

false positive error inside #if condition? #162

Closed iAndyHD3 closed 6 months ago

iAndyHD3 commented 6 months ago

It appears that code inside of false evaluated #if conditions is skipped, as such this code does not produce any errors

#if TEST
ksdfj
jkkfjlkas
#endif

However, this valid code dos not compile if TEST is not defined, which means code is not being skipped correctly? If TEST is defined, it compiles as expected

#if TEST
class Test
{
    public static void Main()
    {
        int a = 0;
        Console.WriteLine($"{a}"); //Test.fu(7): ERROR: Unterminated string literal
    }
}
#endif

Another error that isn't being skipped correctly: (TEST is not defined)

#if TEST
0a //Test.fu(2): ERROR: Invalid integer
#endif

Unrelated: If I have a few small suggestions and questions, should I post them in Discussions?

pfusik commented 6 months ago

It appears that code inside of false evaluated #if conditions is skipped, as such this code does not produce any errors

#if TEST
ksdfj
jkkfjlkas
#endif

This is expected. Code under a false condition is not parsed. Same as with C, C++ and C# compilers.

Why?

  1. With 20 conditional variables, there are over 1 million code cases to be checked.
  2. Some combinations of condition variables might be invalid, e.g.
class Foo {
#if INTERNAL
    internal
#endif
#if PROTECTED
    protected
#endif
    int Bar;
}

However, this valid code dos not compile if TEST is not defined, which means code is not being skipped correctly? If TEST is defined, it compiles as expected

#if TEST
class Test
{
    public static void Main()
    {
        int a = 0;
        Console.WriteLine($"{a}"); //Test.fu(7): ERROR: Unterminated string literal
    }
}
#endif

This is a bug. Interpolated strings need a cooperation between the parser and the lexer and the parser is disabled for false conditions.

Another error that isn't being skipped correctly: (TEST is not defined)

#if TEST
0a //Test.fu(2): ERROR: Invalid integer
#endif

This is expected. Code under a false condition must be a valid token sequence as it gets lexed.

Why? There might be a /* opening a multi-line comment which comments-out the #endif. But we can't just search for /* characters because they might be in a string literal ("/*") or a single-line comment (// /*).

Unrelated: If I have a few small suggestions and questions, should I post them in Discussions?

If you have specific bug reports or feature requests, post them in Issues. For generic discussion and questions, use Discussions.