Skip to content

Commit

Permalink
Implement \s and \th macros (#871)
Browse files Browse the repository at this point in the history
* Implement \s and \th

* Remove unimplemented
  • Loading branch information
itsmeow authored Oct 26, 2022
1 parent 983ef94 commit b822731
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
15 changes: 15 additions & 0 deletions Content.Tests/DMProject/Tests/Text/StringInterpolation6.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


/proc/RunTest()
var/text = "["1"]\s"
ASSERT(text == "1s")
text = "[0]\s"
ASSERT(text == "0s")
text = "[null]\s"
ASSERT(text == "s")
text = "[1]\s"
ASSERT(text == "1")
text = "[1.00000001]\s"
ASSERT(text == "1")
text = "[1.0001]\s"
ASSERT(text == "1.0001s")
19 changes: 19 additions & 0 deletions Content.Tests/DMProject/Tests/Text/StringInterpolation7.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


/proc/RunTest()
var/text = "[0]\th"
ASSERT(text == "0th")
text = "[1]\th"
ASSERT(text == "1st")
text = "[2]\th"
ASSERT(text == "2nd")
text = "[3]\th"
ASSERT(text == "3rd")
text = "[4]\th"
ASSERT(text == "4th")
text = "[-1]\th"
ASSERT(text == "-1th")
// TODO: this should assert/eval to 0th
text = "[null]\th"
ASSERT(findtextEx(text,"th") != 0)

8 changes: 1 addition & 7 deletions DMCompiler/Compiler/DM/DMParserHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ private DMASTExpression ExpressionFromString(Token constantToken)
}
i--;

bool unimplemented = false;
bool skipSpaces = false;
bool consumeSpaceCharacter = false;
switch (escapeSequence)
Expand Down Expand Up @@ -283,13 +282,12 @@ private DMASTExpression ExpressionFromString(Token constantToken)
//Plurals, ordinals, etc
//(things that hug, as a suffix, the [] that they reference)
case "s":
unimplemented = true;
if (CheckInterpolation(hasSeenNonRefInterpolation, interpolationValues, "s")) break;
stringBuilder.Append(StringFormatEncoder.Encode(StringFormatEncoder.FormatSuffix.PluralSuffix));
break;
case "th":
unimplemented = true;
if (CheckInterpolation(hasSeenNonRefInterpolation, interpolationValues, "th")) break;
// TODO: this should error if not DIRECTLY after an expression ([]\s vs []AA\s)
stringBuilder.Append(StringFormatEncoder.Encode(StringFormatEncoder.FormatSuffix.OrdinalIndicator));
break;
default:
Expand All @@ -311,10 +309,6 @@ private DMASTExpression ExpressionFromString(Token constantToken)
break;
}

if (unimplemented)
{
DMCompiler.UnimplementedWarning(constantToken.Location, $"Unimplemented escape sequence \"{escapeSequence}\"");
}
if (skipSpaces)
{
// Note that some macros in BYOND require a single/zero space between them and the []
Expand Down
28 changes: 28 additions & 0 deletions OpenDreamRuntime/Procs/DMOpcodeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,34 @@ private static void HandleSuffixPronoun(ref StringBuilder formattedString, ReadO
case StringFormatEncoder.FormatSuffix.LowerPossessivePronoun:
HandleSuffixPronoun(ref formattedString, interps, prevInterpIndex, new string[] { "his", "hers", "theirs", "its" });
break;
case StringFormatEncoder.FormatSuffix.PluralSuffix:
if (interps[prevInterpIndex].TryGetValueAsFloat(out var pluralNumber) && pluralNumber == 1)
{
continue;
}
formattedString.Append("s");
continue;
case StringFormatEncoder.FormatSuffix.OrdinalIndicator:
// TODO: if the preceding expression value is not a float, it should be replaced with 0 (0th)
if (interps[prevInterpIndex].TryGetValueAsFloat(out var ordinalNumber)) {
switch (ordinalNumber) {
case 1:
formattedString.Append("st");
break;
case 2:
formattedString.Append("nd");
break;
case 3:
formattedString.Append("rd");
break;
default:
formattedString.Append("th");
break;
}
} else {
formattedString.Append("th");
}
continue;
default:
if (Enum.IsDefined(typeof(StringFormatEncoder.FormatSuffix), formatType)) {
//Likely an unimplemented text macro, ignore it
Expand Down

0 comments on commit b822731

Please sign in to comment.