-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
288 lines (262 loc) · 9.38 KB
/
Program.cs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ClearScript;
using Microsoft.ClearScript.Windows;
using System.IO;
using System.Reflection;
namespace LychenBASIC
{
static class Program
{
public static VBScriptEngine vbscriptEngine;
public static Dictionary<string, object> Settings = new Dictionary<string, object>();
[STAThread]
static int Main(string[] args)
{
LoadSettingsDictionary(args);
if (Settings.ContainsKey("/DEBUG") && Settings["/DEBUG"].GetType() == typeof(bool))
{
System.Diagnostics.Debugger.Launch();
}
vbscriptEngine = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging);
AddSymbols();
string script = string.Empty;
string replFile = string.Empty;
var hasRepl = Settings.ContainsKey("/REPL");
if (hasRepl)
{
if (Settings["/REPL"].GetType() != typeof(bool))
{
replFile = Settings["/REPL"].ToString();
}
}
if ((int)Settings["$ARGC"] > 0)
{
script = Settings["$ARG0"].ToString();
if (!File.Exists(script))
{
Console.WriteLine($"Script {script} not found.");
return 2;
}
ConnectoToScriptINI(script);
}
else
{
if (!hasRepl)
{
Console.WriteLine("No script.");
return 1;
}
ConnectoToScriptINI("repl.INI"); // FIXME. Put this somewhere useful
}
// SetupIncludeFunction();
if (script != string.Empty)
{
try
{
vbscriptEngine.Execute(System.IO.File.ReadAllText(script));
}
catch (Exception exception)
{
if (exception is IScriptEngineException scriptException)
{
Console.WriteLine(scriptException.ErrorDetails);
}
}
}
if (hasRepl)
{
RunREPL(replFile);
}
return 0;
}
private static void RunREPL(string fileName)
{
string cmd;
do
{
Console.Write(Settings["$PROMPT"]);
cmd = Console.ReadLine();
if (cmd == "bye")
{
break;
}
if (fileName != string.Empty)
{
File.AppendAllText(fileName, cmd + "\r\n");
}
object evaluand;
try
{
evaluand = vbscriptEngine.ExecuteCommand(cmd);
}
catch (ScriptEngineException see)
{
evaluand = "";
Console.WriteLine(see.ErrorDetails);
Console.WriteLine(see.StackTrace);
}
catch (NullReferenceException nre)
{
evaluand = "";
Console.WriteLine(nre.Message);
}
catch (Exception e)
{
evaluand = "";
Console.WriteLine(e.Message);
}
} while (cmd != "bye");
}
private static void ConnectoToScriptINI(string script)
{
var ini = new INI(Path.ChangeExtension(script, ".INI"));
vbscriptEngine.AddHostObject("CSScriptINI", ini);
}
private static void LoadSettingsDictionary(string[] args)
{
var argv = new List<string>();
var cnt = 0;
var slashCnt = 0;
foreach (var arg in args)
{
if (arg.StartsWith("/"))
{
slashCnt++;
if (arg.Contains(":") || arg.Contains("="))
{
var lhs = arg.Split(new char[] { ':', '=' }, 2);
Settings[lhs[0]] = lhs[1];
}
else
{
Settings[arg] = true;
}
}
else
{
Settings[$"$ARG{cnt}"] = arg;
cnt++;
argv.Add(arg);
}
}
Settings["$ARGC"] = cnt;
Settings["$ARGV"] = argv.ToArray<string>();
Settings["/COUNT"] = slashCnt;
Settings["$PROMPT"] = "LychenBASIC: ";
}
private static void Run(string fname)
{
if (File.Exists(fname))
{
try
{
vbscriptEngine.Execute(File.ReadAllText(fname));
}
catch (ScriptEngineException see)
{
Console.WriteLine(see.ErrorDetails);
}
}
}
private static void AddSymbols()
{
AddInternalSymbols(ref vbscriptEngine);
AddHostSymbols(ref vbscriptEngine);
AddSystemSymbols(ref vbscriptEngine);
vbscriptEngine
.Script
.Print = (Action<object>)Console.WriteLine;
vbscriptEngine
.Script
.Run = (Action<string>)Run;
vbscriptEngine
.Script
.Include = (Action<string>)Run;
vbscriptEngine
.Script
.Attach = (Action<string, string>)Attach;
}
private static void Attach(string dllPath, string name = "")
{
var htc = new HostTypeCollection();
try
{
//var assem = System.Reflection.Assembly.LoadFrom(dllPath);
var assem = Assembly.Load(AssemblyName.GetAssemblyName(dllPath));
htc.AddAssembly(assem);
if (name.Length == 0)
{
name = assem.FullName.Split(',')[0];
}
vbscriptEngine.AddHostObject(name, htc); //FIXME checkout the hosttypes
Console.WriteLine($"Attached {dllPath} as {name}");
}
catch (ReflectionTypeLoadException rtle)
{
foreach (var item in rtle.LoaderExceptions)
{
Console.WriteLine(item.Message);
}
}
catch (FileNotFoundException fnfe)
{
Console.WriteLine(fnfe.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
private static void AddInternalSymbols(ref VBScriptEngine vbscriptEngine)
{
vbscriptEngine.AddHostObject("VBS", vbscriptEngine);
vbscriptEngine.AddHostType("CSINI", typeof(INI));
vbscriptEngine.AddHostObject("CSSettings", Settings);
vbscriptEngine.AddHostType("CSLychen", typeof(Program)); // Experimental. No idea if useful or dangerous.
}
private static void AddSystemSymbols(ref VBScriptEngine vbscriptEngine)
{
vbscriptEngine.AddHostType("CSFile", typeof(System.IO.File));
vbscriptEngine.AddHostType("CSConsole", typeof(System.Console));
vbscriptEngine.AddHostType("CSPath", typeof(System.IO.Path));
vbscriptEngine.AddHostType("CSDirectory", typeof(System.IO.Directory));
vbscriptEngine.AddHostType("CSDirectoryInfo", typeof(System.IO.DirectoryInfo));
vbscriptEngine.AddHostType("CSEnvironment", typeof(System.Environment));
vbscriptEngine.AddHostType("CSString", typeof(System.String));
vbscriptEngine.AddHostType("CSDateTime", typeof(System.DateTime));
vbscriptEngine.AddHostType("CSDebugger", typeof(System.Diagnostics.Debugger));
}
private static void AddHostSymbols(ref VBScriptEngine vbscriptEngine)
{
vbscriptEngine.AddHostObject("CSExtendedHost", new ExtendedHostFunctions());
vbscriptEngine.AddHostObject("CSHost", new HostFunctions());
var htc = new HostTypeCollection();
foreach (var assembly in new string[] { "mscorlib", "System", "System.Core", "System.Data" /*, "RestSharp", "WebDriver", "WebDriver.Support" */})
{
htc.AddAssembly(assembly);
}
if (Settings.ContainsKey("/ASSEMBLIES"))
{
var assemblies = Settings["/ASSEMBLIES"].ToString().Split(',');
foreach (var assembly in assemblies)
{
System.Reflection.Assembly assem;
try
{
assem = System.Reflection.Assembly.LoadFrom(assembly);
htc.AddAssembly(assem);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
vbscriptEngine.AddHostObject("CS", htc);
}
}
}