Skip to content

Commit

Permalink
Don't unescape strings when printing. No more support for special cha…
Browse files Browse the repository at this point in the history
…racters.

Fixes #373
  • Loading branch information
dylanbeattie committed Jan 3, 2025
1 parent 05cdcc8 commit 16a3c43
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Starship/Rockstar.Engine/RockstarEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private Result Ninja(Ninja ninja) {

private Result Output(Output output) {
var value = Eval(output.Expression);
Write(Regex.Unescape(value.ToStrïng().Value));
Write(value.ToStrïng().Value);
Write(output.Suffix);
return new(value);
}
Expand Down
11 changes: 6 additions & 5 deletions Starship/Rockstar.Test/IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ private string Run(string rockstarCode) {
}

[Theory]
[InlineData("""A\nB""", new byte[] { 65, 10, 66 })]
[InlineData("""A\r\nB""", new byte[] { 65, 13, 10, 66 })]
[InlineData("""A\tB""", new byte[] { 65, 9, 66 })]
[InlineData("""A\\B""", new byte[] { 65, 92, 66 })]
public void EscapingOutputStringsWorks(string source, byte[] chars) {
[InlineData("""A\nB""", new byte[] { 65, 92, 110, 66 })]
[InlineData("""A\r\nB""", new byte[] { 65, 92, 114, 92, 110, 66 })]
[InlineData("""A\tB""", new byte[] { 65, 92, 116, 66 })]
[InlineData("""A\\B""", new byte[] { 65, 92, 92, 66 })]
[InlineData(@"\", new byte[] { 92 })]
public void BackspacesAreNotSpecial(string source, byte[] chars) {
var result = Run($"write \"{source}\"");
Encoding.UTF8.GetBytes(result).ShouldBe(chars);
}
Expand Down
6 changes: 6 additions & 0 deletions Starship/Rockstar.Test/Rockstar.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
<None Update="programs\examples\02-types-and-values\number-overflow.rock">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="programs\examples\02-types-and-values\unprintable-characters.rock">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="programs\examples\02-types-and-values\string-literals.rock">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -1203,6 +1206,9 @@
<None Update="programs\fixtures\loops\assignment-inside-loop.rock">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="programs\fixtures\strings\string-literals.rock">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Print "this is a string literal"

Print "This string includes ""quotes"""

Print "backslashes \don't \do \anything \special \r\n\t\w\s\0"

Print "This string
contains
line
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
this is a string literal
This string includes "quotes"
backslashes \don't \do \anything \special \r\n\t\w\s\0
This string
contains
line
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The string is "Name"
Rock it with 9 (append a TAB character)
Rock it with "Instrument"
Rock it with 10 (append a LF)
Rock it with "Eddie", 9, "Guitar", 10
Rock it with "Alex", 9, "Drums", 10
Rock it with "Mike", 9, "Bass", 10
Print it

(prints:
Name Instrument
Eddie Guitar
Alex Drums
Mike Bass

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
the string = "A\tB"
for every char in the string write the char
print the string
(prints: A\\tBA\\tB)
5 changes: 5 additions & 0 deletions codewithrockstar.com/docs/02-types-and-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Rockstar strings are surrounded by double quotes. A string literal includes ever
The keywords `empty``silent`, and `silence` are aliases for the empty string (`""`)

{% rockstar_include string-literals.rock %}

A backslash in Rockstar doesn't do anything special, and Rockstar doesn't have escape sequences like `\n` or `\t`. If you need to add special characters to Rockstar strings, use the `rock` keyword to append characters based on their ASCII / Unicode code point:

{% rockstar_include unprintable-characters.rock %}
## Numbers

**Number literals** are written as ordinary digits; decimals and negative numbers are supported:
Expand All @@ -39,6 +43,7 @@ Numbers with more than 29 digits will be rounded to 29 digits if they have a dec

{% rockstar_include number-29-digits.rock %}
## Booleans

Rockstar supports the Boolean literals `true` (aliases: `yes`, `ok`, `right`) and `false` (aliases: `no`, `wrong`, `lies`).
{% rockstar_include boolean-literals.rock %}
## Null
Expand Down

0 comments on commit 16a3c43

Please sign in to comment.