Skip to content

Commit 035d499

Browse files
committed
fixed negative number processing
1 parent d079951 commit 035d499

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

src/main/java/com/igormaznitsa/prologparser/terms/PrologFloat.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,9 @@ public String toString() {
8080
final String result = this.value.toEngineeringString();
8181
return result.indexOf('.') < 0 ? result + ".0" : result;
8282
}
83+
84+
@Override
85+
public boolean isNegative() {
86+
return this.value.signum() < 0;
87+
}
8388
}

src/main/java/com/igormaznitsa/prologparser/terms/PrologInt.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,9 @@ public Number getNumber() {
113113
public BigInteger getIntValue() {
114114
return this.value;
115115
}
116+
117+
@Override
118+
public boolean isNegative() {
119+
return this.value.signum() < 0;
120+
}
116121
}

src/main/java/com/igormaznitsa/prologparser/terms/PrologNumeric.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ public String getTermText() {
4848
return toString();
4949
}
5050

51+
public abstract boolean isNegative();
52+
5153
public abstract PrologNumeric neg();
5254
}

src/main/java/com/igormaznitsa/prologparser/tokenizer/TreeItem.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private boolean isOperator() {
233233

234234
@Override
235235
public String toString() {
236-
return savedTerm.toString();
236+
return "TreeItem[" + (this.savedTerm == null ? "null" : this.savedTerm.toString()) + ']';
237237
}
238238

239239
PrologTerm convertToTermAndRelease() {
@@ -298,9 +298,10 @@ PrologTerm convertToTermAndRelease() {
298298
right = this.rightBranch == null ? null : this.rightBranch.convertToTermAndRelease();
299299
}
300300

301-
// this code replaces '-'(number) to '-number'
302-
if (right instanceof PrologNumeric && wrapper.getQuotingType() == PrologTerm.QuotingType.NO_QUOTED && left == null && right.getTermType() == TermType.ATOM) {
303-
if ("-".equals(wrapper.getTermText())) {
301+
// this code replaces '-'(number) to '-number' if number is not negative one
302+
if ("-".equals(wrapper.getTermText()) && left == null && right instanceof PrologNumeric) {
303+
final PrologNumeric numeric = (PrologNumeric) right;
304+
if (!numeric.isNegative()) {
304305
result = ((PrologNumeric) right).neg();
305306
break;
306307
}

src/test/java/com/igormaznitsa/prologparser/IntegrationTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,6 @@ public void testParseInteger() {
263263
checkIntegerWithoutPPE("0", 0);
264264
checkIntegerWithoutPPE("1", 1);
265265
checkIntegerWithoutPPE("1313", 1313);
266-
checkIntegerWithoutPPE("-0", 0);
267-
checkIntegerWithoutPPE("-97", -97);
268266
checkIntegerWithoutPPE("-97", -97);
269267
checkIntegerWithoutPPE(Long.toString(Long.MAX_VALUE), Long.MAX_VALUE);
270268

0 commit comments

Comments
 (0)