@shd101wyy/yo 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/cjs/index.cjs +543 -534
- package/out/cjs/yo-cli.cjs +639 -630
- package/out/esm/index.mjs +549 -540
- package/out/types/src/evaluator/builtins/comptime-list-fns.d.ts +5 -0
- package/out/types/src/evaluator/builtins/derive-rule.d.ts +8 -0
- package/out/types/src/evaluator/builtins/derive.d.ts +8 -0
- package/out/types/src/evaluator/builtins/type-fns.d.ts +30 -0
- package/out/types/src/evaluator/calls/comptime-fn.d.ts +1 -0
- package/out/types/src/evaluator/calls/index-trait.d.ts +3 -1
- package/out/types/src/evaluator/context.d.ts +3 -6
- package/out/types/src/expr.d.ts +31 -5
- package/out/types/src/function-value.d.ts +1 -0
- package/out/types/src/parser.d.ts +1 -0
- package/out/types/src/types/definitions.d.ts +1 -0
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/scripts/print-welcome.js +54 -0
- package/std/fmt/to_string.yo +138 -1
- package/std/prelude.yo +902 -4
package/std/prelude.yo
CHANGED
|
@@ -256,7 +256,13 @@ extern "Yo",
|
|
|
256
256
|
__yo_comptime_string_index_range :
|
|
257
257
|
(fn(comptime(self): comptime_string, comptime(idx): Range(comptime_int)) -> comptime(comptime_string)),
|
|
258
258
|
__yo_comptime_string_index_range_inclusive :
|
|
259
|
-
(fn(comptime(self): comptime_string, comptime(idx): RangeInclusive(comptime_int)) -> comptime(comptime_string))
|
|
259
|
+
(fn(comptime(self): comptime_string, comptime(idx): RangeInclusive(comptime_int)) -> comptime(comptime_string)),
|
|
260
|
+
__yo_comptime_list_index :
|
|
261
|
+
(fn(forall(T: Type), comptime(self): *(ComptimeList(T)), comptime(idx): usize, where(T <: Comptime)) -> comptime(*(T))),
|
|
262
|
+
__yo_comptime_list_index_range :
|
|
263
|
+
(fn(forall(T: Type), comptime(self): *(ComptimeList(T)), comptime(idx): Range(usize), where(T <: Comptime)) -> comptime(*(ComptimeList(T)))),
|
|
264
|
+
__yo_comptime_list_index_range_inclusive :
|
|
265
|
+
(fn(forall(T: Type), comptime(self): *(ComptimeList(T)), comptime(idx): RangeInclusive(usize), where(T <: Comptime)) -> comptime(*(ComptimeList(T))))
|
|
260
266
|
;
|
|
261
267
|
|
|
262
268
|
ComptimeRangeOp :: trait(
|
|
@@ -685,6 +691,7 @@ Clone :: trait(
|
|
|
685
691
|
clone :
|
|
686
692
|
fn(self: *(Self)) -> Self
|
|
687
693
|
);
|
|
694
|
+
export Clone;
|
|
688
695
|
|
|
689
696
|
// === Isolation ===
|
|
690
697
|
Isolation :: trait(
|
|
@@ -892,6 +899,10 @@ impl(comptime_string,
|
|
|
892
899
|
|
|
893
900
|
slice : (fn(comptime(self): comptime_string, comptime(start): comptime_int, comptime(end): comptime_int) -> comptime(comptime_string))(
|
|
894
901
|
__yo_comptime_string_slice(self, start, end)
|
|
902
|
+
),
|
|
903
|
+
|
|
904
|
+
to_expr : (fn(comptime(self): comptime_string) -> comptime(Expr))(
|
|
905
|
+
__yo_comptime_string_to_expr(self)
|
|
895
906
|
)
|
|
896
907
|
);
|
|
897
908
|
_comptime_string :: comptime_string;
|
|
@@ -3916,9 +3927,34 @@ impl(forall(T : Type), where(T <: Comptime), ComptimeList(T),
|
|
|
3916
3927
|
|
|
3917
3928
|
len : (fn(comptime(self): Self) -> comptime(usize))(
|
|
3918
3929
|
__yo_comptime_list_length(self)
|
|
3930
|
+
),
|
|
3931
|
+
|
|
3932
|
+
get : (fn(comptime(self): Self, comptime(index): usize) -> comptime(__yo_comptime_list_element_type(Self)))(
|
|
3933
|
+
__yo_comptime_list_get(self, index)
|
|
3919
3934
|
)
|
|
3920
3935
|
);
|
|
3921
3936
|
|
|
3937
|
+
impl(forall(T : Type), where(T <: Comptime), ComptimeList(T), ComptimeIndex(usize)(
|
|
3938
|
+
Output : T,
|
|
3939
|
+
index : (fn(comptime(self): *(Self), comptime(idx): usize) -> comptime(*(Self.Output)))(
|
|
3940
|
+
__yo_comptime_list_index(self, idx)
|
|
3941
|
+
)
|
|
3942
|
+
));
|
|
3943
|
+
|
|
3944
|
+
impl(forall(T : Type), where(T <: Comptime), ComptimeList(T), ComptimeIndex(Range(usize))(
|
|
3945
|
+
Output : ComptimeList(T),
|
|
3946
|
+
index : (fn(comptime(self): *(Self), comptime(idx): Range(usize)) -> comptime(*(Self.Output)))(
|
|
3947
|
+
__yo_comptime_list_index_range(self, idx)
|
|
3948
|
+
)
|
|
3949
|
+
));
|
|
3950
|
+
|
|
3951
|
+
impl(forall(T : Type), where(T <: Comptime), ComptimeList(T), ComptimeIndex(RangeInclusive(usize))(
|
|
3952
|
+
Output : ComptimeList(T),
|
|
3953
|
+
index : (fn(comptime(self): *(Self), comptime(idx): RangeInclusive(usize)) -> comptime(*(Self.Output)))(
|
|
3954
|
+
__yo_comptime_list_index_range_inclusive(self, idx)
|
|
3955
|
+
)
|
|
3956
|
+
));
|
|
3957
|
+
|
|
3922
3958
|
/// ExprList
|
|
3923
3959
|
ExprList :: ComptimeList(Expr);
|
|
3924
3960
|
export ExprList;
|
|
@@ -3975,14 +4011,324 @@ impl(Type,
|
|
|
3975
4011
|
|
|
3976
4012
|
impls : (fn(comptime(self): Type, comptime(marker): Trait) -> comptime(bool))({
|
|
3977
4013
|
return __yo_type_impls(self, marker);
|
|
4014
|
+
}),
|
|
4015
|
+
|
|
4016
|
+
eq : (fn(comptime(a): Type, comptime(b): Type) -> comptime(bool))({
|
|
4017
|
+
return __yo_are_types_equal(a, b);
|
|
4018
|
+
}),
|
|
4019
|
+
|
|
4020
|
+
neq : (fn(comptime(a): Type, comptime(b): Type) -> comptime(bool))({
|
|
4021
|
+
return !((__yo_are_types_equal(a, b)));
|
|
4022
|
+
}),
|
|
4023
|
+
|
|
4024
|
+
to_comptime_string : (fn(comptime(self): Type) -> comptime(comptime_string))({
|
|
4025
|
+
return __yo_type_to_comptime_string(self);
|
|
3978
4026
|
})
|
|
3979
4027
|
);
|
|
3980
|
-
impl(Type, ComptimeToString(
|
|
3981
|
-
to_comptime_string : (self -> __yo_type_to_comptime_string(self))
|
|
3982
|
-
));
|
|
3983
4028
|
_Type :: Type;
|
|
3984
4029
|
export Type : _Type;
|
|
3985
4030
|
|
|
4031
|
+
// === Type Reflection Metadata Structs ===
|
|
4032
|
+
|
|
4033
|
+
// StructKind — discriminant for struct flavors
|
|
4034
|
+
StructKind :: enum(Struct, Object, NewType);
|
|
4035
|
+
impl(StructKind, Comptime());
|
|
4036
|
+
export StructKind;
|
|
4037
|
+
|
|
4038
|
+
// TypeFieldInfo — Yo representation of TypeField
|
|
4039
|
+
TypeFieldInfo :: struct(
|
|
4040
|
+
name : comptime_string,
|
|
4041
|
+
field_type : Type
|
|
4042
|
+
);
|
|
4043
|
+
impl(TypeFieldInfo, Comptime());
|
|
4044
|
+
impl(TypeFieldInfo,
|
|
4045
|
+
to_expr : (fn(comptime(self): TypeFieldInfo) -> comptime(Expr))(
|
|
4046
|
+
__yo_comptime_string_to_expr(self.name)
|
|
4047
|
+
)
|
|
4048
|
+
);
|
|
4049
|
+
export TypeFieldInfo;
|
|
4050
|
+
|
|
4051
|
+
// TraitInfo — lightweight trait reference
|
|
4052
|
+
TraitInfo :: struct(
|
|
4053
|
+
trait_type : Type
|
|
4054
|
+
);
|
|
4055
|
+
impl(TraitInfo, Comptime());
|
|
4056
|
+
export TraitInfo;
|
|
4057
|
+
|
|
4058
|
+
// TraitFieldInfo — trait field metadata
|
|
4059
|
+
TraitFieldInfo :: struct(
|
|
4060
|
+
name : comptime_string,
|
|
4061
|
+
field_type : Type,
|
|
4062
|
+
is_associated_type : bool
|
|
4063
|
+
);
|
|
4064
|
+
impl(TraitFieldInfo, Comptime());
|
|
4065
|
+
export TraitFieldInfo;
|
|
4066
|
+
|
|
4067
|
+
// ParamInfo — function parameter metadata
|
|
4068
|
+
ParamInfo :: struct(
|
|
4069
|
+
name : comptime_string,
|
|
4070
|
+
param_type : Type,
|
|
4071
|
+
is_comptime : bool,
|
|
4072
|
+
is_quote : bool,
|
|
4073
|
+
is_variadic : bool
|
|
4074
|
+
);
|
|
4075
|
+
impl(ParamInfo, Comptime());
|
|
4076
|
+
export ParamInfo;
|
|
4077
|
+
|
|
4078
|
+
// ForallParamInfo — forall type parameter
|
|
4079
|
+
ForallParamInfo :: struct(
|
|
4080
|
+
name : comptime_string,
|
|
4081
|
+
param_type : Type
|
|
4082
|
+
);
|
|
4083
|
+
impl(ForallParamInfo, Comptime());
|
|
4084
|
+
export ForallParamInfo;
|
|
4085
|
+
|
|
4086
|
+
// ImplicitParamInfo — using/effect parameter
|
|
4087
|
+
ImplicitParamInfo :: struct(
|
|
4088
|
+
name : comptime_string,
|
|
4089
|
+
param_type : Type
|
|
4090
|
+
);
|
|
4091
|
+
impl(ImplicitParamInfo, Comptime());
|
|
4092
|
+
export ImplicitParamInfo;
|
|
4093
|
+
|
|
4094
|
+
// FunctionInfo — shared function type metadata
|
|
4095
|
+
FunctionInfo :: struct(
|
|
4096
|
+
params : ComptimeList(ParamInfo),
|
|
4097
|
+
return_type : Type,
|
|
4098
|
+
forall_params : ComptimeList(ForallParamInfo),
|
|
4099
|
+
implicit_params : ComptimeList(ImplicitParamInfo),
|
|
4100
|
+
is_closure : bool
|
|
4101
|
+
);
|
|
4102
|
+
impl(FunctionInfo, Comptime());
|
|
4103
|
+
export FunctionInfo;
|
|
4104
|
+
|
|
4105
|
+
// TraitKind — discriminant for trait flavors
|
|
4106
|
+
TraitKind :: enum(
|
|
4107
|
+
Future(child : Type, effects : ComptimeList(TraitInfo)),
|
|
4108
|
+
Fn(call : FunctionInfo),
|
|
4109
|
+
Normal
|
|
4110
|
+
);
|
|
4111
|
+
impl(TraitKind, Comptime());
|
|
4112
|
+
export TraitKind;
|
|
4113
|
+
|
|
4114
|
+
// VariantInfo — enum variant metadata
|
|
4115
|
+
VariantInfo :: struct(
|
|
4116
|
+
name : comptime_string,
|
|
4117
|
+
fields : ComptimeList(TypeFieldInfo),
|
|
4118
|
+
_enum_type : Type,
|
|
4119
|
+
_variant_index : usize
|
|
4120
|
+
);
|
|
4121
|
+
impl(VariantInfo, Comptime());
|
|
4122
|
+
export VariantInfo;
|
|
4123
|
+
|
|
4124
|
+
// TypeInfo — compile-time enum representing rich type metadata
|
|
4125
|
+
TypeInfo :: enum(
|
|
4126
|
+
// Primitives (fieldless)
|
|
4127
|
+
Unit, Bool,
|
|
4128
|
+
Usize, Isize,
|
|
4129
|
+
U8, I8, U16, I16, U32, I32, U64, I64,
|
|
4130
|
+
F32, F64,
|
|
4131
|
+
|
|
4132
|
+
// C-compatible primitives (fieldless)
|
|
4133
|
+
Char, Short, UShort, Int, UInt,
|
|
4134
|
+
Long, ULong, LongLong, ULongLong, LongDouble,
|
|
4135
|
+
Void,
|
|
4136
|
+
|
|
4137
|
+
// Compound types (with metadata)
|
|
4138
|
+
Array(element : Type, length : comptime_int),
|
|
4139
|
+
Slice(element : Type),
|
|
4140
|
+
Tuple(fields : ComptimeList(TypeFieldInfo)),
|
|
4141
|
+
Struct(
|
|
4142
|
+
fields : ComptimeList(TypeFieldInfo),
|
|
4143
|
+
kind : StructKind
|
|
4144
|
+
),
|
|
4145
|
+
Enum(
|
|
4146
|
+
variants : ComptimeList(VariantInfo)
|
|
4147
|
+
),
|
|
4148
|
+
Union(
|
|
4149
|
+
fields : ComptimeList(TypeFieldInfo)
|
|
4150
|
+
),
|
|
4151
|
+
Function(info : FunctionInfo),
|
|
4152
|
+
Ptr(pointee : Type),
|
|
4153
|
+
Iso(child : Type),
|
|
4154
|
+
Arc(child : Type),
|
|
4155
|
+
Dyn(
|
|
4156
|
+
required_traits : ComptimeList(TraitInfo),
|
|
4157
|
+
negative_traits : ComptimeList(TraitInfo)
|
|
4158
|
+
),
|
|
4159
|
+
|
|
4160
|
+
// Meta types
|
|
4161
|
+
Module(
|
|
4162
|
+
fields : ComptimeList(TypeFieldInfo)
|
|
4163
|
+
),
|
|
4164
|
+
Trait(
|
|
4165
|
+
fields : ComptimeList(TraitFieldInfo),
|
|
4166
|
+
kind : TraitKind
|
|
4167
|
+
),
|
|
4168
|
+
Type(level : comptime_int),
|
|
4169
|
+
Some(
|
|
4170
|
+
name : comptime_string,
|
|
4171
|
+
required_traits : ComptimeList(TraitInfo),
|
|
4172
|
+
negative_traits : ComptimeList(TraitInfo),
|
|
4173
|
+
resolved_type : Type
|
|
4174
|
+
),
|
|
4175
|
+
|
|
4176
|
+
// Comptime only
|
|
4177
|
+
ComptimeInt, ComptimeFloat, ComptimeString,
|
|
4178
|
+
ComptimeList(element : Type),
|
|
4179
|
+
|
|
4180
|
+
// Metaprogramming (fieldless)
|
|
4181
|
+
Expr,
|
|
4182
|
+
EffectsRow,
|
|
4183
|
+
TypeApplication
|
|
4184
|
+
);
|
|
4185
|
+
impl(TypeInfo, Comptime());
|
|
4186
|
+
impl(TypeInfo,
|
|
4187
|
+
is_struct : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4188
|
+
match(self, .Struct(_, _) => true, _ => false)
|
|
4189
|
+
),
|
|
4190
|
+
is_enum : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4191
|
+
match(self, .Enum(_) => true, _ => false)
|
|
4192
|
+
),
|
|
4193
|
+
is_union : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4194
|
+
match(self, .Union(_) => true, _ => false)
|
|
4195
|
+
),
|
|
4196
|
+
is_tuple : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4197
|
+
match(self, .Tuple(_) => true, _ => false)
|
|
4198
|
+
),
|
|
4199
|
+
is_array : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4200
|
+
match(self, .Array(_, _) => true, _ => false)
|
|
4201
|
+
),
|
|
4202
|
+
is_slice : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4203
|
+
match(self, .Slice(_) => true, _ => false)
|
|
4204
|
+
),
|
|
4205
|
+
is_function : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4206
|
+
match(self, .Function(_) => true, _ => false)
|
|
4207
|
+
),
|
|
4208
|
+
is_pointer : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4209
|
+
match(self, .Ptr(_) => true, _ => false)
|
|
4210
|
+
),
|
|
4211
|
+
is_trait : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4212
|
+
match(self, .Trait(_, _) => true, _ => false)
|
|
4213
|
+
),
|
|
4214
|
+
is_module : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4215
|
+
match(self, .Module(_) => true, _ => false)
|
|
4216
|
+
),
|
|
4217
|
+
is_void : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4218
|
+
match(self, .Void => true, _ => false)
|
|
4219
|
+
),
|
|
4220
|
+
is_primitive : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4221
|
+
match(self,
|
|
4222
|
+
.Bool => true,
|
|
4223
|
+
.Usize => true, .Isize => true,
|
|
4224
|
+
.U8 => true, .I8 => true,
|
|
4225
|
+
.U16 => true, .I16 => true,
|
|
4226
|
+
.U32 => true, .I32 => true,
|
|
4227
|
+
.U64 => true, .I64 => true,
|
|
4228
|
+
.F32 => true, .F64 => true,
|
|
4229
|
+
.Char => true,
|
|
4230
|
+
.Short => true, .UShort => true,
|
|
4231
|
+
.Int => true, .UInt => true,
|
|
4232
|
+
.Long => true, .ULong => true,
|
|
4233
|
+
.LongLong => true, .ULongLong => true,
|
|
4234
|
+
.LongDouble => true,
|
|
4235
|
+
_ => false
|
|
4236
|
+
)
|
|
4237
|
+
),
|
|
4238
|
+
is_integer : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4239
|
+
match(self,
|
|
4240
|
+
.Usize => true, .Isize => true,
|
|
4241
|
+
.U8 => true, .I8 => true,
|
|
4242
|
+
.U16 => true, .I16 => true,
|
|
4243
|
+
.U32 => true, .I32 => true,
|
|
4244
|
+
.U64 => true, .I64 => true,
|
|
4245
|
+
.Char => true,
|
|
4246
|
+
.Short => true, .UShort => true,
|
|
4247
|
+
.Int => true, .UInt => true,
|
|
4248
|
+
.Long => true, .ULong => true,
|
|
4249
|
+
.LongLong => true, .ULongLong => true,
|
|
4250
|
+
_ => false
|
|
4251
|
+
)
|
|
4252
|
+
),
|
|
4253
|
+
is_float : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4254
|
+
match(self,
|
|
4255
|
+
.F32 => true, .F64 => true,
|
|
4256
|
+
.LongDouble => true,
|
|
4257
|
+
_ => false
|
|
4258
|
+
)
|
|
4259
|
+
),
|
|
4260
|
+
is_comptime : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4261
|
+
match(self,
|
|
4262
|
+
.ComptimeInt => true, .ComptimeFloat => true, .ComptimeString => true,
|
|
4263
|
+
.ComptimeList(_) => true, .Expr => true,
|
|
4264
|
+
_ => false
|
|
4265
|
+
)
|
|
4266
|
+
),
|
|
4267
|
+
is_numeric : (fn(comptime(self) : TypeInfo) -> comptime(bool))(
|
|
4268
|
+
match(self,
|
|
4269
|
+
.Usize => true, .Isize => true,
|
|
4270
|
+
.U8 => true, .I8 => true,
|
|
4271
|
+
.U16 => true, .I16 => true,
|
|
4272
|
+
.U32 => true, .I32 => true,
|
|
4273
|
+
.U64 => true, .I64 => true,
|
|
4274
|
+
.F32 => true, .F64 => true,
|
|
4275
|
+
.Char => true,
|
|
4276
|
+
.Short => true, .UShort => true,
|
|
4277
|
+
.Int => true, .UInt => true,
|
|
4278
|
+
.Long => true, .ULong => true,
|
|
4279
|
+
.LongLong => true, .ULongLong => true,
|
|
4280
|
+
.LongDouble => true,
|
|
4281
|
+
.ComptimeInt => true, .ComptimeFloat => true,
|
|
4282
|
+
_ => false
|
|
4283
|
+
)
|
|
4284
|
+
)
|
|
4285
|
+
);
|
|
4286
|
+
export TypeInfo;
|
|
4287
|
+
|
|
4288
|
+
// Type.get_info() — returns TypeInfo for the given type
|
|
4289
|
+
// Defined after TypeInfo so the return type is available
|
|
4290
|
+
impl(Type,
|
|
4291
|
+
get_info : (fn(comptime(self): Type) -> comptime(TypeInfo))({
|
|
4292
|
+
return __yo_type_get_info(self);
|
|
4293
|
+
}),
|
|
4294
|
+
|
|
4295
|
+
get_struct_fields : (fn(comptime(self): Type) -> comptime(ComptimeList(TypeFieldInfo)))({
|
|
4296
|
+
info :: __yo_type_get_info(self);
|
|
4297
|
+
comptime_assert(info.is_struct(), "Type.get_struct_fields: expected a struct type");
|
|
4298
|
+
match(info, .Struct(f, _) => f, _ => comptime_list(TypeFieldInfo("_", unit)))
|
|
4299
|
+
}),
|
|
4300
|
+
|
|
4301
|
+
get_enum_variants : (fn(comptime(self): Type) -> comptime(ComptimeList(VariantInfo)))({
|
|
4302
|
+
info :: __yo_type_get_info(self);
|
|
4303
|
+
comptime_assert(info.is_enum(), "Type.get_enum_variants: expected an enum type");
|
|
4304
|
+
match(info, .Enum(v) => v, _ => comptime_list(VariantInfo("_", comptime_list(TypeFieldInfo("_", unit)), unit, usize(0))))
|
|
4305
|
+
})
|
|
4306
|
+
);
|
|
4307
|
+
|
|
4308
|
+
// FieldInfo — compile-time struct field metadata for derive rules (legacy alias)
|
|
4309
|
+
FieldInfo :: struct(name : comptime_string, field_type : Type);
|
|
4310
|
+
impl(FieldInfo, Comptime());
|
|
4311
|
+
impl(FieldInfo,
|
|
4312
|
+
to_expr : (fn(comptime(self): FieldInfo) -> comptime(Expr))(
|
|
4313
|
+
__yo_comptime_string_to_expr(self.name)
|
|
4314
|
+
)
|
|
4315
|
+
);
|
|
4316
|
+
export FieldInfo;
|
|
4317
|
+
|
|
4318
|
+
// Type methods that depend on FieldInfo/VariantInfo types
|
|
4319
|
+
MapFieldsFn :: (fn(comptime(_) : FieldInfo) -> comptime(Expr));
|
|
4320
|
+
MapVariantsFn :: (fn(comptime(_) : VariantInfo) -> comptime(Expr));
|
|
4321
|
+
|
|
4322
|
+
impl(Type,
|
|
4323
|
+
join_fields : (fn(comptime(self): Type, comptime(mapper): MapFieldsFn, comptime(combiner): Expr) -> comptime(Expr))({
|
|
4324
|
+
return __yo_type_join_fields(self, mapper, combiner);
|
|
4325
|
+
}),
|
|
4326
|
+
|
|
4327
|
+
map_variants : (fn(comptime(self): Type, comptime(mapper): MapVariantsFn) -> comptime(ComptimeList(Expr)))({
|
|
4328
|
+
return __yo_type_map_variants(self, mapper);
|
|
4329
|
+
})
|
|
4330
|
+
);
|
|
4331
|
+
|
|
3986
4332
|
// Variable
|
|
3987
4333
|
Var :: impl({
|
|
3988
4334
|
print_info :: (fn(quote(variable) : Expr) -> unquote(Expr)) {
|
|
@@ -4168,6 +4514,558 @@ export
|
|
|
4168
4514
|
;
|
|
4169
4515
|
|
|
4170
4516
|
|
|
4517
|
+
// DeriveContext — context passed to derive rule functions (needs Option)
|
|
4518
|
+
DeriveContext :: struct(
|
|
4519
|
+
target : Expr,
|
|
4520
|
+
forall_params : Option(Expr),
|
|
4521
|
+
where_clause : Option(Expr)
|
|
4522
|
+
);
|
|
4523
|
+
impl(DeriveContext, Comptime());
|
|
4524
|
+
|
|
4525
|
+
impl(DeriveContext,
|
|
4526
|
+
make_impl : (fn(comptime(self) : DeriveContext, comptime(trait_body) : Expr) -> comptime(Expr))(
|
|
4527
|
+
cond(
|
|
4528
|
+
self.forall_params.comptime_is_some() => cond(
|
|
4529
|
+
self.where_clause.comptime_is_some() =>
|
|
4530
|
+
quote(impl(
|
|
4531
|
+
unquote(self.forall_params.comptime_unwrap()),
|
|
4532
|
+
unquote(self.target),
|
|
4533
|
+
unquote(self.where_clause.comptime_unwrap()),
|
|
4534
|
+
unquote(trait_body)
|
|
4535
|
+
)),
|
|
4536
|
+
true =>
|
|
4537
|
+
quote(impl(
|
|
4538
|
+
unquote(self.forall_params.comptime_unwrap()),
|
|
4539
|
+
unquote(self.target),
|
|
4540
|
+
unquote(trait_body)
|
|
4541
|
+
))
|
|
4542
|
+
),
|
|
4543
|
+
true => quote(impl(
|
|
4544
|
+
unquote(self.target),
|
|
4545
|
+
unquote(trait_body)
|
|
4546
|
+
))
|
|
4547
|
+
)
|
|
4548
|
+
)
|
|
4549
|
+
);
|
|
4550
|
+
export DeriveContext;
|
|
4551
|
+
|
|
4552
|
+
// ============================================================
|
|
4553
|
+
// Built-in derive rules
|
|
4554
|
+
// ============================================================
|
|
4555
|
+
|
|
4556
|
+
// Helper: fold over a range [0..n) at comptime
|
|
4557
|
+
__yo_comptime_fold_range :: (fn(
|
|
4558
|
+
comptime(n) : usize,
|
|
4559
|
+
comptime(init) : comptime_string,
|
|
4560
|
+
comptime(f) : (fn(comptime(acc) : comptime_string, comptime(i) : usize) -> comptime(comptime_string))
|
|
4561
|
+
) -> comptime(comptime_string))(
|
|
4562
|
+
cond(
|
|
4563
|
+
(n == usize(0)) => init,
|
|
4564
|
+
(n == usize(1)) => f(init, usize(0)),
|
|
4565
|
+
true => f(recur((n - usize(1)), init, f), (n - usize(1)))
|
|
4566
|
+
)
|
|
4567
|
+
);
|
|
4568
|
+
|
|
4569
|
+
// Helper: concat 2 strings
|
|
4570
|
+
__s2 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string) -> comptime(comptime_string))((a + b));
|
|
4571
|
+
// Helper: concat 3 strings
|
|
4572
|
+
__s3 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string, comptime(c) : comptime_string) -> comptime(comptime_string))(
|
|
4573
|
+
((a + b) + c)
|
|
4574
|
+
);
|
|
4575
|
+
// Helper: concat 4 strings
|
|
4576
|
+
__s4 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string, comptime(c) : comptime_string, comptime(d) : comptime_string) -> comptime(comptime_string))(
|
|
4577
|
+
(((a + b) + c) + d)
|
|
4578
|
+
);
|
|
4579
|
+
// Helper: concat 5 strings
|
|
4580
|
+
__s5 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string, comptime(c) : comptime_string, comptime(d) : comptime_string, comptime(e) : comptime_string) -> comptime(comptime_string))(
|
|
4581
|
+
((((a + b) + c) + d) + e)
|
|
4582
|
+
);
|
|
4583
|
+
|
|
4584
|
+
// --- derive_rule for Eq ---
|
|
4585
|
+
__derive_eq :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime(trait_params) : ComptimeList(Expr)) -> comptime(Expr))(
|
|
4586
|
+
{
|
|
4587
|
+
info :: Type.get_info(T);
|
|
4588
|
+
cond(
|
|
4589
|
+
info.is_struct() => {
|
|
4590
|
+
eq_body :: cond(
|
|
4591
|
+
(Type.get_struct_fields(T).len() == usize(0)) => quote(true),
|
|
4592
|
+
true => Type.join_fields(
|
|
4593
|
+
T,
|
|
4594
|
+
(fn(comptime(field) : FieldInfo) -> comptime(Expr))(
|
|
4595
|
+
quote((lhs.(#(field.name.to_expr())) == rhs.(#(field.name.to_expr()))))
|
|
4596
|
+
),
|
|
4597
|
+
quote(&&)
|
|
4598
|
+
)
|
|
4599
|
+
);
|
|
4600
|
+
ctx.make_impl(quote(
|
|
4601
|
+
Eq(...#(trait_params))(
|
|
4602
|
+
(==) : ((lhs, rhs) -> #(eq_body))
|
|
4603
|
+
)
|
|
4604
|
+
))
|
|
4605
|
+
},
|
|
4606
|
+
info.is_enum() => {
|
|
4607
|
+
variants :: Type.get_enum_variants(T);
|
|
4608
|
+
vc :: variants.len();
|
|
4609
|
+
eq_branches :: __yo_comptime_fold_range(vc, "",
|
|
4610
|
+
(fn(comptime(acc) : comptime_string, comptime(vi) : usize) -> comptime(comptime_string))(
|
|
4611
|
+
{
|
|
4612
|
+
v :: variants.get(vi);
|
|
4613
|
+
branch :: cond(
|
|
4614
|
+
(v.fields.len() == 0) =>
|
|
4615
|
+
__s5(".", v.name, " => match(rhs, .", v.name, " => true, _ => false)"),
|
|
4616
|
+
true => {
|
|
4617
|
+
lhs_bindings :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4618
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4619
|
+
{
|
|
4620
|
+
fname :: v.fields.get(fi).name;
|
|
4621
|
+
cond(
|
|
4622
|
+
(a == "") => __s2("__lhs_", fname),
|
|
4623
|
+
true => __s3(a, ", __lhs_", fname)
|
|
4624
|
+
)
|
|
4625
|
+
}
|
|
4626
|
+
)
|
|
4627
|
+
);
|
|
4628
|
+
rhs_bindings :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4629
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4630
|
+
{
|
|
4631
|
+
fname :: v.fields.get(fi).name;
|
|
4632
|
+
cond(
|
|
4633
|
+
(a == "") => __s2("__rhs_", fname),
|
|
4634
|
+
true => __s3(a, ", __rhs_", fname)
|
|
4635
|
+
)
|
|
4636
|
+
}
|
|
4637
|
+
)
|
|
4638
|
+
);
|
|
4639
|
+
eq_cmp :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4640
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4641
|
+
{
|
|
4642
|
+
fname :: v.fields.get(fi).name;
|
|
4643
|
+
part :: __s4("(__lhs_", fname, " == __rhs_", fname);
|
|
4644
|
+
cond(
|
|
4645
|
+
(a == "") => __s2(part, ")"),
|
|
4646
|
+
true => __s5("(", a, " && ", __s2(part, ")"), ")")
|
|
4647
|
+
)
|
|
4648
|
+
}
|
|
4649
|
+
)
|
|
4650
|
+
);
|
|
4651
|
+
__s5(
|
|
4652
|
+
__s3(".", v.name, "("),
|
|
4653
|
+
lhs_bindings,
|
|
4654
|
+
__s3(") => match(rhs, .", v.name, "("),
|
|
4655
|
+
rhs_bindings,
|
|
4656
|
+
__s3(") => ", eq_cmp, ", _ => false)")
|
|
4657
|
+
)
|
|
4658
|
+
}
|
|
4659
|
+
);
|
|
4660
|
+
cond(
|
|
4661
|
+
(acc == "") => branch,
|
|
4662
|
+
true => __s3(acc, ",\n ", branch)
|
|
4663
|
+
)
|
|
4664
|
+
}
|
|
4665
|
+
)
|
|
4666
|
+
);
|
|
4667
|
+
match_body :: __s3("match(lhs,\n ", eq_branches, "\n )");
|
|
4668
|
+
ctx.make_impl(quote(
|
|
4669
|
+
Eq(...#(trait_params))(
|
|
4670
|
+
(==) : ((lhs, rhs) -> #(match_body.to_expr()))
|
|
4671
|
+
)
|
|
4672
|
+
))
|
|
4673
|
+
},
|
|
4674
|
+
true => {
|
|
4675
|
+
ctx.make_impl(quote(
|
|
4676
|
+
Eq(...#(trait_params))(
|
|
4677
|
+
(==) : ((lhs, rhs) -> true)
|
|
4678
|
+
)
|
|
4679
|
+
))
|
|
4680
|
+
}
|
|
4681
|
+
)
|
|
4682
|
+
}
|
|
4683
|
+
);
|
|
4684
|
+
derive_rule(Eq, __derive_eq);
|
|
4685
|
+
|
|
4686
|
+
// --- derive_rule for Clone ---
|
|
4687
|
+
__derive_clone :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime(trait_params) : ComptimeList(Expr)) -> comptime(Expr))(
|
|
4688
|
+
{
|
|
4689
|
+
info :: Type.get_info(T);
|
|
4690
|
+
cond(
|
|
4691
|
+
info.is_struct() => {
|
|
4692
|
+
fields :: Type.get_struct_fields(T);
|
|
4693
|
+
fc :: fields.len();
|
|
4694
|
+
type_name :: Type.to_comptime_string(T);
|
|
4695
|
+
cloned_fields :: __yo_comptime_fold_range(fc, "",
|
|
4696
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4697
|
+
{
|
|
4698
|
+
fname :: fields.get(fi).name;
|
|
4699
|
+
part :: __s3("(&(self.*.", fname, ")).clone()");
|
|
4700
|
+
cond(
|
|
4701
|
+
(a == "") => part,
|
|
4702
|
+
true => __s3(a, ", ", part)
|
|
4703
|
+
)
|
|
4704
|
+
}
|
|
4705
|
+
)
|
|
4706
|
+
);
|
|
4707
|
+
clone_body :: __s3(type_name, "(", __s2(cloned_fields, ")"));
|
|
4708
|
+
ctx.make_impl(quote(
|
|
4709
|
+
Clone(
|
|
4710
|
+
clone : ((self) -> #(clone_body.to_expr()))
|
|
4711
|
+
)
|
|
4712
|
+
))
|
|
4713
|
+
},
|
|
4714
|
+
info.is_enum() => {
|
|
4715
|
+
variants :: Type.get_enum_variants(T);
|
|
4716
|
+
vc :: variants.len();
|
|
4717
|
+
clone_branches :: __yo_comptime_fold_range(vc, "",
|
|
4718
|
+
(fn(comptime(acc) : comptime_string, comptime(vi) : usize) -> comptime(comptime_string))(
|
|
4719
|
+
{
|
|
4720
|
+
v :: variants.get(vi);
|
|
4721
|
+
branch :: cond(
|
|
4722
|
+
(v.fields.len() == 0) => __s4(".", v.name, " => .", v.name),
|
|
4723
|
+
true => {
|
|
4724
|
+
bindings :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4725
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4726
|
+
{
|
|
4727
|
+
fname :: v.fields.get(fi).name;
|
|
4728
|
+
cond(
|
|
4729
|
+
(a == "") => __s2("__v_", fname),
|
|
4730
|
+
true => __s3(a, ", __v_", fname)
|
|
4731
|
+
)
|
|
4732
|
+
}
|
|
4733
|
+
)
|
|
4734
|
+
);
|
|
4735
|
+
cloned :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4736
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4737
|
+
{
|
|
4738
|
+
fname :: v.fields.get(fi).name;
|
|
4739
|
+
cond(
|
|
4740
|
+
(a == "") => __s3("(&(__v_", fname, ")).clone()"),
|
|
4741
|
+
true => __s4(a, ", (&(__v_", fname, ")).clone()")
|
|
4742
|
+
)
|
|
4743
|
+
}
|
|
4744
|
+
)
|
|
4745
|
+
);
|
|
4746
|
+
__s5(
|
|
4747
|
+
__s3(".", v.name, "("),
|
|
4748
|
+
bindings,
|
|
4749
|
+
__s3(") => .", v.name, "("),
|
|
4750
|
+
cloned,
|
|
4751
|
+
")"
|
|
4752
|
+
)
|
|
4753
|
+
}
|
|
4754
|
+
);
|
|
4755
|
+
cond(
|
|
4756
|
+
(acc == "") => branch,
|
|
4757
|
+
true => __s3(acc, ",\n ", branch)
|
|
4758
|
+
)
|
|
4759
|
+
}
|
|
4760
|
+
)
|
|
4761
|
+
);
|
|
4762
|
+
match_body :: __s3("match(self.*,\n ", clone_branches, "\n )");
|
|
4763
|
+
ctx.make_impl(quote(
|
|
4764
|
+
Clone(
|
|
4765
|
+
clone : ((self) -> #(match_body.to_expr()))
|
|
4766
|
+
)
|
|
4767
|
+
))
|
|
4768
|
+
},
|
|
4769
|
+
true => {
|
|
4770
|
+
ctx.make_impl(quote(
|
|
4771
|
+
Clone(
|
|
4772
|
+
clone : ((self) -> self.*)
|
|
4773
|
+
)
|
|
4774
|
+
))
|
|
4775
|
+
}
|
|
4776
|
+
)
|
|
4777
|
+
}
|
|
4778
|
+
);
|
|
4779
|
+
derive_rule(Clone, __derive_clone);
|
|
4780
|
+
|
|
4781
|
+
// --- derive_rule for Hash ---
|
|
4782
|
+
__derive_hash :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime(trait_params) : ComptimeList(Expr)) -> comptime(Expr))(
|
|
4783
|
+
{
|
|
4784
|
+
info :: Type.get_info(T);
|
|
4785
|
+
cond(
|
|
4786
|
+
info.is_struct() => {
|
|
4787
|
+
fields :: Type.get_struct_fields(T);
|
|
4788
|
+
fc :: fields.len();
|
|
4789
|
+
cond(
|
|
4790
|
+
(fc == 0) => {
|
|
4791
|
+
ctx.make_impl(quote(
|
|
4792
|
+
Hash(
|
|
4793
|
+
(hash) : ((self) -> u64(0))
|
|
4794
|
+
)
|
|
4795
|
+
))
|
|
4796
|
+
},
|
|
4797
|
+
true => {
|
|
4798
|
+
hash_stmts :: __yo_comptime_fold_range(fc, "",
|
|
4799
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4800
|
+
{
|
|
4801
|
+
fname :: fields.get(fi).name;
|
|
4802
|
+
cond(
|
|
4803
|
+
(fi == 0) => __s3("(h : u64) = (&(self.*.", fname, ")).hash()"),
|
|
4804
|
+
true => __s4(a, ";\n h = ((h * u64(31)) + (&(self.*.", fname, ")).hash())")
|
|
4805
|
+
)
|
|
4806
|
+
}
|
|
4807
|
+
)
|
|
4808
|
+
);
|
|
4809
|
+
hash_body :: __s3("{\n ", hash_stmts, ";\n return h;\n }");
|
|
4810
|
+
ctx.make_impl(quote(
|
|
4811
|
+
Hash(
|
|
4812
|
+
(hash) : ((self) -> #(hash_body.to_expr()))
|
|
4813
|
+
)
|
|
4814
|
+
))
|
|
4815
|
+
}
|
|
4816
|
+
)
|
|
4817
|
+
},
|
|
4818
|
+
info.is_enum() => {
|
|
4819
|
+
variants :: Type.get_enum_variants(T);
|
|
4820
|
+
vc :: variants.len();
|
|
4821
|
+
hash_branches :: __yo_comptime_fold_range(vc, "",
|
|
4822
|
+
(fn(comptime(acc) : comptime_string, comptime(vi) : usize) -> comptime(comptime_string))(
|
|
4823
|
+
{
|
|
4824
|
+
v :: variants.get(vi);
|
|
4825
|
+
vi_str :: __yo_comptime_usize_to_comptime_string(vi);
|
|
4826
|
+
branch :: cond(
|
|
4827
|
+
(v.fields.len() == 0) => __s4(".", v.name, " => u64(", __s2(vi_str, ")")),
|
|
4828
|
+
true => {
|
|
4829
|
+
bindings :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4830
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4831
|
+
{
|
|
4832
|
+
fname :: v.fields.get(fi).name;
|
|
4833
|
+
cond(
|
|
4834
|
+
(a == "") => __s2("__v_", fname),
|
|
4835
|
+
true => __s3(a, ", __v_", fname)
|
|
4836
|
+
)
|
|
4837
|
+
}
|
|
4838
|
+
)
|
|
4839
|
+
);
|
|
4840
|
+
hash_stmts :: __yo_comptime_fold_range(v.fields.len(), __s3("(h : u64) = u64(", vi_str, ")"),
|
|
4841
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4842
|
+
{
|
|
4843
|
+
fname :: v.fields.get(fi).name;
|
|
4844
|
+
__s4(a, "; h = ((h * u64(31)) + (&(__v_", fname, ")).hash())")
|
|
4845
|
+
}
|
|
4846
|
+
)
|
|
4847
|
+
);
|
|
4848
|
+
__s5(
|
|
4849
|
+
__s3(".", v.name, "("),
|
|
4850
|
+
bindings,
|
|
4851
|
+
") => { ",
|
|
4852
|
+
__s2(hash_stmts, "; h"),
|
|
4853
|
+
"; }"
|
|
4854
|
+
)
|
|
4855
|
+
}
|
|
4856
|
+
);
|
|
4857
|
+
cond(
|
|
4858
|
+
(acc == "") => branch,
|
|
4859
|
+
true => __s3(acc, ",\n ", branch)
|
|
4860
|
+
)
|
|
4861
|
+
}
|
|
4862
|
+
)
|
|
4863
|
+
);
|
|
4864
|
+
match_body :: __s3("match(self.*,\n ", hash_branches, "\n )");
|
|
4865
|
+
ctx.make_impl(quote(
|
|
4866
|
+
Hash(
|
|
4867
|
+
(hash) : ((self) -> #(match_body.to_expr()))
|
|
4868
|
+
)
|
|
4869
|
+
))
|
|
4870
|
+
},
|
|
4871
|
+
true => {
|
|
4872
|
+
ctx.make_impl(quote(
|
|
4873
|
+
Hash(
|
|
4874
|
+
(hash) : ((self) -> u64(0))
|
|
4875
|
+
)
|
|
4876
|
+
))
|
|
4877
|
+
}
|
|
4878
|
+
)
|
|
4879
|
+
}
|
|
4880
|
+
);
|
|
4881
|
+
derive_rule(Hash, __derive_hash);
|
|
4882
|
+
|
|
4883
|
+
// --- derive_rule for Ord ---
|
|
4884
|
+
// Helper: generate lexicographic comparison for struct fields
|
|
4885
|
+
__derive_ord_struct_body :: (fn(comptime(T) : Type, comptime(op) : comptime_string) -> comptime(comptime_string))(
|
|
4886
|
+
{
|
|
4887
|
+
fields :: Type.get_struct_fields(T);
|
|
4888
|
+
fc :: fields.len();
|
|
4889
|
+
cond(
|
|
4890
|
+
(fc == 1) => {
|
|
4891
|
+
fname :: fields.get(0).name;
|
|
4892
|
+
__s5("(lhs.", fname, " ", op, __s3(" rhs.", fname, ")"))
|
|
4893
|
+
},
|
|
4894
|
+
true => {
|
|
4895
|
+
branches :: __yo_comptime_fold_range(fc, "",
|
|
4896
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4897
|
+
{
|
|
4898
|
+
fname :: fields.get(fi).name;
|
|
4899
|
+
branch :: cond(
|
|
4900
|
+
(fi == (fc - 1)) =>
|
|
4901
|
+
__s5("true => (lhs.", fname, " ", op, __s3(" rhs.", fname, ")")),
|
|
4902
|
+
true =>
|
|
4903
|
+
__s5("(lhs.", fname, " != rhs.", fname, __s5(") => (lhs.", fname, " ", op, __s3(" rhs.", fname, ")")))
|
|
4904
|
+
);
|
|
4905
|
+
cond(
|
|
4906
|
+
(a == "") => branch,
|
|
4907
|
+
true => __s3(a, ",\n ", branch)
|
|
4908
|
+
)
|
|
4909
|
+
}
|
|
4910
|
+
)
|
|
4911
|
+
);
|
|
4912
|
+
__s3("cond(\n ", branches, "\n )")
|
|
4913
|
+
}
|
|
4914
|
+
)
|
|
4915
|
+
}
|
|
4916
|
+
);
|
|
4917
|
+
|
|
4918
|
+
// Helper: generate lexicographic comparison for enum variant bindings
|
|
4919
|
+
__derive_ord_variant_body :: (fn(comptime(v) : VariantInfo, comptime(op) : comptime_string) -> comptime(comptime_string))(
|
|
4920
|
+
{
|
|
4921
|
+
cond(
|
|
4922
|
+
(v.fields.len() == 1) => {
|
|
4923
|
+
fname :: v.fields.get(0).name;
|
|
4924
|
+
__s5("(__lhs_", fname, " ", op, __s3(" __rhs_", fname, ")"))
|
|
4925
|
+
},
|
|
4926
|
+
true => {
|
|
4927
|
+
branches :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4928
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
4929
|
+
{
|
|
4930
|
+
fname :: v.fields.get(fi).name;
|
|
4931
|
+
branch :: cond(
|
|
4932
|
+
(fi == (v.fields.len() - 1)) =>
|
|
4933
|
+
__s5("true => (__lhs_", fname, " ", op, __s3(" __rhs_", fname, ")")),
|
|
4934
|
+
true =>
|
|
4935
|
+
__s5("(__lhs_", fname, " != __rhs_", fname, __s5(") => (__lhs_", fname, " ", op, __s3(" __rhs_", fname, ")")))
|
|
4936
|
+
);
|
|
4937
|
+
cond(
|
|
4938
|
+
(a == "") => branch,
|
|
4939
|
+
true => __s3(a, ",\n ", branch)
|
|
4940
|
+
)
|
|
4941
|
+
}
|
|
4942
|
+
)
|
|
4943
|
+
);
|
|
4944
|
+
__s3("cond(\n ", branches, "\n )")
|
|
4945
|
+
}
|
|
4946
|
+
)
|
|
4947
|
+
}
|
|
4948
|
+
);
|
|
4949
|
+
|
|
4950
|
+
__derive_ord :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime(trait_params) : ComptimeList(Expr)) -> comptime(Expr))(
|
|
4951
|
+
{
|
|
4952
|
+
info :: Type.get_info(T);
|
|
4953
|
+
cond(
|
|
4954
|
+
info.is_struct() => {
|
|
4955
|
+
fields :: Type.get_struct_fields(T);
|
|
4956
|
+
fc :: fields.len();
|
|
4957
|
+
cond(
|
|
4958
|
+
(fc == 0) => {
|
|
4959
|
+
ctx.make_impl(quote(
|
|
4960
|
+
Ord(...#(trait_params))(
|
|
4961
|
+
(<) : ((lhs, rhs) -> false),
|
|
4962
|
+
(<=) : ((lhs, rhs) -> true),
|
|
4963
|
+
(>) : ((lhs, rhs) -> false),
|
|
4964
|
+
(>=) : ((lhs, rhs) -> true)
|
|
4965
|
+
)
|
|
4966
|
+
))
|
|
4967
|
+
},
|
|
4968
|
+
true => {
|
|
4969
|
+
lt_body :: __derive_ord_struct_body(T, "<");
|
|
4970
|
+
le_body :: __derive_ord_struct_body(T, "<=");
|
|
4971
|
+
gt_body :: __derive_ord_struct_body(T, ">");
|
|
4972
|
+
ge_body :: __derive_ord_struct_body(T, ">=");
|
|
4973
|
+
ctx.make_impl(quote(
|
|
4974
|
+
Ord(...#(trait_params))(
|
|
4975
|
+
(<) : ((lhs, rhs) -> #(lt_body.to_expr())),
|
|
4976
|
+
(<=) : ((lhs, rhs) -> #(le_body.to_expr())),
|
|
4977
|
+
(>) : ((lhs, rhs) -> #(gt_body.to_expr())),
|
|
4978
|
+
(>=) : ((lhs, rhs) -> #(ge_body.to_expr()))
|
|
4979
|
+
)
|
|
4980
|
+
))
|
|
4981
|
+
}
|
|
4982
|
+
)
|
|
4983
|
+
},
|
|
4984
|
+
info.is_enum() => {
|
|
4985
|
+
variants :: Type.get_enum_variants(T);
|
|
4986
|
+
vc :: variants.len();
|
|
4987
|
+
// Generate one operator's match branches
|
|
4988
|
+
__gen_op_branches :: (fn(comptime(op) : comptime_string, comptime(eq_val) : comptime_string) -> comptime(comptime_string))(
|
|
4989
|
+
{
|
|
4990
|
+
branches :: __yo_comptime_fold_range(vc, "",
|
|
4991
|
+
(fn(comptime(acc) : comptime_string, comptime(vi) : usize) -> comptime(comptime_string))(
|
|
4992
|
+
{
|
|
4993
|
+
v :: variants.get(vi);
|
|
4994
|
+
branch :: cond(
|
|
4995
|
+
(v.fields.len() == 0) =>
|
|
4996
|
+
__s5(".", v.name, " => match(rhs, .", v.name, __s4(" => ", eq_val, ", _ => (i32(lhs) ", __s2(op, " i32(rhs)))"))),
|
|
4997
|
+
true => {
|
|
4998
|
+
lhs_bindings :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
4999
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
5000
|
+
{
|
|
5001
|
+
fname :: v.fields.get(fi).name;
|
|
5002
|
+
cond(
|
|
5003
|
+
(a == "") => __s2("__lhs_", fname),
|
|
5004
|
+
true => __s3(a, ", __lhs_", fname)
|
|
5005
|
+
)
|
|
5006
|
+
}
|
|
5007
|
+
)
|
|
5008
|
+
);
|
|
5009
|
+
rhs_bindings :: __yo_comptime_fold_range(v.fields.len(), "",
|
|
5010
|
+
(fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
|
|
5011
|
+
{
|
|
5012
|
+
fname :: v.fields.get(fi).name;
|
|
5013
|
+
cond(
|
|
5014
|
+
(a == "") => __s2("__rhs_", fname),
|
|
5015
|
+
true => __s3(a, ", __rhs_", fname)
|
|
5016
|
+
)
|
|
5017
|
+
}
|
|
5018
|
+
)
|
|
5019
|
+
);
|
|
5020
|
+
field_cmp :: __derive_ord_variant_body(v, op);
|
|
5021
|
+
__s5(
|
|
5022
|
+
__s3(".", v.name, "("),
|
|
5023
|
+
lhs_bindings,
|
|
5024
|
+
__s3(") => match(rhs, .", v.name, "("),
|
|
5025
|
+
rhs_bindings,
|
|
5026
|
+
__s4(") => ", field_cmp, ", _ => (i32(lhs) ", __s2(op, " i32(rhs)))"))
|
|
5027
|
+
)
|
|
5028
|
+
}
|
|
5029
|
+
);
|
|
5030
|
+
cond(
|
|
5031
|
+
(acc == "") => branch,
|
|
5032
|
+
true => __s3(acc, ",\n ", branch)
|
|
5033
|
+
)
|
|
5034
|
+
}
|
|
5035
|
+
)
|
|
5036
|
+
);
|
|
5037
|
+
__s3("match(lhs,\n ", branches, "\n )")
|
|
5038
|
+
}
|
|
5039
|
+
);
|
|
5040
|
+
lt_match :: __gen_op_branches("<", "false");
|
|
5041
|
+
le_match :: __gen_op_branches("<=", "true");
|
|
5042
|
+
gt_match :: __gen_op_branches(">", "false");
|
|
5043
|
+
ge_match :: __gen_op_branches(">=", "true");
|
|
5044
|
+
ctx.make_impl(quote(
|
|
5045
|
+
Ord(...#(trait_params))(
|
|
5046
|
+
(<) : ((lhs, rhs) -> #(lt_match.to_expr())),
|
|
5047
|
+
(<=) : ((lhs, rhs) -> #(le_match.to_expr())),
|
|
5048
|
+
(>) : ((lhs, rhs) -> #(gt_match.to_expr())),
|
|
5049
|
+
(>=) : ((lhs, rhs) -> #(ge_match.to_expr()))
|
|
5050
|
+
)
|
|
5051
|
+
))
|
|
5052
|
+
},
|
|
5053
|
+
true => {
|
|
5054
|
+
ctx.make_impl(quote(
|
|
5055
|
+
Ord(...#(trait_params))(
|
|
5056
|
+
(<) : ((lhs, rhs) -> false),
|
|
5057
|
+
(<=) : ((lhs, rhs) -> true),
|
|
5058
|
+
(>) : ((lhs, rhs) -> false),
|
|
5059
|
+
(>=) : ((lhs, rhs) -> true)
|
|
5060
|
+
)
|
|
5061
|
+
))
|
|
5062
|
+
}
|
|
5063
|
+
)
|
|
5064
|
+
}
|
|
5065
|
+
);
|
|
5066
|
+
derive_rule(Ord, __derive_ord);
|
|
5067
|
+
|
|
5068
|
+
|
|
4171
5069
|
/// === Result ===
|
|
4172
5070
|
Result :: (fn(comptime(OkType): Type, comptime(ErrorType): Type) -> comptime(Type))
|
|
4173
5071
|
enum(
|