-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLythonEngine.cs
More file actions
177 lines (159 loc) · 6.04 KB
/
Copy pathLythonEngine.cs
File metadata and controls
177 lines (159 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Threading;
using System.Threading.Tasks;
using Lokad.Lython.Frontend;
using Lokad.Lython.Runtime;
namespace Lokad.Lython;
/// <summary>Compiles and executes Python source using Lython's host-mediated runtime.</summary>
public sealed class LythonEngine
{
/// <summary>Compiles source and returns both the reusable script and all frontend diagnostics.</summary>
public LythonCompiledScript Compile(string source)
{
var frontend = LythonFrontend.Compile(source);
var diagnostics = frontend.Diagnostics;
var isValid = diagnostics.All(static d => d.Severity != LythonDiagnosticSeverity.Error);
var script = frontend.Script;
var loweredScript = script is null ? null : LoweredScript.Lower(script);
ExecutableScript? executableScript = null;
if (loweredScript is not null)
{
try
{
executableScript = ExecutableScript.Compile(loweredScript);
}
catch (ExecutableLoweringFallbackException)
{
executableScript = null;
}
}
LythonExecutionResult? CreatePreExecutionFailure(ILythonHost host)
{
if (!isValid)
{
return new LythonExecutionResult(
outcome: LythonExecutionOutcome.CompilationFailed,
returnValue: null,
standardOutput: string.Empty,
standardError: string.Empty,
exitCode: 1,
diagnostics: diagnostics,
failure: null);
}
if (script is null)
{
return null;
}
var hostDiagnostics = StaticAnalyzer.AnalyzeHostRequirements(script, host);
if (hostDiagnostics.Count == 0)
{
return null;
}
return new LythonExecutionResult(
outcome: LythonExecutionOutcome.CompilationFailed,
returnValue: null,
standardOutput: string.Empty,
standardError: string.Empty,
exitCode: 1,
diagnostics: diagnostics.Concat(hostDiagnostics).ToArray(),
failure: null);
}
return new LythonCompiledScript(
source,
diagnostics,
isValid,
(host, options) =>
{
var preExecutionFailure = CreatePreExecutionFailure(host);
if (preExecutionFailure is not null)
{
return preExecutionFailure;
}
var runtime = new LythonRuntime();
return executableScript is not null
? runtime.Run(executableScript, host, options)
: runtime.Run(loweredScript.RequireNotNull(), host, options);
},
async (host, options) =>
{
var preExecutionFailure = CreatePreExecutionFailure(host);
if (preExecutionFailure is not null)
{
return preExecutionFailure;
}
var runtime = new LythonRuntime();
return await runtime.RunAsync(loweredScript.RequireNotNull(), host, options).ConfigureAwait(false);
});
}
/// <summary>Compiles and synchronously runs source with default options.</summary>
public LythonExecutionResult Run(
string source,
ILythonHost host)
{
return Compile(source).Run(host);
}
/// <summary>Compiles and synchronously runs source with initial globals.</summary>
public LythonExecutionResult Run(
string source,
ILythonHost host,
IReadOnlyDictionary<string, object?> globals)
{
return Compile(source).Run(host, new LythonRunOptions { Globals = globals });
}
/// <summary>Compiles and synchronously runs source with explicit execution options.</summary>
public LythonExecutionResult Run(
string source,
ILythonHost host,
LythonRunOptions options)
{
return Compile(source).Run(host, options);
}
/// <summary>Compiles and asynchronously runs source with default options.</summary>
public Task<LythonExecutionResult> RunAsync(
string source,
ILythonHost host)
{
return Compile(source).RunAsync(host);
}
/// <summary>Compiles and asynchronously runs source with an external cancellation token.</summary>
public Task<LythonExecutionResult> RunAsync(
string source,
ILythonHost host,
CancellationToken cancellationToken)
{
return Compile(source).RunAsync(host, cancellationToken);
}
/// <summary>Compiles and asynchronously runs source with initial globals.</summary>
public Task<LythonExecutionResult> RunAsync(
string source,
ILythonHost host,
IReadOnlyDictionary<string, object?> globals)
{
return Compile(source).RunAsync(host, globals);
}
/// <summary>Compiles and asynchronously runs source with initial globals and cancellation.</summary>
public Task<LythonExecutionResult> RunAsync(
string source,
ILythonHost host,
IReadOnlyDictionary<string, object?> globals,
CancellationToken cancellationToken)
{
return Compile(source).RunAsync(host, globals, cancellationToken);
}
/// <summary>Compiles and asynchronously runs source with explicit execution options.</summary>
public Task<LythonExecutionResult> RunAsync(
string source,
ILythonHost host,
LythonRunOptions options)
{
return Compile(source).RunAsync(host, options);
}
/// <summary>Compiles and asynchronously runs source with options and an additional cancellation token.</summary>
public Task<LythonExecutionResult> RunAsync(
string source,
ILythonHost host,
LythonRunOptions options,
CancellationToken cancellationToken)
{
return Compile(source).RunAsync(host, options, cancellationToken);
}
}