Example #1
0
 private static void _extractReturnVariant(MacroContext context, string resultV,
     string retVarV)
 {
     var inv = context.Invocation;
     var intT = new AstConstantTypeExpression(inv.File, inv.Line, inv.Column,
         IntPType.Literal);
     var getRetVar =
         context.CreateGetSetMember(context.CreateCall(EntityRef.Variable.Local.Create(resultV)), PCall.Get,
                                    "Key");
     var asInt = new AstTypecast(inv.File, inv.Line, inv.Column, getRetVar, intT);
     var setRetVar = context.CreateCall(EntityRef.Variable.Local.Create(retVarV), PCall.Set, asInt);
     context.Block.Add(setRetVar);
 }
Example #2
0
        public void DoEmitPartialApplicationCode(CompilerTarget target)
        {
            if (Conditions.Count == 0)
            {
                this.ConstFunc(!ShortcircuitValue).EmitValueCode(target);
                return;
            }

            //only the very last condition may be a placeholder
            for (var node = Conditions.First; node != null; node = node.Next)
            {
                var isPlaceholder = node.Value.IsPlaceholder();
                if (node.Next == null)
                {
                    if (!isPlaceholder)
                    {
                        //there is no placeholder at all, wrap expression in const
                        Debug.Assert(Conditions.All(e => !e.IsPlaceholder()));
                        DoEmitCode(target,StackSemantics.Value);
                        target.EmitCommandCall(Position, 1, Const.Alias);
                        return;
                    }
                }
                else
                {
                    if (isPlaceholder)
                    {
                        _reportInvalidPlaceholders(target);
                        return;
                    }
                }
            }

            //We have expression of the form `e1 and e2 and e3 and ... and ?i`
            var placeholder = (AstPlaceholder) Conditions.Last.Value;
            AstPlaceholder.DeterminePlaceholderIndices(placeholder.Singleton());


            // compile the following code: `if(e1 and e2 and e3) id(?) else const(false)`
            var constExpr = CreatePrefix(Position, Conditions.Take(Conditions.Count - 1));
            //var identityFunc = new AstGetSetSymbol(File, Line, Column, PCall.Get, Commands.Core.Id.Alias, SymbolInterpretations.Command);
            //identityFunc.Arguments.Add(new AstPlaceholder(File, Line, Column, placeholder.Index));
            var identityFunc = new AstTypecast(File, Line, Column,
                placeholder.GetCopy(),
                new AstConstantTypeExpression(File, Line, Column, PType.Bool.ToString()));
            var conditional = new AstConditionalExpression(File, Line, Column, constExpr,
                ShortcircuitValue)
                {
                    IfExpression = identityFunc,
                    ElseExpression = this.ConstFunc(ShortcircuitValue)
                };
            conditional.EmitValueCode(target);
        }