private string ToExpressionStringInternal()

in ExpressionBuilder/ExpressionBuilder/ExpressionNodes/ExpressionBuilder.ExpressionNode.cs [466:590]


        private string ToExpressionStringInternal()
        {
            string ret = "";

            // Do a recursive depth-first traversal of the node tree to print out the full expression string
            switch (GetOperationKind())
            {
                case OperationType.Function:
                    if (_children.Count == 0)
                    {
                        throw new Exception("Can't have an expression function with no params");
                    }

                    ret = $"{GetOperationString()}({_children[0].ToExpressionStringInternal()}";
                    for (int i = 1; i < _children.Count; i++)
                    {
                        ret += "," + _children[i].ToExpressionStringInternal();
                    }
                    ret += ")";
                    break;

                case OperationType.Operator:
                    if (_children.Count != 2)
                    {
                        throw new Exception("Can't have an operator that doesn't have 2 exactly params");
                    }
                    
                    ret = $"({_children[0].ToExpressionStringInternal()} {GetOperationString()} {_children[1].ToExpressionStringInternal()})";
                    break;

                case OperationType.Constant:
                    if (_children.Count == 0)
                    {
                        // If a parameterName was specified, use it. Otherwise write the value.
                        ret = _paramName ?? GetValue();
                    }
                    else
                    {
                        throw new Exception("Constants must have 0 children");
                    }
                    break;

                case OperationType.Swizzle:
                    if (_children.Count != 1)
                    {
                        throw new Exception("Swizzles should have exactly 1 child");
                    }

                    string swizzleString = "";
                    foreach (var sub in _subchannels)
                    {
                        swizzleString += sub;
                    }

                    ret = $"{_children[0].ToExpressionStringInternal()}.{swizzleString}";
                    break;

                case OperationType.Reference:
                    if ((_nodeType == ExpressionNodeType.Reference) ||
                        (_nodeType == ExpressionNodeType.TargetReference))
                    {
                        // This is the reference node itself
                        if (_children.Count != 0)
                        {
                            throw new Exception("References cannot have children");
                        }

                        ret = (this as ReferenceNode).GetReferenceParamString();
                    }
                    else if (_nodeType == ExpressionNodeType.ReferenceProperty)
                    {
                        // This is the property node of the reference
                        if (_children.Count != 1)
                        {
                            throw new Exception("Reference properties must have exactly one child");
                        }

                        if (_propertyName == null)
                        {
                            throw new Exception("Reference properties must have a property name");
                        }

                        ret = $"{_children[0].ToExpressionStringInternal()}.{_propertyName}";
                    }
                    else if (_nodeType == ExpressionNodeType.StartingValueProperty)
                    {
                        // This is a "this.StartingValue" node
                        if (_children.Count != 0)
                        {
                            throw new Exception("StartingValue references Cannot have children");
                        }

                        ret = "this.StartingValue";
                    }
                    else if (_nodeType == ExpressionNodeType.CurrentValueProperty)
                    {
                        // This is a "this.CurrentValue" node
                        if (_children.Count != 0)
                        {
                            throw new Exception("CurrentValue references Cannot have children");
                        }

                        ret = "this.CurrentValue";
                    }
                    else
                    {
                        throw new Exception("Unexpected NodeType for OperationType.Reference");
                    }
                    break;

                case OperationType.Conditional:
                    if (_children.Count != 3)
                    {
                        throw new Exception("Conditionals must have exactly 3 children");
                    }

                    ret = $"(({_children[0].ToExpressionStringInternal()}) ? ({_children[1].ToExpressionStringInternal()}) : ({_children[2].ToExpressionStringInternal()}))";
                    break;

                default:
                    throw new Exception($"Unexpected operation type ({GetOperationKind()}), nodeType = {_nodeType}");
            }

            return ret;
        }