-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·246 lines (210 loc) · 5.67 KB
/
Program.cs
File metadata and controls
executable file
·246 lines (210 loc) · 5.67 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using WattleScript.Commands;
using WattleScript.Commands.Implementations;
using WattleScript.Interpreter;
using WattleScript.Interpreter.REPL;
namespace WattleScript
{
class Program
{
[STAThread]
static void Main(string[] args)
{
CommandManager.Initialize();
Script.DefaultOptions.ScriptLoader = new ReplInterpreterScriptLoader();
if (CheckArgs(args, out ScriptSyntax syntax))
return;
Banner(syntax);
Script script = new Script(syntax == ScriptSyntax.Lua ? CoreModules.Preset_Complete : CoreModules.Preset_CompleteWattle)
{
Options = { Syntax = syntax },
Globals =
{
["makestatic"] = (Func<string, DynValue>) MakeStatic
}
};
ReplInterpreter interpreter = new ReplInterpreter(script)
{
HandleDynamicExprs = true,
HandleClassicExprsSyntax = true
};
while (true)
{
InterpreterLoop(interpreter, script);
}
}
private static DynValue MakeStatic(string type)
{
Type tt = Type.GetType(type);
if (tt == null)
Console.WriteLine("Type '{0}' not found.", type);
else
return UserData.CreateStatic(tt);
return DynValue.Nil;
}
private static void InterpreterLoop(ReplInterpreter interpreter, Script script)
{
Console.Write(interpreter.ClassicPrompt + " ");
string s = Console.ReadLine();
if (s != null && !interpreter.HasPendingCommand && s.StartsWith("!"))
{
ExecuteCommand(script, s[1..]);
return;
}
try
{
DynValue result = interpreter.Evaluate(s);
if (result.IsNotVoid())
Console.WriteLine("{0}", result);
}
catch (InterpreterException ex)
{
Console.WriteLine("{0}", ex.DecoratedMessage ?? ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("{0}", ex.Message);
}
}
private static void Banner(ScriptSyntax syntax)
{
Console.WriteLine(Script.GetBanner("Console"));
Console.WriteLine();
Console.WriteLine($"Type {syntax} code to execute it or type !help to see help on commands.\n");
Console.WriteLine("Welcome.\n");
}
private static void ShowUsage()
{
Console.WriteLine("usage: wattlescript [-L | --lua] [-h | --help | -X \"command\" | -W <dumpfile> <destfile> [--class=name] [--namespace=name] [--internals] [--vb] | <script>]");
}
private static bool CheckArgs(string[] args, out ScriptSyntax syntax)
{
syntax = ScriptSyntax.Wattle;
bool internals = false;
// General options
bool show_help = false;
bool print_version = false;
bool do_exec = false;
// Hardwire Options
string classname = null;
string namespacename = null;
bool lua = false;
var p = new OptionSet()
{
{"h|?|help", "show this message and exit", v => show_help = v != null},
{"v|version", "print version and exit", v => print_version = v != null },
{"X|exec", "runs the specified command", v => do_exec = v != null },
{"L|lua", "run interpreter in lua mode", v => lua = v != null },
{"internals", "enable internals for hardwire generator", v => internals = v != null },
{"class=", "hardwire class name", v => classname = v },
{"namespace=", "hardwire namespace", v => namespacename = v },
};
List<string> extra;
try {
extra = p.Parse (args);
}
catch (OptionException e) {
Console.Write ("WattleScript: ");
Console.WriteLine (e.Message);
Console.WriteLine ("Try `WattleScript --help' for more information.");
Environment.Exit(1);
return false;
}
if (lua)
{
syntax = ScriptSyntax.Lua;
}
if (print_version)
{
Console.WriteLine("WattleScript {0}", Script.VERSION);
return true;
}
if (show_help)
{
Console.WriteLine(Script.GetBanner("Console"));
ShowUsage();
p.WriteOptionDescriptions(Console.Out);
return true;
}
if (do_exec)
{
if (extra.Count > 0)
{
Script script = new Script(lua ? CoreModules.Preset_Complete : CoreModules.Preset_CompleteWattle)
{
Options = { Syntax = lua ? ScriptSyntax.Lua : ScriptSyntax.Wattle },
Globals =
{
["makestatic"] = (Func<string, DynValue>) MakeStatic
}
};
ExecuteCommand(script, extra[0]);
}
else
{
Console.WriteLine("Incorrect syntax");
ShowUsage();
Environment.Exit(1);
}
return true;
}
if (extra.Count > 0)
{
var script = new Script(syntax == ScriptSyntax.Lua ? CoreModules.Preset_Default : CoreModules.Preset_DefaultWattle);
script.Options.Syntax = syntax;
var scriptArgs = new Table(script);
for (int i = 1; i < extra.Count; i++)
{
scriptArgs[i] = extra[i];
}
script.Globals["arg"] = scriptArgs;
if (!File.Exists(extra[0]))
{
Console.Error.WriteLine("File not found: {0}", extra[0]);
Environment.Exit(2);
}
try
{
script.DoFile(extra[0]);
}
catch (InterpreterException ex)
{
Console.Error.WriteLine(ex.DecoratedMessage ?? ex.Message);
Environment.Exit(1);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
Environment.Exit(1);
}
return true;
}
return false;
}
private static void ExecuteCommand(Script script, string cmdline)
{
StringBuilder cmd = new StringBuilder();
StringBuilder args = new StringBuilder();
StringBuilder dest = cmd;
foreach (char t in cmdline)
{
if (dest == cmd && t == ' ')
{
dest = args;
continue;
}
dest.Append(t);
}
string scmd = cmd.ToString().Trim();
string sargs = args.ToString().Trim();
ICommand c = CommandManager.Find(scmd);
if (c == null)
Console.WriteLine("Invalid command '{0}'.", scmd);
else
c.Execute(script, sargs);
}
}
}