mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 06:44:07 +10:00
Added remaining opcodes to inspector
Also used some coloring for terminal outputs
This commit is contained in:
@@ -104,13 +104,43 @@ int inspect_instruction(unsigned char* bytecode, unsigned int pc, unsigned int j
|
|||||||
case TOY_OPCODE_READ:
|
case TOY_OPCODE_READ:
|
||||||
return inspect_read(bytecode, pc, jumps_addr, data_addr);
|
return inspect_read(bytecode, pc, jumps_addr, data_addr);
|
||||||
|
|
||||||
// case TOY_OPCODE_DECLARE:
|
case TOY_OPCODE_DECLARE: {
|
||||||
// case TOY_OPCODE_ASSIGN:
|
unsigned int indexValue = *((unsigned int*)(bytecode + pc + 4));
|
||||||
// case TOY_OPCODE_ASSIGN_COMPOUND:
|
unsigned int jumpValue = *((unsigned int*)(bytecode + jumps_addr + indexValue));
|
||||||
// case TOY_OPCODE_ACCESS:
|
char* cstr = ((char*)(bytecode + data_addr + jumpValue));
|
||||||
// case TOY_OPCODE_INVOKE:
|
printf(MARKER "DECLARE %s: %s%s\n", MARKER_VALUE(pc, unsigned char),
|
||||||
// case TOY_OPCODE_DUPLICATE:
|
cstr,
|
||||||
// case TOY_OPCODE_ELIMINATE:
|
Toy_private_getValueTypeAsCString(bytecode[pc + 1]),
|
||||||
|
bytecode[pc + 3] ? " const" : ""
|
||||||
|
);
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
case TOY_OPCODE_ASSIGN:
|
||||||
|
printf(MARKER "ASSIGN %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] ? "(chained)" : "");
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_ASSIGN_COMPOUND:
|
||||||
|
printf(MARKER "ASSIGN COMPOUND %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] ? "(chained)" : "");
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_ACCESS:
|
||||||
|
printf(MARKER "ACCESS\n", MARKER_VALUE(pc, unsigned char));
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_INVOKE:
|
||||||
|
printf(MARKER "INVOKE %s (%d args)\n", MARKER_VALUE(pc, unsigned char),
|
||||||
|
Toy_private_getValueTypeAsCString(bytecode[pc + 1]),
|
||||||
|
bytecode[pc + 2]);
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_DUPLICATE:
|
||||||
|
printf(MARKER "DUPLICATE %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] ? "and ACCESS" : "");
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_ELIMINATE:
|
||||||
|
printf(MARKER "ELIMINATE\n", MARKER_VALUE(pc, unsigned char));
|
||||||
|
return 4;
|
||||||
|
|
||||||
case TOY_OPCODE_ADD:
|
case TOY_OPCODE_ADD:
|
||||||
printf(MARKER "ADD %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] == TOY_OPCODE_ASSIGN ? "ASSIGN" : "");
|
printf(MARKER "ADD %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] == TOY_OPCODE_ASSIGN ? "ASSIGN" : "");
|
||||||
@@ -172,18 +202,50 @@ int inspect_instruction(unsigned char* bytecode, unsigned int pc, unsigned int j
|
|||||||
printf(MARKER "Keyword RETURN (%u)\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1]);
|
printf(MARKER "Keyword RETURN (%u)\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1]);
|
||||||
return 4;
|
return 4;
|
||||||
|
|
||||||
// case TOY_OPCODE_JUMP:
|
case TOY_OPCODE_JUMP:
|
||||||
// case TOY_OPCODE_ESCAPE:
|
printf(MARKER TOY_CC_DEBUG "JUMP %s%s (%s%d) (GOTO %u)\n" TOY_CC_RESET, MARKER_VALUE(pc, unsigned char),
|
||||||
// case TOY_OPCODE_SCOPE_PUSH:
|
bytecode[pc + 1] == TOY_OP_PARAM_JUMP_ABSOLUTE ? "absolute" : "relative",
|
||||||
// case TOY_OPCODE_SCOPE_POP:
|
bytecode[pc + 2] == TOY_OP_PARAM_JUMP_ALWAYS ? "" :
|
||||||
// case TOY_OPCODE_ASSERT:
|
bytecode[pc + 2] == TOY_OP_PARAM_JUMP_IF_TRUE ? " if true" : " if false",
|
||||||
|
bytecode[pc + 4] > 0 ? "+" : "", //show a + sign when positive
|
||||||
|
bytecode[pc + 4],
|
||||||
|
bytecode[pc + 4] + pc + 8
|
||||||
|
);
|
||||||
|
return 8;
|
||||||
|
|
||||||
case TOY_OPCODE_PRINT:
|
case TOY_OPCODE_ESCAPE:
|
||||||
printf(MARKER "Keyword PRINT\n", MARKER_VALUE(pc, unsigned char));
|
printf(MARKER TOY_CC_DEBUG "ESCAPE relative %s%d (GOTO %u) and pop %d\n" TOY_CC_RESET, MARKER_VALUE(pc, unsigned char),
|
||||||
|
bytecode[pc + 4] > 0 ? "+" : "", //show a + sign when positive
|
||||||
|
bytecode[pc + 4],
|
||||||
|
bytecode[pc + 4] + pc + 12,
|
||||||
|
bytecode[pc + 8]
|
||||||
|
);
|
||||||
|
return 12;
|
||||||
|
|
||||||
|
case TOY_OPCODE_SCOPE_PUSH:
|
||||||
|
printf(MARKER "SCOPE PUSH\n", MARKER_VALUE(pc, unsigned char));
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_SCOPE_POP:
|
||||||
|
printf(MARKER "SCOPE POP\n", MARKER_VALUE(pc, unsigned char));
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_ASSERT:
|
||||||
|
printf(MARKER TOY_CC_WARN "Keyword ASSERT (cond%s)\n" TOY_CC_RESET, MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] > 1 ? ",msg" : "");
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_PRINT:
|
||||||
|
printf(MARKER TOY_CC_NOTICE "Keyword PRINT\n" TOY_CC_RESET, MARKER_VALUE(pc, unsigned char));
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_CONCAT:
|
||||||
|
printf(MARKER "CONCATENATE strings\n", MARKER_VALUE(pc, unsigned char));
|
||||||
|
return 4;
|
||||||
|
|
||||||
|
case TOY_OPCODE_INDEX:
|
||||||
|
printf(MARKER "INDEX (%d elements)\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1]);
|
||||||
return 4;
|
return 4;
|
||||||
|
|
||||||
// case TOY_OPCODE_CONCAT:
|
|
||||||
// case TOY_OPCODE_INDEX:
|
|
||||||
// case TOY_OPCODE_UNUSED:
|
// case TOY_OPCODE_UNUSED:
|
||||||
// case TOY_OPCODE_PASS:
|
// case TOY_OPCODE_PASS:
|
||||||
// case TOY_OPCODE_ERROR:
|
// case TOY_OPCODE_ERROR:
|
||||||
@@ -241,9 +303,12 @@ int inspect_read(unsigned char* bytecode, unsigned int pc, unsigned int jumps_ad
|
|||||||
return 8;
|
return 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case TOY_VALUE_FUNCTION:
|
||||||
|
printf(MARKER "READ FUNCTION (%d params)\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 2]);
|
||||||
|
return 8;
|
||||||
|
|
||||||
case TOY_VALUE_ARRAY:
|
case TOY_VALUE_ARRAY:
|
||||||
case TOY_VALUE_TABLE:
|
case TOY_VALUE_TABLE:
|
||||||
case TOY_VALUE_FUNCTION:
|
|
||||||
case TOY_VALUE_OPAQUE:
|
case TOY_VALUE_OPAQUE:
|
||||||
case TOY_VALUE_ANY:
|
case TOY_VALUE_ANY:
|
||||||
case TOY_VALUE_UNKNOWN:
|
case TOY_VALUE_UNKNOWN:
|
||||||
|
|||||||
11
repl/main.c
11
repl/main.c
@@ -365,6 +365,11 @@ int repl(const char* filepath, bool verbose) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
unsigned char* bytecode = Toy_compileToBytecode(ast);
|
unsigned char* bytecode = Toy_compileToBytecode(ast);
|
||||||
|
|
||||||
|
if (verbose) {
|
||||||
|
inspect_bytecode(bytecode);
|
||||||
|
}
|
||||||
|
|
||||||
Toy_bindVM(&vm, bytecode, NULL);
|
Toy_bindVM(&vm, bytecode, NULL);
|
||||||
|
|
||||||
//run
|
//run
|
||||||
@@ -374,7 +379,6 @@ int repl(const char* filepath, bool verbose) {
|
|||||||
if (verbose) {
|
if (verbose) {
|
||||||
debugStackPrint(vm.stack);
|
debugStackPrint(vm.stack);
|
||||||
debugScopePrint(vm.scope, 0);
|
debugScopePrint(vm.scope, 0);
|
||||||
inspect_bytecode(bytecode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//free the memory, and leave the VM ready for the next loop
|
//free the memory, and leave the VM ready for the next loop
|
||||||
@@ -460,6 +464,10 @@ int main(int argc, const char* argv[]) {
|
|||||||
Toy_freeBucket(&bucket);
|
Toy_freeBucket(&bucket);
|
||||||
free(source);
|
free(source);
|
||||||
|
|
||||||
|
if (cmd.verbose) {
|
||||||
|
inspect_bytecode(bytecode);
|
||||||
|
}
|
||||||
|
|
||||||
//run the compiled code
|
//run the compiled code
|
||||||
Toy_VM vm;
|
Toy_VM vm;
|
||||||
Toy_initVM(&vm);
|
Toy_initVM(&vm);
|
||||||
@@ -471,7 +479,6 @@ int main(int argc, const char* argv[]) {
|
|||||||
if (cmd.verbose) {
|
if (cmd.verbose) {
|
||||||
debugStackPrint(vm.stack);
|
debugStackPrint(vm.stack);
|
||||||
debugScopePrint(vm.scope, 0);
|
debugScopePrint(vm.scope, 0);
|
||||||
inspect_bytecode(bytecode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//cleanup
|
//cleanup
|
||||||
|
|||||||
@@ -226,17 +226,7 @@ static void processAssign(Toy_VM* vm) {
|
|||||||
Toy_Value value = Toy_popStack(&vm->stack);
|
Toy_Value value = Toy_popStack(&vm->stack);
|
||||||
Toy_Value name = Toy_popStack(&vm->stack);
|
Toy_Value name = Toy_popStack(&vm->stack);
|
||||||
|
|
||||||
//TODO: remove 'TOY_STRING_NAME' entirely
|
//URGENT: only allowable type coersion
|
||||||
// //check name string type
|
|
||||||
// if (!TOY_VALUE_IS_STRING(name) || TOY_VALUE_AS_STRING(name)->info.type != TOY_STRING_NAME) {
|
|
||||||
// Toy_error("Invalid assignment target");
|
|
||||||
// Toy_freeValue(name);
|
|
||||||
// Toy_freeValue(value);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //FIXME
|
|
||||||
// //BUGFIX: only allowable type coersion
|
|
||||||
// if (TOY_VALUE_AS_STRING(name)->name.varType == TOY_VALUE_FLOAT && value.type == TOY_VALUE_INTEGER) {
|
// if (TOY_VALUE_AS_STRING(name)->name.varType == TOY_VALUE_FLOAT && value.type == TOY_VALUE_INTEGER) {
|
||||||
// value = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(value) );
|
// value = TOY_VALUE_FROM_FLOAT( (float)TOY_VALUE_AS_INTEGER(value) );
|
||||||
// }
|
// }
|
||||||
@@ -340,13 +330,6 @@ static void processAssignCompound(Toy_VM* vm) {
|
|||||||
static void processAccess(Toy_VM* vm) {
|
static void processAccess(Toy_VM* vm) {
|
||||||
Toy_Value name = Toy_popStack(&vm->stack);
|
Toy_Value name = Toy_popStack(&vm->stack);
|
||||||
|
|
||||||
// //check name string type
|
|
||||||
// if (!TOY_VALUE_IS_STRING(name) || TOY_VALUE_AS_STRING(name)->info.type != TOY_STRING_NAME) {
|
|
||||||
// Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
|
|
||||||
// Toy_error("Invalid access target");
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
//find the value
|
//find the value
|
||||||
Toy_Value* valuePtr = Toy_accessScopeAsPointer(vm->scope, TOY_VALUE_AS_STRING(name));
|
Toy_Value* valuePtr = Toy_accessScopeAsPointer(vm->scope, TOY_VALUE_AS_STRING(name));
|
||||||
|
|
||||||
@@ -370,7 +353,7 @@ static void processAccess(Toy_VM* vm) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void processInvoke(Toy_VM* vm) {
|
static void processInvoke(Toy_VM* vm) {
|
||||||
Toy_ValueType valueType = READ_BYTE(vm); //unused for now
|
Toy_ValueType valueType = READ_BYTE(vm);
|
||||||
unsigned int argCount = (unsigned int)READ_BYTE(vm);
|
unsigned int argCount = (unsigned int)READ_BYTE(vm);
|
||||||
fixAlignment(vm);
|
fixAlignment(vm);
|
||||||
|
|
||||||
|
|||||||
@@ -5,5 +5,5 @@
|
|||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
printf(TOY_CC_WARN "Test not yet implemented: %s\n" TOY_CC_RESET, __FILE__);
|
printf(TOY_CC_WARN "Test not yet implemented: %s\n" TOY_CC_RESET, __FILE__);
|
||||||
return -1;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -534,8 +534,7 @@ int test_value_stringify(void) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//NOTE: Test 'Stringify Dictionary as Value' not implemented, as the order is impossible to know
|
//NOTE: Test 'Stringify Table as Value' not implemented, as the order is impossible to know
|
||||||
printf(TOY_CC_WARN "Test 'Stringify Dictionary as Value' not implemented\n" TOY_CC_RESET);
|
|
||||||
|
|
||||||
//cleanup
|
//cleanup
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|||||||
Reference in New Issue
Block a user