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

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

static void test_number(VmState state) {
	Object numA = number_create(5);
	Object numB = number_create(3);
	vm_stack_push(state, NULL);
	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(numC));
	abort_if(number_value(numC) != 8, "add return value is not 8");
	object_drop(&numA);
	object_drop(&numC);
}

static void test_module(VmState state) {
	extern Object module_create(void);
	Object module = module_create();
	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(numA));
	abort_if(number_value(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);
	abort_if(
		boolean_value(boolA) != false, "code return bool is not false");
	object_drop(&boolA);
	object_drop(&numA);
	object_drop(&module);
}

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