Ejemplo n.º 1
0
        IPhpValue IExternalTranslationContext.TranslateValue(IValue srcValue)
        {
            //var aa = new ExpressionSimplifier(new OptimizeOptions());
            //srcValue = aa.Simplify(srcValue);

            var t = new PhpValueTranslator(this);
            var g = t.TransValue(srcValue);

            return(g);
        }
Ejemplo n.º 2
0
        private void TranslateField(PhpCodeModule module, PhpClassDefinition phpClass, FieldDeclaration field)
        {
            PhpValueTranslator phpValueTranslator = null;

            foreach (var item in field.Items)
            {
                if (item.OptionalFieldInfo == null)
                {
                    continue;
                }
                var fti = Info.GetOrMakeTranslationInfo(item.OptionalFieldInfo);
                switch (fti.Destination)
                {
                case FieldTranslationDestionations.DefinedConst:
                    if (item.Value == null)
                    {
                        throw new NotSupportedException();
                    }
                    if (phpValueTranslator == null)
                    {
                        phpValueTranslator = new PhpValueTranslator(_state);
                    }
                    var definedValue = phpValueTranslator.TransValue(item.Value);
                    {
                        if (fti.IncludeModule != module.Name)
                        {
                            module = GetOrMakeModuleByName(fti.IncludeModule);
                        }
                    }
                    module.DefinedConsts.Add(new KeyValuePair <string, IPhpValue>(fti.ScriptName, definedValue));
                    break;

                case FieldTranslationDestionations.GlobalVariable:
                    if (item.Value != null)
                    {
                        IPhpValue value;
                        // muszę na chwilę wyłączyć current type, bo to jes poza klasą generowane
                        {
                            var saveCurrentType = _state.Principles.CurrentType;
                            _state.Principles.CurrentType = null;
                            try
                            {
                                if (phpValueTranslator == null)
                                {
                                    phpValueTranslator = new PhpValueTranslator(_state);
                                }
                                value = phpValueTranslator.TransValue(item.Value);
                            }
                            finally
                            {
                                _state.Principles.CurrentType = saveCurrentType;
                            }
                        }

                        var assign = new PhpAssignExpression(PhpVariableExpression.MakeGlobal(fti.ScriptName),
                                                             value);
                        module.TopCode.Statements.Add(new PhpExpressionStatement(assign));
                    }

                    break;

                case FieldTranslationDestionations.JustValue:
                    continue;     // don't define

                case FieldTranslationDestionations.NormalField:
                case FieldTranslationDestionations.ClassConst:
                {
                    var def = new PhpClassFieldDefinition();
                    var cti = _state.Principles.GetTi(_state.Principles.CurrentType, true);
                    if (cti.IsArray)
                    {
                        continue;
                    }
                    if (field.Modifiers.Has("const") ^
                        (fti.Destination == FieldTranslationDestionations.ClassConst))
                    {
                        throw new Exception("beige lion");
                    }

                    def.IsConst =
                        fti.Destination ==
                        FieldTranslationDestionations.ClassConst;     // field.Modifiers.Has("const");
                    def.Name = fti.ScriptName;

                    def.IsStatic = def.IsConst || field.Modifiers.Has("static");
                    if (field.Modifiers.Has("public"))
                    {
                        def.Visibility = Visibility.Public;
                    }
                    else if (field.Modifiers.Has("protected"))
                    {
                        def.Visibility = Visibility.Protected;
                    }
                    else
                    {
                        def.Visibility = Visibility.Private;
                    }

                    if (item.Value != null)
                    {
                        if (phpValueTranslator == null)
                        {
                            phpValueTranslator = new PhpValueTranslator(_state);
                        }
                        def.ConstValue = phpValueTranslator.TransValue(item.Value);
                    }

                    phpClass.Fields.Add(def);
                    break;
                }

                default:
                    throw new NotSupportedException();
                }
            }
        }
Ejemplo n.º 3
0
        // Private Methods 

        /*
         *      static IPhpStatement[] MkArray(IPhpStatement x)
         *      {
         *          return new IPhpStatement[] { x };
         *      }
         */

        // Public Methods 


        public void Translate(AssemblySandbox sandbox)
        {
            var classes            = Info.GetClasses();
            var classesToTranslate = Info.ClassTranslations.Values
                                     .Where(u => u.Type.Assembly.FullName == Info.CurrentAssembly.FullName).ToArray();
            //            classesToTranslate = (from i in _info.ClassTranslations.Values
            //                                      where i.Type.Assembly.FullName == _info.CurrentAssembly.FullName
            //                                      select this.ge.ToArray();
            var interfaces = Info.GetInterfaces();

            //     var interfacesToTranslate = info.ClassTranslations.Values.Where(u => u.Type.Assembly == info.CurrentAssembly).ToArray();
            foreach (var classTranslationInfo in classesToTranslate)
            {
                if (classTranslationInfo.Skip)
                {
                    Debug.Write("");
                }
                PhpClassDefinition phpClass;
                var phpModule = GetOrMakeModuleByName(classTranslationInfo.ModuleName);
                // var assemblyTI = _info.GetOrMakeTranslationInfo(_info.CurrentAssembly);

                {
                    PhpQualifiedName phpBaseClassName;
                    {
                        var netBaseType = classTranslationInfo.Type.BaseType;
                        if ((object)netBaseType == null || netBaseType == typeof(object))
                        {
                            phpBaseClassName = PhpQualifiedName.Empty;
                        }
                        else
                        {
                            // _state.Principles.CurrentTyp is null so we will obtain absolute name
                            phpBaseClassName =
                                _state.Principles.GetPhpType(netBaseType, true, null); // absolute name
                            var baseTypeTranslationInfo = _state.Principles.GetOrMakeTranslationInfo(netBaseType);
                            if (baseTypeTranslationInfo.Skip)
                            {
                                phpBaseClassName = PhpQualifiedName.Empty;
                            }
                        }
                    }
                    phpClass = phpModule.FindOrCreateClass(classTranslationInfo.ScriptName, phpBaseClassName);
                }
                _state.Principles.CurrentType     = classTranslationInfo.Type;
                _state.Principles.CurrentAssembly = _state.Principles.CurrentType.Assembly;
                Console.WriteLine(classTranslationInfo.ModuleName);

                IClassMember[] members;

                if (classTranslationInfo.Type.IsInterface)
                {
                    var sources = interfaces.Where(i => i.FullName == classTranslationInfo.Type.FullName).ToArray();
                    members = (from i in sources
                               from j in i.ClassDeclaration.Members
                               select j).ToArray();
                    {
                        var fileNames = classTranslationInfo.Type.GetCustomAttributes <RequireOnceAttribute>()
                                        .Select(i => i.Filename).Distinct().ToArray();
                        if (fileNames.Any())
                        {
                            var b = fileNames.Select(u => new PhpConstValue(u)).ToArray();
                            phpModule.RequiredFiles.AddRange(b);
                        }
                    }
                }
                else
                {
                    var sources = classes.Where(i => i.FullName == classTranslationInfo.Type.FullName).ToArray();
                    members = (from i in sources
                               from j in i.ClassDeclaration.Members
                               select j).ToArray();
                    {
                        var fileNames = classTranslationInfo.Type.GetCustomAttributes <RequireOnceAttribute>()
                                        .Select(i => i.Filename).Distinct().ToArray();
                        if (fileNames.Any())
                        {
                            var b = fileNames.Select(u => new PhpConstValue(u)).ToArray();
                            phpModule.RequiredFiles.AddRange(b);
                        }
                    }
                }

                {
                    var c = members.OfType <ConstructorDeclaration>().ToArray();
                    if (c.Length > 1)
                    {
                        throw new Exception("PHP supports only one constructor per class");
                    }
                    if (c.Any())
                    {
                        TranslateConstructor(phpClass, c.First());
                    }
                }

                {
                    foreach (var methodDeclaration in members.OfType <MethodDeclaration>())
                    {
                        TranslateMethod(phpClass, methodDeclaration);
                    }
                }

                {
                    foreach (var pDeclaration in members.OfType <CsharpPropertyDeclaration>())
                    {
                        TranslateProperty(phpClass, pDeclaration);
                    }
                }

                {
                    foreach (var constDeclaration in members.OfType <FieldDeclaration>())
                    {
                        TranslateField(phpModule, phpClass, constDeclaration);
                    }
                }

                _state.Principles.CurrentType = null;
                {
                    if (classTranslationInfo.IsPage)
                    {
                        {
                            var ati = Info.GetOrMakeTranslationInfo(Info.CurrentAssembly);
                            if (ati.DefaultTimezone.HasValue)
                            {
                                // date_default_timezone_set('America/Los_Angeles');
                                var a  = new PhpValueTranslator(_state);
                                var aa = a.Visit(new ConstValue(ati.DefaultTimezone.Value));
                                var dateDefaultTimezoneSet =
                                    new PhpMethodCallExpression("date_default_timezone_set", aa);
                                phpModule.BottomCode.Statements.Add(new PhpExpressionStatement(dateDefaultTimezoneSet));
                            }
                        }

                        {
                            var mti = MethodTranslationInfo.FromMethodInfo(classTranslationInfo.PageMethod,
                                                                           classTranslationInfo);
                            var callMain = new PhpMethodCallExpression(mti.ScriptName);
                            callMain.SetClassName(
                                classTranslationInfo.ScriptName,
                                mti
                                );
                            phpModule.BottomCode.Statements.Add(new PhpExpressionStatement(callMain));
                        }
                    }
                }

                {
                    var moduleCodeRequests = new List <ModuleCodeRequest>();
                    var codeRequests       = (phpModule as ICodeRelated).GetCodeRequests().ToArray();
                    {
                        var classCodeRequests = (from request in codeRequests.OfType <ClassCodeRequest>()
                                                 where request.ClassName != null
                                                 select request.ClassName.FullName)
                                                .Distinct()
                                                .ToArray();

                        foreach (var req in classCodeRequests)
                        {
                            var m = Info.ClassTranslations.Values.Where(i => i.ScriptName.FullName == req).ToArray();
                            if (m.Length != 1)
                            {
                                throw new NotSupportedException();
                            }
                            var mm = m[0];
                            if (mm.DontIncludeModuleForClassMembers)
                            {
                                continue;
                            }
                            var includeModule = mm.IncludeModule;
                            if (includeModule == null || mm.ModuleName == phpModule.Name)
                            {
                                continue;
                            }
                            var h = new ModuleCodeRequest(includeModule, "class request: " + req);
                            moduleCodeRequests.Add(h);
                        }
                    }
                    {
                        var moduleRequests = (from i in codeRequests.OfType <ModuleCodeRequest>()
                                              where i.ModuleName != null
                                              select i).Union(moduleCodeRequests).ToArray();
                        var moduleNames = (from mReq in moduleRequests
                                           where mReq.ModuleName != phpModule.Name
                                           let mName = mReq.ModuleName
                                                       where mName != null
                                                       select mName
                                           ).Distinct().ToArray();
                        foreach (var i in moduleNames.Where(x => !PhpCodeModuleName.IsFrameworkName(x)))
                        {
                            AppendCodeReq(i, phpModule);
                        }
                    }
                }
            }

            {
                var emptyModules = Modules.Where(a => a.IsEmpty).ToArray();
                foreach (var module in Modules)
                {
                    // if (module.IsEmpty)
                }
            }
        }