Skip to content

Commit 006eb2b

Browse files
committed
add '\' escape sequence
1 parent 30fcfa5 commit 006eb2b

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

docs/syntax.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ var func return if elif else loop in true false as and or not str num bool list
1111

1212
numbers: Can start with 0x (base16), 0b (base2) or nothing (base10) denoting the base. Then a set of number characters valid for the particular base in addition to '-' as a visual seperator. Base ten numbers can also have a decimal point.
1313

14-
string: "" any characters enclosed inside double-qoutes
14+
string:
15+
- "" any characters enclosed inside double-qoutes. Hanldes escape sequences (for example \n becomes newline).
16+
- '' any characters enclosed inside single-qoutes Does not hanlde escape sequences.
1517

1618
identifiers: Must start with a letter (a, b, .. z), underscore (_) or hyphen (-). Can then be any sequence of the aformentioned characters plus any number character (0, 1, .. 9).
1719

src/interpreter/ast.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,16 @@ Stmt *stmt_copy(Arena *arena, Stmt *to_copy)
234234
}
235235
case STMT_CMD: {
236236
((CmdStmt *)copy)->cmd_name = str_view_arena_copy(arena, ((CmdStmt *)to_copy)->cmd_name);
237-
((CmdStmt *)copy)->arg_exprs = arena_ll_alloc(arena);
238-
LLItem *item;
239-
ARENA_LL_FOR_EACH(((CmdStmt *)to_copy)->arg_exprs, item)
240-
{
241-
arena_ll_append(((CmdStmt *)copy)->arg_exprs, expr_copy(arena, item->value));
237+
238+
if (((CmdStmt *)to_copy)->arg_exprs == NULL) {
239+
((CmdStmt *)copy)->arg_exprs = NULL;
240+
} else {
241+
((CmdStmt *)copy)->arg_exprs = arena_ll_alloc(arena);
242+
LLItem *item;
243+
ARENA_LL_FOR_EACH(((CmdStmt *)to_copy)->arg_exprs, item)
244+
{
245+
arena_ll_append(((CmdStmt *)copy)->arg_exprs, expr_copy(arena, item->value));
246+
}
242247
}
243248
break;
244249
}

src/interpreter/lexer.c

+3
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ StateFn lex_string(Lexer *lexer)
558558
case 'n':
559559
str_builder_append_char(&sb, '\n');
560560
break;
561+
case '\\':
562+
str_builder_append_char(&sb, '\\');
563+
break;
561564
default:
562565
report_lex_err(lexer, true, "Unknown escape sequence");
563566
/* Error occured. Continue parsing after string is terminated. */

test/str.slash

+9-17
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,8 @@
77
# $a[0] = "H"
88
# assert $a[0] == "H"
99
# item in
10-
# assert ("Hello" in $a) and ("World!" in $a)
1110
}
1211

13-
# test every method
14-
# {
15-
# var a = "a,b,c"
16-
# var res = $a.split(",")
17-
# assert $res.len() == 3
18-
# assert $res[0] == "a" and $res[1] == "b" and $res[2] == "c"
19-
#
20-
# $a = "a!!b!!c!!"
21-
# $res = $a.split("!!")
22-
# assert $res.len() == 3
23-
# assert $res[0] == "a" and $res[1] == "b" and $res[2] == "c"
24-
# }
25-
26-
assert "hello"\
27-
"world" == "helloworld"
28-
2912
# string comparison
3013
assert "hello" == "hello"
3114
assert "b" > "a"
@@ -40,3 +23,12 @@ assert "something is true"
4023
assert "Hello" in "Hello World"
4124
assert not ("Foo" in "Bar")
4225
assert "" in "something"
26+
27+
# multiline string
28+
assert "hello"\
29+
"world" == "helloworld"
30+
31+
# escape sequences
32+
assert '\n' != "\n" # "\n" becomes newline
33+
assert '\n' == ("\\" + "n") # does not become newline
34+
assert '\' == "\\"

0 commit comments

Comments
 (0)