コード例 #1
0
        protected override IPyValue VisitPyBinaryOperatorExpression(PyBinaryOperatorExpression node)
        {
            var v1 = ConvertAndRefactor(node.Left);
            var v2 = ConvertAndRefactor(node.Right);

            return(node);
        }
コード例 #2
0
        private static IPyValue Try_UseExpressionAttribute(IExternalTranslationContext ctx, CsharpMethodCallExpression src)
        {
            var ats = src.MethodInfo.GetCustomAttribute <UseBinaryExpressionAttribute>(true);

            if (ats == null)
            {
                return(null);
            }
            IPyValue 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 = PyValueTranslator.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 = PyValueTranslator.GetValueForExpression(null, ats.Right);
            }
            var method = new PyBinaryOperatorExpression(ats.Operator, l, r);

            return(method);
        }
コード例 #3
0
 protected virtual T VisitPyBinaryOperatorExpression(PyBinaryOperatorExpression node)
 {
     if (ThrowNotImplementedException)
     {
         throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPyBinaryOperatorExpression", this.GetType().FullName));
     }
     return(default(T));
 }
コード例 #4
0
        private static IPyValue GetDefaultIncludePath(AssemblyTranslationInfo ati, TranslationInfo translationInfo)
        {
            return(new PyConstValue("---FAKE---"));

#if PHP
            var pathElements = new List <IPyValue>();

            #region Take include path variable or const

            if (!string.IsNullOrEmpty(ati.IncludePathConstOrVarName))
            {
                if (ati.IncludePathConstOrVarName.StartsWith("$"))
                {
                    pathElements.Add(new PyVariableExpression(ati.IncludePathConstOrVarName, PyVariableKind.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 PyConstValue(info.Value));
                    }
                    else
                    {
                        pathElements.Add(new PyDefinedConstExpression(tmp, PyCodeModuleName.Cs2PyConfigModuleName));
                    }
                }
            }

            #endregion

            //#region RootPathAttribute
            //{
            //    if (!string.IsNullOrEmpty(ati.RootPath) && ati.RootPath != "/")
            //        pathElements.Add(new PyConstValue(ati.RootPath));
            //}
            //#endregion
            var result = PyBinaryOperatorExpression.ConcatStrings(pathElements.ToArray());
            return(result);
    #else
            throw new NotImplementedException();
#endif
        }
コード例 #5
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);
        }
コード例 #6
0
        private static PyBinaryOperatorExpression VariableOnLeft(PyBinaryOperatorExpression e, string name)
        {
            if (e.Left is PyVariableExpression veL)
            {
                if (veL.VariableName == name)
                {
                    return(e);
                }
            }
            if (e.Right is PyVariableExpression veR)
            {
                if (veR.VariableName == name)
                {
                    var oposite = OpositeOperator(e.Operator);
                    if (oposite == null)
                    {
                        return(e);
                    }
                    return(new PyBinaryOperatorExpression(oposite, e.Right, e.Left));
                }
            }

            return(e);
        }
コード例 #7
0
        protected override IPyStatement VisitPyCodeBlock(PyCodeBlock node)
        {
            var newNode = new PyCodeBlock();

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

            if (op.JoinEchoStatements)
            {
                for (var i = 1; i < newNode.Statements.Count; i++)
                {
                    var e1 = GetPyNativeMethodCall(newNode.Statements[i - 1], "echo");
                    if (e1 == null)
                    {
                        continue;
                    }
                    var e2 = GetPyNativeMethodCall(newNode.Statements[i], "echo");
                    if (e2 == null)
                    {
                        continue;
                    }

                    Func <IPyValue, IPyValue> AddBracketsIfNecessary = ee =>
                    {
                        if (ee is PyParenthesizedExpression || ee is PyConstValue ||
                            ee is PyPropertyAccessExpression)
                        {
                            return(ee);
                        }

                        if (ee is PyBinaryOperatorExpression && ((PyBinaryOperatorExpression)ee).Operator == ".")
                        {
                            return(ee);
                        }
                        return(new PyParenthesizedExpression(ee));
                    };

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

                    IPyValue e = new PyBinaryOperatorExpression(".", a1, a2);
                    e = Simplify(e);
                    IPyValue echo = new PyMethodCallExpression("echo", e);
                    newNode.Statements[i - 1] = new PyExpressionStatement(echo);
                    newNode.Statements.RemoveAt(i);
                    i--;
                }

                for (var i = 0; i < newNode.Statements.Count; i++)
                {
                    var a = newNode.Statements[i];
                    if (a is PySourceBase)
                    {
                        newNode.Statements[i] = Visit(a as PySourceBase);
                    }
                }
            }

            return(PySourceBase.EqualCode_List(node.Statements, newNode.Statements) ? node : newNode);
        }
コード例 #8
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);
                }
            }
        }