Added an open/close interface to the context class.

This commit is contained in:
arookas 2015-12-23 20:03:14 -05:00
parent 44eab8e9e6
commit cde799eadd
2 changed files with 24 additions and 26 deletions

View file

@ -24,11 +24,12 @@ namespace arookas
{ {
throw new ArgumentNullException("resolver"); throw new ArgumentNullException("resolver");
} }
var context = new sunContext();
var results = new sunCompilerResults(); var results = new sunCompilerResults();
var timer = Stopwatch.StartNew(); var timer = Stopwatch.StartNew();
try try
{ {
sunContext context = new sunContext(output, resolver); context.Open(output, resolver);
var result = context.Import(name); var result = context.Import(name);
if (result != sunImportResult.Loaded) if (result != sunImportResult.Loaded)
{ {
@ -48,7 +49,7 @@ namespace arookas
results.BuiltinCount = context.SymbolTable.BuiltinCount; results.BuiltinCount = context.SymbolTable.BuiltinCount;
results.FunctionCount = context.SymbolTable.FunctionCount; results.FunctionCount = context.SymbolTable.FunctionCount;
results.VariableCount = context.SymbolTable.VariableCount; results.VariableCount = context.SymbolTable.VariableCount;
context.Dispose(); context.Close();
} }
catch (sunCompilerException ex) catch (sunCompilerException ex)
{ {

View file

@ -6,7 +6,7 @@ using System.Text;
namespace arookas namespace arookas
{ {
class sunContext : aDisposable class sunContext
{ {
aBinaryWriter writer; aBinaryWriter writer;
uint textOffset, dataOffset, symbolOffset; uint textOffset, dataOffset, symbolOffset;
@ -19,13 +19,20 @@ namespace arookas
public sunLoopStack Loops { get; private set; } public sunLoopStack Loops { get; private set; }
public sunImportResolver ImportResolver { get; private set; } public sunImportResolver ImportResolver { get; private set; }
// open/close public sunContext()
public sunContext(Stream output)
: this(output, sunImportResolver.Default)
{ {
DataTable = new sunDataTable();
SymbolTable = new sunSymbolTable();
Scopes = new sunScopeStack();
Loops = new sunLoopStack();
} }
public sunContext(Stream output, sunImportResolver importResolver)
// open/close
public void Open(Stream output)
{
Open(output, sunImportResolver.Default);
}
public void Open(Stream output, sunImportResolver importResolver)
{ {
if (output == null) if (output == null)
{ {
@ -35,12 +42,7 @@ namespace arookas
{ {
throw new ArgumentNullException("importResolver"); throw new ArgumentNullException("importResolver");
} }
DataTable = new sunDataTable();
SymbolTable = new sunSymbolTable();
Scopes = new sunScopeStack();
Loops = new sunLoopStack();
ImportResolver = importResolver; ImportResolver = importResolver;
writer = new aBinaryWriter(output, Endianness.Big, Encoding.GetEncoding(932)); writer = new aBinaryWriter(output, Endianness.Big, Encoding.GetEncoding(932));
Text = new sunWriter(writer); Text = new sunWriter(writer);
writer.PushAnchor(); writer.PushAnchor();
@ -62,20 +64,15 @@ namespace arookas
DeclareSystemBuiltin("typeof", false, "x"); DeclareSystemBuiltin("typeof", false, "x");
DeclareSystemBuiltin("print", true); DeclareSystemBuiltin("print", true);
} }
protected override bool Dispose(bool destructor) public void Close()
{ {
if (!destructor) writer.PopAnchor();
{ dataOffset = (uint)writer.Position;
writer.PopAnchor(); DataTable.Write(writer);
dataOffset = (uint)writer.Position; symbolOffset = (uint)writer.Position;
DataTable.Write(writer); SymbolTable.Write(writer);
symbolOffset = (uint)writer.Position; writer.Goto(0);
SymbolTable.Write(writer); WriteHeader();
writer.Goto(0);
WriteHeader();
return true; // don't dispose the writer so the stream doesn't get disposed
}
return false;
} }
// imports/compilation // imports/compilation