コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: valdisz/PyToJs
        private static void Transformator(object context)
        {
            ThreadContext p = (ThreadContext)context;

            ScriptRuntime runtime = Python.CreateRuntime();
            ScriptEngine engine = runtime.GetEngine("py");
            LanguageContext languageContext = HostingHelpers.GetLanguageContext(engine);

            while (p.Running)
            {
                string pySrc;
                if (p.Queue.TryDequeue(out pySrc))
                {
                    LocalSink sink = new LocalSink();

                    try
                    {
                        SourceUnit src = HostingHelpers.GetSourceUnit(engine.CreateScriptSourceFromString(pySrc));

                        CompilerContext ctx = new CompilerContext(src, languageContext.GetCompilerOptions(), sink);
                        Parser parser = Parser.CreateParser(ctx, (PythonOptions)languageContext.Options);

                        PythonAst ast = parser.ParseFile(true);
                        JavascriptGenerator generator = new JavascriptGenerator(src, sink);

                        if (sink.IsError)
                        {
                            p.SetOutput(null);
                        }
                        else
                        {
                            p.SetOutput(generator.ToJavaScript(ast));
                        }
                    }
                    catch (Exception e)
                    {
                        if (!sink.IsError)
                        {
                            p.SetOutput(e.ToString());
                        }
                    }
                    finally
                    {
                        p.SetErrors(sink.Messages);
                    }
                }
                else
                {
                    Thread.Sleep(50);
                    if (p.Queue.Count == 0)
                    {
                        p.Suspend();
                    }
                }
            }
        }
コード例 #2
0
ファイル: JavascriptGenerator.cs プロジェクト: valdisz/PyToJs
        private void AddOptinonalParam(StringBuilder sb, Parameter p)
        {
            JavascriptGenerator gen = new JavascriptGenerator(src, sink);
            p.DefaultValue.Walk(gen);

            sb.AppendFormat("{0}if (typeof({1}) == 'undefined') {{",
                Indent(),
                p.Name
            );
            sb.AppendLine();

            sb.AppendFormat("{0}{1} = {2};",
                Indent(indent + 1),
                p.Name,
                gen.ToString()
            );
            sb.AppendLine();

            sb.AppendFormat("{0}}}", Indent());
            sb.AppendLine();
        }