示例#1
0
文件: AstNode.cs 项目: azbyn/oop
        public string ToStringTree(int level)
        {
            var indent = new string(' ', level *2);

            if (level != 0)
            {
                indent += "| ";
            }
            string PrintChildren(params AstNode[] nodes)
            {
                string res = "\n";

                foreach (var n in nodes)
                {
                    res += n.ToStringTree(level + 1);
                }
                return(res);
            }

            var impl = new LambdaVisitor <string>(
                (Int v) => $"int: {v.val}\n",
                (Binary v) => $"binary: " + PrintChildren(v.a, v.b),
                (Unary v) => $"unary: " + PrintChildren(v.a),
                (Variable v) => $"var '{v.name}'\n",
                (FunCall v) => $"funCall: " + PrintChildren(v.fun, v.arg),
                (Lambda v) => $"lambda {v.param}: " + PrintChildren(v.body),
                (Assign v) => $"assign: " + PrintChildren(v.rvalue, v.rvalue),
                (Or v) => $"or: " + PrintChildren(v.a, v.b),
                (And v) => $"and: " + PrintChildren(v.a, v.b),
                (If v) => $"if: " + PrintChildren(v.cond, v.t, v.f)
                );

            return(indent + impl.Visit((dynamic)this));
        }
        private static Expression ChangeSource_VisitLambda <T>(Expression <T> node, Dictionary <string, ParameterExpression> paramters, Dictionary <Type, Type> typeMap)
        {
            var lambdaVisitor = new LambdaVisitor(paramters, typeMap);
            var newLambdaBody = lambdaVisitor.Visit(node.Body);
            var theParamters  = node.Parameters.Select(x => paramters[x.Name]).ToArray();
            var lambda        = Expression.Lambda(newLambdaBody, theParamters);

            return(lambda);
        }
示例#3
0
文件: AstNode.cs 项目: azbyn/oop
        public override string ToString()
        {
            var impl = new LambdaVisitor <string>(
                (Int v) => v.val.ToString(),
                (Binary v) => $"({v.a} @ {v.b})",
                (Unary v) => $"(~{v.a})",
                (Variable v) => $"{v.name}",
                (FunCall v) => $"({v.fun} {v.arg})",
                (Lambda v) => $"λ{v.param}. {v.body}",
                (Assign v) => $"({v.rvalue} = {v.rvalue})",
                (Or v) => $"({v.a} || {v.b})",
                (And v) => $"({v.a} && {v.b})",
                (If v) => $"({v.cond} ? {v.t} : {v.f})"
                );

            return(impl.Visit((dynamic)this));
        }
示例#4
0
 public static Absy Desugar(Absy node, out List<Expr/*!*/>/*!*/ axioms, out List<Function/*!*/>/*!*/ functions) {
   Contract.Requires(node != null);
   Contract.Ensures(cce.NonNullElements(Contract.ValueAtReturn(out functions)));
   Contract.Ensures(cce.NonNullElements(Contract.ValueAtReturn(out axioms)));
   Contract.Ensures(Contract.Result<Absy>() != null);
   LambdaVisitor v = new LambdaVisitor();
   node = v.Visit(node);
   axioms = v.lambdaAxioms;
   functions = v.lambdaFunctions;
   if (CommandLineOptions.Clo.TraceVerify) {
     Console.WriteLine("Desugaring of lambda expressions produced {0} functions and {1} axioms:", functions.Count, axioms.Count);
     TokenTextWriter wr = new TokenTextWriter("<console>", Console.Out);
     foreach (Function f in functions) {
       f.Emit(wr, 0);
     }
     foreach (Expr ax in axioms) {
       ax.Emit(wr);
       Console.WriteLine();
     }
   }
   return node;
 }
示例#5
0
        public Expression Process(Expression expression)
        {
            RootExpression = expression;


            var memberVisitor = new MemberVisitor(MemberMapper, MemberMapper.Options.Safety.PerformNullChecksOnCustomMappings, Variables);

            // Pass 1: Transform member access so they do null-checks first, if needed
            expression     = memberVisitor.Visit(expression);
            RootExpression = expression;

            var paramVisitor = new ParameterVisitor(this.ParametersToReplace);

            // Pass 2: Transform parameter placeholders with their final ones
            expression = paramVisitor.Visit(expression);

            RootExpression = expression;

            var visibilityVisitor = new VisibilityVisitor(this);

            // Pass 3: Check visibility of what is accessed, to establish if we can compile to a new dynamic assembly.
            visibilityVisitor.Visit(expression);

            RootExpression = expression;

            this.NonPublicMembersAccessed = visibilityVisitor.NonPublicMembersAccessed;

            if (this.NonPublicMembersAccessed)
            {
                var lambdaVisitor = new LambdaVisitor(this);

                expression = lambdaVisitor.Visit(expression);

                RootExpression = expression;
            }

            return(expression);
        }
示例#6
0
 public static Absy Desugar(Absy node, out List<Expr/*!*/>/*!*/ axioms, out List<Function/*!*/>/*!*/ functions)
 {
     Contract.Requires(node != null);
       Contract.Ensures(cce.NonNullElements(Contract.ValueAtReturn(out functions)));
       Contract.Ensures(cce.NonNullElements(Contract.ValueAtReturn(out axioms)));
       Contract.Ensures(Contract.Result<Absy>() != null);
       LambdaVisitor v = new LambdaVisitor();
       node = v.Visit(node);
       axioms = v.lambdaAxioms;
       functions = v.lambdaFunctions;
       if (CommandLineOptions.Clo.TraceVerify) {
     Console.WriteLine("Desugaring of lambda expressions produced {0} functions and {1} axioms:", functions.Count, axioms.Count);
     TokenTextWriter wr = new TokenTextWriter("<console>", Console.Out);
     foreach (Function f in functions) {
       f.Emit(wr, 0);
     }
     foreach (Expr ax in axioms) {
       ax.Emit(wr);
       Console.WriteLine();
     }
       }
       return node;
 }