2022-10-28 04:59:24 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const zls = @import("zls");
|
|
|
|
|
|
|
|
const Ast = std.zig.Ast;
|
|
|
|
|
|
|
|
const ComptimeInterpreter = zls.ComptimeInterpreter;
|
|
|
|
|
|
|
|
const allocator: std.mem.Allocator = std.testing.allocator;
|
|
|
|
|
|
|
|
test "ComptimeInterpreter - basic test" {
|
2022-11-11 01:51:02 +00:00
|
|
|
var config = zls.Config{};
|
|
|
|
var doc_store = zls.DocumentStore{
|
|
|
|
.allocator = allocator,
|
|
|
|
.config = &config,
|
|
|
|
};
|
|
|
|
defer doc_store.deinit();
|
|
|
|
|
|
|
|
_ = try doc_store.openDocument("file:///file.zig",
|
2022-10-28 19:24:38 +01:00
|
|
|
\\pub fn ReturnMyType(comptime my_arg: bool) type {
|
|
|
|
\\ var abc = z: {break :z if (!my_arg) 123 else 0;};
|
2022-10-28 06:22:03 +01:00
|
|
|
\\ if (abc == 123) return u69;
|
2022-10-28 04:59:24 +01:00
|
|
|
\\ return u8;
|
|
|
|
\\}
|
|
|
|
);
|
|
|
|
|
2022-11-11 01:51:02 +00:00
|
|
|
var interpreter = ComptimeInterpreter{
|
|
|
|
.allocator = allocator,
|
|
|
|
.document_store = &doc_store,
|
|
|
|
.uri = "file:///file.zig",
|
|
|
|
};
|
2022-10-28 04:59:24 +01:00
|
|
|
defer interpreter.deinit();
|
|
|
|
|
2022-11-11 01:51:02 +00:00
|
|
|
_ = try interpreter.interpret(0, null, .{});
|
|
|
|
|
2022-10-28 19:24:38 +01:00
|
|
|
var bool_type = try interpreter.createType(std.math.maxInt(std.zig.Ast.Node.Index), .{ .@"bool" = .{} });
|
|
|
|
var arg_false = ComptimeInterpreter.Value{
|
2022-11-11 01:51:02 +00:00
|
|
|
.interpreter = &interpreter,
|
2022-10-28 19:24:38 +01:00
|
|
|
.node_idx = std.math.maxInt(std.zig.Ast.Node.Index),
|
|
|
|
.@"type" = bool_type,
|
2022-11-11 01:51:02 +00:00
|
|
|
.value_data = try interpreter.createValueData(.{ .@"bool" = false }),
|
2022-10-28 19:24:38 +01:00
|
|
|
};
|
|
|
|
var arg_true = ComptimeInterpreter.Value{
|
2022-11-11 01:51:02 +00:00
|
|
|
.interpreter = &interpreter,
|
2022-10-28 19:24:38 +01:00
|
|
|
.node_idx = std.math.maxInt(std.zig.Ast.Node.Index),
|
|
|
|
.@"type" = bool_type,
|
2022-11-11 01:51:02 +00:00
|
|
|
.value_data = try interpreter.createValueData(.{ .@"bool" = true }),
|
2022-10-28 19:24:38 +01:00
|
|
|
};
|
|
|
|
|
2022-11-11 01:51:02 +00:00
|
|
|
const rmt = interpreter.root_type.?.getTypeInfo().@"struct".scope.declarations.get("ReturnMyType").?;
|
|
|
|
|
|
|
|
const call_with_false = try interpreter.call(null, rmt.node_idx, &.{
|
2022-10-28 19:24:38 +01:00
|
|
|
arg_false,
|
|
|
|
}, .{});
|
|
|
|
defer call_with_false.scope.deinit();
|
2022-11-11 01:51:02 +00:00
|
|
|
const call_with_true = try interpreter.call(null, rmt.node_idx, &.{
|
2022-10-28 19:24:38 +01:00
|
|
|
arg_true,
|
|
|
|
}, .{});
|
|
|
|
defer call_with_true.scope.deinit();
|
2022-10-28 04:59:24 +01:00
|
|
|
|
2022-10-29 22:28:44 +01:00
|
|
|
try std.testing.expectFmt("u69", "{any}", .{interpreter.formatTypeInfo(call_with_false.result.value.value_data.@"type".getTypeInfo())});
|
|
|
|
try std.testing.expectFmt("u8", "{any}", .{interpreter.formatTypeInfo(call_with_true.result.value.value_data.@"type".getTypeInfo())});
|
2022-10-28 04:59:24 +01:00
|
|
|
}
|
2022-10-28 06:22:03 +01:00
|
|
|
|
|
|
|
test "ComptimeInterpreter - struct" {
|
2022-11-11 01:51:02 +00:00
|
|
|
var config = zls.Config{};
|
|
|
|
var doc_store = zls.DocumentStore{
|
|
|
|
.allocator = allocator,
|
|
|
|
.config = &config,
|
|
|
|
};
|
|
|
|
defer doc_store.deinit();
|
|
|
|
|
|
|
|
_ = try doc_store.openDocument("file:///file.zig",
|
2022-10-28 06:22:03 +01:00
|
|
|
\\pub fn ReturnMyType() type {
|
|
|
|
\\ return struct {
|
|
|
|
\\ slay: bool,
|
|
|
|
\\ var abc = 123;
|
|
|
|
\\ };
|
|
|
|
\\}
|
|
|
|
);
|
|
|
|
|
2022-11-11 01:51:02 +00:00
|
|
|
var interpreter = ComptimeInterpreter{
|
|
|
|
.allocator = allocator,
|
|
|
|
.document_store = &doc_store,
|
|
|
|
.uri = "file:///file.zig",
|
|
|
|
};
|
2022-10-28 06:22:03 +01:00
|
|
|
defer interpreter.deinit();
|
|
|
|
|
2022-11-11 01:51:02 +00:00
|
|
|
_ = try interpreter.interpret(0, null, .{});
|
|
|
|
|
|
|
|
const rmt = interpreter.root_type.?.getTypeInfo().@"struct".scope.declarations.get("ReturnMyType").?;
|
|
|
|
|
|
|
|
const z = try interpreter.call(null, rmt.node_idx, &.{}, .{});
|
2022-10-28 06:22:03 +01:00
|
|
|
defer z.scope.deinit();
|
|
|
|
|
2022-11-11 01:51:02 +00:00
|
|
|
try std.testing.expectFmt("struct {slay: bool, var abc: comptime_int = 123, }", "{any}", .{interpreter.formatTypeInfo(z.result.value.value_data.@"type".getTypeInfo())});
|
2022-10-28 06:22:03 +01:00
|
|
|
}
|