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;
};

struct 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 (struct object *)func;
}

static void func_cleanup(struct object *obj) {
	abort_if(obj->class_data != &func_class,
		"func_cleanup obj is not a func");
	struct func *func = (struct func *)obj;
	free(func);
}

static void func_call(struct object *obj, int arg_count, struct object **args) {
	abort_if(arg_count != 1, "func_add called with more than 1 argument");
	abort_if(obj->class_data != &func_class, "func_call obj is not a func");
	bytecode_run(args, arg_count);
}

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],
};