コード例 #1
0
        // Public Methods 

        public static IPhpValue GetValueForExpression(IPhpValue phpTargetObject, string valueAsString)
        {
            valueAsString = (valueAsString ?? "").Trim();
            if (valueAsString.ToLower() == "this")
            {
                return(phpTargetObject);
            }
            if (valueAsString == "false")
            {
                return(new PhpConstValue(false));
            }
            if (valueAsString == "true")
            {
                return(new PhpConstValue(true));
            }
            if (int.TryParse(valueAsString, out var i))
            {
                return(new PhpConstValue(i));
            }

            if (double.TryParse(valueAsString, NumberStyles.Float, CultureInfo.InvariantCulture, out var d))
            {
                return(new PhpConstValue(d));
            }
            {
                if (PhpValues.TryGetPhpStringValue(valueAsString, out var x))
                {
                    return(new PhpConstValue(x));
                }
            }
            throw new Exception(string.Format("bald boa, Unable to convert {0} into php value", valueAsString));
        }
コード例 #2
0
        protected override IPhpValue VisitClassFieldAccessExpression(ClassFieldAccessExpression src)
        {
            var tmp = state.Principles.NodeTranslators.Translate(state, src);

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

            var isStatic            = src.IsStatic;
            var member              = src.Member;
            var memberName          = member.Name;
            var memberDeclaringType = member.DeclaringType;

            {
                var tInfo = state.Principles.GetOrMakeTranslationInfo(src.Member);
                if (tInfo.IsDefinedInNonincludableModule)
                {
                    var b = state.Principles.GetTi(state.Principles.CurrentType, true);
                    if (tInfo.IncludeModule != b.ModuleName)
                    {
                        throw new Exception(
                                  string.Format(
                                      "Unable to reference to field {1}.{0} from {2}.{3}. Containing module is page and cannot be included.",
                                      memberName,
                                      memberDeclaringType == null
                                    ? "?"
                                    : (memberDeclaringType.FullName ?? memberDeclaringType.Name),
                                      state.Principles.CurrentType.FullName,
                                      state.Principles.CurrentMethod
                                      ));
                    }
                }

                var fieldDeclaringType = memberDeclaringType;
                if (fieldDeclaringType == null)
                {
                    throw new Exception("fieldDeclaringType");
                }
                state.Principles.GetTi(fieldDeclaringType, false);
                {
                    if (fieldDeclaringType.IsEnum)
                    {
                        if (!isStatic)
                        {
                            throw new NotSupportedException();
                        }
                        var asDefinedConstAttribute = member.GetCustomAttribute <AsDefinedConstAttribute>();
                        if (asDefinedConstAttribute != null)
                        {
                            var definedExpression =
                                new PhpDefinedConstExpression(asDefinedConstAttribute.DefinedConstName,
                                                              tInfo.IncludeModule);
                            return(SimplifyPhpExpression(definedExpression));
                        }

                        var renderValueAttribute = member.GetCustomAttribute <RenderValueAttribute>();
                        if (renderValueAttribute != null)
                        {
                            if (PhpValues.TryGetPhpStringValue(renderValueAttribute.Name, out var strCandidate))
                            {
                                var valueExpression = new PhpConstValue(strCandidate);
#if DEBUG
                                {
                                    var a1 = renderValueAttribute.Name.Trim();
                                    var a2 = valueExpression.ToString();
                                    if (a1 != a2)
                                    {
                                        throw new InvalidOperationException();
                                    }
                                }
#endif
                                return(SimplifyPhpExpression(valueExpression));
                            }
                            else
                            {
                                var valueExpression = new PhpFreeExpression(renderValueAttribute.Name);
                                return(SimplifyPhpExpression(valueExpression));
                            }
                        }

                        {
                            // object v1 = ReadEnumValueAndProcessForPhp(member);
                            var v1 = member.GetValue(null);
                            var g  = new PhpConstValue(v1);
                            return(SimplifyPhpExpression(g));
                        }
                        //throw new NotSupportedException();
                    }
                }

                var principles = state.Principles;
                switch (tInfo.Destination)
                {
                case FieldTranslationDestionations.DefinedConst:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException("Unable to convert instance field into PHP defined const");
                    }
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    var definedExpression = new PhpDefinedConstExpression(tInfo.ScriptName, tInfo.IncludeModule);
                    return(SimplifyPhpExpression(definedExpression));

                case FieldTranslationDestionations.GlobalVariable:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException(
                                  "Unable to convert instance field into PHP global variable");
                    }
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    var globalVariable = PhpVariableExpression.MakeGlobal(tInfo.ScriptName);
                    return(SimplifyPhpExpression(globalVariable));

                case FieldTranslationDestionations.JustValue:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException("Unable to convert instance field into compile-time value");
                    }
                    var constValue    = member.GetValue(null);
                    var phpConstValue = new PhpConstValue(constValue, tInfo.UsGlueForValue);
                    return(SimplifyPhpExpression(phpConstValue));

                case FieldTranslationDestionations.NormalField:
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    var rr = new PhpClassFieldAccessExpression
                    {
                        FieldName = tInfo.ScriptName,
                        IsConst   = tInfo.Destination == FieldTranslationDestionations.ClassConst
                    };
                    rr.SetClassName(
                        principles.GetPhpType(memberDeclaringType, true, principles.CurrentType),
                        principles.GetOrMakeTranslationInfo(memberDeclaringType)
                        );
                    return(SimplifyPhpExpression(rr));

                case FieldTranslationDestionations.ClassConst:
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    rr = new PhpClassFieldAccessExpression
                    {
                        FieldName = tInfo.ScriptName,
                        IsConst   = true
                    };
                    rr.SetClassName(
                        principles.GetPhpType(memberDeclaringType, true, principles.CurrentType),
                        principles.GetOrMakeTranslationInfo(memberDeclaringType));

                    return(SimplifyPhpExpression(rr));

                default:
                    throw new NotSupportedException(string.Format(
                                                        "Unable to translate class field with destination option equal {0}", tInfo.Destination));
                }
            }
        }