Exemplo n.º 1
0
        public static CodeCompileUnit Demo1()
        {
            // evaluates a simple expression
            var res = new CodeDomResolver();

            Console.WriteLine(res.Evaluate(SlangParser.ParseExpression("5*4-7")));

            // takes this file and get a codedom from it
            var ccu = SlangParser.ReadCompileUnitFrom("..\\..\\Demo1.cs");

            ccu.ReferencedAssemblies.Add("CodeDomGoKit.dll");
            ccu.ReferencedAssemblies.Add(typeof(CodeObject).Assembly.GetName().ToString());
            // now patch the parsed codedom so it's correct
            // NOTE: slang can't actually bind to this because it is a paramarray method
            // but we don't care here because it's not necessary. Slang only binds to what
            // it needs to and we never need the return type anyway (which is void)
            SlangPatcher.Patch(ccu);
            // now write it out in VB
            Console.WriteLine(CodeDomUtility.ToString(ccu, "vb"));
            // return the code we generated so we can use it for other demos
            // yay recycling
            Console.WriteLine("Press any key...");
            Console.ReadKey();
            Console.Clear();
            return(ccu);
        }
Exemplo n.º 2
0
        static void Demo()
        {
            // note that in the real world cases, you'd need to use SlangPatcher.Patch()
            // on whole compile units to get proper codedom objects back. This method is
            // simply "close enough for government work" and may not work for languages
            // other than VB
            while (true)
            {
                Console.Write("Slang>");
                var s = Console.ReadLine();
                if (null != s)
                {
                    s = s.Trim();
                }
                if (string.IsNullOrEmpty(s))
                {
                    break;
                }
                var        isStatement = s.EndsWith(";") || s.EndsWith("}");
                CodeObject co          = null;
                try
                {
                    if (isStatement)
                    {
                        co = SlangParser.ParseStatement(s, true);
                    }
                    else
                    {
                        co = SlangParser.ParseExpression(s);
                    }
                }
                catch (SlangSyntaxException ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
                if (null != co)
                {
                    var ccu = _RootCode(co);
                    try
                    {
                        SlangPatcher.Patch(ccu);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Warning: Error resolving code - " + ex.Message);
                    }
                    var tc   = new CodeDomTypeConverter();
                    var item = (ccu.Namespaces[0].Types[0].Members[0] as CodeMemberMethod).Statements[0];

                    if (!isStatement)
                    {
                        co = item;
                        var es = item as CodeExpressionStatement;
                        if (null != es)
                        {
                            co = es.Expression;
                        }
                    }
                    else
                    {
                        co = item;
                    }

                    s = CodeDomUtility.ToString(co);
                    s = s.Trim();
                    Console.Write("C#: ");
                    Console.WriteLine(s);
                    s = CodeDomUtility.ToString(co, "vb");
                    s = s.Trim();
                    Console.Write("VB: ");
                    Console.WriteLine(s);

                    s = CodeDomUtility.ToString(CodeDomUtility.Literal(co, tc));
                    s = s.Trim();

                    Console.Write("CodeDom: ");
                    Console.WriteLine(s);
                    if (null != SlangPatcher.GetNextUnresolvedElement(ccu))
                    {
                        Console.WriteLine("Warning: Not all of the code could be resolved.");
                    }
                }
            }
        }