-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
414 lines (386 loc) · 15.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
namespace Arisl
{
using System;
using System.IO;
using System.Threading.Tasks;
using System.Text.Json;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class ARISLInterpreter
{
private static List<string> priorREPLInputs = new List<string>();
private static string usings = @"";
private static FileProcessor fileProcessor = new FileProcessor();
private static DependencyProcessor dependencyProcessor = new DependencyProcessor();
private static CodeOrganizer codeOrganizer = new CodeOrganizer();
public static CodeProcessor codeProcessor = new CodeProcessor();
private static CodeExecutor codeExecutor = new CodeExecutor();
public static string CurrentActiveFilesName { get; set; }
public static string userDllsPath = Path.Combine(AppContext.BaseDirectory, "userdlls");
public static async Task Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Entering REPL mode...");
await REPL();
return;
}
string filename = null;
string directory = null;
bool errorCheck = false;
string memberName = null;
string pkgname = null;
string version = null;
bool listPackages = false;
bool deletePackage = false;
bool downloadPackage = false;
bool displayHelp = false;
ParseArguments(args, ref filename, ref directory, ref errorCheck, ref memberName, ref pkgname, ref version, ref listPackages, ref deletePackage, ref downloadPackage, ref displayHelp);
if (displayHelp)
{
DisplayHelp();
}
else if (listPackages)
{
await ListPkgs();
}
else if (deletePackage)
{
await DeletePkg(pkgname);
}
else if (downloadPackage)
{
await DownloadPkg(pkgname, version);
}
else if (directory != null)
{
await ProcessDirectory(directory);
}
else if (filename != null)
{
CurrentActiveFilesName = filename;
if (memberName != null)
{
await GetMethodInformation(filename, memberName);
}
else if (errorCheck)
{
await RunErrorCheck(filename);
}
else
{
await ProcessFile(filename);
}
}
else
{
Console.WriteLine("No valid filename or directory specified.");
}
}
private static void ParseArguments(string[] args, ref string filename, ref string directory, ref bool errorCheck, ref string memberName, ref string pkgname, ref string version, ref bool listPackages, ref bool deletePackage, ref bool downloadPackage, ref bool displayHelp)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--help")
{
displayHelp = true;
}
else if (args[i] == "--dir" && i + 1 < args.Length)
{
directory = args[i + 1];
i++;
}
else if (args[i] == "--file" && i + 1 < args.Length)
{
filename = args[i + 1];
i++;
}
else if (args[i] == "--errorcheck")
{
errorCheck = true;
}
else if (args[i] == "--method" && i + 1 < args.Length)
{
memberName = args[i + 1];
i++;
}
else if (args[i] == "--download" && i + 1 < args.Length)
{
downloadPackage = true;
pkgname = args[i + 1];
if (i + 2 < args.Length && !args[i + 2].StartsWith("--"))
{
version = args[i + 2];
i++;
}
else
{
version = null; // Default to latest version if not provided
}
i++;
}
else if (args[i] == "--delete" && i + 1 < args.Length)
{
deletePackage = true;
pkgname = args[i + 1];
i++;
}
else if (args[i] == "--list")
{
listPackages = true;
}
else
{
filename = args[i];
}
}
}
private static void DisplayHelp()
{
Console.WriteLine("Usage:");
Console.WriteLine(" ARISLInterpreter [options] [file]");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --help Display this help message.");
Console.WriteLine(" --dir <directory> Process all .ari files in the specified directory.");
Console.WriteLine(" --file <file> Process the specified .ari file.");
Console.WriteLine(" --errorcheck Perform a syntax and basic error check on the specified file.");
Console.WriteLine(" --method <name> Get method information for the specified member name in the file.");
Console.WriteLine(" --download <pkg> Download the specified package (optionally specify version).");
Console.WriteLine(" --delete <pkg> Delete the specified package.");
Console.WriteLine(" --list List all downloaded packages.");
Console.WriteLine();
Console.WriteLine("Examples:");
Console.WriteLine("--file example.ari");
Console.WriteLine("--dir examples");
Console.WriteLine("--file example.ari --errorcheck");
Console.WriteLine("--download Newtonsoft.Json 13.0.1");
}
private static async Task ProcessDirectory(string directory)
{
var files = Directory.GetFiles(directory, "*.ari", SearchOption.AllDirectories);
foreach (var file in files)
{
await ProcessFile(file);
}
}
private static async Task ProcessFile(string filename)
{
if (!File.Exists(filename))
{
Console.WriteLine($"File not found: {filename}");
return;
}
string cleanCode;
var (combinedCode, _) = await fileProcessor.ProcessFileAsync(filename);
var codeWithDependenciesRemoved = await dependencyProcessor.ProcessDependenciesAsync(combinedCode);
usings = codeOrganizer.ExtractUsings(codeWithDependenciesRemoved, out cleanCode);
bool hasParseErrors = false;
var generatedCode = codeProcessor.GenerateCode(cleanCode, out hasParseErrors);
if (hasParseErrors)
{
Console.WriteLine("Failed to generate C# code.");
return;
}
var organizedCode = codeOrganizer.OrganizeCode(generatedCode, usings);
Console.WriteLine(organizedCode);
var executionSuccess = await codeExecutor.ExecuteCodeAsync(organizedCode);
if (!executionSuccess)
{
Console.WriteLine("Failed to execute C# code.");
}
}
private static async Task RunErrorCheck(string filename)
{
if (!File.Exists(filename))
{
Console.WriteLine($"File not found: {filename}");
return;
}
string cleanCode;
var (combinedCode, _) = await fileProcessor.ProcessFileAsync(filename);
var codeWithDependenciesRemoved = await dependencyProcessor.ProcessDependenciesAsync(combinedCode);
usings = codeOrganizer.ExtractUsings(codeWithDependenciesRemoved, out cleanCode);
bool hasParseErrors;
var generatedCode = codeProcessor.GenerateCode(cleanCode, out hasParseErrors);
if (hasParseErrors)
{
Console.WriteLine("Failed to generate C# code.");
return;
}
var organizedCode = codeOrganizer.OrganizeCode(generatedCode, usings);
CSharpCompiler compiler = new CSharpCompiler(codeProcessor.codeGenerator._sourceMap);
await compiler.CheckSyntaxAndBasicErrorsAsync(organizedCode);
}
private static async Task GetMethodInformation(string filename, string memberName)
{
if (!File.Exists(filename))
{
Console.WriteLine($"File not found: {filename}");
return;
}
var (combinedCode, _) = await fileProcessor.ProcessFileAsync(filename);
var codeWithDependenciesRemoved = await dependencyProcessor.ProcessDependenciesAsync(combinedCode);
string cleanCode;
usings = codeOrganizer.ExtractUsings(codeWithDependenciesRemoved, out cleanCode);
var memberInfo = await dependencyProcessor._compiler.GetMethodInfoAsync(memberName);
var methodInfo = JsonSerializer.Deserialize<CSharpCompiler.MethodInfoResult>(memberInfo);
var json = JsonSerializer.Serialize(methodInfo, new JsonSerializerOptions { WriteIndented = true });
var hoverFileName = filename + ".hover";
await File.WriteAllTextAsync(hoverFileName, json);
Console.WriteLine(json);
}
private static async Task DownloadPkg(string pkgname, string version)
{
CSharpCompiler compiler = new CSharpCompiler();
await compiler.DownloadDotNetAssembly(pkgname, version);
if (Directory.Exists(userDllsPath))
{
string[] files = Directory.GetFiles(userDllsPath);
foreach (string file in files)
{
if (file.Contains(pkgname))
{
Console.WriteLine("Downloaded package: " + pkgname + " " + (version ?? "latest"));
return;
}
}
}
Console.WriteLine("Downloaded package: " + pkgname + " " + (version ?? "latest"));
}
private static async Task DeletePkg(string pkgname)
{
if (Directory.Exists(userDllsPath))
{
string[] files = Directory.GetFiles(userDllsPath);
foreach (string file in files)
{
if (file.Contains(pkgname))
{
File.Delete(file);
Console.WriteLine("Deleted package: " + pkgname);
return;
}
}
}
Console.WriteLine("Package not found: " + pkgname);
}
private static async Task ListPkgs()
{
if (Directory.Exists(userDllsPath))
{
string[] files = Directory.GetFiles(userDllsPath);
if (files.Length == 0)
{
Console.WriteLine("No packages found in the userdlls directory.");
}
else
{
Console.WriteLine("Packages in userdlls directory:");
for (int i = 0; i < files.Length; i++)
{
var file = files[i];
var assembly = Assembly.LoadFile(file);
var name = assembly.GetName();
Console.WriteLine($"{i + 1}. Name: {name.Name}");
Console.WriteLine($" Version: {name.Version}");
Console.WriteLine($" Path: {file}");
}
}
}
else
{
Console.WriteLine("The userdlls directory does not exist.");
}
}
private static string REPLMain()
{
var mainFunc = "func Main(){";
foreach (var input in priorREPLInputs)
{
mainFunc += input + "\n";
}
mainFunc += "}";
return mainFunc;
}
private static async Task REPL()
{
//create a temp .ari file
if (!Directory.Exists("temp"))
{
Directory.CreateDirectory("temp");
}
string tempFile = Path.Combine("temp", "temp.ari");
File.WriteAllText(tempFile, "");
while (true)
{
Console.Write("ARISL> ");
string input = Console.ReadLine();
if (input == "exit")
{
break;
}
// Handle commands within REPL
if (input.StartsWith("--"))
{
var args = input.Split(' ');
string filename = null;
string directory = null;
bool errorCheck = false;
string memberName = null;
string pkgname = null;
string version = null;
bool listPackages = false;
bool deletePackage = false;
bool downloadPackage = false;
bool displayHelp = false;
ParseArguments(args, ref filename, ref directory, ref errorCheck, ref memberName, ref pkgname, ref version, ref listPackages, ref deletePackage, ref downloadPackage, ref displayHelp);
if (displayHelp)
{
DisplayHelp();
}
else if (listPackages)
{
await ListPkgs();
}
else if (deletePackage)
{
await DeletePkg(pkgname);
}
else if (downloadPackage)
{
await DownloadPkg(pkgname, version);
}
else if (directory != null)
{
await ProcessDirectory(directory);
}
else if (filename != null)
{
CurrentActiveFilesName = filename;
if (memberName != null)
{
await GetMethodInformation(filename, memberName);
}
else if (errorCheck)
{
await RunErrorCheck(filename);
}
else
{
await ProcessFile(filename);
}
}
}
else
{
// Add input to REPL history and process as ARISL code
priorREPLInputs.Add(input);
File.WriteAllText(tempFile, REPLMain());
await ProcessFile(tempFile);
}
}
}
}
}