Ejemplo n.º 1
0
 public void Translate(string filename, TextReader input, TextWriter output)
 {
     Debug.Print("Translating module {0} in namespace {1}", moduleName, nmspace);
     var lex = new Lexer(filename, input);
     var par = new Parser(filename, lex);
     var stm = par.Parse();
     var unt = new CodeCompileUnit();
     var gen = new CodeGenerator(unt, nmspace, Path.GetFileNameWithoutExtension(moduleName));
     var xlt = new ModuleTranslator(gen);
     xlt.Translate(stm);
     var pvd = new CSharpCodeProvider();
     pvd.GenerateCodeFromCompileUnit(unt, output, new CodeGeneratorOptions { });
 }
Ejemplo n.º 2
0
 public CodeGenerator(CodeCompileUnit unt, string modulePath, string moduleName)
 {
     this.unt = unt;
     this.provider = new CSharpCodeProvider();
     this.Scope = new List<CodeStatement>();  // dummy scope.
     this.CurrentNamespace = new CodeNamespace(modulePath);
     this.CurrentType = new CodeTypeDeclaration(moduleName)
     {
         IsClass = true,
         Attributes = MemberAttributes.Static | MemberAttributes.Public
     };
     CurrentNamespace.Types.Add(CurrentType);
     unt.Namespaces.Add(CurrentNamespace);
 }
Ejemplo n.º 3
0
 public void Write(CodeCompileUnit unit)
 {
     foreach (var n in unit.Namespaces)
     {
         foreach (var comment in n.Comments)
         {
             writer.Write("//");
             writer.Write(comment.Comment);
             writer.WriteLine();
         }
         if (!string.IsNullOrEmpty(n.Name))
         {
             writer.Write("namespace");
             writer.WriteName(" ");
             writer.WriteDottedName(n.Name);
             writer.WriteLine(" {");
             ++writer.IndentLevel;
         }
         foreach (var imp in n.Imports)
         {
             writer.WriteLine();
             writer.Write("using");
             writer.Write(" ");
             writer.WriteDottedName(imp.Namespace);
             writer.WriteLine(";");
         }
         foreach (var type in n.Types)
         {
             writer.WriteLine();
             var tw = new CSharpTypeWriter(type, writer);
             type.Accept(tw);
         }
         if (!string.IsNullOrEmpty(n.Name))
         {
             --writer.IndentLevel;
             writer.WriteLine("}");
         }
     }
 }
Ejemplo n.º 4
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.Parse(); ;
            var unt = new CodeCompileUnit();
            var gen = new CodeGenerator(unt, "test", "testModule");
            var xlt = new ModuleTranslator(gen);
            xlt.Translate(stm);

            var pvd = new CSharpCodeProvider();
            var writer = new StringWriter();
            pvd.GenerateCodeFromCompileUnit(
                unt,
                writer,
            new CodeGeneratorOptions
            {
            });
            return writer.ToString();
        }
Ejemplo n.º 5
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();
 }
Ejemplo n.º 6
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();
 }