cleaning up tests

This commit is contained in:
2022-09-06 09:22:50 +01:00
parent b8f20add66
commit 17f1dc8647
10 changed files with 116 additions and 36 deletions

View File

@@ -1,23 +0,0 @@
var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
week[::-2] = ["first", "second", "third"];
print week;
var str = "0123456789";
str[3:5:-2] = "abc";
print str;
str = "Hello world";
print str; //Hello world
print str[::2]; //Hlowrd
print str[::-2]; //drwolH

View File

@@ -0,0 +1,29 @@
//test basic replacement
{
var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
week[3] = "Holiday";
assert week == ["monday", "tuesday", "wednesday", "Holiday", "friday", "saturday", "sunday"], "basic replacement failed";
}
//test backwards
{
var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
assert week[::-1] == ["sunday", "saturday", "friday", "thursday", "wednesday", "tuesday", "monday"], "backwards failed";
}
//test array weird manipulation
{
var week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
week[::-2] = ["first", "second", "third"];
assert week == ["monday", "tuesday", "third", "thursday", "second", "saturday", "first"], "array weird manipulation failed";
}
print "All good";

View File

@@ -0,0 +1,21 @@
//test basic insertion
{
var d = [:];
d["foo"] = "bar";
assert d == ["foo":"bar"], "basic insertion failed";
}
//test dot insertion
{
var d = [:];
d.foo = "bar";
assert d == ["foo":"bar"], "dot insertion failed";
}
print "All good";

View File

@@ -0,0 +1,21 @@
//test basic replacement
var greeting: string = "hello world";
greeting[0:4] = "goodnight";
assert greeting == "goodnight world", "basic replacement failed";
//test backwards string
assert greeting[::-1] == "dlrow thgindoog", "backwards string failed";
//test string weird manipulation
var numbers = "0123456789";
numbers[::-2] = "abc";
assert numbers == "01234c6b8a", "string weird manipulation failed";
print "All good";

View File

@@ -306,7 +306,7 @@ static Opcode writeCompilerWithJumps(Compiler* compiler, Node* node, void* break
//pass to the child nodes, then embed the binary command (math, etc.)
Opcode override = writeCompilerWithJumps(compiler, node->binary.left, breakAddressesPtr, continueAddressesPtr, jumpOffsets);
//special case for when indexing
//special case for when indexing and assigning
if (override != OP_EOF && node->binary.opcode >= OP_VAR_ASSIGN && node->binary.opcode <= OP_VAR_MODULO_ASSIGN) {
writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets);
compiler->bytecode[compiler->count++] = (unsigned char)override + 2; //1 byte WARNING: enum arithmetic
@@ -314,6 +314,11 @@ static Opcode writeCompilerWithJumps(Compiler* compiler, Node* node, void* break
return OP_EOF;
}
//compensate for... yikes
if (override != OP_EOF) {
compiler->bytecode[compiler->count++] = (unsigned char)override; //1 byte
}
//return from the index-binary
Opcode ret = writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets);

View File

@@ -1355,6 +1355,8 @@ static bool execIndex(Interpreter* interpreter) {
if (!IS_IDENTIFIER(compound)) {
interpreter->errorOutput("Unknown literal found in indexing notation\n");
printLiteralCustom(compound, interpreter->errorOutput);
interpreter->errorOutput("\n");
freeLiteral(third);
freeLiteral(second);
freeLiteral(first);
@@ -1434,6 +1436,8 @@ static bool execDot(Interpreter* interpreter) {
if (!IS_IDENTIFIER(compound)) {
interpreter->errorOutput("Unknown literal found in dot notation\n");
printLiteralCustom(compound, interpreter->errorOutput);
interpreter->errorOutput("\n");
freeLiteral(first);
freeLiteral(compound);
return false;
@@ -1500,6 +1504,8 @@ static bool execIndexAssign(Interpreter* interpreter) {
if (!IS_IDENTIFIER(compound)) {
interpreter->errorOutput("Unknown literal found in index assigning notation\n");
printLiteralCustom(compound, interpreter->errorOutput);
interpreter->errorOutput("\n");
freeLiteral(assign);
freeLiteral(third);
freeLiteral(second);
@@ -1669,6 +1675,8 @@ static bool execDotAssign(Interpreter* interpreter) {
if (!IS_IDENTIFIER(compound)) {
interpreter->errorOutput("Unknown literal found in dot assigning notation\n");
printLiteralCustom(compound, interpreter->errorOutput);
interpreter->errorOutput("\n");
freeLiteral(assign);
freeLiteral(first);
freeLiteral(compound);

View File

@@ -41,7 +41,6 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) {
if (IS_DICTIONARY(compound)) {
value = getLiteralDictionary(AS_DICTIONARY(compound), first);
//dictionary
//dictionary
if (IS_NULL(op)) {
pushLiteralArray(&interpreter->stack, value);
@@ -525,6 +524,8 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) {
for (int i = AS_INTEGER(second) + 1; i < strlen(AS_STRING(compound)); i++) {
result[ resultIndex++ ] = AS_STRING(compound)[ i ];
}
result[ resultIndex++ ] = '\0';
}
//else override elements of the array instead
@@ -532,7 +533,7 @@ int _index(Interpreter* interpreter, LiteralArray* arguments) {
//copy compound to result
snprintf(result, MAX_STRING_LENGTH, AS_STRING(compound));
int min = AS_INTEGER(third) > 0 ? AS_INTEGER(first) : AS_INTEGER(second);
int min = AS_INTEGER(third) > 0 ? AS_INTEGER(first) : AS_INTEGER(second) - 1;
int assignIndex = 0;
for (int i = min; i >= AS_INTEGER(first) && i <= AS_INTEGER(second) && assignIndex < strlen(AS_STRING(assign)); i += AS_INTEGER(third)) {

View File

@@ -55,7 +55,7 @@ typedef enum Opcode {
OP_INDEX_ASSIGN,
OP_DOT_ASSIGN,
//comparion of values
//comparison of values
OP_COMPARE_EQUAL,
OP_COMPARE_NOT_EQUAL,
OP_COMPARE_LESS,

View File

@@ -530,6 +530,11 @@ static Opcode identifier(Parser* parser, Node** nodeHandle) {
//make a copy of the string
Token identifierToken = parser->previous;
if (identifierToken.type != TOKEN_IDENTIFIER) {
error(parser, parser->previous, "Expected identifier");
return OP_EOF;
}
int length = identifierToken.length;
//for safety
@@ -739,10 +744,10 @@ static Opcode indexAccess(Parser* parser, Node** nodeHandle) {
}
if (match(parser, TOKEN_BRACKET_RIGHT)) {
// freeNode(second);
// freeNode(third);
// second = NULL;
// third = NULL;
freeNode(second);
freeNode(third);
second = NULL;
third = NULL;
emitNodeIndex(nodeHandle, first, second, third);
return OP_INDEX;
@@ -756,8 +761,8 @@ static Opcode indexAccess(Parser* parser, Node** nodeHandle) {
}
if (match(parser, TOKEN_BRACKET_RIGHT)) {
// freeNode(third);
// third = NULL;
freeNode(third);
third = NULL;
emitNodeIndex(nodeHandle, first, second, third);
return OP_INDEX;
}
@@ -1267,6 +1272,11 @@ static void importStmt(Parser* parser, Node** nodeHandle) {
Node* node = NULL;
advance(parser);
identifier(parser, &node);
if (node == NULL) {
return;
}
Literal idn = copyLiteral(node->atomic.literal);
freeNode(node);
@@ -1292,6 +1302,11 @@ static void exportStmt(Parser* parser, Node** nodeHandle) {
Node* node = NULL;
advance(parser);
identifier(parser, &node);
if (node == NULL) {
return;
}
Literal idn = copyLiteral(node->atomic.literal);
freeNode(node);

View File

@@ -162,7 +162,6 @@ int main() {
{
//run each file in ../scripts/test/
int count = 15;
char* filenames[] = {
"arithmetic.toy",
"casting.toy",
@@ -170,6 +169,9 @@ int main() {
"dot-and-matrix.toy",
"functions.toy",
"imports-and-exports.toy",
"index-arrays.toy",
"index-dictionaries.toy",
"index-strings.toy",
"jumps.toy",
"jumps-in-functions.toy",
"logicals.toy",
@@ -178,10 +180,11 @@ int main() {
"long-literals.toy",
"native-functions.toy",
"panic-within-functions.toy",
"types.toy"
"types.toy",
NULL
};
for (int i = 0; i < count; i++) {
for (int i = 0; filenames[i]; i++) {
printf("Running %s\n", filenames[i]);
char buffer[128];