/// <summary> /// Insert operation in a operation tree using priorities /// </summary> /// <param name="lastOperation">Previous builded operation</param> /// <param name="operation">Current operation</param> /// <param name="rootOperation">Root operation of a tree</param> private void InsertOperationInTree <T>(ref OperationElement lastOperation, ref T operation, ref OperationElement rootOperation) where T : OperationElement { if (operation.Priority > lastOperation.Priority || (operation.OperationName == lastOperation.OperationName && !OperationPriority.IsCompareToThisAsLessPriority[operation.OperationName])) { operation.SetChild(0, lastOperation.Child(1)); lastOperation.SetChild(1, operation); } else { if (lastOperation.Parent() != null) { OperationElement parentOperation = (OperationElement)lastOperation.Parent(); InsertOperationInTree(ref parentOperation, ref operation, ref rootOperation); } else { operation.SetChild(0, lastOperation); rootOperation = lastOperation = operation; } } }
/// <summary> /// Takes value and insert it in a operation /// </summary> /// <param name="operation">Used operation</param> /// <param name="value">Used value</param> /// <param name="right">If true a value will be inserted as right operand, false - as left</param> private void InsertValInTree(OperationElement operation, ValueElement value, bool right = true) { if (right) { if (operation.HasRightOperand) { if (operation.Child(1) == null) { operation.SetChild(1, value); } else { Compilation.WriteError("Operation '" + operation.OperationName + "' already has right operand", value.Line); } } else { Compilation.WriteError("Operation '" + operation.OperationName + "' hasn't right operand", value.Line); } } else { if (operation.HasLeftOperand) { if (operation.Child(0) == null) { operation.SetChild(0, value); } else { Compilation.WriteError("Operation '" + operation.OperationName + "' already has left operand", value.Line); } } else { Compilation.WriteError("Operation '" + operation.OperationName + "' hasn't left operand", value.Line); } } }