mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +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
|
||||
static void expand(Toy_Bytecode* bc, int amount) {
|
||||
while (bc->count + amount > bc->capacity) {
|
||||
if (bc->count + amount > 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
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);
|
||||
|
||||
//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) );
|
||||
}
|
||||
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) );
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -11,10 +11,12 @@
|
||||
|
||||
//utils
|
||||
static void expand(void** handle, int* capacity, int* count, int amount) {
|
||||
while ((*count) + amount > (*capacity)) {
|
||||
if ((*count) + amount > (*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));
|
||||
}
|
||||
}
|
||||
@@ -95,73 +97,73 @@ static void writeInstructionBinary(Toy_Routine** rt, Toy_AstBinary ast) {
|
||||
if (ast.flag == TOY_AST_FLAG_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);
|
||||
}
|
||||
if (ast.flag == TOY_AST_FLAG_MULTIPLY) {
|
||||
else if (ast.flag == TOY_AST_FLAG_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);
|
||||
}
|
||||
if (ast.flag == TOY_AST_FLAG_MODULO) {
|
||||
else if (ast.flag == TOY_AST_FLAG_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);
|
||||
// //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_ASSIGN);
|
||||
// //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_ASSIGN);
|
||||
// //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_ASSIGN);
|
||||
// //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_ASSIGN);
|
||||
// //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_ASSIGN);
|
||||
// //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);
|
||||
}
|
||||
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_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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
if (ast.flag == TOY_AST_FLAG_COMPARE_GREATER) {
|
||||
else if (ast.flag == TOY_AST_FLAG_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);
|
||||
}
|
||||
|
||||
if (ast.flag == TOY_AST_FLAG_AND) {
|
||||
else if (ast.flag == TOY_AST_FLAG_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);
|
||||
}
|
||||
else {
|
||||
@@ -325,4 +327,4 @@ void* Toy_compileRoutine(Toy_Ast* ast) {
|
||||
TOY_FREE_ARRAY(unsigned char, rt.subs, rt.subsCapacity);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user