fix function call resolution

This commit is contained in:
Techatrix 2023-02-11 00:04:08 +01:00
parent 3fda3b5414
commit 5cb0c98db1
3 changed files with 21 additions and 1 deletions

View File

@ -285,6 +285,7 @@ pub fn interpret(
const name = analysis.getDeclName(tree, node_idx).?; const name = analysis.getDeclName(tree, node_idx).?;
const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{ const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{
.name = name, .name = name,
.node_idx = node_idx,
.ty = .none, .ty = .none,
.val = .none, .val = .none,
.alignment = 0, // TODO .alignment = 0, // TODO
@ -450,7 +451,7 @@ pub fn interpret(
if (decl.ty == .none) return InterpretResult{ .nothing = {} }; if (decl.ty == .none) return InterpretResult{ .nothing = {} };
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = decl.node_idx,
.ty = decl.ty, .ty = decl.ty,
.val = decl.val, .val = decl.val,
} }; } };
@ -987,6 +988,7 @@ pub fn interpret(
const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{ const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{
.name = name, .name = name,
.node_idx = node_idx,
.ty = Index.type_type, .ty = Index.type_type,
.val = function_type, .val = function_type,
.alignment = 0, // TODO .alignment = 0, // TODO
@ -1122,6 +1124,9 @@ pub fn call(
// TODO: type check args // TODO: type check args
const tree = interpreter.getHandle().tree; const tree = interpreter.getHandle().tree;
const node_tags = tree.nodes.items(.tag);
if (node_tags[func_node_idx] != .fn_decl) return error.CriticalAstFailure;
var buf: [1]Ast.Node.Index = undefined; var buf: [1]Ast.Node.Index = undefined;
var proto = tree.fullFnProto(&buf, func_node_idx) orelse return error.CriticalAstFailure; var proto = tree.fullFnProto(&buf, func_node_idx) orelse return error.CriticalAstFailure;
@ -1154,6 +1159,7 @@ pub fn call(
const decls = &interpreter.namespaces.items(.decls)[@enumToInt(fn_namespace)]; const decls = &interpreter.namespaces.items(.decls)[@enumToInt(fn_namespace)];
const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{ const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{
.name = name, .name = name,
.node_idx = name_token,
.ty = tex.val, .ty = tex.val,
.val = arguments[arg_index].val, .val = arguments[arg_index].val,
.alignment = 0, // TODO .alignment = 0, // TODO

View File

@ -152,6 +152,7 @@ pub const DeclIndex = enum(u32) { _ };
pub const Decl = struct { pub const Decl = struct {
name: []const u8, name: []const u8,
node_idx: u32,
ty: Index, ty: Index,
val: Index, val: Index,
alignment: u16, alignment: u16,

View File

@ -261,6 +261,17 @@ test "ComptimeInterpreter - call comptime argument" {
try std.testing.expectEqual(Key{ .int_type = .{ .signedness = .unsigned, .bits = 69 } }, result2.val.?); try std.testing.expectEqual(Key{ .int_type = .{ .signedness = .unsigned, .bits = 69 } }, result2.val.?);
} }
test "ComptimeInterpreter - call inner function" {
try testCall(
\\pub fn Inner() type {
\\ return bool;
\\}
\\pub fn Foo() type {
\\ return Inner();
\\}
, &.{}, .{ .simple_type = .bool });
}
// //
// Helper functions // Helper functions
// //
@ -299,6 +310,8 @@ const Context = struct {
const handle = try document_store.openDocument(test_uri, source); const handle = try document_store.openDocument(test_uri, source);
// TODO handle handle.tree.errors
interpreter.* = .{ interpreter.* = .{
.allocator = allocator, .allocator = allocator,
.ip = try InternPool.init(allocator), .ip = try InternPool.init(allocator),