File tree Expand file tree Collapse file tree 3 files changed +29
-8
lines changed Expand file tree Collapse file tree 3 files changed +29
-8
lines changed Original file line number Diff line number Diff line change @@ -62,8 +62,9 @@ const TestValue = union(Tag) {
62
62
gpa : std.mem.Allocator ,
63
63
args : []const TestValue ,
64
64
) ! TestValue {
65
+ _ = gpa ;
65
66
if (args .len != 0 ) return .{ .err = "'len' wants no arguments" };
66
- return TestValue .from (gpa , str .len );
67
+ return TestValue .from (str .len );
67
68
}
68
69
};
69
70
};
@@ -86,7 +87,7 @@ const TestValue = union(Tag) {
86
87
return .{ .bool = b };
87
88
}
88
89
89
- pub fn from (gpa : std.mem.Allocator , value : anytype ) TestValue {
90
+ pub fn from (gpa : std.mem.Allocator , value : anytype ) ! TestValue {
90
91
_ = gpa ;
91
92
const T = @TypeOf (value );
92
93
switch (T ) {
Original file line number Diff line number Diff line change @@ -44,7 +44,10 @@ pub fn defaultCall(comptime Value: type) fn (
44
44
) error { OutOfMemory , Interrupt }! Value {
45
45
switch (value ) {
46
46
inline else = > | v , tag | {
47
- const Builtin = Value .builtinsFor (tag );
47
+ const Builtin = if (@hasDecl (Value , "builtinsFor" ))
48
+ Value .builtinsFor (tag )
49
+ else
50
+ defaultBuiltinsFor (Value , @TypeOf (v ));
48
51
49
52
inline for (@typeInfo (Builtin ).Struct .decls ) | decl | {
50
53
if (decl .name [0 ] == '_' ) continue ;
@@ -86,3 +89,24 @@ inline fn constify(comptime T: type, comptime mut: bool) type {
86
89
false = > * const T ,
87
90
};
88
91
}
92
+
93
+ pub fn defaultBuiltinsFor (comptime Value : type , comptime Field : type ) type {
94
+ inline for (std .meta .fields (Value )) | f | {
95
+ if (f .type == Field ) {
96
+ switch (@typeInfo (f .type )) {
97
+ .Pointer = > | ptr | {
98
+ if (@typeInfo (ptr .child ) == .Struct ) {
99
+ return @field (ptr .child , "Builtins" );
100
+ }
101
+ },
102
+ .Struct = > {
103
+ return @field (f .type , "Builtins" );
104
+ },
105
+ else = > {},
106
+ }
107
+
108
+ return struct {};
109
+ }
110
+ }
111
+ @compileError ("Value has no field of value " ++ @typeName (Field ));
112
+ }
Original file line number Diff line number Diff line change @@ -16,10 +16,6 @@ pub fn VM(
16
16
comptime _Context : type ,
17
17
comptime _Value : type ,
18
18
) type {
19
- if (! @hasDecl (_Value , "builtinsFor" )) {
20
- @compileLog ("Value type must specify builtinsFor" );
21
- }
22
-
23
19
return struct {
24
20
parser : Parser = .{},
25
21
stack : std .MultiArrayList (Result ) = .{},
@@ -180,7 +176,7 @@ pub fn VM(
180
176
const path = src [start .. end ];
181
177
182
178
const old_value = if (global )
183
- Value .from (gpa , ctx )
179
+ try Value .from (gpa , ctx )
184
180
else blk : {
185
181
if (builtin .mode == .Debug ) {
186
182
const stack_debug = slice .items (.debug );
You can’t perform that action at this time.
0 commit comments