marcobambini / gravity

Gravity Programming Language
https://gravity-lang.org
MIT License
4.3k stars 229 forks source link

C like conditional compilation and suggested features #406

Open ScriptExec opened 1 year ago

ScriptExec commented 1 year ago

Are there any plans to add macros such as #define, #if, #elif and #endif or some other alternative to allow conditional compilation? Also is this project still continued since there weren't any updates lately?

marcobambini commented 1 year ago

It is a feature that I'd like to add to Gravity, but I don't want to increase the complexity of the code. For example, the C preprocessor is bigger than the C itself (as codebase), so I need to carefully balance what to do about macro support.

ScriptExec commented 1 year ago

I agree that keeping it simple is the way to go and yeah fully recreating C like macros would be overkill. I was thinking that there should be #define IDENTIFIER value macro like in C and for conditional compilation #ifdef, #ifndef, #if, #else and #endif but I would allow #if to only receive an identifier that is set to either true or false fe.

//I wouldn't allow this (simplified platform macros)
#define IS_COMPATIBLE OS_WINDOWS && OS_64BIT

//I would only allow this
#define IS_COMPATIBLE 1 //or 0, true and false keywords should also work
//Setting macro values should also be possible via gravity vm's functions

#if IS_COMPATIBLE
    //...
#else
    //...
#endif

//Should other operations be allowed? like:
#if COMPILER_VERSION < 1000
#if COMPILER_VERSION != 1000
//etc

Personally I would also add #warning "message" and #error "message" (they wouldn't be too hard to implement) which would also benefit from the above macros. Another nice thing that the language could have might be attributes like in C#.

//C# version
[Serializable]
public class SampleClass
{
    // Objects of this type can be serialized.
}
//Example Gravity version
@serializable
class SampleClass
{
    //...
}

Also is it possible to save bytecode and then run it via some function or does the API only allow loading the source?