コード例 #1
0
        public void VisitTry(TryStatement t)
        {
            CodeVariableReferenceExpression?successVar = null;

            if (t.elseHandler != null)
            {
                // C# has no equivalent to Python's else handler, so we have
                // to emulate it with a boolean flag.
                successVar = gensym.GenSymLocal("_success", gen.TypeRef(typeof(bool)));
                gen.Assign(successVar, gen.Prim(false));
            }
            var tryStmt = gen.Try(
                () => {
                t.body.Accept(this);
                if (successVar != null)
                {
                    gen.Assign(successVar, gen.Prim(true));
                }
            },
                t.exHandlers.Select(eh => GenerateClause(eh)),
                () =>
            {
                if (t.finallyHandler != null)
                {
                    t.finallyHandler.Accept(this);
                }
            });

            if (successVar != null)
            {
                gen.If(successVar,
                       () => t.elseHandler !.Accept(this));
            }
        }
コード例 #2
0
        private CodeExpression FuseComparisons(CodeBinaryOperatorExpression binL, Op op, CodeExpression r)
        {
            if (binL.Right is not CodeVariableReferenceExpression variable)
            {
                // Python https://docs.python.org/3/reference/expressions.html#comparisons
                // Stats that the second expression in a comparison chain is only evaluated once.
                variable = gensym.GenSymLocal("_tmp_", m.TypeRef(typeof(object)));
                m.Assign(variable, binL.Right);
                binL = m.BinOp(binL.Left, binL.Operator, variable);
            }
            var newR = m.BinOp(variable, mppyoptocsop[op], r);

            return(m.BinOp(binL, CodeOperatorType.LogAnd, newR));
        }
コード例 #3
0
 private CodeVariableReferenceExpression GenSymLocalTuple()
 {
     return(gensym.GenSymLocal("_tup_", new CodeTypeReference(typeof(object))));
 }