public override string GetPyCode(PyEmitStyle style)
        {
            if (Initializers == null || Initializers.Length == 0)
            {
                return("array()");
            }
            style = style ?? new PyEmitStyle();
            var przecinek = style.Compression == EmitStyleCompression.Beauty ? ", " : ",";
            var www       = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";

            var list = new List <string>();

            foreach (var initializeValue in Initializers)
            {
                if (initializeValue is PyAssignExpression)
                {
                    var assignExpression = initializeValue as PyAssignExpression;
                    if (!string.IsNullOrEmpty(assignExpression.OptionalOperator))
                    {
                        throw new NotSupportedException();
                    }

                    if (assignExpression.Left is PyArrayAccessExpression)
                    {
                        var left = assignExpression.Left as PyArrayAccessExpression;
                        if (left.PyArray is PyThisExpression)
                        {
                            var o = left.Index + www + assignExpression.Right;
                            list.Add(o);
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else if (assignExpression.Left is PyInstanceFieldAccessExpression)
                    {
                        var l1 = assignExpression.Left as PyInstanceFieldAccessExpression;
                        var fn = new PyConstValue(l1.FieldName);
                        var o  = fn.GetPyCode(style) + www + assignExpression.Right;
                        list.Add(o);
                    }
                    else
                    {
                        var o = assignExpression.Left.GetPyCode(style) + www + assignExpression.Right;
                        list.Add(o);
                    }
                }
                else
                {
                    list.Add(initializeValue.GetPyCode(style));
                }
            }

            return("[" + string.Join(przecinek, list) + "]");
        }
示例#2
0
        // Public Methods 


        public void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style)
        {
            var c = ConstValue;

            if (IsStatic && c == null)
            {
                c = PyConstValue.DefaultForType(_valueType);
            }
            if (c == null)
            {
                return;
            }
            if (!IsStatic)
            {
                throw new Exception(
                          "Only static fields with initialization are allowed. Move instance field initialization to constructior.");
            }
            ScanForDangerousCode(ConstValue);
            writer.WriteLn(Name + " = " + c.GetPyCode(style));
        }