RyanSquared / fusionscript

A Lua-targeting Programming Language Experiment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`const` and `enum` statements

RyanSquared opened this issue · comments

commented

enum

enum would help with speed at runtime when checking between multiple values:

enum ConnectionType {
    INET;
    INET6;
    UNIX;
};

print(INET == ConnectionType.INET);

INET and ConnectionType.INET both compile to 1, so the resulting code would be similar to:

print(1 == 1)

enum values may be assigned with an "initial" value; values assigned afterwards take the value of the former, adding one:

enum Numbers {
    ZERO = 48;
    ONE;
    TWO;
    THREE;
    FOUR;
    FIVE;
    SIX;
    SEVEN;
    EIGHT;
    NINE;
    TEN;
};

Enumeration values could also be assigned after an initial
assignment, and to any integer, negative or positive:

enum Numbers {
    NFIVE = -5;
    NFOUR;
    NTHREE;
    ZERO = 0;
    ONE;
    TWO;
    TEN = 10;
};

Enumeration values can't go backwards:

enum BadEnum {
    ONE;
    ZERO = 0; -- errors during compilation
};

const

const values are like enumerations, but only one value is assigned per statement:

--- FusionScript
const max_char = 255;
print(string.char(max_char));
--- Lua
print(255);

Unlike enumerations, const values can also be true, false, nil, or a string:

--- FusionScript
const format_pattern = "%s: %s";
print(format_pattern:format(name, message));
--- Lua
print(("%s: %s"):format(name, message))

CONSIDERATION: "%s: %s":format() is not valid Lua. Instead, the
value - if not used only as a value, for instance, as a callable or indexable
object - should be wrapped in parenthesis first.

commented

Done.

done in what commit?

commented

Over a series of about 15 or so commits over the last few days. Still working on import to include importing top-level constants, but that's a different concern.