Skip to content

Commit

Permalink
Escaped characters stored in strings correctly
Browse files Browse the repository at this point in the history
Fixed #147
  • Loading branch information
Ratstail91 committed Oct 30, 2024
1 parent feab02e commit 3cb2132
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 38 deletions.
37 changes: 1 addition & 36 deletions scripts/testificate.toy
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@


print 1 == 1; //true
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
print "\tHello\nworld";
5 changes: 4 additions & 1 deletion source/toy_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,14 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As

case TOY_TOKEN_LITERAL_STRING: {
char buffer[parser->previous.length + 1];
unsigned int escapeCounter = 0;

unsigned int i = 0, o = 0;
do {
buffer[i] = parser->previous.lexeme[o];
if (buffer[i] == '\\' && parser->previous.lexeme[++o]) {
escapeCounter++;

//also handle escape characters
switch(parser->previous.lexeme[o]) {
case 'n':
Expand All @@ -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);

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;
}
Expand Down
5 changes: 4 additions & 1 deletion tests/integrations/test_print.toy
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ print "Hello world!";
//print a concatenated string
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: the `assert` keyword will be useful for these
//TODO: the `assert` keyword will be useful for these

0 comments on commit 3cb2132

Please sign in to comment.