3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-13 22:50:06 +01:00

Added the ternary operator.

Dieser Commit ist enthalten in:
TomyLobo 2011-11-24 19:44:15 +01:00
Ursprung 9456c9e58f
Commit 7cbb7da80b
3 geänderte Dateien mit 61 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -105,6 +105,8 @@ public class Lexer {
characterTokens.add('{'); characterTokens.add('{');
characterTokens.add('}'); characterTokens.add('}');
characterTokens.add(';'); characterTokens.add(';');
characterTokens.add('?');
characterTokens.add(':');
} }
private static final Set<String> keywords = new HashSet<String>(Arrays.asList("if", "else", "while", "do", "for", "break", "continue", "return")); private static final Set<String> keywords = new HashSet<String>(Arrays.asList("if", "else", "while", "do", "for", "break", "continue", "return"));

Datei anzeigen

@ -28,6 +28,7 @@ import java.util.Map;
import com.sk89q.worldedit.expression.Identifiable; import com.sk89q.worldedit.expression.Identifiable;
import com.sk89q.worldedit.expression.lexer.tokens.OperatorToken; import com.sk89q.worldedit.expression.lexer.tokens.OperatorToken;
import com.sk89q.worldedit.expression.lexer.tokens.Token; import com.sk89q.worldedit.expression.lexer.tokens.Token;
import com.sk89q.worldedit.expression.runtime.Conditional;
import com.sk89q.worldedit.expression.runtime.RValue; import com.sk89q.worldedit.expression.runtime.RValue;
import com.sk89q.worldedit.expression.runtime.Operators; import com.sk89q.worldedit.expression.runtime.Operators;
@ -235,9 +236,63 @@ public final class ParserProcessors {
} }
private static RValue processTernaryOps(LinkedList<Identifiable> input) throws ParserException { private static RValue processTernaryOps(LinkedList<Identifiable> input) throws ParserException {
LinkedList<Identifiable> lhs = new LinkedList<Identifiable>();
LinkedList<Identifiable> mhs = new LinkedList<Identifiable>();
LinkedList<Identifiable> rhs = new LinkedList<Identifiable>();
int partsFound = 0;
int conditionalsFound = 0;
for (Identifiable identifiable : input) {
final char character = identifiable.id();
switch (character) {
case '?':
++conditionalsFound;
break;
case ':':
--conditionalsFound;
break;
}
if (conditionalsFound < 0) {
throw new ParserException(identifiable.getPosition(), "Unexpected ':'");
}
switch (partsFound) {
case 0:
if (character == '?') {
System.out.println("question mark found");
partsFound = 1;
} else {
lhs.addLast(identifiable);
}
break;
case 1:
if (conditionalsFound == 0 && character == ':') {
System.out.println("matching colon found");
partsFound = 2;
} else {
mhs.addLast(identifiable);
}
break;
case 2:
rhs.addLast(identifiable);
}
}
if (partsFound < 2) {
return processBinaryOpsLA(input, binaryOpMapsLA.length - 1); return processBinaryOpsLA(input, binaryOpMapsLA.length - 1);
} }
RValue lhsInvokable = processBinaryOpsLA(lhs, binaryOpMapsLA.length - 1);
RValue mhsInvokable = processTernaryOps(mhs);
RValue rhsInvokable = processTernaryOps(rhs);
return new Conditional(input.get(lhs.size()).getPosition(), lhsInvokable, mhsInvokable, rhsInvokable);
}
private static RValue processUnaryOps(LinkedList<Identifiable> input) throws ParserException { private static RValue processUnaryOps(LinkedList<Identifiable> input) throws ParserException {
// Preprocess postfix operators into unary operators // Preprocess postfix operators into unary operators
final Identifiable center; final Identifiable center;

Datei anzeigen

@ -55,8 +55,10 @@ public class Conditional extends Node {
public String toString() { public String toString() {
if (falsePart == null) { if (falsePart == null) {
return "if (" + condition + ") { " + truePart + " }"; return "if (" + condition + ") { " + truePart + " }";
} else { } else if (truePart instanceof Sequence || falsePart instanceof Sequence) {
return "if (" + condition + ") { " + truePart + " } else { " + falsePart + " }"; return "if (" + condition + ") { " + truePart + " } else { " + falsePart + " }";
} else {
return "(" + condition + ") ? (" + truePart + ") : (" + falsePart + ")";
} }
} }