Esempio n. 1
0
        // Public Methods 

        public override void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style)
        {
            style = PyEmitStyle.xClone(style);
            if (!style.AsIncrementor)
            {
                switch (Expression)
                {
                case PyEmptyExpression _:
                    return;

                case PyMethodCallExpression methodCallExpression:
                    break;
                }
            }

            var code = Expression.GetPyCode(style);

            if (style.AsIncrementor)
            {
                writer.Write(code);
            }
            else
            {
                writer.WriteLn(code);
            }
        }
Esempio n. 2
0
        // Protected Methods 

        protected void EmitHeaderStatement(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style, string header, IPyStatement statement)
        {
            style = style ?? new PyEmitStyle();
            var iStyle = PyEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems);

            if (style.UseBracketsEvenIfNotNecessary)
            {
                iStyle.Brackets = ShowBracketsEnum.Always;
            }

            var statementToEmit = PyCodeBlock.Reduce(statement);
            var emptyStatement  = !PyCodeBlock.HasAny(statementToEmit);


            if (emptyStatement)
            {
                header += "{}";
            }
            if (style.Compression == EmitStyleCompression.NearCrypto)
            {
                writer.Write(header);
            }
            else
            {
                writer.WriteLn(header);
            }
            if (emptyStatement)
            {
                return;
            }


            var myBracket = style.UseBracketsEvenIfNotNecessary;

            if (!myBracket)
            {
                var gf = statementToEmit.GetStatementEmitInfo(iStyle);
                if (gf != StatementEmitInfo.NormalSingleStatement)
                {
                    myBracket = true;
                }
            }
            writer.IncIndent();
            if (myBracket)
            {
                iStyle.Brackets = ShowBracketsEnum.Never;
                writer.OpenLn("{");
            }


            statementToEmit.Emit(emiter, writer, iStyle);
            if (myBracket)
            {
                writer.CloseLn("}");
            }

            writer.DecIndent();
        }
Esempio n. 3
0
        // Public Methods 

        public override void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style)
        {
            if (ReturnValue == null)
            {
                writer.WriteLn("return");
            }
            else
            {
                writer.WriteLnF("return {0}", ReturnValue.GetPyCode(style));
            }
        }
Esempio n. 4
0
        // Public Methods 

        public override void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style)
        {
            var s = style?.Compression ?? EmitStyleCompression.Beauty;

            if (s == EmitStyleCompression.NearCrypto)
            {
                writer.Write("continue;");
            }
            else
            {
                writer.WriteLn("continue;");
            }
        }
Esempio n. 5
0
        public virtual void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter code, PyEmitStyle style)
        {
            // public function addFunction($function, $namespace = '')
            var argumentsCode = Arguments.Select(u => u.GetPyCode(style)).ToList();
            var mk            = GetPyMethodKind();

            switch (mk)
            {
            case PyMethodKind.ClassStatic:
                code.WriteLn("@staticmethod");
                argumentsCode.Insert(0, GetCurrentClassAlias());
                break;

            case PyMethodKind.OutOfClass:
                break;

            case PyMethodKind.ClassInstance:
                argumentsCode.Insert(0, "self");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var param = Arguments == null ? "" : string.Join(", ", argumentsCode);

            code.OpenLnF("def {0}({1}):", Name, param);
            {
                var g = GetGlobals();
                if (!string.IsNullOrEmpty(g))
                {
                    code.WriteLnF("global {0};", g);
                }
            }
            foreach (var statement in Statements)
            {
                var g = PyEmitStyle.xClone(style);
                g.Brackets      = ShowBracketsEnum.Never;
                g.CurrentMethod = this;
                statement.Emit(emiter, code, g);
            }

            code.CloseLn("");
        }
Esempio n. 6
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));
        }
Esempio n. 7
0
        public void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style)
        {
            var nsManager = new PyModuleNamespaceManager();

            style.CurrentNamespace = null;
            if (!string.IsNullOrEmpty(_topComments))
            {
                writer.WriteLn("'''\r\n" + _topComments.Trim() + "\r\n'''");
            }
            var module = this;
            {
                // var noBracketStyle = PyEmitStyle.xClone(style, ShowBracketsEnum.Never);

                {
                    // top code
                    var collectedTopCodeBlock = new PyCodeBlock();
                    collectedTopCodeBlock.Statements.AddRange(ConvertRequestedToCode());
                    collectedTopCodeBlock.Statements.AddRange(ConvertDefinedConstToCode());
                    if (TopCode != null)
                    {
                        collectedTopCodeBlock.Statements.AddRange(TopCode.Statements);
                    }
                    nsManager.Add(collectedTopCodeBlock.Statements);
                }
                foreach (var m in Methods)
                {
                    m.Emit(emiter, writer, style);
                }
                {
                    var classesGbNamespace = module.Classes.GroupBy(u => u.Name.Namespace);
                    foreach (var classesInNamespace in classesGbNamespace.OrderBy(i => !i.Key.IsRoot))
                    {
                        foreach (var c in classesInNamespace)
                        {
                            nsManager.Add(c);
                        }
                    }
                }
                if (BottomCode != null)
                {
                    nsManager.Add(BottomCode.Statements);
                }
                if (nsManager.Container.Any())
                {
                    if (nsManager.OnlyOneRootStatement)
                    {
                        foreach (var cl in nsManager.Container[0].Items)
                        {
                            cl.Emit(emiter, writer, style);
                        }
                    }
                    else
                    {
                        foreach (var ns in nsManager.Container)
                        {
                            EmitWithNamespace(ns.Name, emiter, writer, style, ns.Items);
                        }
                    }
                }
            }
        }
Esempio n. 8
0
 public override void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style)
 {
     writer.WriteLn("break;");
 }
Esempio n. 9
0
 public void Emit(PySourceCodeEmiter emiter, PySourceCodeWriter writer, PyEmitStyle style)
 {
     writer.WriteLn(GetPyCode());
 }