diff options
Diffstat (limited to 'src/extended.zig')
| -rw-r--r-- | src/extended.zig | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/extended.zig b/src/extended.zig index ffcce5b..8fb017f 100644 --- a/src/extended.zig +++ b/src/extended.zig | |||
| @@ -29,25 +29,25 @@ pub const Command = struct { | |||
| 29 | params: []const Param, | 29 | params: []const Param, |
| 30 | 30 | ||
| 31 | Result: type, | 31 | Result: type, |
| 32 | default: &const Opaque, | 32 | default: *const Opaque, |
| 33 | 33 | ||
| 34 | pub fn init(comptime Result: type, default: &const Result, params: []const Param) Command { | 34 | pub fn init(comptime Result: type, default: *const Result, params: []const Param) Command { |
| 35 | return Command{ | 35 | return Command{ |
| 36 | .params = params, | 36 | .params = params, |
| 37 | .Result = Result, | 37 | .Result = Result, |
| 38 | .default = @ptrCast(&const Opaque, default), | 38 | .default = @ptrCast(*const Opaque, default), |
| 39 | }; | 39 | }; |
| 40 | } | 40 | } |
| 41 | }; | 41 | }; |
| 42 | 42 | ||
| 43 | pub const Parser = struct { | 43 | pub const Parser = struct { |
| 44 | const UnsafeFunction = &const void; | 44 | const UnsafeFunction = *const void; |
| 45 | 45 | ||
| 46 | FieldType: type, | 46 | FieldType: type, |
| 47 | Errors: type, | 47 | Errors: type, |
| 48 | func: UnsafeFunction, | 48 | func: UnsafeFunction, |
| 49 | 49 | ||
| 50 | pub fn init(comptime FieldType: type, comptime Errors: type, func: parseFunc(FieldType, Errors)) Parser { | 50 | pub fn init(comptime FieldType: type, comptime Errors: type, func: ParseFunc(FieldType, Errors)) Parser { |
| 51 | return Parser { | 51 | return Parser { |
| 52 | .FieldType = FieldType, | 52 | .FieldType = FieldType, |
| 53 | .Errors = Errors, | 53 | .Errors = Errors, |
| @@ -55,20 +55,17 @@ pub const Parser = struct { | |||
| 55 | }; | 55 | }; |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | fn parse(comptime parser: Parser, field_ptr: TakePtr(parser.FieldType), arg: []const u8) parser.Errors!void { | 58 | fn parse(comptime parser: Parser, field_ptr: *parser.FieldType, arg: []const u8) parser.Errors!void { |
| 59 | return @ptrCast(parseFunc(parser.FieldType, parser.Errors), parser.func)(field_ptr, arg); | 59 | return @ptrCast(ParseFunc(parser.FieldType, parser.Errors), parser.func)(field_ptr, arg); |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | // TODO: This is a workaround, since we don't have pointer reform yet. | 62 | fn ParseFunc(comptime FieldType: type, comptime Errors: type) type { |
| 63 | fn TakePtr(comptime T: type) type { return &T; } | 63 | return fn(*FieldType, []const u8) Errors!void; |
| 64 | |||
| 65 | fn parseFunc(comptime FieldType: type, comptime Errors: type) type { | ||
| 66 | return fn(&FieldType, []const u8) Errors!void; | ||
| 67 | } | 64 | } |
| 68 | 65 | ||
| 69 | pub fn int(comptime Int: type, comptime radix: u8) Parser { | 66 | pub fn int(comptime Int: type, comptime radix: u8) Parser { |
| 70 | const func = struct { | 67 | const func = struct { |
| 71 | fn i(field_ptr: &Int, arg: []const u8) !void { | 68 | fn i(field_ptr: *Int, arg: []const u8) !void { |
| 72 | field_ptr.* = try fmt.parseInt(Int, arg, radix); | 69 | field_ptr.* = try fmt.parseInt(Int, arg, radix); |
| 73 | } | 70 | } |
| 74 | }.i; | 71 | }.i; |
| @@ -83,7 +80,7 @@ pub const Parser = struct { | |||
| 83 | []const u8, | 80 | []const u8, |
| 84 | error{}, | 81 | error{}, |
| 85 | struct { | 82 | struct { |
| 86 | fn s(field_ptr: &[]const u8, arg: []const u8) (error{}!void) { | 83 | fn s(field_ptr: *[]const u8, arg: []const u8) (error{}!void) { |
| 87 | field_ptr.* = arg; | 84 | field_ptr.* = arg; |
| 88 | } | 85 | } |
| 89 | }.s | 86 | }.s |
| @@ -98,23 +95,23 @@ pub fn Clap(comptime Result: type) type { | |||
| 98 | params: []const Param, | 95 | params: []const Param, |
| 99 | 96 | ||
| 100 | pub fn parse( | 97 | pub fn parse( |
| 101 | comptime clap: &const Self, | 98 | comptime clap: *const Self, |
| 102 | comptime Error: type, | 99 | comptime Error: type, |
| 103 | iter: &core.ArgIterator(Error), | 100 | iter: *core.ArgIterator(Error), |
| 104 | ) !Result { | 101 | ) !Result { |
| 105 | // We initialize the core.Clap without any params, and fill them out in parseHelper. | 102 | // We initialize the core.Clap without any params, and fill them out in parseHelper. |
| 106 | var c = core.Clap(usize, Error).init([]core.Param(usize){}, iter); | 103 | var c = core.Clap(usize, Error).init([]core.Param(usize){}, iter); |
| 107 | 104 | ||
| 108 | const top_level_command = comptime Command.init(Result, &clap.default, clap.params); | 105 | const top_level_command = comptime Command.init(Result, &clap.default, clap.params); |
| 109 | return try parseHelper(top_level_command, Error, &c); | 106 | return try parseHelper(&top_level_command, Error, &c); |
| 110 | } | 107 | } |
| 111 | 108 | ||
| 112 | fn parseHelper( | 109 | fn parseHelper( |
| 113 | comptime command: &const Command, | 110 | comptime command: *const Command, |
| 114 | comptime Error: type, | 111 | comptime Error: type, |
| 115 | clap: &core.Clap(usize, Error), | 112 | clap: *core.Clap(usize, Error), |
| 116 | ) !command.Result { | 113 | ) !command.Result { |
| 117 | var result = @ptrCast(&const command.Result, command.default).*; | 114 | var result = @ptrCast(*const command.Result, command.default).*; |
| 118 | 115 | ||
| 119 | var handled = comptime blk: { | 116 | var handled = comptime blk: { |
| 120 | var res: [command.params.len]bool = undefined; | 117 | var res: [command.params.len]bool = undefined; |