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

#include "boolean.h"
#include "number.h"
#include "object.h"
#include "vm.h"
#include <stdio.h>

static void test_number(VmState state) {
	Object numA = number_create(state, 5);
	Object numB = number_create(state, 3);
	vm_stack_push(state, object_none());
	vm_stack_push(state, numB);
	numB = object_none();
	vm_call(state, numA, "Add", 2);
	Object numC = vm_stack_pop(state);
	printf("numC value is %i\n", number_value(state, numC));
	vm_abort_if(state, number_value(state, numC) != 8,
		"add return value is not 8");
	object_drop(state, &numA);
	object_drop(state, &numC);
}

static void test_module(VmState state) {
	extern Object module_create(VmState);
	Object module = module_create(state);
	vm_stack_push(state, object_none());
	vm_call(state, module, "MakeNumber", 1);
	Object numA = vm_stack_pop(state);
	printf("code return value is %i\n", number_value(state, numA));
	vm_abort_if(state, number_value(state, numA) != 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_stack_drop(state, 1);
	object_drop(state, &numA);
	object_drop(state, &module);
}

int lang_main(void) {
	VmState state = vm_create();
	test_number(state);
	test_module(state);
	vm_destroy(&state);
	return 0;
}