From cde799eaddff9565917ee5d2130fa92730fa7d6e Mon Sep 17 00:00:00 2001 From: arookas Date: Wed, 23 Dec 2015 20:03:14 -0500 Subject: [PATCH] Added an open/close interface to the context class. --- ssc/compiler.cs | 5 +++-- ssc/context.cs | 45 +++++++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/ssc/compiler.cs b/ssc/compiler.cs index 0f12483..c24527a 100644 --- a/ssc/compiler.cs +++ b/ssc/compiler.cs @@ -24,11 +24,12 @@ namespace arookas { throw new ArgumentNullException("resolver"); } + var context = new sunContext(); var results = new sunCompilerResults(); var timer = Stopwatch.StartNew(); try { - sunContext context = new sunContext(output, resolver); + context.Open(output, resolver); var result = context.Import(name); if (result != sunImportResult.Loaded) { @@ -48,7 +49,7 @@ namespace arookas results.BuiltinCount = context.SymbolTable.BuiltinCount; results.FunctionCount = context.SymbolTable.FunctionCount; results.VariableCount = context.SymbolTable.VariableCount; - context.Dispose(); + context.Close(); } catch (sunCompilerException ex) { diff --git a/ssc/context.cs b/ssc/context.cs index fe44cad..fa6d884 100644 --- a/ssc/context.cs +++ b/ssc/context.cs @@ -6,7 +6,7 @@ using System.Text; namespace arookas { - class sunContext : aDisposable + class sunContext { aBinaryWriter writer; uint textOffset, dataOffset, symbolOffset; @@ -19,13 +19,20 @@ namespace arookas public sunLoopStack Loops { get; private set; } public sunImportResolver ImportResolver { get; private set; } - // open/close - public sunContext(Stream output) - : this(output, sunImportResolver.Default) + public sunContext() { - + 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) { @@ -35,12 +42,7 @@ namespace arookas { throw new ArgumentNullException("importResolver"); } - DataTable = new sunDataTable(); - SymbolTable = new sunSymbolTable(); - Scopes = new sunScopeStack(); - Loops = new sunLoopStack(); ImportResolver = importResolver; - writer = new aBinaryWriter(output, Endianness.Big, Encoding.GetEncoding(932)); Text = new sunWriter(writer); writer.PushAnchor(); @@ -62,20 +64,15 @@ namespace arookas DeclareSystemBuiltin("typeof", false, "x"); DeclareSystemBuiltin("print", true); } - protected override bool Dispose(bool destructor) + public void Close() { - if (!destructor) - { - writer.PopAnchor(); - dataOffset = (uint)writer.Position; - DataTable.Write(writer); - symbolOffset = (uint)writer.Position; - SymbolTable.Write(writer); - writer.Goto(0); - WriteHeader(); - return true; // don't dispose the writer so the stream doesn't get disposed - } - return false; + writer.PopAnchor(); + dataOffset = (uint)writer.Position; + DataTable.Write(writer); + symbolOffset = (uint)writer.Position; + SymbolTable.Write(writer); + writer.Goto(0); + WriteHeader(); } // imports/compilation