BadScript 2
Loading...
Searching...
No Matches
BadScript2.Interactive.BadInteractiveConsole Class Reference

Implements an Interactive Console for the BadScript Language. More...

Public Member Functions

 BadInteractiveConsole (BadRuntime runtime, BadTaskRunner runner, IEnumerable< string > files)
 Constructs a new BadInteractiveConsole instance.
 
void Reset ()
 Resets the Current Context.
 
BadObject LoadIsolated (string file)
 Loads a File isolated from the Interactive Session.
 
void Load (string file)
 Loads a File into the Interactive Session.
 
IEnumerable< object?> RunIsolatedRoutine (string code)
 Runs a set of Expressions isolated from the Interactive Session.
 
BadObject RunIsolated (string code)
 Runs a set of Expressions isolated from the Interactive Session.
 
void Run (string code)
 Runs a set of Expressions.
 

Properties

BadScopeCurrentScope [get]
 The Current Scope of the Interactive Console.
 
bool CatchErrors [get, set]
 If true, the Interactive Console will catch and print errors.
 
bool PreParse [get, set]
 If true, the Interactive Console will pre-parse the input before executing it.
 

Private Member Functions

BadExecutionContext CreateContext ()
 Creates a new Execution Context with the Interactive Console Api.
 
IEnumerable< object?> RunRoutine (IEnumerable< BadExpression > expressions)
 The Routine that is used to execute the Interactive Session.
 
void Run (IEnumerable< BadExpression > expressions)
 Runs a set of Expressions.
 

Private Attributes

readonly BadInteractiveConsoleApi m_Api
 The Interactive API.
 
readonly BadTaskRunner m_Runner
 The Task runner.
 
readonly BadRuntime m_Runtime
 The Execution Context Options.
 
BadExecutionContextm_Context
 The Execution Context.
 

Detailed Description

Implements an Interactive Console for the BadScript Language.

Definition at line 22 of file BadInteractiveConsole.cs.

Constructor & Destructor Documentation

◆ BadInteractiveConsole()

BadScript2.Interactive.BadInteractiveConsole.BadInteractiveConsole ( BadRuntime  runtime,
BadTaskRunner  runner,
IEnumerable< string >  files 
)

Constructs a new BadInteractiveConsole instance.

Parameters
runtimeThe Runtime that the Interactive Console will be started from.
runnerThe Task runner
filesThe Files that are loaded before the interactive session begins

Definition at line 50 of file BadInteractiveConsole.cs.

51 {
52 m_Runner = runner;
53 m_Api = new BadInteractiveConsoleApi(this);
54 m_Runtime = runtime;
55 Reset();
56
57 foreach (string file in files)
58 {
59 LoadIsolated(file);
60 }
61 }
readonly BadTaskRunner m_Runner
The Task runner.
readonly BadInteractiveConsoleApi m_Api
The Interactive API.
BadObject LoadIsolated(string file)
Loads a File isolated from the Interactive Session.
readonly BadRuntime m_Runtime
The Execution Context Options.

Member Function Documentation

◆ CreateContext()

BadExecutionContext BadScript2.Interactive.BadInteractiveConsole.CreateContext ( )
private

Creates a new Execution Context with the Interactive Console Api.

Returns
Execution Context

Definition at line 82 of file BadInteractiveConsole.cs.

83 {
85 BadTable apiTable = new BadTable();
86 m_Api.Load(ctx, apiTable);
87
88 ctx.Scope.DefineVariable(m_Api.Name, apiTable);
89
90 return ctx;
91 }
BadExecutionContext CreateContext(string workingDirectory)
Creates a new Context with the configured Options.
Public interface for the filesystem abstraction of the BadScript Engine.
static IFileSystem Instance
File System implementation.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
void DefineVariable(string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
Defines a new Variable in the current scope.
Definition BadScope.cs:929
void Load(BadExecutionContext ctx, BadTable table)
Loads the API into the given Table.
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14
string GetCurrentDirectory()
Returns the Current Directory.

◆ Load()

void BadScript2.Interactive.BadInteractiveConsole.Load ( string  file)

Loads a File into the Interactive Session.

Parameters
fileThe File to be Loaded

Definition at line 129 of file BadInteractiveConsole.cs.

130 {
132 Run(parser.Parse());
133 }
static string ReadAllText(this IFileSystem fileSystem, string path)
void Run(IEnumerable< BadExpression > expressions)
Runs a set of Expressions.
The Parser of the Language. It turns Source Code into an Expression Tree.
static BadSourceParser Create(string fileName, string source)
Creates a BadSourceParser Instance based on the source and filename provided.
static IEnumerable< BadExpression > Parse(string fileName, string source)
Parses a BadExpression from the Source Reader.

◆ LoadIsolated()

BadObject BadScript2.Interactive.BadInteractiveConsole.LoadIsolated ( string  file)

Loads a File isolated from the Interactive Session.

Parameters
fileThe File to be Executed
Returns
Result of the Execution
Exceptions
BadRuntimeExceptionGets raised if the context was not initialized

Definition at line 108 of file BadInteractiveConsole.cs.

109 {
110 if (m_Context == null)
111 {
112 throw new BadRuntimeException("Context is not initialized");
113 }
114
115 Reset();
118 Run(parser.Parse());
119
120 Reset();
121
122 return current.Scope.ReturnValue ?? current.Scope.GetTable();
123 }
BadExecutionContext? m_Context
The Execution Context.
BadTable GetTable()
Returns the Variable Table of the current scope.
Definition BadScope.cs:778

◆ Reset()

void BadScript2.Interactive.BadInteractiveConsole.Reset ( )

Resets the Current Context.

Definition at line 97 of file BadInteractiveConsole.cs.

98 {
100 }
BadExecutionContext CreateContext()
Creates a new Execution Context with the Interactive Console Api.

◆ Run() [1/2]

void BadScript2.Interactive.BadInteractiveConsole.Run ( IEnumerable< BadExpression expressions)
private

Runs a set of Expressions.

Parameters
expressionsThe Expressions to be Executed
Exceptions
BadRuntimeExceptionGets raised if the context was not initialized

Definition at line 182 of file BadInteractiveConsole.cs.

183 {
184 IEnumerable<BadExpression> exprs = expressions;
185
186 if (PreParse)
187 {
188 exprs = exprs.ToArray();
189 }
190
191 if (BadNativeOptimizationSettings.Instance.UseConstantFoldingOptimization)
192 {
194 }
195
196 if (m_Context == null)
197 {
198 throw new BadRuntimeException("Context is not initialized");
199 }
200
201
202 m_Runner.AddTask(new BadTask(BadRunnable.Create(m_Context.Execute(exprs)), "Main"), true);
203
204 if (CatchErrors)
205 {
206 try
207 {
208 while (!m_Runner.IsIdle)
209 {
211 }
212 }
213 catch (Exception e)
214 {
215 BadConsole.WriteLine(e.Message);
216 }
217 }
218 else
219 {
220 while (!m_Runner.IsIdle)
221 {
223 }
224 }
225
226 }
Wrapper class for the console abstraction.
Definition BadConsole.cs:12
static void WriteLine(string str)
Writes a string to the console and appends a newline.
Definition BadConsole.cs:76
bool CatchErrors
If true, the Interactive Console will catch and print errors.
bool PreParse
If true, the Interactive Console will pre-parse the input before executing it.
Implements a Runnable Object.
static BadRunnable Create(IEnumerable< BadObject > e)
Creates a Runnable from an Enumeration.
Implements a Task Object.
Definition BadTask.cs:17
void RunStep()
Runs a single step of the Task Runner.
void AddTask(BadTask task, bool runImmediately=false)
Adds a Task to the Task Runner.
bool IsIdle
Is true if there are no tasks to run.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
IEnumerable< BadObject > Execute(IEnumerable< BadExpression > expressions)
Executes an enumeration of expressions.
static T Instance
Returns the Instance of the Settings Provider.

◆ Run() [2/2]

void BadScript2.Interactive.BadInteractiveConsole.Run ( string  code)

Runs a set of Expressions.

Parameters
codeThe Source Code

Definition at line 280 of file BadInteractiveConsole.cs.

281 {
282 BadSourceParser parser = BadSourceParser.Create("<stdin>", code);
283 Run(parser.Parse());
284 }

◆ RunIsolated()

BadObject BadScript2.Interactive.BadInteractiveConsole.RunIsolated ( string  code)

Runs a set of Expressions isolated from the Interactive Session.

Parameters
codeThe Source Code
Returns
The Result of the Execution
Exceptions
BadRuntimeExceptionGets raised if the context was not initialized

Definition at line 259 of file BadInteractiveConsole.cs.

260 {
261 if (m_Context == null)
262 {
263 throw new BadRuntimeException("Context is not initialized");
264 }
265
267 Reset();
269 BadSourceParser parser = BadSourceParser.Create("<stdin>", code);
270 Run(parser.Parse());
271 m_Context = ctx;
272
273 return current.Scope.ReturnValue ?? current.Scope.GetTable();
274 }

◆ RunIsolatedRoutine()

IEnumerable< object?> BadScript2.Interactive.BadInteractiveConsole.RunIsolatedRoutine ( string  code)

Runs a set of Expressions isolated from the Interactive Session.

Parameters
codeThe Source Code
Returns
The Results of the Execution
Exceptions
BadRuntimeExceptionGets raised if the context was not initialized

Definition at line 234 of file BadInteractiveConsole.cs.

235 {
236 if (m_Context == null)
237 {
238 throw new BadRuntimeException("Context is not initialized");
239 }
240
242 Reset();
243 BadSourceParser parser = BadSourceParser.Create("<stdin>", code);
244
245 foreach (object? o in RunRoutine(parser.Parse()))
246 {
247 yield return o;
248 }
249
250 m_Context = ctx;
251 }
IEnumerable< object?> RunRoutine(IEnumerable< BadExpression > expressions)
The Routine that is used to execute the Interactive Session.

◆ RunRoutine()

IEnumerable< object?> BadScript2.Interactive.BadInteractiveConsole.RunRoutine ( IEnumerable< BadExpression expressions)
private

The Routine that is used to execute the Interactive Session.

Parameters
expressionsThe Expressions to be executed
Returns
Enumeration of objects
Exceptions
BadRuntimeExceptionGets raised if the context was not initialized

Definition at line 141 of file BadInteractiveConsole.cs.

142 {
143 IEnumerable<BadExpression> exprs = expressions;
144
145 if (PreParse)
146 {
147 exprs = exprs.ToArray();
148 }
149
150 if (BadNativeOptimizationSettings.Instance.UseConstantFoldingOptimization)
151 {
153 }
154
155 if (BadNativeOptimizationSettings.Instance.UseConstantSubstitutionOptimization)
156 {
158 }
159
160 if (m_Context == null)
161 {
162 throw new BadRuntimeException("Context is not initialized");
163 }
164
165
166 m_Runner.AddTask(new BadTask(BadRunnable.Create(m_Context.Execute(exprs)), "Main"), true);
167
168 while (!m_Runner.IsIdle)
169 {
171
172 yield return null;
173 }
174
175 }
Contains the Implementation of the Constant Substitution Optimization This optimization replaces expr...
static IEnumerable< BadExpression > Optimize(BadConstantSubstitutionOptimizerScope scope, IEnumerable< BadExpression > expressions)
Substitutes all variables in the expressions with their constant value.

Member Data Documentation

◆ m_Api

readonly BadInteractiveConsoleApi BadScript2.Interactive.BadInteractiveConsole.m_Api
private

The Interactive API.

Definition at line 27 of file BadInteractiveConsole.cs.

◆ m_Context

BadExecutionContext? BadScript2.Interactive.BadInteractiveConsole.m_Context
private

The Execution Context.

Definition at line 42 of file BadInteractiveConsole.cs.

◆ m_Runner

readonly BadTaskRunner BadScript2.Interactive.BadInteractiveConsole.m_Runner
private

The Task runner.

Definition at line 32 of file BadInteractiveConsole.cs.

◆ m_Runtime

readonly BadRuntime BadScript2.Interactive.BadInteractiveConsole.m_Runtime
private

The Execution Context Options.

Definition at line 37 of file BadInteractiveConsole.cs.

Property Documentation

◆ CatchErrors

bool BadScript2.Interactive.BadInteractiveConsole.CatchErrors
getset

If true, the Interactive Console will catch and print errors.

Definition at line 71 of file BadInteractiveConsole.cs.

71{ get; set; }

◆ CurrentScope

BadScope? BadScript2.Interactive.BadInteractiveConsole.CurrentScope
get

The Current Scope of the Interactive Console.

Definition at line 66 of file BadInteractiveConsole.cs.

◆ PreParse

bool BadScript2.Interactive.BadInteractiveConsole.PreParse
getset

If true, the Interactive Console will pre-parse the input before executing it.

Definition at line 76 of file BadInteractiveConsole.cs.

76{ get; set; }

The documentation for this class was generated from the following file: