Beispiel #1
0
        public override Runtime.Object GenerateRuntimeObject()
        {
            // End = end flow immediately
            // Done = return from thread or instruct the flow that it's safe to exit
            if (isEnd)
            {
                return(Runtime.ControlCommand.End());
            }
            if (isDone)
            {
                return(Runtime.ControlCommand.Done());
            }

            runtimeDivert = new Runtime.Divert();

            // Normally we resolve the target content during the
            // Resolve phase, since we expect all runtime objects to
            // be available in order to find the final runtime path for
            // the destination. However, we need to resolve the target
            // (albeit without the runtime target) early so that
            // we can get information about the arguments - whether
            // they're by reference - since it affects the code we
            // generate here.
            ResolveTargetContent();


            CheckArgumentValidity();

            // Passing arguments to the knot
            bool requiresArgCodeGen = arguments != null && arguments.Count > 0;

            if (requiresArgCodeGen || isFunctionCall || isTunnel || isThread)
            {
                var container = new Runtime.Container();

                // Generate code for argument evaluation
                // This argument generation is coded defensively - it should
                // attempt to generate the code for all the parameters, even if
                // they don't match the expected arguments. This is so that the
                // parameter objects themselves are generated correctly and don't
                // get into a state of attempting to resolve references etc
                // without being generated.
                if (requiresArgCodeGen)
                {
                    // Function calls already in an evaluation context
                    if (!isFunctionCall)
                    {
                        container.AddContent(Runtime.ControlCommand.EvalStart());
                    }

                    List <FlowBase.Argument> targetArguments = null;
                    if (targetContent)
                    {
                        targetArguments = (targetContent as FlowBase).arguments;
                    }

                    for (var i = 0; i < arguments.Count; ++i)
                    {
                        Expression        argToPass   = arguments [i];
                        FlowBase.Argument argExpected = null;
                        if (targetArguments != null && i < targetArguments.Count)
                        {
                            argExpected = targetArguments [i];
                        }

                        // Pass by reference: argument needs to be a variable reference
                        if (argExpected != null && argExpected.isByReference)
                        {
                            var varRef = argToPass as VariableReference;
                            if (varRef == null)
                            {
                                Error("Expected variable name to pass by reference to 'ref " + argExpected.name + "' but saw " + argToPass.ToString());
                                break;
                            }

                            // Check that we're not attempting to pass a read count by reference
                            var           targetPath     = new Path(varRef.path);
                            Parsed.Object targetForCount = targetPath.ResolveFromContext(this);
                            if (targetForCount != null)
                            {
                                Error("can't pass a read count by reference. '" + targetPath.dotSeparatedComponents + "' is a knot/stitch/label, but '" + target.dotSeparatedComponents + "' requires the name of a VAR to be passed.");
                                break;
                            }

                            var varPointer = new Runtime.VariablePointerValue(varRef.name);
                            container.AddContent(varPointer);
                        }

                        // Normal value being passed: evaluate it as normal
                        else
                        {
                            argToPass.GenerateIntoContainer(container);
                        }
                    }

                    // Function calls were already in an evaluation context
                    if (!isFunctionCall)
                    {
                        container.AddContent(Runtime.ControlCommand.EvalEnd());
                    }
                }


                // Starting a thread? A bit like a push to the call stack below... but not.
                // It sort of puts the call stack on a thread stack (argh!) - forks the full flow.
                if (isThread)
                {
                    container.AddContent(Runtime.ControlCommand.StartThread());
                }

                // If this divert is a function call, tunnel, we push to the call stack
                // so we can return again
                else if (isFunctionCall || isTunnel)
                {
                    runtimeDivert.pushesToStack = true;
                    runtimeDivert.stackPushType = isFunctionCall ? Runtime.PushPopType.Function : Runtime.PushPopType.Tunnel;
                }

                // Jump into the "function" (knot/stitch)
                container.AddContent(runtimeDivert);

                return(container);
            }

            // Simple divert
            else
            {
                return(runtimeDivert);
            }
        }
Beispiel #2
0
        protected FlowBase.Argument FlowDeclArgument()
        {
            // Possible forms:
            //  name
            //  -> name      (variable divert target argument
            //  ref name
            //  ref -> name  (variable divert target by reference)
            var firstIden = Parse(Identifier);
            Whitespace ();
            var divertArrow = ParseDivertArrow ();
            Whitespace ();
            var secondIden = Parse(Identifier);

            if (firstIden == null && secondIden == null)
                return null;


            var flowArg = new FlowBase.Argument ();
            if (divertArrow != null) {
                flowArg.isDivertTarget = true;
            }

            // Passing by reference
            if (firstIden == "ref") {

                if (secondIden == null) {
                    Error ("Expected an parameter name after 'ref'");
                }

                flowArg.name = secondIden;
                flowArg.isByReference = true;
            } 

            // Simple argument name
            else {

                if (flowArg.isDivertTarget) {
                    flowArg.name = secondIden;
                } else {
                    flowArg.name = firstIden;
                }

                if (flowArg.name == null) {
                    Error ("Expected an parameter name");
                }

                flowArg.isByReference = false;
            }

            return flowArg;
        }
Beispiel #3
0
        // Returns false if there's an error
        void CheckArgumentValidity()
        {
            if (isEmpty)
            {
                return;
            }

            // Argument passing: Check for errors in number of arguments
            var numArgs = 0;

            if (arguments != null && arguments.Count > 0)
            {
                numArgs = arguments.Count;
            }

            // Missing content?
            // Can't check arguments properly. It'll be due to some
            // other error though, so although there's a problem and
            // we report false, we don't need to report a specific error.
            // It may also be because it's a valid call to an external
            // function, that we check at the resolve stage.
            if (targetContent == null)
            {
                return;
            }

            FlowBase targetFlow = targetContent as FlowBase;

            // No error, crikey!
            if (numArgs == 0 && (targetFlow == null || !targetFlow.hasParameters))
            {
                return;
            }

            if (targetFlow == null && numArgs > 0)
            {
                Error("target needs to be a knot or stitch in order to pass arguments");
                return;
            }

            if (targetFlow.arguments == null && numArgs > 0)
            {
                Error("target (" + targetFlow.name + ") doesn't take parameters");
                return;
            }

            if (this.parent is DivertTarget)
            {
                if (numArgs > 0)
                {
                    Error("can't store arguments in a divert target variable");
                }
                return;
            }

            var paramCount = targetFlow.arguments.Count;

            if (paramCount != numArgs)
            {
                string butClause;
                if (numArgs == 0)
                {
                    butClause = "but there weren't any passed to it";
                }
                else if (numArgs < paramCount)
                {
                    butClause = "but only got " + numArgs;
                }
                else
                {
                    butClause = "but got " + numArgs;
                }
                Error("to '" + targetFlow.name + "' requires " + paramCount + " arguments, " + butClause);
                return;
            }

            // Light type-checking for divert target arguments
            for (int i = 0; i < paramCount; ++i)
            {
                FlowBase.Argument flowArg    = targetFlow.arguments [i];
                Parsed.Expression divArgExpr = arguments [i];

                // Expecting a divert target as an argument, let's do some basic type checking
                if (flowArg.isDivertTarget)
                {
                    // Not passing a divert target or any kind of variable reference?
                    var varRef = divArgExpr as VariableReference;
                    if (!(divArgExpr is DivertTarget) && varRef == null)
                    {
                        Error("Target '" + targetFlow.name + "' expects a divert target for the parameter named -> " + flowArg.name + " but saw " + divArgExpr, divArgExpr);
                    }

                    // Passing 'a' instead of '-> a'?
                    // i.e. read count instead of divert target
                    else if (varRef != null)
                    {
                        // Unfortunately have to manually resolve here since we're still in code gen
                        var           knotCountPath  = new Path(varRef.path);
                        Parsed.Object targetForCount = knotCountPath.ResolveFromContext(varRef);
                        if (targetForCount != null)
                        {
                            Error("Passing read count of '" + knotCountPath.dotSeparatedComponents + "' instead of a divert target. You probably meant '" + knotCountPath + "'");
                        }
                    }
                }
            }

            if (targetFlow == null)
            {
                Error("Can't call as a function or with arguments unless it's a knot or stitch");
                return;
            }

            return;
        }
Beispiel #4
0
        // Returns false if there's an error
        void CheckArgumentValidity()
        {
            if (isToGather)
            {
                return;
            }

            // Argument passing: Check for errors in number of arguments
            var numArgs = 0;

            if (arguments != null && arguments.Count > 0)
            {
                numArgs = arguments.Count;
            }

            // Missing content?
            // Can't check arguments properly. It'll be due to some
            // other error though, so although there's a problem and
            // we report false, we don't need to report a specific error.
            // It may also be because it's a valid call to an external
            // function, that we check at the resolve stage.
            if (targetContent == null)
            {
                return;
            }

            FlowBase targetFlow = targetContent as FlowBase;

            // No error, crikey!
            if (numArgs == 0 && (targetFlow == null || !targetFlow.hasParameters))
            {
                return;
            }

            if (targetFlow == null && numArgs > 0)
            {
                Error("target needs to be a knot or stitch in order to pass arguments");
                return;
            }

            if (targetFlow.arguments == null && numArgs > 0)
            {
                Error("target (" + targetFlow.name + ") doesn't take parameters");
                return;
            }

            var paramCount = targetFlow.arguments.Count;

            if (paramCount > 0 && this.parent is DivertTarget)
            {
                Error("Can't store a link to a knot that takes parameters in a variable");
                return;
            }

            if (paramCount != numArgs)
            {
                string butClause;
                if (numArgs == 0)
                {
                    butClause = "but there weren't any passed to it";
                }
                else if (numArgs < paramCount)
                {
                    butClause = "but only got " + numArgs;
                }
                else
                {
                    butClause = "but got " + numArgs;
                }
                Error("to '" + targetFlow.name + "' requires " + paramCount + " arguments, " + butClause);
                return;
            }

            // Light type-checking for divert target arguments
            for (int i = 0; i < paramCount; ++i)
            {
                FlowBase.Argument flowArg    = targetFlow.arguments [i];
                Parsed.Expression divArgExpr = arguments [i];

                if (flowArg.isDivertTarget)
                {
                    if (!(divArgExpr is VariableReference || divArgExpr is DivertTarget))
                    {
                        Error("Target '" + targetFlow.name + "' expects a divert target for the parameter named -> " + flowArg.name + " but saw " + divArgExpr, divArgExpr);
                    }
                }
            }

            if (targetFlow == null)
            {
                Error("Can't call as a function or with arguments unless it's a knot or stitch");
                return;
            }

            return;
        }