Example #1
0
 public TranslatorBase(TranslationState state)
 {
     if (state == null)
     {
         throw new ArgumentNullException(nameof(state));
     }
     this.state = state;
 }
Example #2
0
        public Translator(TranslationState translationState)
        {
            if (translationState == null)
                throw new ArgumentNullException("translationState");
#if DEBUG
            if (translationState == null)
                throw new ArgumentNullException("translationState");

#endif
            _state = translationState; // ?? new TranslationState(new TranslationInfo());
            _info = translationState.Principles;
        }
Example #3
0
        public Translator(TranslationState translationState)
        {
            if (translationState == null)
            {
                throw new ArgumentNullException(nameof(translationState));
            }
#if DEBUG
            if (translationState == null)
            {
                throw new ArgumentNullException("translationState");
            }
#endif
            _state = translationState; // ?? new TranslationState(new TranslationInfo());
            Info   = translationState.Principles;
        }
Example #4
0
 public TranslatorBase(TranslationState state)
 {
     if (state == null)
         throw new ArgumentNullException("state");
     this.state = state;
 }
Example #5
0
        public void Compile()
        {
            using (new AppConfigManipulator())
            {
                CompilerEngine.ExecuteInSeparateAppDomain(
                    ce =>
                    {
#if DEBUG
                        ce.Configuration = "DEBUG";
#else
        ce.Configuration = "RELEASE";
#endif
                        ce.CsProject = LangPhpTestCsProj;
                        ce.OutDir = ce.BinaryOutputDir = Path.GetTempPath();
                        ce.Referenced.Clear();

                        var types = new[]
                        {
                            typeof (Func<,>),
                            typeof (EmitContext),
                            typeof (ThisExpression),
                            typeof (CompilerEngine)
                        };
                        {
                            var tmp =
                                Assembly.Load(
                                    "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                            ce.Referenced.Add(tmp.GetCodeLocation().FullName);
                        }
                        ce.Referenced.AddRange(types.Select(type => type.Assembly.GetCodeLocation().FullName));


                        ce.TranlationHelpers.Clear();
                        ce.ReferencedPhpLibsLocations.Clear();
                        ce.Check();

                        using (var comp = ce.PreparePhpCompiler())
                        {

                            Console.WriteLine("Compilation");
                            var emitResult = comp.CompileCSharpProject(comp.Sandbox, ce.DllFilename);
                            if (!emitResult.Success)
                                foreach (var i in emitResult.Diagnostics.Where(a => a.Severity == DiagnosticSeverity.Error))
                                    throw new Exception("Compilation error: " + i.GetMessage());


                            var translationInfo = comp.ParseCsSource();


                            translationInfo.CurrentAssembly = comp.CompiledAssembly;
                            var assemblyTi = translationInfo.GetOrMakeTranslationInfo(comp.CompiledAssembly);
                            var ecBaseDir = Path.Combine(Directory.GetCurrentDirectory(),
                                assemblyTi.RootPath.Replace("/", "\\"));
                            Console.WriteLine("Output root {0}", ecBaseDir);

                            var translationState = new TranslationState(translationInfo);
                            var translator = new Translator(translationState);

                            translator.Translate(comp.Sandbox);

                            // =============
                            var m =
                                string.Join(", ", translator.Modules.Select(i => i.Name.Name).OrderBy(i => i)).ToArray();
                            
                            //Assert.True(m == "Lang_Php_Test_Code_MyCode, Lang_Php_Test_Code_SampleEmptyClass", m);

                            MethodTranslation(ModuleMycode, ClassMycode, "BasicMath1", translator);
                            MethodTranslation(ModuleMycode, ClassMycode, "Collections", translator);
                            MethodTranslation(ModuleMycode, ClassMycode, "CostantsAndVariables", translator);
                            MethodTranslation(ModuleMycode, ClassMycode, "Filters", translator);
                            MethodTranslation(ModuleMycode, ClassMycode, "StringConcats", translator);
                            MethodTranslation(ModuleMycode, ClassMycode, "PregTest", translator);
                            
//                            ModuleTranslation("Lang_Php_Test_Code_SampleEmptyClass", translator);
//                            ModuleTranslation("Lang_Php_Test_Code_BusinessClass", translator);
//                            ModuleTranslation("Lang_Php_Test_Code_BusinessClassDefinedConst", translator);

                            foreach (var moduleName in translator.Modules.Select(i => i.Name.Name))                            
                                ModuleTranslation(moduleName, translator);                                
                            
                        }
                        ;
                    }, AppDomain.CurrentDomain);
            }
        }
Example #6
0
 public PhpValueTranslator(TranslationState state)
 {
     this.state = state;
 }
Example #7
0
        private void TranslateAndCreatePhpFiles(TranslationInfo translationInfo, string outDir)
        {
            if (_verboseToConsole)
                Console.WriteLine("Translate C# -> Php");

            translationInfo.CurrentAssembly = _compiledAssembly;
            var assemblyTi = translationInfo.GetOrMakeTranslationInfo(_compiledAssembly);
            var ecBaseDir = Path.Combine(outDir, assemblyTi.RootPath.Replace("/", "\\"));
            Console.WriteLine("Output root {0}", ecBaseDir);

            if (!string.IsNullOrEmpty(assemblyTi.PhpPackageSourceUri))
            {
                DownloadAndUnzip(assemblyTi.PhpPackageSourceUri, ecBaseDir, assemblyTi.PhpPackagePathStrip);
                return; //??? czy return?
            }
            var translationState = new TranslationState(translationInfo);
            var translator = new Translator.Translator(translationState);

            translator.Translate(_sandbox);

            var libName = assemblyTi.LibraryName;


            if (_verboseToConsole)
                Console.WriteLine("Create Php output files");
            #region Tworzenie plików php
            {
                // var emitContext = new EmitContext();
                var emitStyle = new PhpEmitStyle();

                translationInfo.CurrentAssembly = _compiledAssembly;// dla pewności
                foreach (var module in translator.Modules.Where(i => i.Name.Library == libName && !i.IsEmpty))
                {
                    var fileName = module.Name.MakeEmitPath(ecBaseDir, 1);
                    foreach (var modProcessor in translationInfo.ModuleProcessors)
                    {
                        modProcessor.BeforeEmit(module, translationInfo);
                    }
                    var emiter = new PhpSourceCodeEmiter();
                    module.Emit(emiter, emitStyle, fileName);

                }
            }

            #endregion
        }
 public PhpStatementTranslatorVisitor(TranslationState state)
 {
     _state = state;
 }
Example #9
0
        // Internal Methods 

        internal static Translator PrepareTranslator()
        {
            if (_translator != null)
                return _translator;
            var csProject = LangPhpTestCsProj;
            using (var comp = new Cs2PhpCompiler { VerboseToConsole = true, ThrowExceptions = true })
            {
                Console.WriteLine("Try to load " + csProject);

#if DEBUG
                comp.LoadProject(csProject, "DEBUG");
#else
                comp.LoadProject(csProject, "RELEASE");
#endif

                /*
   linked with C:\programs\_CS2PHP\PUBLIC\Lang.Php.Compiler\bin\Debug\Lang.Php.Compiler.dll
   linked with C:\programs\_CS2PHP\PUBLIC\Lang.Php.Framework\bin\Debug\Lang.Php.Framework.dll
   linked with C:\programs\_CS2PHP\PUBLIC\Lang.Php.Test\bin\Debug\Lang.Php.dll
             */


                Console.WriteLine("Preparing before compilation");
                string[] removeL = "Lang.Php.Compiler,Lang.Php.Framework,Lang.Php".Split(',');

                #region Remove Lang.Php reference

                {
                    foreach (var r in removeL)
                    {
                        // ... will be replaced by reference to dll from compiler base dir
                        // I know - compilation libraries should be loaded into separate application domain
                        var remove =
                            comp.CSharpProject.MetadataReferences.FirstOrDefault(i => i.Display.EndsWith(r + ".dll"));
                        if (remove != null)
                            comp.RemoveMetadataReferences(remove);
                    }
                }

                #endregion

                string[] filenames;

                #region We have to remove and add again references - strange

                {
                    // in other cases some referenced libraries are ignored
                    var refToRemove = comp.CSharpProject.MetadataReferences.OfType<MetadataFileReference>().ToList();
                    foreach (var i in refToRemove)
                        comp.RemoveMetadataReferences(i);
                    var ref1 = refToRemove.Select(i => i.FilePath).ToList();
                    // foreach (var r in removeL)
                    //     ref1.Add(Path.Combine(Directory.GetCurrentDirectory(), r + ".dll"));
                    ref1.Add(typeof(DirectCallAttribute).Assembly.Location);
                    ref1.Add(typeof(EmitContext).Assembly.Location);
                    ref1.Add(typeof(Extension).Assembly.Location);
                    filenames = ref1.Distinct().ToArray();
                }

                #endregion

                #region Translation assemblies

                {
                    comp.TranslationAssemblies.Add(typeof(Extension).Assembly);
                    comp.TranslationAssemblies.Add(typeof(Translator).Assembly);
                }

                #endregion

                foreach (var fileName in filenames)
                {
                    var g = new MetadataFileReference(fileName, MetadataReferenceProperties.Assembly);
                    comp.AddMetadataReferences(g);
                    Console.WriteLine("  Add reference     {0}", g.Display);
                }

                //                using (var sandbox = new AssemblySandbox())
                //                {
                //                //
                //                Console.WriteLine("Start compile");
                //                var result = comp.CompileCSharpProject(sandbox, comp.DllFileName);
                //                if (!result.Success)
                //                {
                //                    foreach (var i in result.Diagnostics)
                //                        Console.WriteLine(i);
                //                }
                //                Assert.True(result.Success, "Compilation failed");
                //                }
                TranslationInfo translationInfo = comp.ParseCsSource();


                translationInfo.CurrentAssembly = comp.CompiledAssembly;
                var assemblyTi = translationInfo.GetOrMakeTranslationInfo(comp.CompiledAssembly);
                var ecBaseDir = Path.Combine(Directory.GetCurrentDirectory(), assemblyTi.RootPath.Replace("/", "\\"));
                Console.WriteLine("Output root {0}", ecBaseDir);

                var translationState = new TranslationState(translationInfo);
                _translator = new Translator(translationState);

                _translator.Translate(comp.Sandbox);
                return _translator;
            }
        }
Example #10
0
 public StatementTranslatorVisitor(TranslationState state)
 {
     this.state = state;
 }