Ejemplo n.º 1
0
        static void Demo4(CodeCompileUnit ccu)
        {
            // create one of these lil guys
            var res = new CodeDomResolver();

            // add our code to it
            res.CompileUnits.Add(ccu);
            // give it a chance to build its information over our code
            res.Refresh();
            CodeDomVisitor.Visit(ccu, (ctx) => {
                // for every expression...
                var expr = ctx.Target as CodeExpression;
                if (null != expr)
                {
                    // except method reference expressions...
                    var mri = expr as CodeMethodReferenceExpression;
                    if (null != mri)
                    {
                        return;
                    }
                    // get the expression type
                    var type = res.TryGetTypeOfExpression(expr);
                    // write it along with the expression itself
                    Console.WriteLine(
                        "Expression type {0}: {1} is {2}",
                        expr.GetType().Name,
                        CodeDomUtility.ToString(expr),
                        null != type?CodeDomUtility.ToString(type):"unresolvable");
                }
            });
            Console.WriteLine("Press any key...");
            Console.ReadKey();
            Console.Clear();
        }
Ejemplo n.º 2
0
        static void RunResolver()
        {
            byte[] data;
            using (var stream = File.OpenRead(@"myfile.bin"))
            {
                data = new byte[(int)stream.Length];
                stream.Read(data, 0, data.Length);
            }

            // create a resolver
            var res = new CodeDomResolver();

            // read the resolver sample into the compile unit
            CodeCompileUnit ccu;

            using (var stm = File.OpenRead(@"..\..\Resolver.cs"))
                ccu = SlangParser.ReadCompileUnitFrom(stm);

            // remember to patch it!
            SlangPatcher.Patch(ccu);

            Console.Error.WriteLine(CU.ToString(ccu));

            // add the compile unit to the resolver
            res.CompileUnits.Add(ccu);

            // prepare the resolver
            // any time you add compile units you'll need
            // to call Refresh()
            res.Refresh();

            // go through all expressions in the
            // graph and try to get their type
            CodeDomVisitor.Visit(ccu, (ctx) => {
                var expr = ctx.Target as CodeExpression;
                if (null != expr)
                {
                    // we want everything except CodeTypeReferenceExpression
                    var ctre = expr as CodeTypeReferenceExpression;
                    if (null == ctre)
                    {
                        // get the scope of the expression
                        var scope             = res.GetScope(expr);
                        CodeTypeReference ctr = res.TryGetTypeOfExpression(expr, scope);
                        if (null != ctr)
                        {
                            Console.WriteLine(CU.ToString(expr) + " is type: " + CU.ToString(ctr));
                            Console.WriteLine("Scope Dump:");
                            Console.WriteLine(scope.ToString());
                        }
                    }
                }
            });
        }