Skip to content

Commit 232446f

Browse files
author
aafent
committed
Initial support of Asynchronous execution
1 parent 2540bf3 commit 232446f

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

FAST.FBasicInterpreter/Core/Interpreter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public partial class Interpreter : IFBasicError, IInterpreter
7777
/// </summary>
7878
public Value Result { get; set; }
7979

80+
/// <summary>
81+
/// True if runs in Asynchronous mode (Exec or ExecAsync)
82+
/// </summary>
83+
public bool AsynchronousMode { get; private set; }
84+
8085
/// <summary>
8186
/// The GetNextToken handler, no need to be defined as the the interpreter uses the default built-in.
8287
/// </summary>

FAST.FBasicInterpreter/Core/Interpreter_Extensions.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,90 @@ public static ExecutionResult ExecWithResult(this Interpreter interpreter,bool c
108108
return result;
109109
}
110110

111+
/// <summary>
112+
/// Execute the program and return the results Asynchronous
113+
/// </summary>
114+
/// <param name="interpreter">The interpreter</param>
115+
/// <param name="copyOfTheVariables">Optional,if true return a copy of the variables otherwise a reference to them</param>
116+
/// <returns>the results</returns>
117+
public async static Task<ExecutionResult> ExecWithResultAsync(this Interpreter interpreter, bool copyOfTheVariables = false)
118+
{
119+
bool exception = false;
120+
ExecutionResult result = new();
121+
try
122+
{
123+
result.programStartWhen = DateTime.Now;
124+
await interpreter.ExecAsync();
125+
result.programEndWhen = DateTime.Now;
126+
result.hasError = false;
127+
result.value = interpreter.Result;
128+
if (copyOfTheVariables) // (v) copy of the variables
129+
{
130+
var variables = interpreter.GetVariables();
131+
result.variables = new();
132+
foreach (var item in variables)
133+
{
134+
result.variables.Add(item.Key, new Value(item.Value));
135+
}
136+
}
137+
else
138+
{
139+
result.variables = interpreter.GetVariables(); // Reference to the variables
140+
}
141+
}
142+
catch (FBasicException e)
143+
{
144+
result.hasError = true;
145+
result.errorText = e.ToString();
146+
result.lineOfError = e.line;
147+
result.errorSourceLine = e.sourceLine;
148+
#if DEBUG
149+
result.exception = e;
150+
#endif
151+
exception = true;
152+
}
153+
catch (IndexOutOfRangeException e1)
154+
{
155+
result.hasError = true;
156+
result.errorText = Errors.E130_OutOfRange(interpreter.lex.Identifier);
157+
result.lineOfError = interpreter.lex.CurrentSourceMarker.Line;
158+
result.errorSourceLine = interpreter.lex.GetLine(result.lineOfError);
159+
#if DEBUG
160+
result.exception = e1;
161+
#endif
162+
exception = true;
163+
}
164+
catch (Exception ex)
165+
{
166+
result.hasError = true;
167+
result.lineOfError = interpreter.lex.CurrentSourceMarker.Line;
168+
result.errorText = Errors.X012_GeneralException("", ex, $" Near line {result.lineOfError}");
169+
result.errorSourceLine = interpreter.lex.GetLine(result.lineOfError);
170+
#if DEBUG
171+
result.exception = ex;
172+
#endif
173+
exception = true;
174+
}
175+
finally
176+
{
177+
if (exception) result.programEndWhen = DateTime.Now;
178+
foreach (var lib in interpreter.librariesWithMemory)
179+
{
180+
if (lib.Value is IDisposable)
181+
{
182+
try // do not produce errors from Dispose
183+
{
184+
((IDisposable)lib.Value).Dispose();
185+
}
186+
catch
187+
{
188+
}
189+
}
190+
}
191+
}
192+
return result;
193+
}
194+
111195

112196
/// <summary>
113197
/// Get a value of any name (variable, collection item)

FAST.FBasicInterpreter/Core/Interpreter_Methods.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,25 @@ public void Exec()
491491
while (!exit) Line(); // do all lines
492492
}
493493

494+
/// <summary>
495+
/// Execute the program asynchronous
496+
/// </summary>
497+
public async Task ExecAsync()
498+
{
499+
if (this.librariesWithMemory != null)
500+
{
501+
foreach (var lib in this.librariesWithMemory.Values)
502+
{
503+
lib.PrepareToExecute();
504+
}
505+
}
506+
RefreshLexerStatements();
507+
GetNextToken = interpreter_GetNextToken;
508+
exit = false;
509+
GetNextToken();
510+
while (!exit) Line(); // do all lines
511+
}
512+
494513

495514
/// <summary>
496515
/// Evaluate (Run) a code

0 commit comments

Comments
 (0)