Example #1
0
        // Public Methods 

        public void Emit(PySourceCodeEmiter emiter, PyEmitStyle style, string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }

            var writer = new PySourceCodeWriter();
            var styleCurrentNamespace = style.CurrentNamespace;

            try
            {
                Emit(emiter, writer, style);

                {
                    var fi = new FileInfo(filename);
                    fi.Directory?.Create();
                    var codeStr = writer.GetCode();
                    var binary  = Encoding.UTF8.GetBytes(codeStr);
                    File.WriteAllBytes(fi.FullName, binary);
                }
            }
            finally
            {
                style.CurrentNamespace = styleCurrentNamespace;
            }
        }
Example #2
0
        private static void WriteCode(IEmitable emitable, string expected)
        {
            var writer = new PySourceCodeWriter();

            emitable.Emit(new PySourceCodeEmiter(), writer, new PyEmitStyle());
            var code = writer.GetCode();

            Assert.Equal(expected.Trim(), code.Trim());
        }
Example #3
0
        protected static CompilationTestInfo ParseCs(string code, bool translate, Info info = null)
        {
            var project = CreateOneFileProject(code, info?.Ref);
            var c       = new Cs2PyCompiler
            {
                CSharpProject = project
            };

            c.TranslationAssemblies.Add(typeof(AssemblySandbox).Assembly);

            c.ReferencedAssemblies.Add(typeof(Tensorflow.TensorShape).Assembly);
            c.ReferencedAssemblies.Add(typeof(System.Linq.Enumerable).Assembly);
            c.ReferencedAssemblies.Add(typeof(System.Data.Linq.Binary).Assembly);
            var filename = Path.GetTempFileName().Replace(".tmp", ".dll");
            var er       = c.CompileCSharpProject(c.Sandbox, filename);

            if (!er.Success && er.Diagnostics.Any())
            {
                throw new Exception(er.Diagnostics[0].GetMessage());
            }
            Assert.True(er.Success);
            var translationInfo = c.ParseCsSource();

            if (!translate)
            {
                return(new CompilationTestInfo(c, translationInfo, null));
            }
            var translationState = new TranslationState(translationInfo);
            var translator       = new Translator(translationState);

            translator.Info.CurrentAssembly = c.CompiledAssembly;
            translator.Translate(c.Sandbox);
            var compare = info?.Compare;

            if (compare != null)
            {
                compare = compare.Trim();
                if (!compare.StartsWith("'''"))
                {
                    compare = "'''\r\nGenerated with cs2py\r\n'''\r\n" + compare;
                }
                var emiter = new PySourceCodeEmiter();
                var writer = new PySourceCodeWriter();
                translator.Modules[0].Emit(emiter, writer, new PyEmitStyle());
                var pyCode = writer.GetCode();
                Assert.Equal(compare.Trim(), pyCode.Trim());
            }

            return(new CompilationTestInfo(c, translationInfo, translator));
        }
Example #4
0
        // Private Methods 

        private static string Collect(PySourceCodeEmiter emiter, PyEmitStyle style, IPyStatement[] collection)
        {
            var list   = new List <string>();
            var xStyle = PyEmitStyle.xClone(style);

            xStyle.AsIncrementor = true;
            foreach (var item in collection)
            {
                var writer = new PySourceCodeWriter();
                writer.Clear();
                item.Emit(emiter, writer, xStyle);
                list.Add(writer.GetCode().Trim());
            }

            return(string.Join(", ", list));
        }
Example #5
0
        public string GetPyCode(PyEmitStyle style)
        {
            /*
             * echo preg_replace_callback('~-([a-z])~', function ($match) {
             * return strtoupper($match[1]);
             * }, 'hello-world');
             * // outputs helloWorld
             */
            var s = PyEmitStyle.xClone(style);

            s.AsIncrementor = true;
            var e   = new PySourceCodeEmiter();
            var wde = new PySourceCodeWriter();

            wde.Clear();
            MethodDefinition.Emit(e, wde, s);
            var code = wde.GetCode().Trim();

            return(code);
        }