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);
        }
 public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CsharpMethodCallExpression src)
 {
     if (src.MethodInfo.DeclaringType == typeof(StringBuilder))
     {
         var fn = src.MethodInfo.ToString();
         if (fn == "System.Text.StringBuilder Append(System.String)")
         {
             var sb  = ctx.TranslateValue(src.TargetObject);
             var arg = ctx.TranslateValue(src.Arguments[0].MyValue);
             return(new PhpAssignExpression(sb, arg, "."));
         }
         if (fn == "System.Text.StringBuilder AppendLine(System.String)")
         {
             var sb      = ctx.TranslateValue(src.TargetObject);
             var arg     = ctx.TranslateValue(src.Arguments[0].MyValue);
             var eol     = new PhpDefinedConstExpression("PHP_EOL", null);
             var arg_eol = PhpBinaryOperatorExpression.ConcatStrings(arg, eol);
             return(new PhpAssignExpression(sb, arg_eol, "."));
         }
         if (fn == "System.String ToString()")
         {
             return(ctx.TranslateValue(src.TargetObject));
         }
         Console.WriteLine(fn);
         throw new NotSupportedException();
     }
     return(null);
 }
示例#3
0
 protected virtual T VisitPhpBinaryOperatorExpression(PhpBinaryOperatorExpression node)
 {
     if (ThrowNotImplementedException)
     {
         throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPhpBinaryOperatorExpression", this.GetType().FullName));
     }
     return(default(T));
 }
示例#4
0
        public static IPhpValue MakePathValueRelatedToFile(string path)
        {
            path = MakeUnixPath(path + UNIX_SEP);
            if (!path.StartsWith(UNIX_SEP))
            {
                path = UNIX_SEP + path;
            }
            var _FILE_  = new PhpDefinedConstExpression("__FILE__", null);
            var dirinfo = new PhpMethodCallExpression("dirname", _FILE_);
            var a2      = new PhpConstValue(path);
            var concat  = new PhpBinaryOperatorExpression(".", dirinfo, a2);

            return(concat);
        }
        // Private Methods 

        private IPhpValue _JoinArray()
        {
            if (list.Count == 1)
            {
                return(list[0]);
            }
            var e = list[0];

            for (var i = 1; i < list.Count; i++)
            {
                var a = list[i];
                e = new PhpBinaryOperatorExpression(".", e, a);
            }
            return(e);
        }
示例#6
0
 public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CallConstructor src)
 {
     if (src.Info.DeclaringType != typeof(DateTime))
     {
         return(null);
     }
     switch (src.Info.ToString())
     {
     case "Void .ctor(Int32, Int32, Int32)":
     {
         //                $date = new DateTime();
         //$date->setDate(2001, 2, 3);
         var dtObject = PhpMethodCallExpression.MakeConstructor("DateTime", null);
         dtObject.DontIncludeClass = true;
         // date_date_set
         var b = new PhpMethodCallExpression("date_date_set",
                                             dtObject,
                                             ctx.TranslateValue(src.Arguments[0]),
                                             ctx.TranslateValue(src.Arguments[1]),
                                             ctx.TranslateValue(src.Arguments[2])
                                             );
         var c = new PhpMethodCallExpression("date_time_set",
                                             b,
                                             new PhpConstValue(0),
                                             new PhpConstValue(0),
                                             new PhpConstValue(0)
                                             );
         var mktime = new PhpMethodCallExpression("mktime",
                                                  new PhpConstValue(0),
                                                  new PhpConstValue(0),
                                                  new PhpConstValue(0),
                                                  ctx.TranslateValue(src.Arguments[1]), //month
                                                  ctx.TranslateValue(src.Arguments[2]), // day
                                                  ctx.TranslateValue(src.Arguments[0])  // year
                                                  );
         var epoch = new PhpBinaryOperatorExpression(".", new PhpConstValue("@"), mktime);
         dtObject.Arguments.Add(new PhpMethodInvokeValue(epoch));
         return(dtObject);
         // int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
         // $datetimeobject = new DateTime(mktime(0, 0, 0, $data[$j]['month'], $data[$j]['day'],$data[$j]['year']));
     }
     }
     throw new NotImplementedException();
 }
        private static IPhpValue GetDefaultIncludePath(AssemblyTranslationInfo ati, TranslationInfo translationInfo)
        {
            var pathElements = new List <IPhpValue>();

            #region Take include path variable or const

            if (!string.IsNullOrEmpty(ati.IncludePathConstOrVarName))
            {
                if (ati.IncludePathConstOrVarName.StartsWith("$"))
                {
                    pathElements.Add(new PhpVariableExpression(ati.IncludePathConstOrVarName, PhpVariableKind.Global));
                }
                else
                {
                    var tmp = ati.IncludePathConstOrVarName;
                    if (!tmp.StartsWith("\\")) // defined const is in global namespace ALWAYS
                    {
                        tmp = "\\" + tmp;
                    }

                    KnownConstInfo info;
                    if (translationInfo != null && translationInfo.KnownConstsValues.TryGetValue(tmp, out info) &&
                        info.UseFixedValue)
                    {
                        pathElements.Add(new PhpConstValue(info.Value));
                    }
                    else
                    {
                        pathElements.Add(new PhpDefinedConstExpression(tmp, PhpCodeModuleName.Cs2PhpConfigModuleName));
                    }
                }
            }

            #endregion

            //#region RootPathAttribute
            //{
            //    if (!string.IsNullOrEmpty(ati.RootPath) && ati.RootPath != "/")
            //        pathElements.Add(new PhpConstValue(ati.RootPath));
            //}
            //#endregion
            var result = PhpBinaryOperatorExpression.ConcatStrings(pathElements.ToArray());
            return(result);
        }
        private static IPhpValue MkHeader(IExternalTranslationContext ctx, string key, IValue v, IValue replace = null)
        {
            if (v is FunctionArgument)
            {
                v = (v as FunctionArgument).MyValue;
            }
            var a1     = new PhpConstValue(key + ": ");
            var a2     = ctx.TranslateValue(v);
            var concat = new PhpBinaryOperatorExpression(".", a1, a2);
            PhpMethodCallExpression phpm;

            if (replace != null)
            {
                var a3 = ctx.TranslateValue(replace);
                phpm = new PhpMethodCallExpression("header", concat, a3);
            }
            else
            {
                phpm = new PhpMethodCallExpression("header", concat);
            }
            return(phpm);
        }
示例#9
0
        public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CsharpMethodCallExpression src)
        {
            var dt = src.MethodInfo.DeclaringType;

            if (dt.IsGenericType)
            {
                dt = dt.GetGenericTypeDefinition();
            }
            if (dt == typeof(Stack <>))
            {
                var fn = src.MethodInfo.ToString();
                if (fn == "System.String Peek()")
                {
                    var to    = ctx.TranslateValue(src.TargetObject);
                    var cnt   = new PhpMethodCallExpression("count", to);
                    var cnt_1 = new PhpBinaryOperatorExpression("-", cnt, new PhpConstValue(1));
                    var ar    = new PhpArrayAccessExpression(to, cnt_1);
                    return(ar);
                }
                return(null);
            }
            return(null);
        }
        public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, ClassFieldAccessExpression src)
        {
            var    t = src.Member.DeclaringType;
            string n = src.Member.Name;

            if (t == typeof(Wp))
            {
                if (n == "DoingAutosave")
                {
                    //  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
                    var a1 = new PhpMethodCallExpression("defined", new PhpConstValue("DOING_AUTOSAVE"));
                    var a2 = new PhpDefinedConstExpression("DOING_AUTOSAVE", null);
                    var a3 = new PhpBinaryOperatorExpression("&&", a1, a2);
                    var a4 = new PhpParenthesizedExpression(a3);
                    return(a4);
                }
                if (n == "HookSuffix")
                {
                    var a1 = new PhpVariableExpression("hook_suffix", PhpVariableKind.Global);
                    return(a1);
                }
            }
            return(null);
        }
示例#11
0
 protected override IPhpValue VisitPhpUnaryOperatorExpression(PhpUnaryOperatorExpression node)
 {
     if (node.Operator == "!" && node.Operand is PhpBinaryOperatorExpression)
     {
         var bin = node.Operand as PhpBinaryOperatorExpression;
         if (bin.Operator == "!==")
         {
             var bin2 = new PhpBinaryOperatorExpression("===", bin.Left, bin.Right);
             return(bin2);
         }
         var be = new PhpParenthesizedExpression(node.Operand);
         node = new PhpUnaryOperatorExpression(be, node.Operator);
         return(node);
     }
     if (node.Operator == "!" && node.Operand is PhpUnaryOperatorExpression)
     {
         var bin = node.Operand as PhpUnaryOperatorExpression;
         if (bin.Operator == "!")
         {
             return(Simplify(bin.Operand));
         }
     }
     return(node);
 }
示例#12
0
        protected override IPhpStatement VisitPhpCodeBlock(PhpCodeBlock node)
        {
            var newNode = new PhpCodeBlock();

            foreach (var i in node.GetPlain())
            {
                newNode.Statements.Add(Simplify(i));
            }

            #region Łączenie kolejnych echo
            if (op.JoinEchoStatements)
            {
                //newNode.Statements.Clear();
                {
                    for (var i = 1; i < newNode.Statements.Count; i++)
                    {
                        var e1 = GetPhpNativeMethodCall(newNode.Statements[i - 1], "echo");
                        if (e1 == null)
                        {
                            continue;
                        }
                        var e2 = GetPhpNativeMethodCall(newNode.Statements[i], "echo");
                        if (e2 == null)
                        {
                            continue;
                        }

                        Func <IPhpValue, IPhpValue> AddBracketsIfNecessary = (ee) =>
                        {
                            if (ee is PhpParenthesizedExpression || ee is PhpConstValue || ee is PhpPropertyAccessExpression)
                            {
                                return(ee);
                            }

                            if (ee is PhpBinaryOperatorExpression && ((PhpBinaryOperatorExpression)ee).Operator == ".")
                            {
                                return(ee);
                            }
                            return(new PhpParenthesizedExpression(ee));
                        };

                        var a1 = AddBracketsIfNecessary(e1.Arguments[0].Expression);
                        var a2 = AddBracketsIfNecessary(e2.Arguments[0].Expression);

                        IPhpValue e = new PhpBinaryOperatorExpression(".", a1, a2);
                        e = Simplify(e);
                        IPhpValue echo = new PhpMethodCallExpression("echo", e);
                        newNode.Statements[i - 1] = new PhpExpressionStatement(echo);
                        newNode.Statements.RemoveAt(i);
                        i--;
                    }
                    for (var i = 0; i < newNode.Statements.Count; i++)
                    {
                        var a = newNode.Statements[i];
                        if (a is PhpSourceBase)
                        {
                            newNode.Statements[i] = Visit(a as PhpSourceBase);
                        }
                    }
                }
            }
            #endregion
            return(PhpSourceBase.EqualCode_List(node.Statements, newNode.Statements) ? node : newNode);
        }
示例#13
0
        protected override IPhpValue VisitPhpBinaryOperatorExpression(PhpBinaryOperatorExpression node)
        {
            switch (node.Operator)
            {
            case ".":
            {
                var _left  = Simplify(node.Left);
                var _right = Simplify(node.Right);
                var n      = new PhpBinaryOperatorExpression(node.Operator, _left, _right);
                var c      = ExplodeConcats(n, ".").ToList();



                for (var i = 1; i < c.Count; i++)
                {
                    var L = c[i - 1];
                    var R = c[i];
                    if (L is PhpConstValue && R is PhpConstValue)
                    {
                        var LValue = (L as PhpConstValue).Value;
                        var RValue = (R as PhpConstValue).Value;
                        if (LValue is string && RValue is string)
                        {
                            c[i - 1] = new PhpConstValue((string)LValue + (string)RValue);
                            c.RemoveAt(i);
                            i--;
                            continue;
                        }
                        var    LCode = PhpValues.ToPhpCodeValue(LValue);
                        var    RCode = PhpValues.ToPhpCodeValue(RValue);
                        string left, right;
                        if (LCode.TryGetPhpString(out left) && RCode.TryGetPhpString(out right))
                        {
                            c[i - 1] = new PhpConstValue(left + right);
                            c.RemoveAt(i);
                            i--;
                            continue;
                        }

                        var msg = string.Format("left={0}, right={1} '{2}+{3}'", LValue, RValue, LValue == null ? null : LValue.GetType().FullName, RValue == null ? null : RValue.GetType().FullName);

#if DEBUG
                        throw new NotImplementedException(msg);
#else
                        Console.WriteLine(msg);
                        Console.WriteLine(L.GetPhpCode(null));
                        Console.WriteLine(R.GetPhpCode(null));
                        continue;
#endif
                    }
                }
                var result = c[0];
                if (c.Count > 1)
                {
                    foreach (var x2 in c.Skip(1))
                    {
                        result = new PhpBinaryOperatorExpression(".", result, x2);
                    }
                }
                return(ReturnSubst(node, result));
            }

            case "|":
            {
                //                    var aLeft = node.Left as PhpConstValue;
                //                    var aRight = node.Right as PhpConstValue;
                //                    if (aLeft != null && aRight != null && aLeft.Value!=null && aRight.Value!=null)
                //                    {
                //                        var typeLeft = aLeft.Value.GetType();
                //                        var typeRight = aRight.Value.GetType();
                //                        if (typeLeft.IsEnum && typeLeft == typeRight)
                //                        {
                //                            var leftValue = (int)aLeft.Value;
                //                            var rightValue = (int)aRight.Value;
                //                            var c = leftValue | rightValue;
                //                            var dd = PhpCodeValue.FromInt(c, true);
                //                            var d = new PhpConstValue(c, false);
                //                            return d;
                //                        }
                //                    }
            }
            break;
            }
            return(node);
        }
示例#14
0
        protected override IPhpValue VisitInstancePropertyAccessExpression(CsharpInstancePropertyAccessExpression src)
        {
            var pri       = PropertyTranslationInfo.FromPropertyInfo(src.Member);
            var ownerInfo = state.Principles.GetOrMakeTranslationInfo(src.Member.DeclaringType);

            if (src.TargetObject == null)
            {
                throw new NotImplementedException("statyczny");
            }
            var translatedByExternalNodeTranslator = state.Principles.NodeTranslators.Translate(state, src);

            if (translatedByExternalNodeTranslator != null)
            {
                return(SimplifyPhpExpression(translatedByExternalNodeTranslator));
            }

            var phpTargetObject = TransValue(src.TargetObject);

            if (ownerInfo.IsArray)
            {
                var idx       = new PhpConstValue(pri.FieldScriptName);
                var arrayExpr = new PhpArrayAccessExpression(phpTargetObject, idx);
                return(arrayExpr);
            }

            {
                var propertyInfo  = src.Member;
                var classReplacer = state.FindOneClassReplacer(propertyInfo.DeclaringType);
                if (classReplacer != null)
                {
                    var newPropertyInfo = classReplacer.ReplaceBy.GetProperty(src.Member.Name,
                                                                              BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    if (newPropertyInfo == null)
                    {
                        throw new Exception(string.Format("Klasa {0} nie zawiera własności {1}",
                                                          classReplacer.ReplaceBy, src.Member));
                    }
                    if (newPropertyInfo.GetIndexParameters().Length > 0)
                    {
                        throw new NotSupportedException("energetic gecko, Property with index");
                    }
                    propertyInfo = newPropertyInfo;
                }

                {
                    var ats = propertyInfo.GetCustomAttribute <DirectCallAttribute>(true);
                    if (ats != null)
                    {
                        if (string.IsNullOrEmpty(ats.Name))
                        {
                            var tmp = ats.MapArray;
                            if (tmp == null || tmp.Length <= 0)
                            {
                                return(phpTargetObject);
                            }
                            if (tmp.Length > 1 || tmp[0] != DirectCallAttribute.This)
                            {
                                throw new NotSupportedException(string.Format(
                                                                    "Property {1}.{0} has invalid 'Map' parameter in DirectCallAttribute",
                                                                    propertyInfo.Name, propertyInfo.DeclaringType));
                            }
                            return(phpTargetObject);
                        }

                        switch (ats.MemberToCall)
                        {
                        case ClassMembers.Method:
                            if (ats.Name == "this")
                            {
                                return(phpTargetObject);
                            }

                            var method = new PhpMethodCallExpression(ats.Name);
                            switch (ats.CallType)
                            {
                            case MethodCallStyles.Procedural:
                                method.Arguments.Add(new PhpMethodInvokeValue(phpTargetObject));
                                return(method);
                                //    case MethodCallStyles.:
                                //        method.Arguments.Add(new PhpMethodInvokeValue(phpTargetObject));
                                //        return method;
                                //    default:
                                //        throw new NotSupportedException();
                            }

                            throw new NotImplementedException();

                        case ClassMembers.Field:
                            switch (ats.CallType)
                            {
                            case MethodCallStyles.Instance:
                                if (ats.Name == "this")
                                {
                                    return(phpTargetObject);
                                }
                                var includeModule = ownerInfo.IncludeModule;
                                var field         = new PhpInstanceFieldAccessExpression(ats.Name,
                                                                                         phpTargetObject,
                                                                                         includeModule);
                                return(field);

                            default:
                                throw new NotSupportedException();
                            }

                        //var f = new PhpMethodCallExpression(ats.Name);
                        //method.Arguments.Add(new PhpMethodInvokeValue(phpTargetObject));
                        //return method;
                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

                {
                    var ats = propertyInfo.GetCustomAttribute <UseBinaryExpressionAttribute>(true);
                    if (ats != null)
                    {
                        var left   = GetValueForExpression(phpTargetObject, ats.Left);
                        var right  = GetValueForExpression(phpTargetObject, ats.Right);
                        var method = new PhpBinaryOperatorExpression(ats.Operator, left, right);
                        return(method);
                    }
                }
                {
                    pri = PropertyTranslationInfo.FromPropertyInfo(src.Member);
                    var to = TransValue(src.TargetObject);
                    var a  = new PhpPropertyAccessExpression(pri, to);
                    return(a);
                }
            }
        }