コード例 #1
0
        protected override IPyValue VisitMethodExpression(MethodExpression src)
        {
            if (src.Method.IsStatic)
            {
                var pyClassName =
                    _state.Principles.GetPyType(src.Method.DeclaringType, true, _state.Principles.CurrentType);
                if (pyClassName.IsEmpty)
                {
                    throw new Exception("PyClassName cannot be null");
                }
                pyClassName = pyClassName.MakeAbsolute();
                var className             = new PyConstValue(pyClassName.FullName);
                var methodTranslationInfo = _state.Principles.GetOrMakeTranslationInfo(src.Method);
                if (!src.Method.IsPublic)
                {
                    WriteWarning(string.Format("Using not public method {0}.{1} as expression",
                                               src.Method.DeclaringType, src.Method.Name));
                }
                var methodName    = new PyConstValue(methodTranslationInfo.ScriptName);
                var arrayCreation = new PyArrayCreateExpression(className, methodName);
                return(SimplifyPyExpression(arrayCreation));
            }

            {
                // ryzykuję z this
                var targetObject          = new PyThisExpression();
                var methodTranslationInfo = _state.Principles.GetOrMakeTranslationInfo(src.Method);
                var methodName            = new PyConstValue(methodTranslationInfo.ScriptName);
                var arrayCreation         = new PyArrayCreateExpression(targetObject, methodName);
                return(SimplifyPyExpression(arrayCreation));
            }
        }
コード例 #2
0
        protected override IPyValue VisitInstanceFieldAccessExpression(InstanceFieldAccessExpression src)
        {
            var fti = _state.Principles.GetOrMakeTranslationInfo(src.Member);
            var to  = TransValue(src.TargetObject);

            if (src.Member.DeclaringType.IsDefined(typeof(AsArrayAttribute)))
            {
                switch (fti.Destination)
                {
                case FieldTranslationDestionations.NormalField:
                    IPyValue index;
                    if (fti.IsScriptNamePyEncoded)
                    {
                        index = PyConstValue.FromPyValue(fti.ScriptName);
                    }
                    else
                    {
                        index = new PyConstValue(fti.ScriptName);
                    }
                    var tmp = new PyArrayAccessExpression(to, index);
                    return(SimplifyPyExpression(tmp));

                case FieldTranslationDestionations.DefinedConst:
                    break;     // obsłużę to dalej jak dla zwykłej klasy

                default:
                    throw new NotSupportedException();
                }
            }
            var a = new PyInstanceFieldAccessExpression(fti.ScriptName, to, fti.IncludeModule);

            return(a);
        }
コード例 #3
0
 protected virtual T VisitPyConstValue(PyConstValue node)
 {
     if (ThrowNotImplementedException)
     {
         throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPyConstValue", this.GetType().FullName));
     }
     return(default(T));
 }
コード例 #4
0
ファイル: PathUtil.cs プロジェクト: isukces/cs2python
        public static IPyValue MakePathValueRelatedToFile(string path)
        {
            path = MakeUnixPath(path + UNIX_SEP);
            if (!path.StartsWith(UNIX_SEP))
            {
                path = UNIX_SEP + path;
            }
            var _FILE_  = new PyDefinedConstExpression("__FILE__", null);
            var dirinfo = new PyMethodCallExpression("dirname", _FILE_);
            var a2      = new PyConstValue(path);
            var concat  = new PyBinaryOperatorExpression(".", dirinfo, a2);

            return(concat);
        }
コード例 #5
0
        protected override IPyValue VisitInstanceMemberAccessExpression(InstanceMemberAccessExpression src)
        {
            if (src.Member == null)
            {
                throw new NotSupportedException();
            }
            if (!(src.Member is MethodInfo))
            {
                throw new NotSupportedException();
            }
            var mi = src.Member as MethodInfo;

            if (mi.IsStatic)
            {
                throw new Exception("Metoda nie może być statyczna");
            }
            var mmi = _state.Principles.GetOrMakeTranslationInfo(mi); // MethodTranslationInfo.FromMethodInfo(mi);
            var a   = new PyConstValue(TransValue(src.Expression));
            var b   = new PyConstValue(mmi.ScriptName);
            var o   = new PyArrayCreateExpression(a, b);

            return(o);
        }
コード例 #6
0
        protected override IPyValue 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(SimplifyPyExpression(translatedByExternalNodeTranslator));
            }

            var pyTargetObject = TransValue(src.TargetObject);

            if (ownerInfo.IsArray)
            {
                var idx       = new PyConstValue(pri.FieldScriptName);
                var arrayExpr = new PyArrayAccessExpression(pyTargetObject, 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(pyTargetObject);
                            }
                            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(pyTargetObject);
                        }

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

                            var pyMethodCall = new PyMethodCallExpression(ats.Name);
                            switch (ats.CallType)
                            {
                            case MethodCallStyles.Procedural:
                                pyMethodCall.Arguments.Add(new PyMethodInvokeValue(pyTargetObject));
                                return(pyMethodCall);

                            case MethodCallStyles.Instance:
                                pyMethodCall.TargetObject = pyTargetObject;
                                return(pyMethodCall);
                            }

                            throw new NotImplementedException();

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

                            default:
                                throw new NotSupportedException();
                            }

                        //var f = new PyMethodCallExpression(ats.Name);
                        //method.Arguments.Add(new PyMethodInvokeValue(PyTargetObject));
                        //return method;
                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

                {
                    var ats = propertyInfo.GetCustomAttribute <UseBinaryExpressionAttribute>(true);
                    if (ats != null)
                    {
                        var left   = GetValueForExpression(pyTargetObject, ats.Left);
                        var right  = GetValueForExpression(pyTargetObject, ats.Right);
                        var method = new PyBinaryOperatorExpression(ats.Operator, left, right);
                        return(method);
                    }
                }
                {
                    pri = PropertyTranslationInfo.FromPropertyInfo(src.Member);
                    var to = TransValue(src.TargetObject);
                    var a  = new PyPropertyAccessExpression(pri, to);
                    return(a);
                }
            }
        }
コード例 #7
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));
                }
            }
        }