public override List <string> getCommands() { List <string> leftCommands = getLeft().getCommands(); List <string> rightCommands = getRight().getCommands(); EntityNode left = getLeft().getTemp(); EntityNode right = getRight().getTemp(); bool changeable = left.isLast; List <string> operationCommands = new List <string>(); operationCommands.AddRange(leftCommands); operationCommands.AddRange(rightCommands); switch (_operator) { case plus: if (!changeable && right.isLast) { EntityNode temp = left; left = right; right = temp; changeable = left.isLast; } if (changeable) { if (getRight() is ConstantNode) { operationCommands.Add(string.Format("scoreboard players Add {0} {1} {2}", left.selector, left.scbObj, right.selector)); } else { operationCommands.Add(string.Format("scoreboard players operation {0} {1} += {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); } temp = left; } else { temp = new TempNode(); operationCommands.Add(string.Format("scoreboard players operation {0} {1} = {2} {3}", temp.selector, temp.scbObj, left.selector, left.scbObj)); operationCommands.Add(string.Format("scoreboard players operation {0} {1} += {2} {3}", temp.selector, temp.scbObj, right.selector, right.scbObj)); } break; case minus: if (changeable) { if (getRight() is ConstantNode) { operationCommands.Add(string.Format("scoreboard players remove {0} {1} {2}", left.selector, left.scbObj, right.selector)); } else { operationCommands.Add(string.Format("scoreboard players operation {0} {1} -= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); } temp = left; } else { temp = new TempNode(); operationCommands.Add(string.Format("scoreboard players operation {0} {1} = {2} {3}", temp.selector, temp.scbObj, left.selector, left.scbObj)); operationCommands.Add(string.Format("scoreboard players operation {0} {1} -= {2} {3}", temp.selector, temp.scbObj, right.selector, right.scbObj)); } break; case multiply: if (!changeable && right.isLast) { EntityNode temp = left; left = right; right = temp; changeable = left.isLast; } if (changeable) { operationCommands.Add(string.Format("scoreboard players operation {0} {1} *= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); temp = left; } else { temp = new TempNode(); operationCommands.Add(string.Format("scoreboard players operation {0} {1} = {2} {3}", temp.selector, temp.scbObj, left.selector, left.scbObj)); operationCommands.Add(string.Format("scoreboard players operation {0} {1} *= {2} {3}", temp.selector, temp.scbObj, right.selector, right.scbObj)); } break; case divide: if (changeable) { operationCommands.Add(string.Format("scoreboard players operation {0} {1} /= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); temp = left; } else { temp = new TempNode(); operationCommands.Add(string.Format("scoreboard players operation {0} {1} = {2} {3}", temp.selector, temp.scbObj, left.selector, left.scbObj)); operationCommands.Add(string.Format("scoreboard players operation {0} {1} /= {2} {3}", temp.selector, temp.scbObj, right.selector, right.scbObj)); } break; case mod: if (changeable) { operationCommands.Add(string.Format("scoreboard players operation {0} {1} %= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); temp = left; } else { temp = new TempNode(); operationCommands.Add(string.Format("scoreboard players operation {0} {1} = {2} {3}", temp.selector, temp.scbObj, left.selector, left.scbObj)); operationCommands.Add(string.Format("scoreboard players operation {0} {1} %= {2} {3}", temp.selector, temp.scbObj, right.selector, right.scbObj)); } break; case assign: if (left.selector != right.selector || left.scbObj != right.scbObj) { operationCommands.Add(string.Format("scoreboard players operation {0} {1} = {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); temp = left; } break; case plus_assign: if (getRight() is ConstantNode) { operationCommands.Add(string.Format("scoreboard players Add {0} {1} {2}", left.selector, left.scbObj, right.selector)); } else { operationCommands.Add(string.Format("scoreboard players operation {0} {1} += {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); } temp = left; break; case minus_assign: if (getRight() is ConstantNode) { operationCommands.Add(string.Format("scoreboard players remove {0} {1} {2}", left.selector, left.scbObj, right.selector)); } else { operationCommands.Add(string.Format("scoreboard players operation {0} {1} -= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); } temp = left; break; case multiply_assign: operationCommands.Add(string.Format("scoreboard players operation {0} {1} *= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); temp = left; break; case divide_assign: operationCommands.Add(string.Format("scoreboard players operation {0} {1} /= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); temp = left; break; case mod_assign: operationCommands.Add(string.Format("scoreboard players operation {0} {1} %= {2} {3}", left.selector, left.scbObj, right.selector, right.scbObj)); temp = left; break; } return(operationCommands); }
public Node buildNode() { Stack <Node> bracketNode = new Stack <Node>(); Stack <Operator> operatorStack = new Stack <Operator>(); Operator lastOperator = Operator.none; Node currentNode = null; string token; while ((token = getNextToken()) != "") { Node newNode; switch (currentToken) { case Token.operate: Operator _operator = OperatorMethod.parseOperator(token); if (lastToken == Token.operate || lastToken == Token.none) { if (_operator == Operator.minus) { newNode = new OperatorNode(Operator.multiply); if (currentNode == null) { currentNode = new ConstantNode("-1"); insertUpper(currentNode, newNode); } else { currentNode.setRight(new ConstantNode("-1")); insertLower(currentNode, newNode); } } else { throw new Exception("invalid syntax"); } } else { if (currentNode == null) { throw new Exception("invalid syntax"); } newNode = new OperatorNode(_operator); if (lastOperator == Operator.none || !OperatorMethod.isLarger (_operator, lastOperator)) { insertUpper(currentNode, newNode); } else { insertLower(currentNode, newNode); } } lastOperator = _operator; currentNode = newNode; break; case Token.constant: if (lastToken != Token.operate && lastToken != Token.none && lastToken != Token.bracket) { throw new Exception("invalid syntax"); } newNode = new ConstantNode(token); if (currentNode == null) { currentNode = newNode; } else { currentNode.setRight(newNode); } break; case Token.selector: if (lastToken != Token.operate && lastToken != Token.none && lastToken != Token.bracket) { throw new Exception("invalid syntax"); } string[] pair = token.Split('.'); newNode = new EntityNode(pair[0], pair[1]); if (currentNode == null) { currentNode = newNode; } else { currentNode.setRight(newNode); } break; case Token.fakePlayer: if (lastToken != Token.operate && lastToken != Token.none && lastToken != Token.bracket) { throw new Exception("invalid syntax"); } string[] pair2 = token.Split('.'); newNode = new FakePlayerNode(pair2[0], pair2[1]); if (currentNode == null) { currentNode = newNode; } else { currentNode.setRight(newNode); } break; case Token.bracket: if (token == "(") { bracketNode.Push(currentNode); operatorStack.Push(lastOperator); currentNode = null; lastOperator = Operator.none; continue; } else { if (bracketNode.Count == 0) { throw new Exception("imbalance bracket"); } else { Node _node = bracketNode.Pop(); if (currentNode != null) { while (currentNode.getParent() != null) { currentNode = currentNode.getParent(); } insertLower(_node, currentNode); currentNode = _node; } lastOperator = operatorStack.Pop(); } } break; } lastToken = currentToken; } Node node = currentNode; if (node == null) { return(null); } while (node.getParent() != null) { node = node.getParent(); } if (!node.isComplete()) { throw new Exception("missing value(fake player/selector/constant)"); } node.checkLast(); return(node); }