Newer
Older
Tardis / lang / func.c
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 John Watts and the LuminaSensum contributors

#include "bytecode.h"
#include "error.h"
#include "object.h"
#include <stdlib.h>

static struct object_class func_class;

struct func {
	struct object obj;
};

Object func_create(void) {
	struct func *func = malloc(sizeof(struct func));
	abort_if(!func, "unable to allocate func");
	func->obj.class_data = &func_class;
	func->obj.ref_count = 1;
	return (Object)func;
}

static void func_cleanup(Object obj) { free(obj); }

static void func_call(VmState state, Object obj) {
	(void)obj;
	int arg_count = vm_stack_depth(state);
	abort_if(arg_count != 1, "func_add called with more than 1 argument");
	bytecode_run(state);
}

static struct object_call calls[] = {
	{.name = "Call", .handler = func_call}, {.name = NULL, /* end */}};

static struct object_class func_class = {
	.cleanup = func_cleanup,
	.calls = &calls[0],
};