Skip to content

Commit 354cce4

Browse files
committed
Fix minus sign for hex/bin/oct/dec literals
1 parent 2da11c0 commit 354cce4

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

src/main/java/com/laytonsmith/core/MethodScriptCompiler.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ private MethodScriptCompiler() {
103103

104104
private static final Pattern VAR_PATTERN = Pattern.compile("\\$[\\p{L}0-9_]+");
105105
private static final Pattern IVAR_PATTERN = Pattern.compile(IVariable.VARIABLE_NAME_REGEX);
106+
private static final Pattern NUM_PATTERN = Pattern.compile("[0-9]+");
107+
private static final Pattern FUNC_NAME_PATTERN = Pattern.compile("[_a-zA-Z0-9]+");
106108

107109
/**
108110
* Lexes the script, and turns it into a token stream.This looks through the script character by character.
@@ -593,7 +595,7 @@ public static TokenStream lex(String script, Environment env, File file,
593595
} else {
594596
// It's not a keyword, but a normal function.
595597
String funcName = buf.toString();
596-
if(funcName.matches("[_a-zA-Z0-9]+")) {
598+
if(FUNC_NAME_PATTERN.matcher(funcName).matches()) {
597599
tokenList.add(new Token(TType.FUNC_NAME, funcName, target));
598600
} else {
599601
tokenList.add(new Token(TType.UNKNOWN, funcName, target));
@@ -911,8 +913,7 @@ public static TokenStream lex(String script, Environment env, File file,
911913
if(!prevNonWhitespace.type.isIdentifier() // Don't convert "number/string/var ± ...".
912914
&& prevNonWhitespace.type != TType.FUNC_END // Don't convert "func() ± ...".
913915
&& prevNonWhitespace.type != TType.RSQUARE_BRACKET // Don't convert "] ± ..." (arrays).
914-
&& !IVAR_PATTERN.matcher(t.val()).matches() // Don't convert "± @var".
915-
&& !VAR_PATTERN.matcher(t.val()).matches()) { // Don't convert "± $var".
916+
&& NUM_PATTERN.matcher(t.val()).matches()) { // Only convert numbers
916917
// It is a negative/positive number: Absorb the sign.
917918
t.value = prev1.value + t.value;
918919
it.remove(); // Remove 'prev1'.
@@ -1769,28 +1770,35 @@ public static ParseTree compile(TokenStream stream, Environment environment,
17691770
} catch (ConfigRuntimeException ex) {
17701771
throw new ConfigCompileException(ex);
17711772
}
1772-
if((c instanceof CInt || c instanceof CDecimal) && next1.type == TType.DOT && next2.type == TType.LIT) {
1773-
// make CDouble/CDecimal here because otherwise Long.parseLong() will remove
1774-
// minus zero before decimals and leading zeroes after decimals
1773+
// make CDouble/CDecimal here because otherwise Long.parseLong() will remove
1774+
// minus zero before decimals and leading zeroes after decimals
1775+
if(c instanceof CInt && next1.type == TType.DOT && next2.type == TType.LIT) {
17751776
try {
1776-
if(t.value.startsWith("0m")) {
1777-
// CDecimal
1778-
String neg = "";
1779-
if(prev1.value.equals("-")) {
1780-
neg = "-";
1781-
}
1782-
c = new CDecimal(neg + t.value.substring(2) + '.' + next2.value, t.target);
1783-
} else {
1784-
// CDouble
1785-
c = new CDouble(Double.parseDouble(t.val() + '.' + next2.val()), t.target);
1786-
}
1777+
c = new CDouble(Double.parseDouble(t.val() + '.' + next2.val()), t.target);
17871778
i += 2;
17881779
} catch (NumberFormatException e) {
17891780
// Not a double
17901781
}
1782+
constructCount.peek().incrementAndGet();
1783+
} else if(c instanceof CDecimal) {
1784+
String neg = "";
1785+
if(prev1.value.equals("-")) {
1786+
// Absorb sign unless neg() becomes compatible with CDecimal
1787+
neg = "-";
1788+
tree.removeChildAt(tree.getChildren().size() - 1);
1789+
} else {
1790+
constructCount.peek().incrementAndGet();
1791+
}
1792+
if(next1.type == TType.DOT && next2.type == TType.LIT) {
1793+
c = new CDecimal(neg + t.value.substring(2) + '.' + next2.value, t.target);
1794+
i += 2;
1795+
} else {
1796+
c = new CDecimal(neg + t.value.substring(2), t.target);
1797+
}
1798+
} else {
1799+
constructCount.peek().incrementAndGet();
17911800
}
17921801
tree.addChild(new ParseTree(c, fileOptions));
1793-
constructCount.peek().incrementAndGet();
17941802
} else if(t.type.equals(TType.STRING) || t.type.equals(TType.COMMAND)) {
17951803
tree.addChild(new ParseTree(new CString(t.val(), t.target), fileOptions));
17961804
constructCount.peek().incrementAndGet();

src/test/java/com/laytonsmith/core/MethodScriptCompilerTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,6 @@ public void testWhitespaceAroundSymbol1() throws Exception {
10051005
// verify(fakePlayer).sendMessage("yep yep");
10061006
// }
10071007
@Test
1008-
@Ignore("Ignore for now, until the feature is implemented")
10091008
public void testLiteralDecimal() throws Exception {
10101009
assertEquals("123.4", SRun("0m123.4", fakePlayer));
10111010
assertEquals("123", SRun("0m123", fakePlayer));
@@ -1024,11 +1023,10 @@ public void testLiteralBinary() throws Exception {
10241023
}
10251024

10261025
@Test
1027-
@Ignore("Ignore for now, until the feature is implemented")
10281026
public void testLiteralBinary2() throws Exception {
10291027
assertEquals("6", SRun("0b0110", fakePlayer));
10301028
assertEquals("-6", SRun("-0b0110", fakePlayer));
1031-
assertEquals("int", SRun("typeof(-0b0110)", fakePlayer));
1029+
assertEquals("ms.lang.int", SRun("typeof(-0b0110)", fakePlayer));
10321030
}
10331031

10341032
@Test

0 commit comments

Comments
 (0)