Skip to content

Commit c999a3b

Browse files
committed
Support integer comparisons in ArgScript booleans
1 parent cbfbc1f commit c999a3b

1 file changed

Lines changed: 54 additions & 51 deletions

File tree

src/sporemodder/file/argscript/ArgScriptLexer.java

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -541,33 +541,37 @@ private long parseIntParenthesis() throws DocumentException {
541541
* @throws DocumentException If there is an error while parsing the expression.
542542
*/
543543
public boolean parseBoolean() throws DocumentException {
544+
return parseBooleanInternal() != 0;
545+
}
546+
547+
private long parseBooleanInternal() throws DocumentException {
544548
isHexadecimal = false;
545-
549+
546550
skipWhitespaces();
547-
548-
boolean left = parseBoolAnd();
549-
551+
552+
long left = parseBoolAnd();
553+
550554
while (true) {
551555
skipWhitespaces();
552-
556+
553557
startIndex = index;
554-
558+
555559
StringBuilder sb = new StringBuilder();
556560
while (index < array.length && Character.isAlphabetic(array[index])) {
557561
sb.append(array[index]);
558562
index++;
559563
}
560564
String keyword = sb.toString();
561-
565+
562566
switch(keyword) {
563-
case "or":
564-
// We must do the function first, because otherwise it won't run if left is true
565-
left = parseBoolAnd() || left;
566-
break;
567-
default:
568-
// The keyword does not belong to this method, restore the original position
569-
index = startIndex;
570-
return left;
567+
case "or":
568+
// We must do the function first, because otherwise it won't run if left is true
569+
left = parseBoolAnd() != 0 || left != 0 ? 1 : 0;
570+
break;
571+
default:
572+
// The keyword does not belong to this method, restore the original position
573+
index = startIndex;
574+
return left;
571575
}
572576
}
573577
}
@@ -576,15 +580,15 @@ public boolean parseBoolean() throws DocumentException {
576580
* Parses a boolean expression inside a parenthesis, and returns the result of evaluating that expression.
577581
* @throws DocumentException If any of the parenthesis are missing, or there is an error while parsing the expression.
578582
*/
579-
private boolean parseBoolParenthesis() throws DocumentException {
583+
private long parseBoolParenthesis() throws DocumentException {
580584
skipWhitespaces();
581585

582586
if (index >= array.length || array[index] != '(') {
583587
throw new DocumentException(new DocumentError("Expected '(' in boolean expression.", index, index+1));
584588
}
585589
index++;
586-
587-
boolean result = parseBoolean();
590+
591+
long result = parseBooleanInternal();
588592
skipWhitespaces();
589593

590594
if (index >= array.length || array[index] != ')') {
@@ -597,19 +601,19 @@ private boolean parseBoolParenthesis() throws DocumentException {
597601

598602
/**
599603
* Parses a boolean keyword and returns the result of evaluating it.
600-
* It supports: <code>true, false, on, off</code>, boolean expressions inside parenthesis, and integer expressions that return either 1 (true) or 0 (false).
604+
* It supports: <code>true, false, on, off</code>, boolean expressions inside parenthesis, and integer expressions.
601605
* @throws DocumentException If there is an error while parsing the expression.
602606
*/
603-
private boolean parseBoolKeyword() throws DocumentException {
607+
private long parseBoolKeyword() throws DocumentException {
604608

605609
skipWhitespaces();
606-
610+
607611
if (array[index] == '(') {
608612
return parseBoolParenthesis();
609613
}
610614

611615
if (!Character.isAlphabetic(array[index])) {
612-
return parseInteger() == 0 ? false : true;
616+
return parseInteger();
613617
}
614618

615619
startIndex = index;
@@ -624,18 +628,18 @@ private boolean parseBoolKeyword() throws DocumentException {
624628
switch (keyword) {
625629
case "true":
626630
case "on":
627-
return true;
631+
return 1;
628632
case "false":
629633
case "off":
630-
return false;
634+
return 0;
631635
default:
632636
ArgScriptFunction function = functions.getOrDefault(keyword, null);
633637
if (function != null) {
634-
return function.getBoolean(this);
638+
return function.getInt(this);
635639
} else {
636640
// The keyword does not belong to this method, restore the original position
637641
index = startIndex;
638-
return parseInteger() == 0 ? false : true;
642+
return parseInteger();
639643
}
640644
}
641645
}
@@ -645,28 +649,30 @@ private boolean parseBoolKeyword() throws DocumentException {
645649
* comparison at all.
646650
* @throws DocumentException If there is an error while parsing the expression.
647651
*/
648-
private boolean parseBoolComparison() throws DocumentException {
652+
private long parseBoolComparison() throws DocumentException {
649653

650-
boolean left = parseBoolKeyword();
654+
long left = parseBoolKeyword();
651655

652656
while (true) {
653657
skipWhitespaces();
654658

655659
if (index >= array.length) {
656660
return left;
657661
}
658-
659-
// although the comparation symbols are supported, it does the same ==
662+
660663
switch (array[index]) {
661-
662664
case '>':
665+
index++;
666+
return left > parseBoolComparison() ? 1 : 0;
663667
case '<':
668+
index++;
669+
return left < parseBoolComparison() ? 1 : 0;
664670
case '=':
665671
index++;
666672
if (array[index] == '=') {
667673
index++;
668674
}
669-
left = left == parseBoolComparison();
675+
left = left == parseBoolComparison() ? 1 : 0;
670676
break;
671677

672678
case '!':
@@ -675,7 +681,7 @@ private boolean parseBoolComparison() throws DocumentException {
675681
throw new DocumentException(new DocumentError("Invalid operator !" + array[index] + "'. Did you mean != or 'not'?", index-1, index+1));
676682
}
677683
index++;
678-
left = left != parseBoolComparison();
684+
left = left != parseBoolComparison() ? 1 : 0;
679685
break;
680686

681687
default:
@@ -688,7 +694,7 @@ private boolean parseBoolComparison() throws DocumentException {
688694
* Same as {@link #parseBoolComparison()}, but this one supports the keyword 'not'.
689695
* @throws DocumentException
690696
*/
691-
private boolean parseBoolExtendedComparison() throws DocumentException {
697+
private long parseBoolExtendedComparison() throws DocumentException {
692698

693699
skipWhitespaces();
694700

@@ -700,24 +706,23 @@ private boolean parseBoolExtendedComparison() throws DocumentException {
700706
index++;
701707
}
702708
String keyword = sb.toString();
703-
704-
switch(keyword) {
705-
case "not":
706-
return !parseBoolExtendedComparison();
707-
default:
709+
710+
if (keyword.equals("not")) {
711+
return parseBoolExtendedComparison() == 0 ? 1 : 0;
712+
} else {
708713
// The keyword does not belong to this method, restore the original position
709714
index = startIndex;
710715
return parseBoolComparison();
711716
}
712-
}
717+
}
713718

714719
/**
715720
* Parses a boolean expression and checks if it is followed by 'and' and another expression.
716721
* @throws DocumentException
717722
*/
718-
private boolean parseBoolAnd() throws DocumentException {
723+
private long parseBoolAnd() throws DocumentException {
719724

720-
boolean left = parseBoolExtendedComparison();
725+
long left = parseBoolExtendedComparison();
721726

722727
while (true) {
723728
skipWhitespaces();
@@ -730,17 +735,15 @@ private boolean parseBoolAnd() throws DocumentException {
730735
index++;
731736
}
732737
String keyword = sb.toString();
733-
734-
switch(keyword) {
735-
case "and":
738+
739+
if (keyword.equals("and")) {
736740
// Order is important, if we do it the other way it won't finish parsing
737-
left = parseBoolExtendedComparison() && left;
738-
break;
739-
default:
741+
left = parseBoolExtendedComparison() != 0 && left != 0 ? 1 : 0;
742+
} else {
740743
// The keyword does not belong to this method, restore the original position
741-
index = startIndex;
742-
return left;
743-
}
744+
index = startIndex;
745+
return left;
746+
}
744747
}
745748
}
746749

0 commit comments

Comments
 (0)