Escaped characters stored in strings correctly

Fixed #147
This commit is contained in:
2024-10-30 21:13:57 +11:00
parent feab02e790
commit 3cb2132bfa
3 changed files with 9 additions and 38 deletions

View File

@@ -1,38 +1,3 @@
print 1 == 1; //true print "\tHello\nworld";
print 1 != 1; //false
print 1 < 2; //true
print "foo" > "bar"; //true
print 1 < 2; //true
print 1 > 2; //false
print 2 <= 2; //true
print 2 >= 2; //true
print 1 <= 2; //true
print 1 >= 2; //false
print true && true; //true
print true && false; //false
print false && true; //false
print false && false; //false
print true || true; //true
print true || false; //true
print false || true; //true
print false || false; //false
print !true; //false
print !false; //true
//nesting
print true && false || true; //TODO: a warning is needed for this

View File

@@ -309,11 +309,14 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As
case TOY_TOKEN_LITERAL_STRING: { case TOY_TOKEN_LITERAL_STRING: {
char buffer[parser->previous.length + 1]; char buffer[parser->previous.length + 1];
unsigned int escapeCounter = 0;
unsigned int i = 0, o = 0; unsigned int i = 0, o = 0;
do { do {
buffer[i] = parser->previous.lexeme[o]; buffer[i] = parser->previous.lexeme[o];
if (buffer[i] == '\\' && parser->previous.lexeme[++o]) { if (buffer[i] == '\\' && parser->previous.lexeme[++o]) {
escapeCounter++;
//also handle escape characters //also handle escape characters
switch(parser->previous.lexeme[o]) { switch(parser->previous.lexeme[o]) {
case 'n': case 'n':
@@ -334,7 +337,7 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As
} while (parser->previous.lexeme[o++] && i < parser->previous.length); } while (parser->previous.lexeme[o++] && i < parser->previous.length);
buffer[i] = '\0'; buffer[i] = '\0';
Toy_private_emitAstValue(bucketHandle, rootHandle, TOY_VALUE_FROM_STRING(Toy_createStringLength(bucketHandle, buffer, i))); Toy_private_emitAstValue(bucketHandle, rootHandle, TOY_VALUE_FROM_STRING(Toy_createStringLength(bucketHandle, buffer, i - escapeCounter)));
return TOY_AST_FLAG_NONE; return TOY_AST_FLAG_NONE;
} }

View File

@@ -10,6 +10,9 @@ print "Hello world!";
//print a concatenated string //print a concatenated string
print "Hello" .. "world!"; print "Hello" .. "world!";
//print with escaped characters
print "\tHello\nworld";
//TODO: in the repl, -s to supress output, or -d to print debugging info //TODO: in the repl, -s to supress output, or -d to print debugging info
//TODO: the `assert` keyword will be useful for these //TODO: the `assert` keyword will be useful for these