Skip to content

Commit

Permalink
Various bugfixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
uxmal committed Jul 23, 2015
1 parent 1d4e507 commit 31bbc4f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/DirectoryWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pytocs.TypeInference;

namespace Pytocs
{
Expand Down Expand Up @@ -70,7 +71,10 @@ public void ProcessDirectoryFiles(EnumerationState state)
{
foreach (var file in Directory.GetFiles(state.DirectoryName, "*.py", SearchOption.TopDirectoryOnly))
{
var xlator = new Translator(state.Namespace, Path.GetFileNameWithoutExtension(file));
var xlator = new Translator(
state.Namespace,
Path.GetFileNameWithoutExtension(file),
new ConsoleLogger());
xlator.TranslateFile(file, file + ".cs");
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pytocs.TypeInference;

namespace Pytocs
{
Expand All @@ -14,7 +15,8 @@ static void Main(string[] args)
{
if (args.Length == 0)
{
var xlator = new Translator("", "module_name");
var xlator = new Translator("", "module_name",
new ConsoleLogger());
xlator.Translate("-", Console.In, Console.Out);
Console.Out.Flush();
return;
Expand All @@ -29,15 +31,17 @@ static void Main(string[] args)
if (args[0].ToLower() == "-r")
{
var walker = new DirectoryWalker("*.py");
var symtab = new SymbolTable();
walker.Enumerate();
}
else
{
var symtab = new SymbolTable();
foreach (var fileName in args)
{
//ProcessFile(fileName, symtab);
var xlator = new Translator(
"",
Path.GetFileNameWithoutExtension(fileName),
new ConsoleLogger());
xlator.TranslateFile(fileName, fileName + ".cs");
}
}
}
Expand Down
30 changes: 23 additions & 7 deletions src/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Pytocs.TypeInference;

namespace Pytocs
{
public class Translator
{
private string nmspace;
private string moduleName;
private ILogger logger;

public Translator(string nmspace, string moduleName)
public Translator(string nmspace, string moduleName, ILogger logger)
{
this.nmspace = nmspace;
this.moduleName = moduleName;
this.logger = logger;
}

public void Translate(string filename, TextReader input, TextWriter output)
Expand All @@ -42,14 +45,27 @@ public void TranslateFile(string inputFileName, string outputFileName)
TextWriter writer = null;
try
{
reader = new StreamReader(inputFileName);
writer = new StreamWriter(new FileStream(outputFileName, FileMode.Create, FileAccess.Write), new UTF8Encoding(false));
try
{
reader = new StreamReader(inputFileName);
}
catch (IOException ex)
{
logger.Error(ex, "Unable to open file {0} for reading.", inputFileName);
return;
}
try
{
writer = new StreamWriter(new FileStream(outputFileName, FileMode.Create, FileAccess.Write), new UTF8Encoding(false));
}
catch (IOException ex)
{
logger.Error(ex, "Unable to open file {0} for writing.", outputFileName);
return;
}
Translate(inputFileName, reader, writer);
}
catch (Exception)
{
// Diagnostic.
}

finally
{
if (writer != null) writer.Dispose();
Expand Down
44 changes: 41 additions & 3 deletions src/TypeInference/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,44 @@ public interface ILogger
{
TraceLevel Level { get; set; }

void Error(Exception ex, string p);
void Error(Exception ex, string format, params object [] args);
void Error(string format, params object [] args);
void Inform(string p);
void Verbose(string p);
}

public class ConsoleLogger : ILogger{

public void Error(Exception ex, string format, params object[] args)
{
Console.Error.Write("Error: ");
Console.Error.WriteLine(format, args);
while (ex != null)
{
Console.Error.WriteLine(" {0}", ex.Message);
ex = ex.InnerException;
}
}

public void Error(string format, params object[] args)
{
Console.Error.Write("Error: ");
Console.Error.WriteLine(format, args);
}

public void Inform(string p)
{
throw new NotImplementedException();
}

public void Verbose(string p)
{
throw new NotImplementedException();
}

public TraceLevel Level { get; set; }
}

public class Logger : ILogger
{
private string title;
Expand All @@ -27,10 +60,15 @@ public Logger(string title)

public TraceLevel Level { get; set; }

public void Error(Exception ex, string msg)
public void Error(string format, params object[] args)
{
Write(EventLogEntryType.Error, string.Format(format, args));
}

public void Error(Exception ex, string format, params object[] args)
{
var sb = new StringBuilder();
sb.AppendLine(msg);
sb.AppendFormat(format, args);
while (ex != null)
{
sb.Append(" ");
Expand Down
1 change: 1 addition & 0 deletions src/pytocs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Commandlineparameters>foo.py</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down

0 comments on commit 31bbc4f

Please sign in to comment.