상속: IStatementVisitor
예제 #1
0
        public void Xlat(SuiteStatement suite)
        {
            var comments = StatementTranslator.ConvertFirstStringToComments(suite.stmts);

            stmtXlat.Xlat(suite);
            gen.CurrentMemberComments.AddRange(comments);
        }
예제 #2
0
        private string XlatStmts(string pyStmt)
        {
            var rdr = new StringReader(pyStmt);
            var lex = new Syntax.Lexer("foo.py", rdr);
            var flt = new Syntax.CommentFilter(lex);
            var par = new Syntax.Parser("foo.py", flt);
            var stm = par.stmt();
            var gen = new CodeGenerator(new CodeCompileUnit(), "", "module");

            gen.SetCurrentMethod(new CodeMemberMethod());
            var types = new TypeReferenceTranslator(new Dictionary <Node, DataType>());
            var xlt   = new StatementTranslator(types, gen, new SymbolGenerator(), new HashSet <string>());

            stm[0].Accept(xlt);
            var pvd    = new CSharpCodeProvider();
            var writer = new StringWriter();

            foreach (CodeStatement csStmt in gen.Scope)
            {
                pvd.GenerateCodeFromStatement(
                    csStmt,
                    writer,
                    new CodeGeneratorOptions
                {
                });
            }
            return(writer.ToString());
        }
예제 #3
0
        private string XlatModule(string pyModule)
        {
            var rdr = new StringReader(pyModule);
            var lex = new Syntax.Lexer("foo.py", rdr);
            var par = new Syntax.Parser("foo.py", lex);
            var stm = par.stmt();
            var unt = new CodeCompileUnit();
            var gen = new CodeGenerator(unt, "test", "testModule");
            var sym = new SymbolGenerator();
            var xlt = new StatementTranslator(gen, sym);

            stm.Accept(xlt);
            var pvd    = new CSharpCodeProvider();
            var writer = new StringWriter();

            foreach (CodeNamespace ns in unt.Namespaces)
            {
                foreach (CodeNamespaceImport imp in ns.Imports)
                {
                    writer.WriteLine("using {0};", SanitizeNamespace(imp.Namespace, gen));
                }
                foreach (CodeTypeDeclaration type in ns.Types)
                {
                    pvd.GenerateCodeFromType(
                        type,
                        writer,
                        new CodeGeneratorOptions
                    {
                    });
                }
            }
            return(writer.ToString());
        }
예제 #4
0
        private string XlatStmts(string pyStmt)
        {
            var rdr = new StringReader(pyStmt);
            var lex = new Syntax.Lexer("foo.py", rdr);
            var par = new Syntax.Parser("foo.py", lex);
            var stm = par.stmt();
            var gen = new CodeGenerator(new CodeCompileUnit(), "", "module");

            gen.SetCurrentMethod(new CodeMemberMethod());
            var xlt = new StatementTranslator(gen, new SymbolGenerator());

            stm.Accept(xlt);
            var pvd    = new CSharpCodeProvider();
            var writer = new StringWriter();

            foreach (CodeStatement csStmt in gen.Scope)
            {
                pvd.GenerateCodeFromStatement(
                    csStmt,
                    writer,
                    new CodeGeneratorOptions
                {
                });
            }
            return(writer.ToString());
        }
예제 #5
0
        public void VisitClass(ClassDef c)
        {
            if (VisitDecorators(c))
            {
                return;
            }
            var baseClasses = c.args.Select(a => GenerateBaseClassName(a)).ToList();
            var comments    = ConvertFirstStringToComments(c.body.stmts);
            var gensym      = new SymbolGenerator();
            var stmtXlt     = new StatementTranslator(types, gen, gensym, new HashSet <string>());

            stmtXlt.currentClass = c;
            stmtXlt.properties   = FindProperties(c.body.stmts);
            var csClass = gen.Class(
                c.name.Name,
                baseClasses,
                () => GenerateFields(c),
                () => c.body.Accept(stmtXlt));

            csClass.Comments.AddRange(comments);
            if (customAttrs != null)
            {
                csClass.CustomAttributes.AddRange(customAttrs);
                customAttrs = null;
            }
        }
예제 #6
0
        public CodeMemberMethod Generate()
        {
            this.autos = new Dictionary<string, LocalSymbol>();
            this.xlat = new ExpTranslator(gen);
            this.stmtXlat = new StatementTranslator(gen, autos);

            return Generate(CreateFunctionParameters(args)); // () => bodyGenerator(f.body));
        }
예제 #7
0
        public CodeMemberMethod Generate()
        {
            this.gensym   = new SymbolGenerator();
            this.xlat     = new ExpTranslator(gen, gensym);
            this.stmtXlat = new StatementTranslator(gen, gensym);

            return(Generate(CreateFunctionParameters(args))); // () => bodyGenerator(f.body));
        }
예제 #8
0
        public void VisitClass(ClassDef c)
        {
            var baseClasses = c.args.Select(a => GenerateBaseClassName(a)).ToList();
            var comments    = ConvertFirstStringToComments(c.body.stmts);
            var gensym      = new SymbolGenerator();
            var stmtXlt     = new StatementTranslator(gen, gensym);

            stmtXlt.currentClass = c;
            var csClass = gen.Class(c.name.Name, baseClasses, () => c.body.Accept(stmtXlt));

            csClass.Comments.AddRange(comments);
            if (customAttrs != null)
            {
                csClass.CustomAttributes.AddRange(customAttrs);
                customAttrs = null;
            }
        }
예제 #9
0
 public MethodGenerator(
     FunctionDef f,
     string fnName,
     List <Parameter> args,
     bool isStatic,
     bool isAsync,
     TypeReferenceTranslator types,
     CodeGenerator gen)
 {
     this.f        = f;
     this.fnName   = fnName;
     this.args     = args;
     this.isStatic = isStatic;
     this.isAsync  = isAsync;
     this.gen      = gen;
     this.gensym   = new SymbolGenerator();
     this.types    = new TypeReferenceTranslator(new Dictionary <Node, DataType>());
     this.xlat     = new ExpTranslator(this.types, gen, gensym);
     this.globals  = new HashSet <string>();
     this.stmtXlat = new StatementTranslator(types, gen, gensym, globals);
 }
예제 #10
0
        private void XlatConstructor(SuiteStatement stmt)
        {
            if (stmt == null)
            {
                return;
            }

            var comments = StatementTranslator.ConvertFirstStringToComments(stmt.stmts);

            stmt.Accept(this.stmtXlat);
            if (gen.Scope.Count == 0)
            {
                return;
            }
            gen.Scope[0].ToString();
            var expStm = gen.Scope[0] as CodeExpressionStatement;

            if (expStm == null)
            {
                return;
            }
            var appl = expStm.Expression as CodeApplicationExpression;

            if (appl == null)
            {
                return;
            }
            var method = appl.Method as CodeFieldReferenceExpression;

            if (method == null || method.FieldName != "__init__")
            {
                return;
            }
            var ctor = (CodeConstructor)gen.CurrentMember;

            ctor.Comments.AddRange(comments);
            ctor.BaseConstructorArgs.AddRange(appl.Arguments.Skip(1));
            gen.Scope.RemoveAt(0);
        }
예제 #11
0
 public void VisitClass(ClassDef c)
 {
     var baseClasses = c.args.Select(a => GenerateBaseClassName(a)).ToList();
     var comments = ConvertFirstStringToComments(c.body.stmts);
     var stmtXlt = new StatementTranslator(gen, new Dictionary<string, LocalSymbol>());
     stmtXlt.currentClass = c;
     var csClass = gen.Class(c.name.Name, baseClasses, () => c.body.Accept(stmtXlt));
     csClass.Comments.AddRange(comments);
     if (customAttrs != null)
     {
         csClass.CustomAttributes.AddRange(customAttrs);
         customAttrs = null;
     }
 }
예제 #12
0
 private string XlatModule(string pyModule)
 {
     var rdr = new StringReader(pyModule);
     var lex = new Syntax.Lexer("foo.py", rdr);
     var par = new Syntax.Parser("foo.py", lex);
     var stm = par.stmt();
     var unt = new CodeCompileUnit();
     var gen = new CodeGenerator(unt, "test", "testModule");
     var xlt = new StatementTranslator(gen, new Dictionary<string, LocalSymbol>());
     stm.Accept(xlt);
     var pvd = new CSharpCodeProvider();
     var writer = new StringWriter();
     foreach (CodeNamespace ns in unt.Namespaces)
     {
         foreach (CodeNamespaceImport imp in ns.Imports)
         {
             writer.WriteLine("using {0};", SanitizeNamespace(imp.Namespace, gen));
         }
         foreach (CodeTypeDeclaration type in ns.Types)
         {
             pvd.GenerateCodeFromType(
                 type,
                 writer,
             new CodeGeneratorOptions
             {
             });
         }
     }
     return writer.ToString();
 }
예제 #13
0
 private string XlatMember(string pyModule)
 {
     var rdr = new StringReader(pyModule);
     var lex = new Syntax.Lexer("foo.py", rdr);
     var par = new Syntax.Parser("foo.py", lex);
     var stm = par.stmt();
     var unt = new CodeCompileUnit();
     var gen = new CodeGenerator(unt, "test", "testModule");
     var xlt = new StatementTranslator(gen, new Dictionary<string, LocalSymbol>());
     stm.Accept(xlt);
     var pvd = new CSharpCodeProvider();
     var writer = new StringWriter();
     foreach (CodeNamespace ns in unt.Namespaces)
     {
         foreach (var member in ns.Types[0].Members)
         {
             pvd.GenerateCodeFromMember(
                 member, writer,
                 new CodeGeneratorOptions
                 {
                 });
             writer.WriteLine();
         }
     }
     return writer.ToString();
 }
예제 #14
0
 private string XlatStmts(string pyStmt)
 {
     var rdr = new StringReader(pyStmt);
     var lex = new Syntax.Lexer("foo.py", rdr);
     var par = new Syntax.Parser("foo.py", lex);
     var stm = par.stmt();
     var gen = new CodeGenerator(new CodeCompileUnit(), "", "module");
     gen.CurrentMethod = new CodeMemberMethod();
     var xlt = new StatementTranslator(gen, new Dictionary<string, LocalSymbol>());
     stm.Accept(xlt);
     var pvd = new CSharpCodeProvider();
     var writer = new StringWriter();
     foreach (CodeStatement csStmt in gen.Scope)
     {
         pvd.GenerateCodeFromStatement(
             csStmt,
             writer,
             new CodeGeneratorOptions
             {
             });
     }
     return writer.ToString();
 }