From 3cb2132bfa0ac8f60701f6af0884e3815f03e782 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 30 Oct 2024 21:13:57 +1100 Subject: [PATCH] Escaped characters stored in strings correctly Fixed #147 --- scripts/testificate.toy | 37 +------------------------------ source/toy_parser.c | 5 ++++- tests/integrations/test_print.toy | 5 ++++- 3 files changed, 9 insertions(+), 38 deletions(-) diff --git a/scripts/testificate.toy b/scripts/testificate.toy index 6c92569..97099df 100644 --- a/scripts/testificate.toy +++ b/scripts/testificate.toy @@ -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 \ No newline at end of file +print "\tHello\nworld"; diff --git a/source/toy_parser.c b/source/toy_parser.c index a41c590..0201608 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -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': @@ -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; } diff --git a/tests/integrations/test_print.toy b/tests/integrations/test_print.toy index 26e615c..e89846c 100644 --- a/tests/integrations/test_print.toy +++ b/tests/integrations/test_print.toy @@ -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 \ No newline at end of file +//TODO: the `assert` keyword will be useful for these