mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Patched some very obscure bugs
This commit is contained in:
@@ -1448,11 +1448,8 @@ int Toy_hookCompound(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L
|
|||||||
{"_getKeys", nativeGetKeys}, //dictionary
|
{"_getKeys", nativeGetKeys}, //dictionary
|
||||||
{"_getValues", nativeGetValues}, //dictionary
|
{"_getValues", nativeGetValues}, //dictionary
|
||||||
{"_indexOf", nativeIndexOf}, //array
|
{"_indexOf", nativeIndexOf}, //array
|
||||||
// {"_insert", native}, //array, dictionary, string
|
|
||||||
{"_map", nativeMap}, //array, dictionary
|
{"_map", nativeMap}, //array, dictionary
|
||||||
{"_reduce", nativeReduce}, //array, dictionary
|
{"_reduce", nativeReduce}, //array, dictionary
|
||||||
// {"_remove", native}, //array, dictionary
|
|
||||||
// {"_replace", native}, //string
|
|
||||||
{"_some", nativeSome}, //array, dictionary
|
{"_some", nativeSome}, //array, dictionary
|
||||||
// {"_sort", native}, //array
|
// {"_sort", native}, //array
|
||||||
{"_toLower", nativeToLower}, //string
|
{"_toLower", nativeToLower}, //string
|
||||||
|
|||||||
@@ -1,10 +1,22 @@
|
|||||||
|
//polyfill the _insert function
|
||||||
|
fn _insert(self, k, v) {
|
||||||
|
var tmp1 = v;
|
||||||
|
var tmp2;
|
||||||
|
for (var i = k; i < self.length(); i++) {
|
||||||
|
tmp2 = self[i];
|
||||||
|
self[i] = tmp1;
|
||||||
|
tmp1 = tmp2;
|
||||||
|
}
|
||||||
|
|
||||||
var xrel: int = 0;
|
self.push(tmp1);
|
||||||
var yrel: int = 0;
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
if (xrel > 1 || xrel < -1 || yrel > 1 || yrel < -1) {
|
var a = [1, 2, 3];
|
||||||
print "outside";
|
|
||||||
}
|
a = a.insert(1, 42);
|
||||||
else {
|
|
||||||
print "inside";
|
assert a == [1, 5, 2, 3], "index assignment left failed";
|
||||||
}
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
@@ -302,6 +302,12 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
|
|||||||
//special case for when indexing and assigning
|
//special case for when indexing and assigning
|
||||||
if (override != TOY_OP_EOF && node->binary.opcode >= TOY_OP_VAR_ASSIGN && node->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN) {
|
if (override != TOY_OP_EOF && node->binary.opcode >= TOY_OP_VAR_ASSIGN && node->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN) {
|
||||||
Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
|
Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
|
||||||
|
|
||||||
|
//Special case if there's an index on both sides of the sign, just set it as indexing
|
||||||
|
if (node->binary.left->type == TOY_AST_NODE_BINARY && node->binary.right->type == TOY_AST_NODE_BINARY && node->binary.left->binary.opcode == TOY_OP_INDEX && node->binary.right->binary.opcode == TOY_OP_INDEX) {
|
||||||
|
compiler->bytecode[compiler->count++] = (unsigned char)TOY_OP_INDEX;
|
||||||
|
}
|
||||||
|
|
||||||
compiler->bytecode[compiler->count++] = (unsigned char)TOY_OP_INDEX_ASSIGN; //1 byte WARNING: enum trickery
|
compiler->bytecode[compiler->count++] = (unsigned char)TOY_OP_INDEX_ASSIGN; //1 byte WARNING: enum trickery
|
||||||
compiler->bytecode[compiler->count++] = (unsigned char)node->binary.opcode; //1 byte
|
compiler->bytecode[compiler->count++] = (unsigned char)node->binary.opcode; //1 byte
|
||||||
return TOY_OP_EOF;
|
return TOY_OP_EOF;
|
||||||
@@ -315,7 +321,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
|
|||||||
//return this if...
|
//return this if...
|
||||||
Toy_Opcode ret = Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
|
Toy_Opcode ret = Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode);
|
||||||
|
|
||||||
if (node->binary.opcode == TOY_OP_INDEX && rootNode->type == TOY_AST_NODE_BINARY && (rootNode->binary.opcode >= TOY_OP_VAR_ASSIGN && rootNode->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN)) { //range-based check for assignment type
|
if (node->binary.opcode == TOY_OP_INDEX && rootNode->type == TOY_AST_NODE_BINARY && (rootNode->binary.opcode >= TOY_OP_VAR_ASSIGN && rootNode->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN) && rootNode->binary.right != node) { //range-based check for assignment type; make sure the index is on the left of the assignment symbol
|
||||||
return TOY_OP_INDEX_ASSIGN_INTERMEDIATE;
|
return TOY_OP_INDEX_ASSIGN_INTERMEDIATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
test/scripts/index-assignment-both-bugfix.toy
Normal file
15
test/scripts/index-assignment-both-bugfix.toy
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
This ensures that when indexing on both sides of an assignment,
|
||||||
|
it works correctly.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
var a = [1, 2, 3];
|
||||||
|
var b = [4, 5, 6];
|
||||||
|
|
||||||
|
a[1] = b[1];
|
||||||
|
|
||||||
|
assert a == [1, 5, 3], "index assignment both failed";
|
||||||
|
|
||||||
|
print "All good";
|
||||||
19
test/scripts/index-assignment-left-bugfix.toy
Normal file
19
test/scripts/index-assignment-left-bugfix.toy
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Compiler note:
|
||||||
|
This is also to test a specific element in the compiler.
|
||||||
|
It ensures that when doing indexing and assignment in one statement,
|
||||||
|
the index is NOT on the right. If it is, then it is treated like a normal
|
||||||
|
assignment.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//polyfill the _insert function
|
||||||
|
var a = [1, 2, 3];
|
||||||
|
|
||||||
|
var b = a[1];
|
||||||
|
|
||||||
|
assert b == 2, "index assignment left failed";
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
@@ -160,7 +160,8 @@ import compound;
|
|||||||
var a = [1, 2, 42, 3];
|
var a = [1, 2, 42, 3];
|
||||||
|
|
||||||
//results are zero-indexed
|
//results are zero-indexed
|
||||||
assert a.indexOf(42) == 2, "_indexOf failed";
|
assert a.indexOf(42) == 2, "_indexOf() failed";
|
||||||
|
assert a.indexOf(4) == null, "_indexOf() == null failed";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
22
test/scripts/polyfill-insert.toy
Normal file
22
test/scripts/polyfill-insert.toy
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
//polyfill the _insert function
|
||||||
|
fn _insert(self, k, v) {
|
||||||
|
var tmp1 = v;
|
||||||
|
var tmp2;
|
||||||
|
for (var i = k; i < self.length(); i++) {
|
||||||
|
tmp2 = self[i];
|
||||||
|
self[i] = tmp1;
|
||||||
|
tmp1 = tmp2;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.push(tmp1);
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
var a = [1, 2, 3];
|
||||||
|
|
||||||
|
a = a.insert(1, 42);
|
||||||
|
|
||||||
|
assert a == [1, 42, 2, 3], "polyfill insert failed";
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
23
test/scripts/polyfill-remove.toy
Normal file
23
test/scripts/polyfill-remove.toy
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
//polyfill the remove function
|
||||||
|
fn _remove(self, k) {
|
||||||
|
var result = [];
|
||||||
|
|
||||||
|
for (var i = 0; i <= k - 1; i++) {
|
||||||
|
result.push( self[i] );
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = k + 1; i < self.length(); i++) {
|
||||||
|
result.push( self[i] );
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
var a = [1, 2, 3];
|
||||||
|
|
||||||
|
assert a.remove(0) == [2, 3], "polyfill remove(start) failed";
|
||||||
|
assert a.remove(1) == [1, 3], "polyfill remove(middle) failed";
|
||||||
|
assert a.remove(2) == [1, 2], "polyfill remove(end) failed";
|
||||||
|
|
||||||
|
|
||||||
|
print "All good";
|
||||||
@@ -117,6 +117,8 @@ int main() {
|
|||||||
"dottify-bugfix.toy",
|
"dottify-bugfix.toy",
|
||||||
"functions.toy",
|
"functions.toy",
|
||||||
"index-arrays.toy",
|
"index-arrays.toy",
|
||||||
|
"index-assignment-both-bugfix.toy",
|
||||||
|
"index-assignment-left-bugfix.toy",
|
||||||
"index-dictionaries.toy",
|
"index-dictionaries.toy",
|
||||||
"index-strings.toy",
|
"index-strings.toy",
|
||||||
"jumps.toy",
|
"jumps.toy",
|
||||||
@@ -128,6 +130,8 @@ int main() {
|
|||||||
"native-functions.toy",
|
"native-functions.toy",
|
||||||
"or-chaining-bugfix.toy",
|
"or-chaining-bugfix.toy",
|
||||||
"panic-within-functions.toy",
|
"panic-within-functions.toy",
|
||||||
|
"polyfill-insert.toy",
|
||||||
|
"polyfill-remove.toy",
|
||||||
"ternary-expressions.toy",
|
"ternary-expressions.toy",
|
||||||
"types.toy",
|
"types.toy",
|
||||||
NULL
|
NULL
|
||||||
|
|||||||
Reference in New Issue
Block a user