mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
add read/write for bools, and fixed sanitize issue
This commit is contained in:
@@ -165,20 +165,20 @@ static int nativeRead(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal valueLiteral = Toy_popLiteralArray(arguments);
|
Toy_Literal typeLiteral = Toy_popLiteralArray(arguments);
|
||||||
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
Toy_Literal selfLiteral = Toy_popLiteralArray(arguments);
|
||||||
|
|
||||||
// parse the value (if it's an identifier)
|
// parse the type (if it's an identifier)
|
||||||
Toy_Literal valueLiteralIdn = valueLiteral;
|
Toy_Literal typeLiteralIdn = typeLiteral;
|
||||||
if (TOY_IS_IDENTIFIER(valueLiteral) && Toy_parseIdentifierToValue(interpreter, &valueLiteral)) {
|
if (TOY_IS_IDENTIFIER(typeLiteral) && Toy_parseIdentifierToValue(interpreter, &typeLiteral)) {
|
||||||
Toy_freeLiteral(valueLiteralIdn);
|
Toy_freeLiteral(typeLiteralIdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the value type
|
// check the type type
|
||||||
if (!TOY_IS_TYPE(valueLiteral)) {
|
if (!TOY_IS_TYPE(typeLiteral)) {
|
||||||
interpreter->errorOutput("Incorrect argument type expected a type as the first argument to read(type)\n");
|
interpreter->errorOutput("Incorrect argument type expected a type as the first argument to read(type)\n");
|
||||||
Toy_freeLiteral(selfLiteral);
|
Toy_freeLiteral(selfLiteral);
|
||||||
Toy_freeLiteral(valueLiteral);
|
Toy_freeLiteral(typeLiteral);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -193,7 +193,7 @@ static int nativeRead(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
if (!TOY_IS_OPAQUE(selfLiteral) && TOY_GET_OPAQUE_TAG(selfLiteral) != TOY_OPAQUE_TAG_FILE) {
|
if (!TOY_IS_OPAQUE(selfLiteral) && TOY_GET_OPAQUE_TAG(selfLiteral) != TOY_OPAQUE_TAG_FILE) {
|
||||||
interpreter->errorOutput("Incorrect self type, read(type) expects a file type\n");
|
interpreter->errorOutput("Incorrect self type, read(type) expects a file type\n");
|
||||||
Toy_freeLiteral(selfLiteral);
|
Toy_freeLiteral(selfLiteral);
|
||||||
Toy_freeLiteral(valueLiteral);
|
Toy_freeLiteral(typeLiteral);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -201,24 +201,21 @@ static int nativeRead(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
||||||
|
|
||||||
Toy_Literal resultLiteral = TOY_TO_NULL_LITERAL;
|
Toy_Literal resultLiteral = TOY_TO_NULL_LITERAL;
|
||||||
|
int error = 0;
|
||||||
|
|
||||||
switch (valueLiteral.as.type.typeOf) {
|
switch (TOY_AS_TYPE(typeLiteral).typeOf) {
|
||||||
// case TOY_LITERAL_BOOLEAN:
|
case TOY_LITERAL_BOOLEAN: {
|
||||||
// {
|
char value = '0';
|
||||||
// char value[TOY_MAX_STRING_LENGTH] = {0};
|
error = fscanf(file->fp, "%c", &value);
|
||||||
// fgets(value, sizeof(value) - 1, file->fp);
|
|
||||||
// value[TOY_MAX_STRING_LENGTH] = '\0';
|
|
||||||
|
|
||||||
// Toy_Literal stringLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString(value));
|
resultLiteral = TOY_TO_BOOLEAN_LITERAL(value != '0');
|
||||||
// resultLiteral = TOY_TO_BOOLEAN_LITERAL(TOY_IS_TRUTHY(stringLiteral));
|
|
||||||
// Toy_freeLiteral(stringLiteral);
|
|
||||||
|
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
|
|
||||||
case TOY_LITERAL_INTEGER: {
|
case TOY_LITERAL_INTEGER: {
|
||||||
int value = 0;
|
int value = 0;
|
||||||
fscanf(file->fp, "%i", &value);
|
error = fscanf(file->fp, "%i", &value);
|
||||||
|
|
||||||
resultLiteral = TOY_TO_INTEGER_LITERAL(value);
|
resultLiteral = TOY_TO_INTEGER_LITERAL(value);
|
||||||
|
|
||||||
@@ -227,7 +224,7 @@ static int nativeRead(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
|
|
||||||
case TOY_LITERAL_FLOAT: {
|
case TOY_LITERAL_FLOAT: {
|
||||||
float value = 0.0f;
|
float value = 0.0f;
|
||||||
fscanf(file->fp, "%f", &value);
|
error = fscanf(file->fp, "%f", &value);
|
||||||
|
|
||||||
resultLiteral = TOY_TO_FLOAT_LITERAL(value);
|
resultLiteral = TOY_TO_FLOAT_LITERAL(value);
|
||||||
|
|
||||||
@@ -247,14 +244,19 @@ static int nativeRead(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
default: {
|
default: {
|
||||||
// TODO handle other types
|
// TODO handle other types
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
|
if (error != EOF) {
|
||||||
|
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Toy_pushLiteralArray(&interpreter->stack, TOY_TO_NULL_LITERAL);
|
||||||
|
}
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
Toy_freeLiteral(resultLiteral);
|
Toy_freeLiteral(resultLiteral);
|
||||||
Toy_freeLiteral(valueLiteral);
|
Toy_freeLiteral(typeLiteral);
|
||||||
Toy_freeLiteral(selfLiteral);
|
Toy_freeLiteral(selfLiteral);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@@ -307,6 +309,11 @@ static int nativeWrite(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments
|
|||||||
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
switch (valueLiteral.type) {
|
switch (valueLiteral.type) {
|
||||||
|
case TOY_LITERAL_BOOLEAN: {
|
||||||
|
result = fprintf(file->fp, "%i", TOY_AS_BOOLEAN(valueLiteral));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case TOY_LITERAL_INTEGER: {
|
case TOY_LITERAL_INTEGER: {
|
||||||
result = fprintf(file->fp, "%i", TOY_AS_INTEGER(valueLiteral));
|
result = fprintf(file->fp, "%i", TOY_AS_INTEGER(valueLiteral));
|
||||||
break;
|
break;
|
||||||
@@ -479,8 +486,8 @@ static int nativeSeek(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
||||||
|
Toy_RefString* orginString = TOY_AS_STRING(originLiteral);
|
||||||
int offset = TOY_AS_INTEGER(offsetLiteral);
|
int offset = TOY_AS_INTEGER(offsetLiteral);
|
||||||
Toy_RefString* orginString = Toy_copyRefString(TOY_AS_STRING(originLiteral));
|
|
||||||
|
|
||||||
int origin = 0;
|
int origin = 0;
|
||||||
if (Toy_equalsRefStringCString(orginString, "bgn")) {
|
if (Toy_equalsRefStringCString(orginString, "bgn")) {
|
||||||
@@ -500,8 +507,8 @@ static int nativeSeek(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
|
Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
Toy_deleteRefString(orginString);
|
|
||||||
Toy_freeLiteral(resultLiteral);
|
Toy_freeLiteral(resultLiteral);
|
||||||
|
Toy_freeLiteral(originLiteral);
|
||||||
Toy_freeLiteral(offsetLiteral);
|
Toy_freeLiteral(offsetLiteral);
|
||||||
Toy_freeLiteral(selfLiteral);
|
Toy_freeLiteral(selfLiteral);
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
1
|
||||||
8
|
8
|
||||||
12.5
|
12.5
|
||||||
test
|
test
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
8
|
08
|
||||||
12.500000
|
12.500000
|
||||||
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||||
|
|||||||
@@ -21,9 +21,10 @@ fn reset() {
|
|||||||
{
|
{
|
||||||
var reader = open("scripts:/lib/file/inputs.txt", "r");
|
var reader = open("scripts:/lib/file/inputs.txt", "r");
|
||||||
|
|
||||||
|
assert reader.read(bool) == true, "read true bool failed";
|
||||||
assert reader.read(int) == 8, "read int failed";
|
assert reader.read(int) == 8, "read int failed";
|
||||||
assert reader.read(float) == 12.5, "read float failed";
|
assert reader.read(float) == 12.5, "read float failed";
|
||||||
assert reader.read(string) == "\ntest\n", "read string failed";
|
assert reader.read(string) == "\ntest", "read string failed";
|
||||||
|
|
||||||
// invaild types
|
// invaild types
|
||||||
assert input.read(type) == null, "read type failed";
|
assert input.read(type) == null, "read type failed";
|
||||||
@@ -35,9 +36,11 @@ fn reset() {
|
|||||||
// test write
|
// test write
|
||||||
{
|
{
|
||||||
var writer = open("scripts:/lib/file/outputs.txt", "w");
|
var writer = open("scripts:/lib/file/outputs.txt", "w");
|
||||||
|
assert writer.write(false) == true, "write bool failed";
|
||||||
assert writer.write(8) == true, "write int failed";
|
assert writer.write(8) == true, "write int failed";
|
||||||
assert writer.write("\n") == true, "write string failed";
|
assert writer.write("\n") == true, "write string failed";
|
||||||
assert writer.write(12.5) == true, "write float failed";
|
assert writer.write(12.5) == true, "write float failed";
|
||||||
|
assert writer.write("\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n") == true, "write long string failed";
|
||||||
|
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
@@ -53,18 +56,6 @@ fn reset() {
|
|||||||
assert file == null, "open failed on nonexisting file";
|
assert file == null, "open failed on nonexisting file";
|
||||||
}
|
}
|
||||||
|
|
||||||
// test write
|
|
||||||
{
|
|
||||||
var writer = open(PATH, "w");
|
|
||||||
assert writer != null, "open failed in writing mode";
|
|
||||||
|
|
||||||
assert writer.read(string) != null, "read in writing mode failed";
|
|
||||||
assert writer.write("writen text") == true, "write in writing mode failed";
|
|
||||||
|
|
||||||
writer.close();
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
// test append
|
// test append
|
||||||
{
|
{
|
||||||
var appender = open(PATH, "a");
|
var appender = open(PATH, "a");
|
||||||
|
|||||||
Reference in New Issue
Block a user