BadScript 2
Loading...
Searching...
No Matches
BadInteractiveConsole.cs
Go to the documentation of this file.
3using BadScript2.IO;
12
13
18
23{
28
32 private readonly BadTaskRunner m_Runner;
33
37 private readonly BadRuntime m_Runtime;
38
43
50 public BadInteractiveConsole(BadRuntime runtime, BadTaskRunner runner, IEnumerable<string> files)
51 {
52 m_Runner = runner;
54 m_Runtime = runtime;
55 Reset();
56
57 foreach (string file in files)
58 {
59 LoadIsolated(file);
60 }
61 }
62
67
71 public bool CatchErrors { get; set; }
72
76 public bool PreParse { get; set; }
77
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 }
92
93
97 public void Reset()
98 {
100 }
101
108 public BadObject LoadIsolated(string file)
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 }
124
129 public void Load(string file)
130 {
132 Run(parser.Parse());
133 }
134
141 private IEnumerable<object?> RunRoutine(IEnumerable<BadExpression> expressions)
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 }
176
182 private void Run(IEnumerable<BadExpression> expressions)
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 }
227
234 public IEnumerable<object?> RunIsolatedRoutine(string code)
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 }
252
259 public BadObject RunIsolated(string code)
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 }
275
280 public void Run(string code)
281 {
282 BadSourceParser parser = BadSourceParser.Create("<stdin>", code);
283 Run(parser.Parse());
284 }
285}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
BadExecutionContext CreateContext(string workingDirectory)
Creates a new Context with the configured Options.
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
Public interface for the filesystem abstraction of the BadScript Engine.
static string ReadAllText(this IFileSystem fileSystem, string path)
static IFileSystem Instance
File System implementation.
Implements an Interactive Console for the BadScript Language.
BadExecutionContext? m_Context
The Execution Context.
readonly BadTaskRunner m_Runner
The Task runner.
void Load(string file)
Loads a File into the Interactive Session.
bool CatchErrors
If true, the Interactive Console will catch and print errors.
BadObject RunIsolated(string code)
Runs a set of Expressions isolated from the Interactive Session.
void Run(IEnumerable< BadExpression > expressions)
Runs a set of Expressions.
BadExecutionContext CreateContext()
Creates a new Execution Context with the Interactive Console Api.
BadScope? CurrentScope
The Current Scope of the Interactive Console.
IEnumerable< object?> RunRoutine(IEnumerable< BadExpression > expressions)
The Routine that is used to execute the Interactive Session.
IEnumerable< object?> RunIsolatedRoutine(string code)
Runs a set of Expressions isolated from the Interactive Session.
BadInteractiveConsole(BadRuntime runtime, BadTaskRunner runner, IEnumerable< string > files)
Constructs a new BadInteractiveConsole instance.
readonly BadInteractiveConsoleApi m_Api
The Interactive API.
void Run(string code)
Runs a set of Expressions.
BadObject LoadIsolated(string file)
Loads a File isolated from the Interactive Session.
readonly BadRuntime m_Runtime
The Execution Context Options.
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.
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.
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.
The Execution Context. Every execution of a script needs a context the script is running in....
IEnumerable< BadObject > Execute(IEnumerable< BadExpression > expressions)
Executes an enumeration of expressions.
BadScope Scope
The Root Scope of the Context.
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
BadTable GetTable()
Returns the Variable Table of the current scope.
Definition BadScope.cs:778
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.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14
static T Instance
Returns the Instance of the Settings Provider.
string GetCurrentDirectory()
Returns the Current Directory.
Contains a Console Abstraction Layer to be able to Simulate Console Input/Output over the Network.
Definition BadConsole.cs:6
Contains IO Implementation for the BadScript2 Runtime.
Contains the interactive console Implementation.
Contains task/async Extensions and Integrations for the BadScript2 Runtime.
Contains the BadScript2 Constant Folding Optimizations.
Contains the BadScript2 Constant Substitution Optimizations.
Contains the Expressions for the BadScript2 Language.
Contains the Parser for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains Runtime Settings Objects.
Contains the Runtime Implementation.