internal override void CollectInputExpressions(List <StatementSyntax> expressions)
        {
            // Base will traverse to input nodes
            base.CollectInputExpressions(expressions);

            string firstVariableName = null, secondVariableName = null;

            // Save names of variables
            firstVariableName  = m_inputANode.VariableSyntaxName(ObjectBuilder.InputAID.VariableName);
            secondVariableName = m_inputBNode != null?m_inputBNode.VariableSyntaxName(ObjectBuilder.InputBID.VariableName) : "null";

            if (m_inputBNode == null && ObjectBuilder.Operation != "==" && ObjectBuilder.Operation != "!=")
            {
                throw new Exception("Null check with Operation " + ObjectBuilder.Operation + " is prohibited.");
            }

            // Create expression for arithmetic operation
            Debug.Assert(!string.IsNullOrEmpty(firstVariableName) && !string.IsNullOrEmpty(secondVariableName));
            expressions.Add(
                MySyntaxFactory.ArithmeticStatement(
                    VariableSyntaxName(),
                    firstVariableName,
                    secondVariableName,
                    ObjectBuilder.Operation)
                );
        }
Example #2
0
        internal override void CollectInputExpressions(List <StatementSyntax> expressions)
        {
            base.CollectInputExpressions(expressions);

            expressions.Add(
                MySyntaxFactory.CastExpression(
                    m_inputNode.VariableSyntaxName(ObjectBuilder.InputID.VariableName),
                    ObjectBuilder.Type,
                    VariableSyntaxName())
                );
        }
        internal override void CollectInputExpressions(List <StatementSyntax> expressions)
        {
            base.CollectInputExpressions(expressions);

            if (ObjectBuilder.ValueInputID.NodeID != -1)
            {
                m_inputVariableName = m_inputNode.VariableSyntaxName(ObjectBuilder.ValueInputID.VariableName);
                Debug.Assert(!string.IsNullOrEmpty(m_inputVariableName));
            }

            expressions.Add(GetCorrectAssignmentsExpression());
        }
        internal override void CollectSequenceExpressions(List <StatementSyntax> expressions)
        {
            CollectInputExpressions(expressions);

            var valueVariableName = m_valueInput.VariableSyntaxName(ObjectBuilder.ValueInput.VariableName);

            var switchSections = new List <SwitchSectionSyntax>();
            var syntaxList     = new List <StatementSyntax>();

            for (int index = 0; index < m_sequenceOutputs.Count; index++)
            {
                var sequenceOutputNode = m_sequenceOutputs[index];
                if (sequenceOutputNode == null)
                {
                    continue;
                }

                syntaxList.Clear();
                // Collect
                sequenceOutputNode.CollectSequenceExpressions(syntaxList);
                // Add break to the end of the section
                syntaxList.Add(SyntaxFactory.BreakStatement());

                // New section syntax
                var section = SyntaxFactory.SwitchSection().WithLabels(
                    SyntaxFactory.SingletonList <SwitchLabelSyntax>(
                        SyntaxFactory.CaseSwitchLabel(
                            MySyntaxFactory.Literal(ObjectBuilder.NodeType, ObjectBuilder.Options[index].Option)
                            )
                        )).WithStatements(SyntaxFactory.List(syntaxList));

                switchSections.Add(section);
            }

            var switchSyntax = SyntaxFactory.SwitchStatement(
                SyntaxFactory.IdentifierName(valueVariableName)
                )
                               .WithSections(SyntaxFactory.List(switchSections));

            expressions.Add(switchSyntax);
        }
        internal override void CollectInputExpressions(List <StatementSyntax> expressions)
        {
            // Base will traverse to input nodes
            base.CollectInputExpressions(expressions);

            string firstVariableName = null, secondVariableName = null;

            // Save names of variables
            firstVariableName  = m_inputANode.VariableSyntaxName(ObjectBuilder.InputAID.VariableName);
            secondVariableName = m_inputBNode.VariableSyntaxName(ObjectBuilder.InputBID.VariableName);

            // Create expression for arithmetic operation
            Debug.Assert(!string.IsNullOrEmpty(firstVariableName) && !string.IsNullOrEmpty(secondVariableName));
            expressions.Add(
                MySyntaxFactory.ArithmeticStatement(
                    VariableSyntaxName(),
                    firstVariableName,
                    secondVariableName,
                    ObjectBuilder.Operation)
                );
        }
        internal override void CollectSequenceExpressions(List <StatementSyntax> expressions)
        {
            // Collect mine input expressions and pushed expressions
            CollectInputExpressions(expressions);

            var trueStatments  = new List <StatementSyntax>();
            var falseStatments = new List <StatementSyntax>();

            // Sequence outputs can be connected only to one input
            if (m_nextTrueSequenceNode != null)
            {
                m_nextTrueSequenceNode.CollectSequenceExpressions(trueStatments);
            }

            if (m_nextFalseSequenceNode != null)
            {
                m_nextFalseSequenceNode.CollectSequenceExpressions(falseStatments);
            }

            var inputNodeVariableName = m_comparerNode.VariableSyntaxName(ObjectBuilder.InputID.VariableName);

            // write my expression before the collected inputs
            expressions.Add(MySyntaxFactory.IfExpressionSyntax(inputNodeVariableName, trueStatments, falseStatments));
        }
Example #7
0
        internal override void CollectSequenceExpressions(List <StatementSyntax> expressions)
        {
            base.CollectInputExpressions(expressions);

            // We can ignore the nodes with empty bodies
            if (m_bodySequence != null)
            {
                var bodySyntax = new List <StatementSyntax>();
                // Collect body expressions
                m_bodySequence.CollectSequenceExpressions(bodySyntax);

                // Create syntax for first index of for loop
                ExpressionSyntax firstIndexExpression;
                if (m_firstInput != null)
                {
                    firstIndexExpression = SyntaxFactory.IdentifierName(
                        m_firstInput.VariableSyntaxName(ObjectBuilder.FirstIndexValueInput.VariableName)
                        );
                }
                else
                {
                    firstIndexExpression = MySyntaxFactory.Literal(
                        typeof(int).Signature(),
                        ObjectBuilder.FirstIndexValue
                        );
                }

                // Create syntax for last index condition
                ExpressionSyntax lastIndexExpression;
                if (m_lastInput != null)
                {
                    lastIndexExpression = SyntaxFactory.IdentifierName(
                        m_lastInput.VariableSyntaxName(ObjectBuilder.LastIndexValueInput.VariableName)
                        );
                }
                else
                {
                    lastIndexExpression = MySyntaxFactory.Literal(typeof(int).Signature(), ObjectBuilder.LastIndexValue);
                }

                // Create syntax for increment
                ExpressionSyntax incrementExpression;
                if (m_incrementInput != null)
                {
                    incrementExpression = SyntaxFactory.IdentifierName(
                        m_incrementInput.VariableSyntaxName(ObjectBuilder.IncrementValueInput.VariableName)
                        );
                }
                else
                {
                    incrementExpression = MySyntaxFactory.Literal(typeof(int).Signature(), ObjectBuilder.IncrementValue);
                }

                // Put the for statement syntax together
                var forStatement = SyntaxFactory.ForStatement(
                    SyntaxFactory.Block(bodySyntax)
                    ).WithDeclaration(
                    SyntaxFactory.VariableDeclaration(
                        SyntaxFactory.PredefinedType(
                            SyntaxFactory.Token(SyntaxKind.IntKeyword)))
                    .WithVariables(
                        SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                            SyntaxFactory.VariableDeclarator(
                                SyntaxFactory.Identifier(VariableSyntaxName()))
                            .WithInitializer(
                                SyntaxFactory.EqualsValueClause(
                                    firstIndexExpression
                                    )
                                )
                            )
                        )
                    ).WithCondition(
                    SyntaxFactory.BinaryExpression(
                        SyntaxKind.LessThanOrEqualExpression,
                        SyntaxFactory.IdentifierName(VariableSyntaxName()),
                        lastIndexExpression
                        )
                    ).WithIncrementors(
                    SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(
                        SyntaxFactory.AssignmentExpression(
                            SyntaxKind.AddAssignmentExpression,
                            SyntaxFactory.IdentifierName(VariableSyntaxName()),
                            incrementExpression
                            )
                        )
                    );

                expressions.Add(forStatement);
            }

            // Ignore if not present
            if (m_finishSequence != null)
            {
                m_finishSequence.CollectSequenceExpressions(expressions);
            }
        }
        internal override void CollectInputExpressions(List <StatementSyntax> expressions)
        {
            // Insert expressions from connected nodes
            base.CollectInputExpressions(expressions);

            // Create container variables for output parameters.
            // Assign correct identifiers to method call.
            var args       = new List <SyntaxNodeOrToken>();
            var parameters = m_methodInfo.GetParameters();
            var index      = 0;

            // Skip the first parameter for extension methods.
            if (m_methodInfo.IsDefined(typeof(ExtensionAttribute), false))
            {
                index++;
            }

            for (; index < parameters.Length; index++)
            {
                var parameter = parameters[index];
                // Create container variables for output parameters.
                if (parameter.IsOut)
                {
                    // Output parameters
                    var localOutputVariableName = VariableSyntaxName(parameter.Name);
                    // add variable creation expression
                    expressions.Add(MySyntaxFactory.LocalVariable(parameter.ParameterType.GetElementType().Signature(), localOutputVariableName));
                    // add variable name to parameters
                    args.Add(SyntaxFactory.Argument(
                                 SyntaxFactory.IdentifierName(localOutputVariableName)
                                 ).WithNameColon(SyntaxFactory.NameColon(parameter.Name))
                             .WithRefOrOutKeyword(SyntaxFactory.Token(SyntaxKind.OutKeyword)
                                                  )
                             );
                }
                // Get the names of the variables supplying the values to the method call

                else
                {
                    // Find the value input node
                    MyTuple <MyVisualSyntaxNode, MyVariableIdentifier> inputData;
                    if (m_parametersToInputs.TryGetValue(parameter, out inputData))
                    {
                        // FOUND!
                        // add variable name to arguments
                        var variableName = inputData.Item1.VariableSyntaxName(inputData.Item2.VariableName);
                        Debug.Assert(variableName != null);
                        args.Add(SyntaxFactory.Argument(
                                     SyntaxFactory.IdentifierName(
                                         variableName
                                         )
                                     ).WithNameColon(SyntaxFactory.NameColon(parameter.Name))
                                 );
                    }
                    else
                    {
                        // Not FOUND! will be probably an constant argument
                        var paramValue =
                            ObjectBuilder.InputParameterValues.Find(value => value.ParameterName == parameter.Name);
                        if (paramValue == null)
                        {
                            // Dont panic, there could be still the default value.
                            if (!parameter.HasDefaultValue)
                            {
                                // Add null / some default value
                                args.Add(
                                    MySyntaxFactory.ConstantDefaultArgument(parameter.ParameterType)
                                    .WithNameColon(SyntaxFactory.NameColon(parameter.Name))
                                    );
                            }
                            else
                            {
                                // default value will take care of this
                                continue; // no comma for you args
                            }
                        }
                        else
                        {
                            // Add constant value to arguments
                            args.Add(MySyntaxFactory.ConstantArgument(
                                         parameter.ParameterType.Signature(), paramValue.Value
                                         ).WithNameColon(SyntaxFactory.NameColon(parameter.Name))
                                     );
                        }
                    }
                }

                // add comma
                args.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
            }

            // remove last comma
            if (args.Count > 0)
            {
                args.RemoveAt(args.Count - 1);
            }

            // Create the invocation syntax
            InvocationExpressionSyntax methodInvocation = null;

            if (m_methodInfo.IsStatic && !m_methodInfo.IsDefined(typeof(ExtensionAttribute)))
            {
                // Static call
                methodInvocation = MySyntaxFactory.MethodInvocationExpressionSyntax(
                    SyntaxFactory.IdentifierName(m_methodInfo.DeclaringType.FullName + "." + m_methodInfo.Name),
                    SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList <ArgumentSyntax>(args))
                    );
            }
            else
            {
                var declaringType = m_methodInfo.DeclaringType;
                // Non static local method invocation
                if (declaringType == m_scriptBaseType)
                {
                    methodInvocation = MySyntaxFactory.MethodInvocationExpressionSyntax(
                        SyntaxFactory.IdentifierName(m_methodInfo.Name),
                        SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList <ArgumentSyntax>(args))
                        );
                }
                // Non static instance method invocation or extension method
                else
                {
                    if (m_instance == null)
                    {
                        throw new Exception("FunctionNode: " + ObjectBuilder.ID +
                                            " Is missing mandatory instance input.");
                    }

                    var instanceVariableName = m_instance.VariableSyntaxName(ObjectBuilder.InstanceInputID.VariableName);
                    methodInvocation = MySyntaxFactory.MethodInvocationExpressionSyntax(
                        SyntaxFactory.IdentifierName(m_methodInfo.Name),
                        SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList <ArgumentSyntax>(args)),
                        SyntaxFactory.IdentifierName(instanceVariableName)
                        );
                }
            }

            // Finalize the syntax
            if (m_methodInfo.ReturnType == typeof(void))
            {
                // just add invocation
                expressions.Add(
                    SyntaxFactory.ExpressionStatement(methodInvocation)
                    );
            }
            else
            {
                // Create variable for return value
                expressions.Add(
                    MySyntaxFactory.LocalVariable(
                        string.Empty,
                        VariableSyntaxName("Return"),
                        methodInvocation
                        )
                    );
            }
        }
        internal override void CollectSequenceExpressions(List <StatementSyntax> expressions)
        {
            // Collect input only from these nodes that are inputs for this node.
            m_toCollectNodeCache.Clear();
            foreach (var subTreeNode in SubTreeNodes)
            {
                var directConnection = false;
                foreach (var outputNode in subTreeNode.Outputs)
                {
                    // Must be an input
                    if (outputNode == this && !outputNode.Collected)
                    {
                        directConnection = true;
                        break;
                    }
                }

                if (directConnection)
                {
                    subTreeNode.CollectInputExpressions(expressions);
                }
                else
                {
                    // Cache these that are getting the input from this one.
                    m_toCollectNodeCache.Add(subTreeNode);
                }
            }

            // We can ignore the nodes with empty bodies
            if (m_bodySequence != null)
            {
                var bodySyntax = new List <StatementSyntax>();

                // Collect the assigned bodies from
                foreach (var node in m_toCollectNodeCache)
                {
                    node.CollectInputExpressions(bodySyntax);
                }

                // Collect body expressions
                m_bodySequence.CollectSequenceExpressions(bodySyntax);

                // Create syntax for first index of for loop
                ExpressionSyntax firstIndexExpression;
                if (m_firstInput != null)
                {
                    firstIndexExpression = SyntaxFactory.IdentifierName(
                        m_firstInput.VariableSyntaxName(ObjectBuilder.FirstIndexValueInput.VariableName)
                        );
                }
                else
                {
                    firstIndexExpression = MySyntaxFactory.Literal(
                        typeof(int).Signature(),
                        ObjectBuilder.FirstIndexValue
                        );
                }

                // Create syntax for last index condition
                ExpressionSyntax lastIndexExpression;
                if (m_lastInput != null)
                {
                    lastIndexExpression = SyntaxFactory.IdentifierName(
                        m_lastInput.VariableSyntaxName(ObjectBuilder.LastIndexValueInput.VariableName)
                        );
                }
                else
                {
                    lastIndexExpression = MySyntaxFactory.Literal(typeof(int).Signature(), ObjectBuilder.LastIndexValue);
                }

                // Create syntax for increment
                ExpressionSyntax incrementExpression;
                if (m_incrementInput != null)
                {
                    incrementExpression = SyntaxFactory.IdentifierName(
                        m_incrementInput.VariableSyntaxName(ObjectBuilder.IncrementValueInput.VariableName)
                        );
                }
                else
                {
                    incrementExpression = MySyntaxFactory.Literal(typeof(int).Signature(), ObjectBuilder.IncrementValue);
                }

                // Put the for statement syntax together
                var forStatement = SyntaxFactory.ForStatement(
                    SyntaxFactory.Block(bodySyntax)
                    ).WithDeclaration(
                    SyntaxFactory.VariableDeclaration(
                        SyntaxFactory.PredefinedType(
                            SyntaxFactory.Token(SyntaxKind.IntKeyword)))
                    .WithVariables(
                        SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                            SyntaxFactory.VariableDeclarator(
                                SyntaxFactory.Identifier(VariableSyntaxName()))
                            .WithInitializer(
                                SyntaxFactory.EqualsValueClause(
                                    firstIndexExpression
                                    )
                                )
                            )
                        )
                    ).WithCondition(
                    SyntaxFactory.BinaryExpression(
                        SyntaxKind.LessThanOrEqualExpression,
                        SyntaxFactory.IdentifierName(VariableSyntaxName()),
                        lastIndexExpression
                        )
                    ).WithIncrementors(
                    SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(
                        SyntaxFactory.AssignmentExpression(
                            SyntaxKind.AddAssignmentExpression,
                            SyntaxFactory.IdentifierName(VariableSyntaxName()),
                            incrementExpression
                            )
                        )
                    );

                expressions.Add(forStatement);
            }

            // Ignore if not present
            if (m_finishSequence != null)
            {
                m_finishSequence.CollectSequenceExpressions(expressions);
            }
        }