// =========================================================================
        // Code snippet decalartion
        // -------------------------------------------------------------------------
        /// Declares the return value formated as "localVariable= ".
        ///
        /// @param node The node for which the return value will be declared.
        /// @return The return value decalartion.
        ///
        string DeclareReturnVariable(iCS_EditorObject node)
        {
            // No return variable necessary
            var returnPort = GetReturnPort(node);

            if (returnPort == null)
            {
                return("");
            }
            var consumerPorts = returnPort.SegmentEndConsumerPorts;

            if (consumerPorts.Length == 0)
            {
                return("");
            }
            // Don't need to generate return variable if no real consumer
            var hasConsumer = false;

            foreach (var c in consumerPorts)
            {
                if (c.IsEnablePort || c.ParentNode.IsKindOfFunction)
                {
                    hasConsumer = true;
                }
                else
                {
                    var consumerCode = Context.GetCodeFor(c);
                    if (consumerCode != null)
                    {
                        hasConsumer = true;
                    }
                }
            }
            if (hasConsumer == false)
            {
                return("");
            }
            // Build return variable for the given node.
            var result = new StringBuilder(32);

            if (myReturnVariable != null)
            {
                result.Append(myReturnVariable.GenerateBody(0));
            }
            else
            {
                result.Append(GetNameFor(returnPort));
            }
            result.Append("= ");
            return(result.ToString());
        }
Beispiel #2
0
        // -------------------------------------------------------------------------
        /// Returns list of trigger ports requiring code generation.
        ///
        /// @param node Root node from which the code will be generated.
        /// @return The list of trigger ports that need code generation.
        ///
        iCS_EditorObject[] GetTriggerPortsNeedingCode(iCS_EditorObject node)
        {
            var triggerPorts = node.FilterChildRecursive(
                p => {
                if (p.IsTriggerPort)
                {
                    return(ShouldGenerateTriggerCode(p));
                }
                return(false);
            }
                );

            return(triggerPorts.ToArray());
        }
Beispiel #3
0
        // ===================================================================
        // PROPERTIES
        // -------------------------------------------------------------------

        // ===================================================================
        // INFORMATION GATHERING FUNCTIONS
        // -------------------------------------------------------------------
        /// Builds a Function specific code context object.
        ///
        /// @param node VS objects associated with the function.
        /// @param codeBlock The code block this assignment belongs to.
        /// @return The newly created code context.
        ///
        public FunctionDefinition(iCS_EditorObject node, CodeBase parent, AccessSpecifier accessType, ScopeSpecifier scopeType)
            : base(node, parent)
        {
            myAccessSpecifier = accessType;
            myScopeSpecifier  = scopeType;

            // Build parameter list.
            BuildParameterList();

            // Build execution list.
            var generatedCode = GetFunctionBodyParts(node);

            generatedCode = SortDependencies(generatedCode);
            BuildExecutionList(generatedCode);
        }
        // -------------------------------------------------------------------------
        /// Generate return type cast.
        ///
        /// @param node The function call VS node.
        /// @return The return cast code string.
        ///
        protected string GenerateReturnTypeCastFragment(iCS_EditorObject node)
        {
            var returnPort = GetReturnPort(node);

            if (returnPort == null)
            {
                return("");
            }
            var consumerType = GetMostSpecializedTypeForProducerPort(returnPort);

            if (consumerType == typeof(void) || iCS_Types.IsA(consumerType, returnPort.RuntimeType))
            {
                return("");
            }
            // Change return variable type.
            var returnVariable = myReturnVariable as ReturnVariableDefinition;

            if (returnVariable != null)
            {
                returnVariable.SetRuntimeType(consumerType);
            }
            return(" as " + ToTypeName(consumerType));
        }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds a function call code context.
 ///
 /// @param vsObj VS node associated with the function call.
 /// @param codeBlock The code block this assignment belongs to.
 /// @return The newly created function call definition.
 ///
 public FunctionCallDefinition(iCS_EditorObject vsObj, CodeBase parent)
     : base(vsObj, parent)
 {
     BuildParameterInformation();
     BuildOutputParameters();
 }
Beispiel #6
0
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds a reference to a visual script constant.
 ///
 /// @param vsObject VS objects being referenced.
 /// @param parent The parent code context.
 /// @return The newly created reference.
 ///
 public ConstantDefinition(iCS_EditorObject vsObject, CodeBase parent)
     : base(vsObject, parent)
 {
 }
Beispiel #7
0
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds a reference to a visual script value.
 ///
 /// @param vsObject VS objects being referenced.
 /// @param parent The parent code context.
 /// @return The newly created value.
 ///
 public ValueDefinition(iCS_EditorObject vsObject, CodeBase parent)
     : base(vsObject, parent)
 {
 }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds a parameter definition.
 ///
 /// @param port The VS port of the parameter.
 /// @param parent The parent code context.
 /// @return The newly created reference.
 ///
 public FunctionCallParameterDefinition(iCS_EditorObject port, CodeBase parent, Type neededType = null)
     : base(port, parent)
 {
     myType = neededType;
 }
Beispiel #9
0
        // -------------------------------------------------------------------
        /// Returns the runtime type for the given visual script object.
        ///
        /// @param vsObject The visual script object.
        /// @return The runtime type.
        public Type GetRuntimeTypeFor(iCS_EditorObject vsObject)
        {
            var code = GetCodeFor(vsObject);

            return(code != null?code.GetRuntimeType() : vsObject.RuntimeType);
        }
Beispiel #10
0
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds an output parameter definition.
 ///
 /// @param port The VS port producing the data.
 /// @param parent The parent code context.
 /// @return The newly created reference.
 ///
 public TriggerVariableDefinition(iCS_EditorObject port, CodeBase parent, TriggerSetDefinition triggerSet)
     : base(port, parent, AccessSpecifier.Private, ScopeSpecifier.NonStatic)
 {
     myTriggerSet = triggerSet;
 }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds the property/field _SET_ code definition.
 ///
 /// @param vsObj VS node associated with the property/field _SET_.
 /// @return The newly created SET definition.
 ///
 public SetPropertyCallDefinition(iCS_EditorObject vsObj, CodeBase parent)
     : base(vsObj, parent)
 {
 }
Beispiel #12
0
        // -------------------------------------------------------------------
        void BuildExecutionList(CodeBase[] codeBlock)
        {
            EnableBlockDefinition currentEnableBlock = null;

            iCS_EditorObject[] currentEnables = new iCS_EditorObject[0];
            var len = codeBlock.Length;

            for (int i = 0; i < len; ++i)
            {
                var code            = codeBlock[i];
                var functionEnables = code.GetRelatedEnablePorts();
                if (!IsSameConditionalContext(currentEnables, functionEnables))
                {
                    var enableIdx = LengthOfSameEnables(currentEnables, functionEnables);
                    if (enableIdx < currentEnables.Length)
                    {
                        // Terminate existing If-Statement(s)
                        var removeIdx = enableIdx;
                        while (removeIdx < currentEnables.Length)
                        {
                            currentEnableBlock = currentEnableBlock.Parent as EnableBlockDefinition;
                            do
                            {
                                ++removeIdx;
                            } while(removeIdx < currentEnables.Length && currentEnables[removeIdx - 1].ParentNode == currentEnables[removeIdx].ParentNode);
                        }
                    }
                    if (enableIdx < functionEnables.Length)
                    {
                        // Add new If-Statement(s)
                        var addIdx = enableIdx;
                        while (addIdx < functionEnables.Length)
                        {
                            var ifEnables = new List <iCS_EditorObject>();
                            do
                            {
                                ifEnables.Add(functionEnables[addIdx]);
                                ++addIdx;
                            } while(addIdx < functionEnables.Length && functionEnables[addIdx - 1].ParentNode == functionEnables[addIdx].ParentNode);
                            var newEnableBlock = new EnableBlockDefinition(this, ifEnables.ToArray());
                            if (currentEnableBlock == null)
                            {
                                AddExecutable(newEnableBlock);
                            }
                            else
                            {
                                currentEnableBlock.AddExecutable(newEnableBlock);
                            }
                            currentEnableBlock = newEnableBlock;
                        }
                    }
                    currentEnables = functionEnables;
                }
                // Allocate Code Definition
                if (currentEnableBlock == null)
                {
                    AddExecutable(code);
                }
                else
                {
                    currentEnableBlock.AddExecutable(code);
                }
            }
        }
        // -------------------------------------------------------------------------
        /// Generates the function call prefix code fragment.
        ///
        /// @param memberInfo The member information of the function to call.
        /// @param node Visual script function call node.
        /// @return The code fragment to prepend to the function call.
        ///
        protected string FunctionCallPrefix(iCS_EditorObject node)
        {
            var result = new StringBuilder(32);

            if (IsStatic())
            {
                if (!GraphInfo.IsLocalType(VSObject))
                {
                    result.Append(ToTypeName(node.RuntimeType));
                    result.Append(".");
                }
            }
            else
            {
                var thisPort = GraphInfo.GetTargetPort(node);
                if (thisPort != null)
                {
//					result.Append(GetExpressionFor(thisPort));
//					result.Append(".");
                    // -- Prepend target code producer if optimized. --
                    if (myTargetCode != null)
                    {
                        result.Append(myTargetCode.GenerateBody(0));
                        result.Append(".");
                        return(result.ToString());
                    }
                    var producerPort = GraphInfo.GetProducerPort(thisPort);
                    var producerType = Context.GetRuntimeTypeFor(producerPort);
                    var producerCode = Context.GetCodeFor(producerPort);
                    if (producerCode is FunctionDefinitionParameter)
                    {
                        var producerPortName = GetNameFor(producerPort);
                        if (producerPortName == null || producerPortName == "null")
                        {
                            Debug.LogWarning("producer port is null: " + producerPort.CodeName);
                        }
                        result.Append(GetNameFor(producerPort));
                        result.Append(".");
                    }
                    else if (producerCode is LocalVariableDefinition)
                    {
                        var producerPortName = Parent.GetLocalVariableName(producerPort);
                        if (producerPortName == null || producerPortName == "null")
                        {
                            Debug.LogWarning("producer port is null: " + producerPort.CodeName);
                        }
                        result.Append(producerPortName);
                        result.Append(".");
                    }
                    else if (producerPort.IsOwner)
                    {
                        if (producerType == typeof(Transform))
                        {
                            result.Append("transform.");
                        }
                        else if (producerType == typeof(GameObject))
                        {
                            result.Append("gameObject.");
                        }
                    }
                    else if (producerPort != thisPort)
                    {
                        var desiredType     = VSObject.RuntimeType;
                        var desiredTypeName = ToTypeName(desiredType);
                        var isUpcastNeeded  = producerType != desiredType && iCS_Types.IsA(producerType, desiredType);
                        if (isUpcastNeeded)
                        {
                            result.Append("(");
                        }
                        var producerNode = producerPort.ParentNode;
                        if (producerNode.IsConstructor)
                        {
                            result.Append(GetNameFor(producerNode));
                        }
                        else
                        {
                            result.Append(GetNameFor(producerPort));
                        }
                        if (isUpcastNeeded)
                        {
                            result.Append(" as ");
                            result.Append(desiredTypeName);
                            result.Append(")");
                        }
                        result.Append(".");
                    }
                }
            }
            return(result.ToString());
        }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds an package code block.
 ///
 /// @param codeBlock The code block this assignment belongs to.
 /// @param enables The enable ports that affect this code block.
 /// @return The newly created code context.
 ///
 public PackageDefinition(iCS_EditorObject node, CodeBase parent, iCS_EditorObject[] enables)
     : base(node, parent)
 {
 }
        // ===================================================================
        // FIELDS
        // -------------------------------------------------------------------

        // ===================================================================
        // INFORMATION GATHERING FUNCTIONS
        // -------------------------------------------------------------------
        /// Builds a parameter definition.
        ///
        /// @param port The VS port producing the data.
        /// @param parent The parent code context.
        /// @return The newly created reference.
        ///
        public FunctionDefinitionParameter(iCS_EditorObject port, CodeBase parent)
            : base(port, parent)
        {
        }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds an output parameter definition.
 ///
 /// @param port The VS port producing the data.
 /// @param parent The parent code context.
 /// @return The newly created reference.
 ///
 public TriggerSetDefinition(iCS_EditorObject port, CodeBase parent)
     : base(null, parent)
 {
     myTriggerPort = port;
 }
Beispiel #17
0
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds textual code definition.
 ///
 /// @param vsObj The visual script object associated with this code.
 /// @param parent The parent code block.
 /// @param code The textual code to be injected.
 /// @return The newly created code definition.
 ///
 public InjectedCodeDefinition(iCS_EditorObject vsObj, CodeBase parent, string code)
     : base(null, parent)
 {
     myCode = code;
 }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds a return variable reference.
 ///
 /// @param vsObject Visual Script return port.
 /// @param parent The parent code context.
 /// @return The newly created return variable.
 ///
 public ReturnReferenceDefinition(iCS_EditorObject vsObject, CodeBase parent)
     : base(vsObject, parent)
 {
 }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds variable reference code definition.
 ///
 /// @param variable The port referensenting the variable.
 /// @return The newly created variable reference.
 ///
 public VariableReferenceDefinition(iCS_EditorObject variable, CodeBase parent)
     : base(null, parent)
 {
     myVariable = variable;
 }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds an output parameter definition.
 ///
 /// @param port The VS port producing the data.
 /// @param parent The parent code context.
 /// @return The newly created reference.
 ///
 public FunctionCallOutParameterDefinition(iCS_EditorObject port, CodeBase parent)
     : base(port, parent)
 {
 }
Beispiel #21
0
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds a function call code context.
 ///
 /// @param vsObj VS node associated with the function call.
 /// @param codeBlock The code block this assignment belongs to.
 /// @return The newly created function call definition.
 ///
 public ConstructorDefinition(iCS_EditorObject vsObj, CodeBase parent)
     : base(vsObj, parent)
 {
 }
Beispiel #22
0
        // ===================================================================
        // FIELDS
        // -------------------------------------------------------------------

        // ===================================================================
        // INFORMATION GATHERING FUNCTIONS
        // -------------------------------------------------------------------
        /// Builds a Function specific code context object.
        ///
        /// @param node VS objects associated with the function.
        /// @return The newly created code context.
        ///
        public EventHandlerDefinition(iCS_EditorObject node, CodeBase parent, AccessSpecifier accessType, ScopeSpecifier scopeType)
            : base(node, parent, accessType, scopeType)
        {
            CreateLocalVariables();
        }
Beispiel #23
0
 // -------------------------------------------------------------------
 /// Returns the Code associated with the given visual script object.
 ///
 /// @param vsObject The visual script object.
 /// @return The associated Code or _'null'_ if not found.
 public CodeBase GetCodeFor(iCS_EditorObject vsObject)
 {
     return(ObjectToCodeTable[vsObject.InstanceId]);
 }
Beispiel #24
0
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds an execution code block.
 ///
 /// @param vsObject The visual script objects associated with this code.
 /// @param codeBlock The code block this assignment belongs to.
 /// @return The newly created code context.
 ///
 public ExecutionBlockDefinition(iCS_EditorObject vsObject, CodeBase parent)
     : base(vsObject, parent)
 {
 }
 // ===================================================================
 // INFORMATION GATHERING FUNCTIONS
 // -------------------------------------------------------------------
 /// Builds a return variable.
 ///
 /// @param vsObject Visual Script return port.
 /// @param parent The parent code context.
 /// @return The newly created return variable.
 ///
 public ReturnVariableDefinition(iCS_EditorObject vsObject, CodeBase parent)
     : base(vsObject, parent)
 {
     myRuntimeType = vsObject.RuntimeType;
 }