Exemplo n.º 1
0
        private static IPhpValue Try_UseExpressionAttribute(IExternalTranslationContext ctx, CsharpMethodCallExpression src)
        {
            var ats = src.MethodInfo.GetCustomAttribute <UseBinaryExpressionAttribute>(true);

            if (ats == null)
            {
                return(null);
            }
            IPhpValue l, r;
            var       re = new Regex("^\\s*\\$(\\d+)\\s*$");
            var       m  = re.Match(ats.Left);

            if (m.Success)
            {
                l = ctx.TranslateValue(src.Arguments[int.Parse(m.Groups[1].Value)].MyValue);
            }
            else
            {
                l = PhpValueTranslator.GetValueForExpression(null, ats.Left);
            }

            m = re.Match(ats.Right);
            if (m.Success)
            {
                r = ctx.TranslateValue(src.Arguments[int.Parse(m.Groups[1].Value)].MyValue);
            }
            else
            {
                r = PhpValueTranslator.GetValueForExpression(null, ats.Right);
            }
            var method = new PhpBinaryOperatorExpression(ats.Operator, l, r);

            return(method);
        }
Exemplo n.º 2
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;
        }
Exemplo n.º 3
0
        // Private Methods 

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

        #endregion Static Methods

        #region Methods

        // 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);

                #region Szukanie / Tworzenie PhpClassDefinition
                {
                    PhpQualifiedName phpBaseClassName;
                    #region Szukanie nazwy klasy bazowej
                    {
                        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;
                        }
                    }
                    #endregion
                    phpClass = phpModule.FindOrCreateClass(classTranslationInfo.ScriptName, phpBaseClassName);
                }
                #endregion
                _state.Principles.CurrentType = classTranslationInfo.Type;
                _state.Principles.CurrentAssembly = _state.Principles.CurrentType.Assembly;
                Console.WriteLine(classTranslationInfo.ModuleName);

                Cs.Compiler.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);
                        }
                    }
                }
                #region Constructors
                {
                    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());
                }
                #endregion
                #region Metody
                {
                    foreach (var methodDeclaration in members.OfType<MethodDeclaration>())
                        TranslateMethod(phpClass, methodDeclaration);
                }
                #endregion
                #region Własności
                {
                    foreach (var pDeclaration in members.OfType<CsharpPropertyDeclaration>())
                        TranslateProperty(phpClass, pDeclaration);
                }
                #endregion
                #region Pola, stałe
                {
                    foreach (var constDeclaration in members.OfType<FieldDeclaration>())
                        TranslateField(phpModule, phpClass, constDeclaration);
                }
                #endregion

                _state.Principles.CurrentType = null;
                #region Wywołanie metody defaulttimezone oraz MAIN dla PAGE
                {
                    if (classTranslationInfo.IsPage)
                    {
                        #region Timezone
                        {
                            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));
                            }
                        }
                        #endregion
                        #region Wywołanie main
                        {
                            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));
                        }
                        #endregion
                    }
                }
                #endregion
                #region includy
                {


                    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);

                    }
                }
                #endregion
            }
            {
                var emptyModules = _modules.Where(a => a.IsEmpty).ToArray();
                foreach (var module in _modules)
                {
                    // if (module.IsEmpty) 
                }
            }
        }
Exemplo n.º 4
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;
                                }
                            }

                            #region Tworzenie kodu
                            var assign = new PhpAssignExpression(PhpVariableExpression.MakeGlobal(fti.ScriptName), value);
                            module.TopCode.Statements.Add(new PhpExpressionStatement(assign));
                            #endregion
                        }
                        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();
                }
            }
        }