Fixed negative variables handled incorrectly

This commit is contained in:
2026-05-26 18:01:49 +10:00
parent 69175e801a
commit 190477add8
6 changed files with 40 additions and 3 deletions
+4
View File
@@ -233,6 +233,10 @@ int inspect_instruction(unsigned char* bytecode, unsigned int pc, unsigned int j
printf(MARKER "MODULO %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] == TOY_OPCODE_ASSIGN ? "and ASSIGN" : ""); printf(MARKER "MODULO %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] == TOY_OPCODE_ASSIGN ? "and ASSIGN" : "");
return 4; return 4;
case TOY_OPCODE_INVERT:
printf(MARKER "INVERT\n", MARKER_VALUE(pc, unsigned char));
return 4;
case TOY_OPCODE_COMPARE_EQUAL: case TOY_OPCODE_COMPARE_EQUAL:
printf(MARKER "COMPARE %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] != TOY_OPCODE_NEGATE ? "==" : "!="); printf(MARKER "COMPARE %s\n", MARKER_VALUE(pc, unsigned char), bytecode[pc + 1] != TOY_OPCODE_NEGATE ? "==" : "!=");
return 4; return 4;
+3 -2
View File
@@ -86,9 +86,10 @@ typedef enum Toy_AstFlag {
TOY_AST_FLAG_PREFIX_DECREMENT = 42, TOY_AST_FLAG_PREFIX_DECREMENT = 42,
TOY_AST_FLAG_POSTFIX_INCREMENT = 43, TOY_AST_FLAG_POSTFIX_INCREMENT = 43,
TOY_AST_FLAG_POSTFIX_DECREMENT = 44, TOY_AST_FLAG_POSTFIX_DECREMENT = 44,
TOY_AST_FLAG_INVERT = 45,
TOY_AST_FLAG_INVOKATION = 45, TOY_AST_FLAG_INVOKATION = 46,
TOY_AST_FLAG_ATTRIBUTE = 46, TOY_AST_FLAG_ATTRIBUTE = 47,
// TOY_AST_FLAG_TERNARY, // TOY_AST_FLAG_TERNARY,
} Toy_AstFlag; } Toy_AstFlag;
+11
View File
@@ -377,6 +377,17 @@ static unsigned int writeInstructionUnary(Toy_Bytecode** mb, Toy_AstUnary ast) {
result = 1; result = 1;
} }
else if (ast.flag == TOY_AST_FLAG_INVERT) {
result = writeBytecodeFromAst(mb, ast.child);
EMIT_BYTE(mb, code, TOY_OPCODE_INVERT);
//4-byte alignment
EMIT_BYTE(mb, code, 0);
EMIT_BYTE(mb, code, 0);
EMIT_BYTE(mb, code, 0);
}
else { else {
fprintf(stderr, TOY_CC_ERROR "ERROR: Invalid AST unary flag found\n" TOY_CC_RESET); fprintf(stderr, TOY_CC_ERROR "ERROR: Invalid AST unary flag found\n" TOY_CC_RESET);
exit(-1); exit(-1);
+1
View File
@@ -22,6 +22,7 @@ typedef enum Toy_OpcodeType {
TOY_OPCODE_MULTIPLY, TOY_OPCODE_MULTIPLY,
TOY_OPCODE_DIVIDE, TOY_OPCODE_DIVIDE,
TOY_OPCODE_MODULO, TOY_OPCODE_MODULO,
TOY_OPCODE_INVERT, //negative numbers
//comparison instructions //comparison instructions
TOY_OPCODE_COMPARE_EQUAL, TOY_OPCODE_COMPARE_EQUAL,
+1 -1
View File
@@ -417,7 +417,7 @@ static Toy_AstFlag unary(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast*
} }
else { else {
//actually emit the negation node //actually emit the negation node
Toy_private_emitAstUnary(bucketHandle, rootHandle, TOY_AST_FLAG_NEGATE); Toy_private_emitAstUnary(bucketHandle, rootHandle, TOY_AST_FLAG_INVERT);
} }
} }
+20
View File
@@ -554,6 +554,25 @@ static void processIterate(Toy_VM* vm) {
} }
static void processArithmetic(Toy_VM* vm, Toy_OpcodeType opcode) { static void processArithmetic(Toy_VM* vm, Toy_OpcodeType opcode) {
//BUGFIX: handle negative variables
if (opcode == TOY_OPCODE_INVERT) {
Toy_Value value = Toy_popStack(&vm->stack);
if (TOY_VALUE_IS_INTEGER(value)) {
int intermediate = TOY_VALUE_AS_INTEGER(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_INTEGER(-intermediate));
}
else if (TOY_VALUE_IS_FLOAT(value)) {
float intermediate = TOY_VALUE_AS_FLOAT(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_FLOAT(-intermediate));
}
else {
Toy_error("Can't negate a non-number");
Toy_freeValue(value);
Toy_pushStack(&vm->stack, TOY_VALUE_FROM_NULL());
}
return;
}
Toy_Value right = Toy_popStack(&vm->stack); Toy_Value right = Toy_popStack(&vm->stack);
Toy_Value left = Toy_popStack(&vm->stack); Toy_Value left = Toy_popStack(&vm->stack);
@@ -1096,6 +1115,7 @@ static unsigned int process(Toy_VM* vm) {
case TOY_OPCODE_MULTIPLY: case TOY_OPCODE_MULTIPLY:
case TOY_OPCODE_DIVIDE: case TOY_OPCODE_DIVIDE:
case TOY_OPCODE_MODULO: case TOY_OPCODE_MODULO:
case TOY_OPCODE_INVERT:
processArithmetic(vm, opcode); processArithmetic(vm, opcode);
break; break;