2015-12-10 13:13:29 +09:00
|
|
|
|
using System;
|
2015-12-07 13:15:02 +09:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace arookas
|
|
|
|
|
{
|
|
|
|
|
public class sunCompiler
|
|
|
|
|
{
|
2015-12-09 10:11:35 +09:00
|
|
|
|
public sunCompilerResults Compile(string name, Stream output)
|
2015-12-07 13:15:02 +09:00
|
|
|
|
{
|
2015-12-09 10:11:35 +09:00
|
|
|
|
return Compile(name, output, sunImportResolver.Default);
|
2015-12-07 13:15:02 +09:00
|
|
|
|
}
|
2015-12-09 10:11:35 +09:00
|
|
|
|
public sunCompilerResults Compile(string name, Stream output, sunImportResolver resolver)
|
2015-12-07 13:15:02 +09:00
|
|
|
|
{
|
2015-12-09 10:11:35 +09:00
|
|
|
|
if (name == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("name");
|
|
|
|
|
}
|
|
|
|
|
if (output == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("output");
|
|
|
|
|
}
|
|
|
|
|
if (resolver == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("resolver");
|
|
|
|
|
}
|
2015-12-07 13:15:02 +09:00
|
|
|
|
var results = new sunCompilerResults();
|
|
|
|
|
var timer = Stopwatch.StartNew();
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-12-09 10:11:35 +09:00
|
|
|
|
sunContext context = new sunContext(output, resolver);
|
|
|
|
|
var result = context.Import(name);
|
|
|
|
|
if (result != sunImportResult.Loaded)
|
|
|
|
|
{
|
|
|
|
|
throw new sunImportException(name, result);
|
|
|
|
|
}
|
2015-12-10 13:13:29 +09:00
|
|
|
|
context.Text.Terminate(); // NOTETOSELF: don't do this via sunNode.Compile because imported files will add this as well
|
2015-12-07 13:15:02 +09:00
|
|
|
|
foreach (var function in context.SymbolTable.Functions)
|
|
|
|
|
{
|
|
|
|
|
function.Compile(context);
|
|
|
|
|
}
|
|
|
|
|
foreach (var function in context.SymbolTable.Functions)
|
|
|
|
|
{
|
|
|
|
|
function.CloseCallSites(context);
|
|
|
|
|
}
|
2015-12-11 07:16:56 +09:00
|
|
|
|
results.DataCount = context.DataTable.Count;
|
2015-12-07 13:15:02 +09:00
|
|
|
|
results.SymbolCount = context.SymbolTable.Count;
|
|
|
|
|
results.BuiltinCount = context.SymbolTable.BuiltinCount;
|
|
|
|
|
results.FunctionCount = context.SymbolTable.FunctionCount;
|
|
|
|
|
results.VariableCount = context.SymbolTable.VariableCount;
|
|
|
|
|
context.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (sunCompilerException ex)
|
|
|
|
|
{
|
|
|
|
|
results.Error = ex;
|
|
|
|
|
}
|
|
|
|
|
timer.Stop();
|
|
|
|
|
results.CompileTime = timer.Elapsed;
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class sunCompilerResults
|
|
|
|
|
{
|
|
|
|
|
// success
|
|
|
|
|
public bool Success { get { return Error == null; } }
|
|
|
|
|
public sunCompilerException Error { get; internal set; }
|
|
|
|
|
|
|
|
|
|
// statistics
|
2015-12-11 07:16:56 +09:00
|
|
|
|
public int DataCount { get; internal set; }
|
2015-12-07 13:15:02 +09:00
|
|
|
|
public int SymbolCount { get; internal set; }
|
|
|
|
|
public int BuiltinCount { get; internal set; }
|
|
|
|
|
public int FunctionCount { get; internal set; }
|
|
|
|
|
public int VariableCount { get; internal set; }
|
|
|
|
|
|
|
|
|
|
public TimeSpan CompileTime { get; internal set; }
|
|
|
|
|
}
|
|
|
|
|
}
|