Hacky mess but it works (only if your function is the first root decl tho :P)

This commit is contained in:
Auguste Rame
2022-10-28 14:24:38 -04:00
parent 06e8756849
commit 779c3c0710
4 changed files with 133 additions and 116 deletions

View File

@@ -9,8 +9,8 @@ const allocator: std.mem.Allocator = std.testing.allocator;
test "ComptimeInterpreter - basic test" {
var tree = try std.zig.parse(allocator,
\\pub fn ReturnMyType() type {
\\ var abc = z: {break :z if (!false) 123 else 0;};
\\pub fn ReturnMyType(comptime my_arg: bool) type {
\\ var abc = z: {break :z if (!my_arg) 123 else 0;};
\\ if (abc == 123) return u69;
\\ return u8;
\\}
@@ -20,10 +20,29 @@ test "ComptimeInterpreter - basic test" {
var interpreter = ComptimeInterpreter{ .tree = tree, .allocator = allocator };
defer interpreter.deinit();
const z = try interpreter.call(tree.rootDecls()[0], &.{}, .{});
defer z.scope.deinit();
var bool_type = try interpreter.createType(std.math.maxInt(std.zig.Ast.Node.Index), .{ .@"bool" = .{} });
var arg_false = ComptimeInterpreter.Value{
.node_idx = std.math.maxInt(std.zig.Ast.Node.Index),
.@"type" = bool_type,
.value_data = .{ .@"bool" = false },
};
var arg_true = ComptimeInterpreter.Value{
.node_idx = std.math.maxInt(std.zig.Ast.Node.Index),
.@"type" = bool_type,
.value_data = .{ .@"bool" = true },
};
try std.testing.expectFmt("u69", "{any}", .{interpreter.formatTypeInfo(interpreter.typeToTypeInfo(z.result.value.value_data.@"type"))});
const call_with_false = try interpreter.call(tree.rootDecls()[0], &.{
arg_false,
}, .{});
defer call_with_false.scope.deinit();
const call_with_true = try interpreter.call(tree.rootDecls()[0], &.{
arg_true,
}, .{});
defer call_with_true.scope.deinit();
try std.testing.expectFmt("u69", "{any}", .{interpreter.formatTypeInfo(interpreter.typeToTypeInfo(call_with_false.result.value.value_data.@"type"))});
try std.testing.expectFmt("u8", "{any}", .{interpreter.formatTypeInfo(interpreter.typeToTypeInfo(call_with_true.result.value.value_data.@"type"))});
}
test "ComptimeInterpreter - struct" {