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

#include "boolean.h"
#include "error.h"
#include "module.h"
#include "object.h"
#include "rational.h"
#include "vm.h"
#include <stdio.h>

static void test_class(VmState state) {
	Object module = module_find(state, "Main");
	modules_free(state);
	vm_stack_push(state, object_none());
	vm_call(state, module, "TwoNumbersTest", 1);
	Object ret = vm_stack_pop(state);
	vm_abort_if(state, !boolean_value(state, ret), "class test failed");
	object_drop(state, &ret);
	object_drop(state, &module);
}

static void test_rational(VmState state) {
	Object ratioA = rational_create(state, 5, 1);
	Object ratioB = rational_create(state, 3, 1);
	vm_stack_push(state, object_none());
	vm_stack_push(state, ratioB);
	ratioB = object_none();
	vm_call(state, ratioA, "Add", 2);
	Object ratioC = vm_stack_pop(state);
	printf("ratioC value is %i\n", rational_integer(state, ratioC));
	vm_abort_if(state, rational_integer(state, ratioC) != 8,
		"add return value is not 8");
	object_drop(state, &ratioA);
	object_drop(state, &ratioC);
}

static void test_module(VmState state) {
	Object module = module_find(state, "Main");
	modules_free(state);
	vm_stack_push(state, object_none());
	vm_call(state, module, "MakeNumber", 1);
	Object ratioA = vm_stack_pop(state);
	printf("code return value is %i\n", rational_integer(state, ratioA));
	vm_abort_if(state, rational_integer(state, ratioA) != 1234,
		"code return value is not 1234");
	vm_stack_push(state, object_none());
	vm_call(state, module, "BoolTest", 1);
	Object boolA = vm_stack_pop(state);
	vm_abort_if(state, boolean_value(state, boolA) != false,
		"code return bool is not false");
	object_drop(state, &boolA);
	vm_stack_push(state, object_none());
	vm_call(state, module, "NoneTest", 1);
	Object noneA = vm_stack_pop(state);
	vm_abort_if(
		state, noneA != object_none(), "code return none is not none");
	object_drop(state, &noneA);
	vm_stack_push(state, object_none());
	vm_call(state, module, "IfTest", 1);
	Object ifA = vm_stack_pop(state);
	vm_abort_if(state, boolean_value(state, ifA) == false,
		"if return bool is not true");
	object_drop(state, &ifA);
	vm_stack_push(state, object_none());
	vm_call(state, module, "LoopTest", 1);
	vm_call(state, module, "RatioTest", 1);
	vm_stack_drop(state, 1);
	object_drop(state, &ratioA);
	object_drop(state, &module);
}

int lang_main(void) {
	VmState state = vm_create();
	test_class(state);
	test_rational(state);
	test_module(state);
	vm_destroy(&state);
	printf("All done!\n");
	return 0;
}