Added negate opcode to equality opcode, in the second byte

This commit is contained in:
2024-10-03 10:15:58 +10:00
parent 5db3e407b1
commit ab1c9b941f
8 changed files with 74 additions and 26 deletions

View File

@@ -73,9 +73,8 @@ int test_setup_and_teardown(Toy_Bucket** bucket) {
return 0;
}
//tests
int test_simple_execution(Toy_Bucket** bucket) {
//basic init & quit
//test execution
{
//generate bytecode for testing
const char* source = "(1 + 2) * (3 + 4);";
@@ -104,7 +103,51 @@ int test_simple_execution(Toy_Bucket** bucket) {
TOY_VALUE_AS_INTEGER( Toy_peekStack(&vm.stack) ) != 21
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed run the Toy_VM, source: %s\n" TOY_CC_RESET, source);
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected result in 'Toy_VM', source: %s\n" TOY_CC_RESET, source);
//cleanup and return
Toy_freeVM(&vm);
return -1;
}
//teadown
Toy_freeVM(&vm);
}
return 0;
}
int test_opcode_not_equal(Toy_Bucket** bucket) {
//testing a specific opcode; '!=' is compressed into a single word, so lets check it works
{
//generate bytecode for testing
const char* source = "3 != 5;";
Toy_Lexer lexer;
Toy_bindLexer(&lexer, source);
Toy_Parser parser;
Toy_bindParser(&parser, &lexer);
Toy_Ast* ast = Toy_scanParser(bucket, &parser);
Toy_Bytecode bc = Toy_compileBytecode(ast);
//run the setup
Toy_VM vm;
Toy_bindVM(&vm, bc.ptr, bc.capacity);
//run
Toy_runVM(&vm);
//check the final state of the stack
if (vm.stack == NULL ||
vm.stack->count != 1 ||
TOY_VALUE_IS_BOOLEAN( Toy_peekStack(&vm.stack) ) != true ||
TOY_VALUE_AS_BOOLEAN( Toy_peekStack(&vm.stack) ) != true
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected result in 'Toy_VM', source: %s\n" TOY_CC_RESET, source);
//cleanup and return
Toy_freeVM(&vm);
@@ -142,5 +185,15 @@ int main() {
total += res;
}
{
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
res = test_opcode_not_equal(&bucket);
Toy_freeBucket(&bucket);
if (res == 0) {
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
}
total += res;
}
return total;
}