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.76k stars 55 forks source link

Correctly generate the main function for C/C++ #132

Closed ghost closed 1 year ago

ghost commented 1 year ago

For example, this Fusion code correctly translated to C# and Java:

public static void Main(string[] args)

But it doesn't work for C/C++.

I tried a main function without parameters:

public class Test {
    public static void main() {
    }
}

It translated to void Test_main(void) in C and void Test::main() in C++.

I suggest treating the Main function as a special function if translating to target C/C++. It should be put outside of any classes or structs in the generated code.

I also suggest Fusion to correctly translate this Main function to C/C++:

public static int Main(int argc, string[] argv)

It should correctly translate to int main(int argc, char *argv[]).

Why I suggest using another Main function in Fusion code if translating to C/C++? Because this Main function will not correctly translate to C/C++:

public static void Main(string[] args)

It's more optimized for C# and Java.

Following this discipline, we will have to write two Main function. One is specific to C/C++. One is for the rest. Conditional compilation will help. This is acceptable IMO.

pfusik commented 1 year ago

Duplicate of #73

ghost commented 1 year ago

Duplicate of #73

Yup. Duplicate. But do you have any progress since then? The last reply was from 2022. Did the OP on that thread suggest any solutions nor workarounds like me? Nope. He only raised the problem. And my issue is about C/C++ exclusively, not as broad as his issue, please note.

pfusik commented 1 year ago

Fusion is a language for authoring libraries, not command-line programs. Libraries do not have main. Why would you need main generated from your Fusion code?

There's a trivial workaround, simply link with this C code:

#include "myfusionlib.h"

int main(int argc, char **argv)
{
    MyFusionClass_Main(argc, argv);
}