zls/tests/language_features/comptime_interpreter.zig

99 lines
3.2 KiB
Zig
Raw Normal View History

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",
\\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,
2023-01-04 10:12:29 +00:00
.arena = std.heap.ArenaAllocator.init(allocator),
2022-11-11 01:51:02 +00:00
.document_store = &doc_store,
.uri = "file:///file.zig",
};
2022-10-28 04:59:24 +01:00
defer interpreter.deinit();
2023-01-04 10:12:29 +00:00
_ = try interpreter.interpret(0, 0, .{});
var bool_type = try interpreter.ip.get(allocator, .{ .simple = .bool });
var bool_true = try interpreter.ip.get(allocator, .{ .simple = .bool_true });
var bool_false = try interpreter.ip.get(allocator, .{ .simple = .bool_false });
2022-11-11 01:51:02 +00:00
var arg_false = ComptimeInterpreter.Value{
2022-11-11 01:51:02 +00:00
.interpreter = &interpreter,
2023-01-04 10:12:29 +00:00
.node_idx = std.math.maxInt(Ast.Node.Index),
.ty = bool_type,
.val = bool_false,
};
var arg_true = ComptimeInterpreter.Value{
2022-11-11 01:51:02 +00:00
.interpreter = &interpreter,
2023-01-04 10:12:29 +00:00
.node_idx = std.math.maxInt(Ast.Node.Index),
.ty = bool_type,
.val = bool_true,
};
2023-01-04 10:12:29 +00:00
const function_node: Ast.Node.Index = 4;
2022-11-11 01:51:02 +00:00
2023-01-04 10:12:29 +00:00
const call_with_false = try interpreter.call(0, function_node, &.{arg_false}, .{});
const call_with_true = try interpreter.call(0, function_node, &.{arg_true}, .{});
2022-10-28 04:59:24 +01:00
2023-01-04 10:12:29 +00:00
try std.testing.expectFmt("u69", "{any}", .{call_with_false.result.value.val.fmtValue(call_with_false.result.value.ty, &interpreter.ip)});
try std.testing.expectFmt("u8", "{any}", .{call_with_true.result.value.val.fmtValue(call_with_true.result.value.ty, &interpreter.ip)});
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,
2023-01-04 10:12:29 +00:00
.arena = std.heap.ArenaAllocator.init(allocator),
2022-11-11 01:51:02 +00:00
.document_store = &doc_store,
.uri = "file:///file.zig",
};
2022-10-28 06:22:03 +01:00
defer interpreter.deinit();
2023-01-04 10:12:29 +00:00
_ = try interpreter.interpret(0, 0, .{});
2022-11-11 01:51:02 +00:00
2023-01-04 10:12:29 +00:00
const function_node: Ast.Node.Index = 3;
2022-11-11 01:51:02 +00:00
2023-01-04 10:12:29 +00:00
const call_result = try interpreter.call(0, function_node, &.{}, .{});
2022-10-28 06:22:03 +01:00
2023-01-04 10:12:29 +00:00
const result_struct = interpreter.ip.indexToKey(call_result.result.value.val).struct_type;
2023-01-06 13:38:28 +00:00
try std.testing.expectEqual(@intCast(usize, 1), result_struct.fields.len);
try std.testing.expectEqualStrings("slay", result_struct.fields[0].name);
try std.testing.expectFmt("bool", "{}", .{result_struct.fields[0].ty.fmtType(&interpreter.ip)});
2022-10-28 06:22:03 +01:00
}