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 "vm.h"
#include <stddef.h>

static struct object_class func_class;

Object func_create(void) {
	Object obj = object_create(&func_class, 1);
	abort_if(!obj, "unable to allocate func");
	return obj;
}

static void func_cleanup(Object obj) { (void)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");
	// clang-format off
	static const unsigned char bytecode[] = {
	0x09, 0x01, 0x05, 0x05, 0x05, 0x01, 0x6a, 0x02, 0x00, 0x00,
	0x07, 0x01, 0x05, 0x06, 0x01, 0x06, 0x01, 0x04, 0x02, 0x41,
	0x64, 0x64, 0x00, 0x09, 0x05, 0x07, 0x02, 0x05, 0x01, 0x02,
	0x00, 0x00, 0x00, 0x06, 0x02, 0x04, 0x02, 0x4d, 0x69, 0x6e,
	0x75, 0x73, 0x00, 0x09, 0x05, 0x07, 0x03, 0x06, 0x03, 0x07,
	0x00, 0x08, 0x03, 0x03,
	};
	// clang-format on
	bytecode_run(state, bytecode);
}

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