Exemplo n.º 1
0
        // Public Methods 

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

            if (double.TryParse(valueAsString, NumberStyles.Float, CultureInfo.InvariantCulture, out var d))
            {
                return(new PyConstValue(d));
            }
            {
                if (PyValues.TryGetPyStringValue(valueAsString, out var x))
                {
                    return(new PyConstValue(x));
                }
            }
            throw new Exception(string.Format("bald boa, Unable to convert {0} into Py value", valueAsString));
        }
Exemplo n.º 2
0
        protected override IPyValue VisitClassFieldAccessExpression(ClassFieldAccessExpression src)
        {
            var tmp = _state.Principles.NodeTranslators.Translate(_state, src);

            if (tmp != null)
            {
                return(SimplifyPyExpression(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 PyDefinedConstExpression(asDefinedConstAttribute.DefinedConstName,
                                                             tInfo.IncludeModule);
                            return(SimplifyPyExpression(definedExpression));
                        }

                        var renderValueAttribute = member.GetCustomAttribute <RenderValueAttribute>();
                        if (renderValueAttribute != null)
                        {
                            if (PyValues.TryGetPyStringValue(renderValueAttribute.Name, out var strCandidate))
                            {
                                var valueExpression = new PyConstValue(strCandidate);
#if DEBUG
                                {
                                    var a1 = renderValueAttribute.Name.Trim();
                                    var a2 = valueExpression.ToString();
                                    if (a1 != a2)
                                    {
                                        throw new InvalidOperationException();
                                    }
                                }
#endif
                                return(SimplifyPyExpression(valueExpression));
                            }
                            else
                            {
                                var valueExpression = new PyFreeExpression(renderValueAttribute.Name);
                                return(SimplifyPyExpression(valueExpression));
                            }
                        }

                        {
                            // object v1 = ReadEnumValueAndProcessForPy(member);
                            var v1 = member.GetValue(null);
                            var g  = new PyConstValue(v1);
                            return(SimplifyPyExpression(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 Py defined const");
                    }
                    if (tInfo.IsScriptNamePyEncoded)
                    {
                        throw new Exception("Encoded Py values are not supported");
                    }
                    var definedExpression = new PyDefinedConstExpression(tInfo.ScriptName, tInfo.IncludeModule);
                    return(SimplifyPyExpression(definedExpression));

                case FieldTranslationDestionations.GlobalVariable:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException(
                                  "Unable to convert instance field into Py global variable");
                    }
                    if (tInfo.IsScriptNamePyEncoded)
                    {
                        throw new Exception("Encoded Py values are not supported");
                    }
                    var globalVariable = PyVariableExpression.MakeGlobal(tInfo.ScriptName);
                    return(SimplifyPyExpression(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 pyConstValue = new PyConstValue(constValue, tInfo.UsGlueForValue);
                    return(SimplifyPyExpression(pyConstValue));

                case FieldTranslationDestionations.NormalField:
                    if (tInfo.IsScriptNamePyEncoded)
                    {
                        throw new Exception("Encoded Py values are not supported");
                    }
                    var rr = new PyClassFieldAccessExpression
                    {
                        FieldName = tInfo.ScriptName,
                        IsConst   = tInfo.Destination == FieldTranslationDestionations.ClassConst
                    };
                    rr.SetClassName(
                        principles.GetPyType(memberDeclaringType, true, principles.CurrentType),
                        principles.GetOrMakeTranslationInfo(memberDeclaringType)
                        );
                    return(SimplifyPyExpression(rr));

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

                    return(SimplifyPyExpression(rr));

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