mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Multiple bugfixes, read more
* expand() will only touch the memory once * fixed missing if-else cascade at toy_routine.c:90 * unary negate will not optimise numbers unless the '-' character is immediately prior * fixed broken test for unary negation of groups
This commit is contained in:
@@ -9,10 +9,12 @@
|
|||||||
|
|
||||||
//utils
|
//utils
|
||||||
static void expand(Toy_Bytecode* bc, int amount) {
|
static void expand(Toy_Bytecode* bc, int amount) {
|
||||||
while (bc->count + amount > bc->capacity) {
|
if (bc->count + amount > bc->capacity) {
|
||||||
int oldCapacity = bc->capacity;
|
int oldCapacity = bc->capacity;
|
||||||
|
|
||||||
bc->capacity = TOY_GROW_CAPACITY(oldCapacity);
|
while (bc->count + amount > bc->capacity) {
|
||||||
|
bc->capacity = TOY_GROW_CAPACITY(bc->capacity);
|
||||||
|
}
|
||||||
bc->ptr = TOY_GROW_ARRAY(unsigned char, bc->ptr, oldCapacity, bc->capacity);
|
bc->ptr = TOY_GROW_ARRAY(unsigned char, bc->ptr, oldCapacity, bc->capacity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -273,13 +273,15 @@ static Toy_AstFlag unary(Toy_Bucket** bucket, Toy_Parser* parser, Toy_Ast** root
|
|||||||
//this function takes the libery of peeking into the uppermost node, to see if it can apply this to it
|
//this function takes the libery of peeking into the uppermost node, to see if it can apply this to it
|
||||||
|
|
||||||
if (parser->previous.type == TOY_TOKEN_OPERATOR_SUBTRACT) {
|
if (parser->previous.type == TOY_TOKEN_OPERATOR_SUBTRACT) {
|
||||||
|
|
||||||
|
bool connectedDigit = parser->previous.lexeme[1] >= '0' && parser->previous.lexeme[1] <= '9'; //BUGFIX: '- 1' should not be optimised into a negative
|
||||||
parsePrecedence(bucket, parser, root, PREC_UNARY);
|
parsePrecedence(bucket, parser, root, PREC_UNARY);
|
||||||
|
|
||||||
//negative numbers
|
//negative numbers
|
||||||
if ((*root)->type == TOY_AST_VALUE && TOY_VALUE_IS_INTEGER((*root)->value.value)) {
|
if ((*root)->type == TOY_AST_VALUE && TOY_VALUE_IS_INTEGER((*root)->value.value) && connectedDigit) {
|
||||||
(*root)->value.value = TOY_VALUE_TO_INTEGER( -TOY_VALUE_AS_INTEGER((*root)->value.value) );
|
(*root)->value.value = TOY_VALUE_TO_INTEGER( -TOY_VALUE_AS_INTEGER((*root)->value.value) );
|
||||||
}
|
}
|
||||||
else if ((*root)->type == TOY_AST_VALUE && TOY_VALUE_IS_FLOAT((*root)->value.value)) {
|
else if ((*root)->type == TOY_AST_VALUE && TOY_VALUE_IS_FLOAT((*root)->value.value) && connectedDigit) {
|
||||||
(*root)->value.value = TOY_VALUE_TO_FLOAT( -TOY_VALUE_AS_FLOAT((*root)->value.value) );
|
(*root)->value.value = TOY_VALUE_TO_FLOAT( -TOY_VALUE_AS_FLOAT((*root)->value.value) );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -11,10 +11,12 @@
|
|||||||
|
|
||||||
//utils
|
//utils
|
||||||
static void expand(void** handle, int* capacity, int* count, int amount) {
|
static void expand(void** handle, int* capacity, int* count, int amount) {
|
||||||
while ((*count) + amount > (*capacity)) {
|
if ((*count) + amount > (*capacity)) {
|
||||||
int oldCapacity = (*capacity);
|
int oldCapacity = (*capacity);
|
||||||
|
|
||||||
(*capacity) = TOY_GROW_CAPACITY(oldCapacity); //TODO: don't need GROW
|
while ((*count) + amount > (*capacity)) {
|
||||||
|
(*capacity) = TOY_GROW_CAPACITY(*capacity);
|
||||||
|
}
|
||||||
(*handle) = TOY_GROW_ARRAY(unsigned char, (*handle), oldCapacity, (*capacity));
|
(*handle) = TOY_GROW_ARRAY(unsigned char, (*handle), oldCapacity, (*capacity));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,73 +97,73 @@ static void writeInstructionBinary(Toy_Routine** rt, Toy_AstBinary ast) {
|
|||||||
if (ast.flag == TOY_AST_FLAG_ADD) {
|
if (ast.flag == TOY_AST_FLAG_ADD) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_ADD);
|
EMIT_BYTE(rt, TOY_OPCODE_ADD);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_SUBTRACT) {
|
else if (ast.flag == TOY_AST_FLAG_SUBTRACT) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_SUBTRACT);
|
EMIT_BYTE(rt, TOY_OPCODE_SUBTRACT);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_MULTIPLY) {
|
else if (ast.flag == TOY_AST_FLAG_MULTIPLY) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_MULTIPLY);
|
EMIT_BYTE(rt, TOY_OPCODE_MULTIPLY);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_DIVIDE) {
|
else if (ast.flag == TOY_AST_FLAG_DIVIDE) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_DIVIDE);
|
EMIT_BYTE(rt, TOY_OPCODE_DIVIDE);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_MODULO) {
|
else if (ast.flag == TOY_AST_FLAG_MODULO) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_MODULO);
|
EMIT_BYTE(rt, TOY_OPCODE_MODULO);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (ast.flag == TOY_AST_FLAG_ASSIGN) {
|
// else if (ast.flag == TOY_AST_FLAG_ASSIGN) {
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
||||||
// //TODO: emit the env symbol to store TOP(S) within
|
// //TODO: emit the env symbol to store TOP(S) within
|
||||||
// }
|
// }
|
||||||
// if (ast.flag == TOY_AST_FLAG_ADD_ASSIGN) {
|
// else if (ast.flag == TOY_AST_FLAG_ADD_ASSIGN) {
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_ADD);
|
// EMIT_BYTE(rt, TOY_OPCODE_ADD);
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
||||||
// //TODO: emit the env symbol to store TOP(S) within
|
// //TODO: emit the env symbol to store TOP(S) within
|
||||||
// }
|
// }
|
||||||
// if (ast.flag == TOY_AST_FLAG_SUBTRACT_ASSIGN) {
|
// else if (ast.flag == TOY_AST_FLAG_SUBTRACT_ASSIGN) {
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_SUBTRACT);
|
// EMIT_BYTE(rt, TOY_OPCODE_SUBTRACT);
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
||||||
// //TODO: emit the env symbol to store TOP(S) within
|
// //TODO: emit the env symbol to store TOP(S) within
|
||||||
// }
|
// }
|
||||||
// if (ast.flag == TOY_AST_FLAG_MULTIPLY_ASSIGN) {
|
// else if (ast.flag == TOY_AST_FLAG_MULTIPLY_ASSIGN) {
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_MULTIPLY);
|
// EMIT_BYTE(rt, TOY_OPCODE_MULTIPLY);
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
||||||
// //TODO: emit the env symbol to store TOP(S) within
|
// //TODO: emit the env symbol to store TOP(S) within
|
||||||
// }
|
// }
|
||||||
// if (ast.flag == TOY_AST_FLAG_DIVIDE_ASSIGN) {
|
// else if (ast.flag == TOY_AST_FLAG_DIVIDE_ASSIGN) {
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_DIVIDE);
|
// EMIT_BYTE(rt, TOY_OPCODE_DIVIDE);
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
||||||
// //TODO: emit the env symbol to store TOP(S) within
|
// //TODO: emit the env symbol to store TOP(S) within
|
||||||
// }
|
// }
|
||||||
// if (ast.flag == TOY_AST_FLAG_MODULO_ASSIGN) {
|
// else if (ast.flag == TOY_AST_FLAG_MODULO_ASSIGN) {
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_MODULO);
|
// EMIT_BYTE(rt, TOY_OPCODE_MODULO);
|
||||||
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
// EMIT_BYTE(rt, TOY_OPCODE_ASSIGN);
|
||||||
// //TODO: emit the env symbol to store TOP(S) within
|
// //TODO: emit the env symbol to store TOP(S) within
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (ast.flag == TOY_AST_FLAG_COMPARE_EQUAL) {
|
else if (ast.flag == TOY_AST_FLAG_COMPARE_EQUAL) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_EQUAL);
|
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_EQUAL);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_COMPARE_NOT) {
|
else if (ast.flag == TOY_AST_FLAG_COMPARE_NOT) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_EQUAL);
|
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_EQUAL);
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_NEGATE);
|
EMIT_BYTE(rt, TOY_OPCODE_NEGATE);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_COMPARE_LESS) {
|
else if (ast.flag == TOY_AST_FLAG_COMPARE_LESS) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_LESS);
|
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_LESS);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_COMPARE_LESS_EQUAL) {
|
else if (ast.flag == TOY_AST_FLAG_COMPARE_LESS_EQUAL) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_LESS_EQUAL);
|
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_LESS_EQUAL);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_COMPARE_GREATER) {
|
else if (ast.flag == TOY_AST_FLAG_COMPARE_GREATER) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_GREATER);
|
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_GREATER);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_COMPARE_GREATER_EQUAL) {
|
else if (ast.flag == TOY_AST_FLAG_COMPARE_GREATER_EQUAL) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_GREATER_EQUAL);
|
EMIT_BYTE(rt, TOY_OPCODE_COMPARE_GREATER_EQUAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ast.flag == TOY_AST_FLAG_AND) {
|
else if (ast.flag == TOY_AST_FLAG_AND) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_AND);
|
EMIT_BYTE(rt, TOY_OPCODE_AND);
|
||||||
}
|
}
|
||||||
if (ast.flag == TOY_AST_FLAG_OR) {
|
else if (ast.flag == TOY_AST_FLAG_OR) {
|
||||||
EMIT_BYTE(rt, TOY_OPCODE_OR);
|
EMIT_BYTE(rt, TOY_OPCODE_OR);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -283,7 +283,7 @@ int test_unary(Toy_Bucket* bucket) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//ensure unary negation doesn't affect other things (such as group)
|
//ensure unary negation doesn't occur with a group
|
||||||
{
|
{
|
||||||
Toy_Ast* ast = makeAstFromSource(&bucket, "-(42);");
|
Toy_Ast* ast = makeAstFromSource(&bucket, "-(42);");
|
||||||
|
|
||||||
@@ -292,15 +292,29 @@ int test_unary(Toy_Bucket* bucket) {
|
|||||||
ast == NULL ||
|
ast == NULL ||
|
||||||
ast->type != TOY_AST_BLOCK ||
|
ast->type != TOY_AST_BLOCK ||
|
||||||
ast->block.child == NULL ||
|
ast->block.child == NULL ||
|
||||||
ast->block.child->type != TOY_AST_VALUE ||
|
ast->block.child->type == TOY_AST_VALUE)
|
||||||
TOY_VALUE_IS_INTEGER(ast->block.child->value.value) == false ||
|
|
||||||
TOY_VALUE_AS_INTEGER(ast->block.child->value.value) != -42)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: unexpected successful unary negation in parser with grouped value -(42)\n" TOY_CC_RESET);
|
fprintf(stderr, TOY_CC_ERROR "ERROR: unexpected successful unary negation in parser with grouped value -(42)\n" TOY_CC_RESET);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ensure unary negation doesn't occur with a space
|
||||||
|
{
|
||||||
|
Toy_Ast* ast = makeAstFromSource(&bucket, "- 42;");
|
||||||
|
|
||||||
|
//check if it worked
|
||||||
|
if (
|
||||||
|
ast == NULL ||
|
||||||
|
ast->type != TOY_AST_BLOCK ||
|
||||||
|
ast->block.child == NULL ||
|
||||||
|
ast->block.child->type == TOY_AST_VALUE)
|
||||||
|
{
|
||||||
|
fprintf(stderr, TOY_CC_ERROR "ERROR: unexpected successful unary negation in parser with space character '- 42'\n" TOY_CC_RESET);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user