-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
204 lines (164 loc) · 6.63 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
using System.Data.SqlTypes;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using SlimScript;
internal class Program
{
public static bool Debug = false;
public static bool CompressStandalone = false;
public static string? BasePath { get; set; }
public static ExitCode ExitCode { get; set; }
public static SourceChunk? MainChunk { get; set; }
public static bool interactive = false;
private static readonly Stopwatch watch = new();
public static void Main(string[] args)
{
try
{
if (args.Length != 0 && args[0] == "-i")
RunInteractive();
else if (args.Length != 0 && (args[0] == "-h" || args[0] == "--help"))
PrintHelp();
else
{
if (args.Length == 0 || !File.Exists(args[0]))
{
Console.ForegroundColor = ConsoleColor.Red;
Write.StandartOutput.WriteLine("No Input File...");
Exit(ExitCode.NoInputFile);
}
BasePath = Directory.GetCurrentDirectory();
Debug = args.Contains("-D");
CompressStandalone = args.Contains("-C");
MainChunk = new(args[0]);
Directory.SetCurrentDirectory(BasePath);
var indexOfArgsBegin = System.Array.IndexOf(args, "%p");
indexOfArgsBegin = indexOfArgsBegin == -1 ? -2 : indexOfArgsBegin;
try
{
MainChunk.CreateVar("os.args", Variable.ClrToVar(args[(indexOfArgsBegin + 1)..]));
MainChunk.CreateVar(
"os.argc",
Variable.ClrToVar(args.Length - (indexOfArgsBegin + 1))
);
}
catch (IndexOutOfRangeException)
{
MainChunk.CreateVar("os.args", new Null());
MainChunk.CreateVar("os.argc", Variable.ClrToVar(0));
}
catch (ArgumentOutOfRangeException)
{
MainChunk.CreateVar("os.args", new Null());
MainChunk.CreateVar("os.argc", Variable.ClrToVar(0));
}
watch.Start();
MainChunk.Run();
if (MainChunk.VarExists("main"))
{
((Function?)MainChunk.GetVar("main"))?.Run(
MainChunk.GetVar("os.args") ?? new Null(),
MainChunk.GetVar("os.argc") ?? Variable.ClrToVar(0)
);
}
watch.Stop();
Write.StandartOutput.WriteLine($"\nProgram Exited in {watch.ElapsedMilliseconds}ms");
Exit(ExitCode.Normal);
}
}
catch (Exception e)
{
Exception GetInnerMost(Exception e) {
if (e.InnerException != null)
return GetInnerMost(e.InnerException);
return e;
}
var line = MainChunk?.Lines[MainChunk.Parser.lineNumber - 1];
var message =
$"An Exception [{GetInnerMost(e).GetType().Name}] in source code occured during runtime.\nMessage: {e.Message}\nSource:\n{e.StackTrace}\nFile: {Path.GetFileNameWithoutExtension(MainChunk?._file)}_p.sso\nLine: {MainChunk?.Parser.lineNumber}\nExpression: {string.Join(' ', line?.Select(t => t.Text) ?? new[] { "" })}";
Console.ForegroundColor = ConsoleColor.Red;
Write.StandartOutput.WriteLine(message);
Exit(ExitCode.RuntimeError);
}
}
private static void PrintHelp()
{
StringBuilder sb = new();
sb.Append($"\n{GlobalSettings.AppInfo}\n");
sb.AppendLine("Usage:");
sb.AppendLine("\tSlimScript <file>");
sb.AppendLine("\tSlimScript <file> <flags>");
sb.AppendLine("\tSlimScript <file> <flags> %p <arguments>");
sb.AppendLine("\tSlimScript -i");
sb.AppendLine("\tSlimScript -h --help");
sb.AppendLine("\nAvailable Flags:");
sb.AppendLine(
"\t-D Run With Debug Mode. Generates lexer output (for curious ones). (SlimScript <file> -D)"
);
sb.AppendLine(
"\t-C Generates Compressed Standalone Script File That Has No\n\t\t\tDependencies On Any Other File. (SlimScript <file> -C)"
);
sb.AppendLine(
"\t-i Run Interactive Mode (SlimScript -i)"
);
sb.AppendLine(
"\t-h --help Show This Output. (SlimScript -h --help)"
);
sb.AppendLine("\n\tPassing Arguments:");
sb.AppendLine(
"\t\t\tSlimScript <file> <flags> %p <arguments> Arguments are passed to main function as an array."
);
sb.AppendLine();
Write.StandartOutput.Write(sb);
}
private static void RunInteractive()
{
interactive = true;
int line = 0;
MainChunk = new();
while (true)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Write.StandartOutput.Write("\n>>> ");
Console.ResetColor();
string input = Console.ReadLine() ?? "";
if (string.IsNullOrEmpty(input))
continue;
if (input == "qqq")
break;
MainChunk.RunInteractive(input);
line++;
}
Exit(ExitCode.Normal);
}
public static byte[] Compress(byte[] data)
{
using var compressedStream = new MemoryStream();
using var zipStream = new GZipStream(compressedStream, CompressionMode.Compress);
zipStream.Write(data, 0, data.Length);
zipStream.Close();
return compressedStream.ToArray();
}
public static byte[] Decompress(byte[] data)
{
using var compressedStream = new MemoryStream(data);
using var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress);
using var resultStream = new MemoryStream();
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
public static void Exit(ExitCode code)
{
if (interactive)
return;
ExitCode = code;
Console.ResetColor();
Write.StandartOutput.WriteLine($"\nExit Code: {(int)code} <{code}>");
Write.StandartOutput.Flush();
Write.StandartOutput.Close();
Environment.Exit((int)code);
}
}