-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.cs
90 lines (79 loc) · 3.26 KB
/
Interpreter.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
using System.Net;
using System.Security;
using System.IO;
namespace ironide {
namespace Scripting {
public class Interpreter {
public static string ansval;
public static void Interpret(string[] script) {
foreach (string line in script) {
var parsed = ParseCommandLine(line);
if (parsed.Length == 0)
continue;
if (parsed[0] == "cast") {
Console.WriteLine(parsed[1].Replace("%ANSVAL", ansval));
}
else if (parsed[0] == "install") {
if (parsed.Length >= 4 && parsed[2] == "at") {
try {
string directoryPath = Path.GetDirectoryName(parsed[3].Replace("%ANSVAL", ansval));
if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
using (WebClient client = new WebClient()) {
client.DownloadFile(parsed[1], parsed[3].Replace("%ANSVAL", ansval));
}
}
catch(WebException ex) {
Console.WriteLine(ex.Message);
}
}
}
else if (parsed[0] == "ask") {
Console.Write(parsed[1]);
ansval = Console.ReadLine();
}
}
Console.WriteLine("\n--- ironide INACTIVE ---\n");
}
static string[] ParseCommandLine(string commandLine)
{
List<string> tokens = new List<string>();
bool inQuotes = false;
int tokenStart = 0;
for (int i = 0; i < commandLine.Length; i++)
{
char c = commandLine[i];
if (c == '"')
{
inQuotes = !inQuotes;
}
else if (char.IsWhiteSpace(c) && !inQuotes)
{
if (tokenStart < i)
{
string token = commandLine.Substring(tokenStart, i - tokenStart);
// Remove quotes from the token
token = RemoveQuotes(token);
tokens.Add(token);
}
tokenStart = i + 1;
}
}
if (tokenStart < commandLine.Length)
{
string lastToken = commandLine.Substring(tokenStart);
// Remove quotes from the last token
lastToken = RemoveQuotes(lastToken);
tokens.Add(lastToken);
}
return tokens.ToArray();
}
static string RemoveQuotes(string input)
{
return input.Trim('"', '\'');
}
}
}
}