enums
This commit is contained in:
@@ -12,6 +12,10 @@ call_mapper :: func(mapper native.Imported_Mapper) c_int {
|
||||
return mapper?(21)
|
||||
}
|
||||
|
||||
enum_identity :: c_func(value native.Imported_Enum) native.Imported_Enum {
|
||||
return value
|
||||
}
|
||||
|
||||
main :: func() void {
|
||||
_ = native.imported_add(pass_alias(20), 22)
|
||||
_ = native.imported_scalar(3, 4)
|
||||
@@ -19,6 +23,14 @@ main :: func() void {
|
||||
_ = native.imported_read(native.imported_handle()?)
|
||||
_ = native.imported_string("hello")
|
||||
_ = native.imported_apply(double_value, 21)
|
||||
enum_value native.Imported_Enum :: native.IMPORTED_ENUM_NEGATIVE
|
||||
enum_same native.Imported_Enum :: native.IMPORTED_ENUM_SAME
|
||||
_ = native.imported_enum_check(enum_value, native.IMPORTED_ENUM_VALUE)
|
||||
_ = native.imported_enum_variadic(-2, enum_same)
|
||||
enum_record native.Imported_Enum_Record :: native.Imported_Enum_Record { value = native.IMPORTED_ENUM_BACK }
|
||||
_ = native.imported_enum_record(enum_record)
|
||||
_ = native.imported_enum_apply(enum_identity, native.imported_enum_global)
|
||||
_ = native.IMPORTED_ANON_ENUM
|
||||
_ = call_mapper(double_value)
|
||||
_ = native.configured_value(9)
|
||||
_ = native.IMPORTED_SHADOW_OBJECT
|
||||
|
||||
@@ -80,8 +80,18 @@ typedef struct Imported_Anonymous {
|
||||
};
|
||||
} Imported_Anonymous;
|
||||
typedef enum Imported_Enum {
|
||||
IMPORTED_ENUM_VALUE,
|
||||
IMPORTED_ENUM_NEGATIVE = -2,
|
||||
IMPORTED_ENUM_SAME = -2,
|
||||
IMPORTED_ENUM_VALUE = 7,
|
||||
IMPORTED_ENUM_BACK = 3,
|
||||
} Imported_Enum;
|
||||
enum {
|
||||
IMPORTED_ANON_ENUM = 9,
|
||||
};
|
||||
typedef struct Imported_Enum_Record {
|
||||
Imported_Enum value;
|
||||
} Imported_Enum_Record;
|
||||
typedef Imported_Enum (*Imported_Enum_Callback)(Imported_Enum value);
|
||||
|
||||
int imported_add(imported_int_alias left, int right);
|
||||
unsigned long imported_scalar(unsigned char value, unsigned long extra);
|
||||
@@ -89,6 +99,10 @@ Imported_Handle *imported_handle(void);
|
||||
int imported_read(const Imported_Handle *handle);
|
||||
int imported_string(const char *value);
|
||||
int imported_apply(Imported_Mapper mapper, int value);
|
||||
int imported_enum_check(Imported_Enum first, Imported_Enum second);
|
||||
int imported_enum_variadic(int expected, ...);
|
||||
Imported_Enum_Record imported_enum_record(Imported_Enum_Record value);
|
||||
Imported_Enum imported_enum_apply(Imported_Enum_Callback callback, Imported_Enum value);
|
||||
Imported_Value imported_by_value(Imported_Value value);
|
||||
int imported_check_state(
|
||||
Imported_Color color,
|
||||
@@ -126,6 +140,7 @@ extern int imported_redeclared_array[4];
|
||||
extern Imported_Value imported_record_global;
|
||||
extern const Imported_Array_Record imported_const_array_record;
|
||||
extern Imported_Array_Record imported_mutable_array_record;
|
||||
extern Imported_Enum imported_enum_global;
|
||||
extern _Thread_local int imported_tls_global;
|
||||
extern int IMPORTED_SHADOW_OBJECT;
|
||||
int IMPORTED_SHADOW_FUNCTION(void);
|
||||
|
||||
@@ -16,6 +16,7 @@ int imported_redeclared_array[4] = {13, 14, 15, 16};
|
||||
Imported_Value imported_record_global = {40};
|
||||
const Imported_Array_Record imported_const_array_record = {{20, 21}};
|
||||
Imported_Array_Record imported_mutable_array_record = {{0, 1}};
|
||||
Imported_Enum imported_enum_global = IMPORTED_ENUM_VALUE;
|
||||
int child_shared_global = 0;
|
||||
|
||||
int child_value(int value) {
|
||||
@@ -56,6 +57,38 @@ int imported_apply(Imported_Mapper mapper, int value) {
|
||||
return result;
|
||||
}
|
||||
|
||||
int imported_enum_check(Imported_Enum first, Imported_Enum second) {
|
||||
if (!(first == IMPORTED_ENUM_NEGATIVE && second == IMPORTED_ENUM_VALUE)) {
|
||||
abort();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int imported_enum_variadic(int expected, ...) {
|
||||
va_list args;
|
||||
va_start(args, expected);
|
||||
int value = va_arg(args, int);
|
||||
va_end(args);
|
||||
if (value != expected) {
|
||||
abort();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Imported_Enum_Record imported_enum_record(Imported_Enum_Record value) {
|
||||
if (value.value != IMPORTED_ENUM_BACK) {
|
||||
abort();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Imported_Enum imported_enum_apply(Imported_Enum_Callback callback, Imported_Enum value) {
|
||||
if (callback == NULL || callback(value) != value) {
|
||||
abort();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
int imported_check_state(
|
||||
Imported_Color color,
|
||||
int macro_value,
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
Animal :: enum {
|
||||
dog
|
||||
cat
|
||||
}
|
||||
|
||||
favorite :: func() Animal {
|
||||
return .cat
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
animals :: import "./animals"
|
||||
|
||||
State :: enum(u16) {
|
||||
started = 10
|
||||
running
|
||||
stopped = 20
|
||||
}
|
||||
|
||||
initial State :: State.started
|
||||
|
||||
same :: func(left State, right State) bool {
|
||||
return left == right
|
||||
}
|
||||
|
||||
identity :: c_func(value State) State {
|
||||
return value
|
||||
}
|
||||
|
||||
main :: func() i32 {
|
||||
state State = identity(.running)
|
||||
values [2]State :: [.started, State.stopped]
|
||||
animal animals.Animal :: animals.Animal.dog
|
||||
if same(state, .running) and
|
||||
values[0] != values[1] and
|
||||
animal != animals.favorite() and
|
||||
initial == State.started {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
Reference in New Issue
Block a user