| ... | ... | @@ -0,0 +1,113 @@ |
| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | const testing = std.testing; |
| 4 | |
| 5 | const string = "hello world"; |
| 6 | |
| 7 | const TempRef = struct { |
| 8 | index: usize, |
| 9 | is_weak: bool, |
| 10 | }; |
| 11 | |
| 12 | const BuiltinEnum = struct { |
| 13 | name: []const u8, |
| 14 | }; |
| 15 | |
| 16 | const ParamType = union(enum) { |
| 17 | boolean, |
| 18 | buffer, |
| 19 | one_of: BuiltinEnum, |
| 20 | }; |
| 21 | |
| 22 | const CallArg = struct { |
| 23 | value: Expression, |
| 24 | }; |
| 25 | |
| 26 | const Expression = union(enum) { |
| 27 | literal_boolean: bool, |
| 28 | literal_enum_value: EnumLiteral, |
| 29 | }; |
| 30 | |
| 31 | const EnumLiteral = struct { |
| 32 | label: []const u8, |
| 33 | }; |
| 34 | |
| 35 | const ExpressionResult = union(enum) { |
| 36 | temp_buffer: TempRef, |
| 37 | literal_boolean: bool, |
| 38 | literal_enum_value: []const u8, |
| 39 | }; |
| 40 | |
| 41 | fn commitCalleeParam(result: ExpressionResult, callee_param_type: ParamType) ExpressionResult { |
| 42 | switch (callee_param_type) { |
| 43 | .boolean => { |
| 44 | return result; |
| 45 | }, |
| 46 | .buffer => { |
| 47 | return ExpressionResult{ |
| 48 | .temp_buffer = .{ .index = 0, .is_weak = false }, |
| 49 | }; |
| 50 | }, |
| 51 | .one_of => { |
| 52 | return result; |
| 53 | }, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn genExpression(expr: Expression) !ExpressionResult { |
| 58 | switch (expr) { |
| 59 | .literal_boolean => |value| { |
| 60 | return ExpressionResult{ |
| 61 | .literal_boolean = value, |
| 62 | }; |
| 63 | }, |
| 64 | .literal_enum_value => |v| { |
| 65 | try testing.expectEqualStrings(string, v.label); |
| 66 | const result: ExpressionResult = .{ |
| 67 | .literal_enum_value = v.label, |
| 68 | }; |
| 69 | switch (result) { |
| 70 | .literal_enum_value => |w| { |
| 71 | try testing.expectEqualStrings(string, w); |
| 72 | }, |
| 73 | else => {}, |
| 74 | } |
| 75 | return result; |
| 76 | }, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | test { |
| 81 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO |
| 82 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 83 | if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO |
| 84 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 85 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 86 | |
| 87 | var param: ParamType = .{ |
| 88 | .one_of = .{ .name = "name" }, |
| 89 | }; |
| 90 | var arg: CallArg = .{ |
| 91 | .value = .{ |
| 92 | .literal_enum_value = .{ |
| 93 | .label = string, |
| 94 | }, |
| 95 | }, |
| 96 | }; |
| 97 | |
| 98 | const result = try genExpression(arg.value); |
| 99 | switch (result) { |
| 100 | .literal_enum_value => |w| { |
| 101 | try testing.expectEqualStrings(string, w); |
| 102 | }, |
| 103 | else => {}, |
| 104 | } |
| 105 | |
| 106 | const derp = commitCalleeParam(result, param); |
| 107 | switch (derp) { |
| 108 | .literal_enum_value => |w| { |
| 109 | try testing.expectEqualStrings(string, w); |
| 110 | }, |
| 111 | else => {}, |
| 112 | } |
| 113 | } |