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 m_Runner.AddTask(new BadTask(BadRunnable.Create(m_Context.Execute(exprs)), "Main"), true);
166
167 while (!m_Runner.IsIdle)
168 {
170
171 yield return null;
172 }
173 }
174
180 private void Run(IEnumerable<BadExpression> expressions)
181 {
182 IEnumerable<BadExpression> exprs = expressions;
183
184 if (PreParse)
185 {
186 exprs = exprs.ToArray();
187 }
188
189 if (BadNativeOptimizationSettings.Instance.UseConstantFoldingOptimization)
190 {
192 }
193
194 if (m_Context == null)
195 {
196 throw new BadRuntimeException("Context is not initialized");
197 }
198
199 m_Runner.AddTask(new BadTask(BadRunnable.Create(m_Context.Execute(exprs)), "Main"), true);
200
201 if (CatchErrors)
202 {
203 try
204 {
205 while (!m_Runner.IsIdle)
206 {
208 }
209 }
210 catch (Exception e)
211 {
212 BadConsole.WriteLine(e.Message);
213 }
214 }
215 else
216 {
217 while (!m_Runner.IsIdle)
218 {
220 }
221 }
222 }
223
230 public IEnumerable<object?> RunIsolatedRoutine(string code)
231 {
232 if (m_Context == null)
233 {
234 throw new BadRuntimeException("Context is not initialized");
235 }
236
238 Reset();
239 BadSourceParser parser = BadSourceParser.Create("<stdin>", code);
240
241 foreach (object? o in RunRoutine(parser.Parse()))
242 {
243 yield return o;
244 }
245
246 m_Context = ctx;
247 }
248
255 public BadObject RunIsolated(string code)
256 {
257 if (m_Context == null)
258 {
259 throw new BadRuntimeException("Context is not initialized");
260 }
261
263 Reset();
265 BadSourceParser parser = BadSourceParser.Create("<stdin>", code);
266 Run(parser.Parse());
267 m_Context = ctx;
268
269 return current.Scope.ReturnValue ?? current.Scope.GetTable();
270 }
271
276 public void Run(string code)
277 {
278 BadSourceParser parser = BadSourceParser.Create("<stdin>", code);
279 Run(parser.Parse());
280 }
281}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:30
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:78
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:16
BadTable GetTable()
Returns the Variable Table of the current scope.
Definition BadScope.cs:583
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:784
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.