- New const enum declaration syntax.

- New enum associated value syntax.
This commit is contained in:
Christoffer Lerno
2026-02-12 14:43:56 +01:00
parent f079fa82b2
commit acc4a900f5
59 changed files with 751 additions and 701 deletions

View File

@@ -1,7 +1,7 @@
import std;
fn int foo() => 0;
alias FooFn = fn int();
enum Bar : const FooFn
const enum Bar : FooFn
{
FOO = fn () => (int)(iptr)ExclusiveRange{int}.typeid,
BAR = &foo,

View File

@@ -3,9 +3,9 @@ import std;
const VERSION = "2.0";
enum Subcommand : (String name, String desc, String[] args)
{
FOO = {"oekfe", "foek", { "foke", "foekfe"}},
BAR = {"foek", "foek", { "foekf", "foekfe"}},
BAZ = {"Foekfef", "foek", { "foke", "foekfe" }}
FOO {"oekfe", "foek", { "foke", "foekfe"}},
BAR {"foek", "foek", { "foekf", "foekfe"}},
BAZ {"Foekfef", "foek", { "foke", "foekfe" }}
}
macro void help (String program_name, bool to_stderr = false)
{

View File

@@ -1,10 +1,10 @@
// #target: macos-x64
module test;
enum TokenType: char (char token) {
ERROR = 0,
INCP = '>',
DECP = '>',
LOOPS = '[',
ERROR { 0 },
INCP { '>' },
DECP { '>' },
LOOPS { '[' },
}
macro populate_token_table() {

View File

@@ -25,7 +25,7 @@ struct ThisOtherStruct @TaggedAttr(VALUE_STRUCT)
enum Example : int (String str) @TaggedAttr(VALUE_STRUCT)
{
FOO = FOO_STR,
FOO {FOO_STR},
}
const int[2][1] BAR = { { 1, 2} };

View File

@@ -4,8 +4,8 @@ import std;
enum Foo : (String x, int val)
{
ABC = { "Hello", 3 },
DEF = { "World", -100 },
ABC { "Hello", 3 },
DEF { "World", -100 },
}
fn int main()

View File

@@ -3,8 +3,8 @@ import std;
enum Foo : (String x, int val)
{
ABC = { "Hello", 3 },
DEF = { "World", -100 },
ABC { "Hello", 3 },
DEF { "World", -100 },
}
fn int main()

View File

@@ -1,6 +1,6 @@
import std::io;
enum MyEnum : const inline CInt
const enum MyEnum : inline CInt
{
FOO = 1,
BAR = 5,

View File

@@ -1,6 +1,6 @@
// #target: macos-x64
module test;
enum Foo : const inline String
const enum Foo : inline String
{
HELO = "Helo"
}

View File

@@ -5,7 +5,7 @@ struct Bar
uint x, y, z;
}
enum Foo : const inline Bar
const enum Foo : inline Bar
{
X = {1, 0, 0},
Y = {0, 1, 0},

View File

@@ -1,7 +1,7 @@
// #target: macos-x64
module test;
enum Enum : const int
const enum Enum : int
{
E0, E1
}

View File

@@ -1,4 +1,4 @@
enum MyEnum : const
const enum MyEnum
{
VAL1 = VAL3, // #error: Unable to properly resolve enum constant value, this can sometimes happen in recursive definitions
VAL2 ,

View File

@@ -2,7 +2,7 @@
module test;
import std;
enum Color : const char[<4>]
const enum Color : char[<4>]
{
LIGHTGRAY = { 200, 200, 200, 255 },
}

View File

@@ -6,11 +6,11 @@ alias FooFn = fn int();
enum Foo : (FooFn f)
{
FOO = fn int() => 0, // allowed
BAR = &foo, // allowed
FOO { fn int() => 0 }, // allowed
BAR { &foo }, // allowed
}
enum Bar : const FooFn
const enum Bar : FooFn
{
FOO = fn int() => 0, // allowed
BAR = &foo, // Error: Expected an constant enum value.

View File

@@ -4,8 +4,8 @@ import libc;
enum Foo : uint (int val, char* testme)
{
A = { 123, "Number A" },
B = { 333, "Number B" },
A { 123, "Number A" },
B { 333, "Number B" },
}
fn void main()

View File

@@ -18,8 +18,8 @@ int dabc;
struct Abc { int x; }
enum Foo : int (String val)
{
ABC = "hello" ,
DEF = "world"
ABC {"hello"},
DEF {"world"}
}
/* #expect: abc.ll

View File

@@ -2,13 +2,13 @@
module test;
import std;
enum Bar : const String
const enum Bar : String
{
XABC = "foekf",
XDEF = "foekfokef"
}
enum Foo : const int
const enum Foo : int
{
ABC = 4,
DEF,
@@ -18,7 +18,7 @@ enum Foo : const int
}
enum Bde2 : const int
const enum Bde2 : int
{
FOO = 2,
BAR = 100
@@ -30,7 +30,7 @@ const BDE3_BAR = 100;
enum Abc2 : const int
const enum Abc2 : int
{
UP,
DOWN,

View File

@@ -12,7 +12,7 @@ fn void Foo.x(&self) @dynamic
io::printn("Foo!");
}
enum Tester : const inline Foo
const enum Tester : inline Foo
{
ABC = 1
}

View File

@@ -8,7 +8,7 @@ struct Foo
module bar;
enum MyEnum : const CInt
const enum MyEnum : CInt
{
VAL1 = 1,
VAL2 = 2,

View File

@@ -2,7 +2,7 @@ module generic_module_enum <N>;
enum Foo : char (String s)
{
BAR = "bar",
BAR {"bar"},
}
module generic_module_enum_test;

View File

@@ -1,14 +1,14 @@
enum Test : int (int kindof, int qnameof)
{
FOO = {1, 2}
FOO {1, 2}
}
enum Test2 : (int a, int nameof) // #error: 'nameof' is not a valid parameter name for enums
{
FOO = {1, 2}
FOO {1, 2}
}
enum Test3 : (int a, int ordinal) // #error: 'ordinal' is not a valid parameter name for enums
{
FOO = {1, 2}
FOO {1, 2}
}

View File

@@ -1,5 +1,5 @@
module test;
enum MyEnum : const inline short
const enum MyEnum : inline short
{
ITEM1,
ITEM2,

View File

@@ -1,4 +1,4 @@
enum Foo : (int x, int y)
{
ABC = { 1 2 } // #error: A comma or a closing brace
ABC { 1 2 } // #error: A comma or a closing brace
}

View File

@@ -2,13 +2,13 @@ module test;
enum Bar : (Foo x)
{
ABC = BAR
ABC {BAR}
}
enum Foo : (Bar b, Foo a)
{
FOO = { ABC, BAR },
BAR = { ABC, BAR }
FOO { ABC, BAR },
BAR { ABC, BAR }
}
fn void main()

View File

@@ -1,9 +1,9 @@
enum Test : (int a, int b)
{
FOO = { 1, 2 }
FOO { 1, 2 }
}
enum Test2 : int (int a, int a) // #error: Duplicate parameter name 'a'
{
FOO = { 1, 2 }
FOO { 1, 2 }
}

View File

@@ -1,7 +1,7 @@
enum Test2 : (usz a, usz b) {
FOO = {4, 5, },
FOO {4, 5, },
}
enum Test : (usz a, usz b) {
FOO = {a: 1, b: 1}, // #error: This looks like
FOO {a: 1, b: 1}, // #error: This looks like
}

View File

@@ -6,10 +6,10 @@ const FG_GREEN = "\e[0;38;2;192;255;192m";
const FG_RED = "\e[0;38;2;255;40;40m";
enum SeverityTag : int (String fg, String label) {
INFO = { FG_GREEN, "info" },
WARN = { FG_YELLOW, "warn" },
FATAL = { FG_RED, "fatal" },
FATAL2 = { FG_RED, "fatal2" },
INFO { FG_GREEN, "info" },
WARN { FG_YELLOW, "warn" },
FATAL { FG_RED, "fatal" },
FATAL2 { FG_RED, "fatal2" },
}
fn void main()

View File

@@ -1,7 +1,7 @@
// #target: macos-x64
module boom;
enum Boom: int (String a) {
BOOM = {0}
BOOM {{0}}
}
module app;

View File

@@ -3,7 +3,7 @@ module test;
alias SomethingFn = fn void();
enum TestEnum : char (SomethingFn f)
{
FOO = fn () {}
FOO {fn () {}}
}
/* #expect: test.ll

View File

@@ -2,7 +2,7 @@ module main;
faultdef F;
enum Foo : const fault
const enum Foo : fault
{
F = F // #error: Recursive resolution of expression
}

View File

@@ -1,9 +1,9 @@
enum Baz : (String a, int z)
{
BAR = { "123", 1, 3 }, // #error: You're adding too many values, only 2 associated
BAR { "123", 1, 3 }, // #error: You're adding too many values, only 2 associated
}
enum Baz2 : (String a, int z)
{
BAR = { "123" }, // #error: Expected 2 associated values for this enum value
BAR { "123" }, // #error: Expected 2 associated values for this enum value
}

View File

@@ -6,7 +6,7 @@ enum Foo : inline int
{
ABC,
}
enum Bar : const inline int
const enum Bar : inline int
{
HELLO = 1
}

View File

@@ -409,16 +409,16 @@ alias Kind = lexer::Kind{Token, Comment};
enum Token : char (String token)
{
KEYWORD1 = "keyword1",
KEYWORD2 = "keyword2",
SINGLE = "//",
MULTI = "/*",
KEYWORD1 { "keyword1" },
KEYWORD2 { "keyword2" },
SINGLE { "//" },
MULTI { "/*" },
}
enum Comment : char (Token start, String end)
{
SINGLE = { SINGLE, "\n" },
MULTI = { MULTI, "*/" },
SINGLE { SINGLE, "\n" },
MULTI { MULTI, "*/" },
}
fn bool is_ident_char(usz i, char c)

View File

@@ -186,7 +186,7 @@ enum Foobar : inline char
BAZ
}
enum Foobar2 : const inline int
const enum Foobar2 : inline int
{
ABC = 3,
DEF = 5,

View File

@@ -63,7 +63,7 @@ enum Foobar : inline char
BAZ
}
enum Foobar2 : const inline int
const enum Foobar2 : inline int
{
ABC = 3,
DEF = 5,