Esempio n. 1
0
        protected override void DoExpand(MacroContext context)
        {
            if (context.Invocation.Arguments.Count == 0)
            {
                context.ReportMessage(
                    Message.Error(
                        string.Format(Resources.CallSubInterpret_OneArgument, Alias), context.Invocation.Position,
                        MessageClasses.SubUsage));
                return;
            }

            if (context.CurrentLoopBlock != null && !context.IsJustEffect)
            {
                context.ReportMessage(
                    Message.Error(
                        string.Format(
                            Resources.CallSubInterpret_asExpressionInLoop, CallSub.Alias, Alias),
                        context.Invocation.Position, MessageClasses.SubAsExpressionInLoop));
                return;
            }

            //Store result of call
            var resultV = context.AllocateTemporaryVariable();
            _storeResult(context, resultV);

            //Extract return variant as int into retVarV
            var retVarV = context.AllocateTemporaryVariable();
            _extractReturnVariant(context, resultV, retVarV);

            Func<AstGetSet> retVar = () => context.CreateCall(EntityRef.Variable.Local.Create(retVarV));

            //Extract return value into retValueV (which happens to be the same as resultV)
            var retValueV = resultV;
            _extractReturnValue(context, resultV, retValueV);

// ReSharper disable ImplicitlyCapturedClosure // perfectly safe as neither lambda survives the method
            Func<AstGetSet> retValue = () => context.CreateCall(EntityRef.Variable.Local.Create(retValueV));
// ReSharper restore ImplicitlyCapturedClosure

            //Break and Continue behave differently outside loop blocks
            AstNode contStmt, breakStmt;
            _determineActions(context, retValue, out contStmt, out breakStmt);

            //Generate check for continue
            _genChecks(context, retVar, contStmt, breakStmt);

            context.Block.Expression = retValue();

            context.FreeTemporaryVariable(retVarV);
            context.FreeTemporaryVariable(resultV);
        }