-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
428 lines (405 loc) · 14.5 KB
/
MainForm.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lang.language;
using System.Collections;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Lang
{
public partial class MainForm : Form
{
#region Members
string path = null;
bool FileChanged = false;
string lastSavedFile = "";
string curCode;
#endregion
public MainForm(string[] args)
{
InitializeComponent();
LineNumberer.BackgroundGradient_AlphaColor = SystemColors.Control;
LineNumberer.BackgroundGradient_BetaColor = SystemColors.Control;
LineNumberer.BorderLines_Color = SystemColors.Control;
LineNumberer.GridLines_Color = SystemColors.Control;
LineNumberer.MarginLines_Color = SystemColors.Control;
LineNumberer.LineNrs_LeadingZeroes = false;
if (args.Length > 0)
{
bool r = false;
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("-"))
{
switch (args[i])
{
case "-r":
r = true;
break;
}
}
else
{
path = args[i];
Text += " | " + path.Substring(path.LastIndexOf('\\') + 1);
codeRTB.Text = File.ReadAllText(path);
}
}
if (r)
{
Run();
Hide();
Thread.CurrentThread.Abort();
}
}
else
{
Text = Text + " | Untitled.lan";
}
}
private void Run()
{
curCode = codeRTB.Text;
string fileName = Directory.GetCurrentDirectory() + "\\";
if (path != null)
{
fileName = path;
}
LangManager langManager = new LangManager(fileName);
langManager.interpreter.breakpoints = (ArrayList)LineNumberer.BreakPoints.Clone();
try
{
langManager.updateCode(curCode);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Thread runnerThread = new Thread(new ThreadStart(langManager.run), 33554432);
Thread.Sleep(500);
runnerThread.SetApartmentState(ApartmentState.STA);
runnerThread.Start();
LiveErrors.Enabled = false;
}
private void RunButton_Click(object sender, EventArgs e)
{
Run();
}
#region MainForm Events
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (codeRTB.Text != lastSavedFile)
AskToSave();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
ButtonPressed(e);
}
private void MainForm_Resize(object sender, EventArgs e)
{
RunButton.Location = new Point(this.Size.Width - 99, RunButton.Location.Y);
codeRTB.Size = new Size(this.Size.Width - 51, this.Size.Height - 128);
}
#endregion
#region File Managment Functions
private void TextChangedChanged()
{
if (FileChanged)
{
if (!Text.EndsWith(" *"))
{
Text += " *";
}
}
else
{
if (Text.EndsWith(" *"))
{
Text = Text.Substring(0, Text.Length - 2);
}
}
}
private void SaveFile()
{
if (path == null)
{
SaveFileDialog sa = new SaveFileDialog();
sa.Filter = "Lang Files | *.lan";
sa.InitialDirectory = Directory.GetCurrentDirectory();
sa.DefaultExt = "lan";
if (sa.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.CreateText(sa.FileName);
path = sa.FileName;
sw.Write(codeRTB.Text);
sw.Close();
FileChanged = false;
TextChangedChanged();
Text = "Lang | " + path.Substring(path.LastIndexOf('\\') + 1);
lastSavedFile = codeRTB.Text;
sw.Dispose();
}
}
else
{
StreamWriter sw = File.CreateText(path);
sw.Write(codeRTB.Text);
sw.Close();
FileChanged = false;
TextChangedChanged();
lastSavedFile = codeRTB.Text;
sw.Dispose();
}
}
private void AskToSave()
{
string fileName = "";
if (path != null)
{
fileName = path.Substring(path.LastIndexOf('\\') + 1);
}
else
{
fileName = "Untitled.lan";
}
DialogResult dr = MessageBox.Show("The file " + fileName + " has been modified, Do you want to Save it?", "Save", MessageBoxButtons.YesNo);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
SaveFile();
}
}
private void OpenFile()
{
if (codeRTB.Text != lastSavedFile)
{
AskToSave();
}
OpenFileDialog op = new OpenFileDialog();
op.DefaultExt = "lan";
op.Filter = "Lang Files | *.lan";
op.InitialDirectory = Directory.GetCurrentDirectory();
if (op.ShowDialog() == DialogResult.OK)
{
StreamReader sr = File.OpenText(op.FileName);
path = op.FileName;
codeRTB.Text = sr.ReadToEnd();
sr.Close();
Text = "Lang | " + path.Substring(path.LastIndexOf('\\') + 1);
lastSavedFile = codeRTB.Text;
sr.Dispose();
}
op.Dispose();
}
private void NewFile()
{
if (codeRTB.Text != lastSavedFile)
AskToSave();
path = null;
codeRTB.Text = "";
Text = "Lang | Untitled.lan";
lastSavedFile = "";
FileChanged = false;
TextChangedChanged();
}
#endregion
#region File Managment Events
private void SaveButton_Click(object sender, EventArgs e)
{
SaveFile();
}
private void OpenButton_Click(object sender, EventArgs e)
{
OpenFile();
}
private void NewButton_Click(object sender, EventArgs e)
{
NewFile();
}
private void SaveAsButton_Click(object sender, EventArgs e)
{
SaveFileDialog sa = new SaveFileDialog();
sa.Filter = "Lang Files | *.lan";
sa.InitialDirectory = Directory.GetCurrentDirectory();
sa.DefaultExt = "lan";
if (sa.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.CreateText(sa.FileName);
path = sa.FileName;
sw.Write(codeRTB.Text);
sw.Close();
FileChanged = false;
Text = "Lang | " + path.Substring(path.LastIndexOf('\\') + 1);
lastSavedFile = Text;
sw.Dispose();
}
}
private void CodeRTB_TextChanged(object sender, EventArgs e)
{
LiveErrors.Stop();
LiveErrors.Start();
if (codeRTB.Text == lastSavedFile)
{
FileChanged = false;
}
else
{
FileChanged = true;
}
TextChangedChanged();
}
#endregion
private void ButtonPressed(KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.S)
{
SaveFile();
}
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.O)
{
OpenFile();
}
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.N)
{
NewFile();
}
if (e.KeyCode == Keys.F5)
{
Run();
}
}
int getLineNumberAt(string code, int idx)
{
int line = 1;
for (int i = 0; i < idx; i++)
{
if (code[i] == '\n')
line++;
}
return line;
}
private void codeRTB_KeyDown(object sender, KeyEventArgs e)
{
ButtonPressed(e);
if (e.KeyCode == Keys.Tab)
{
codeRTB.SelectedText = " ";
e.SuppressKeyPress = true;
}
if (e.KeyCode == Keys.Enter)
{
int selpos = codeRTB.SelectionStart;
int lastbn = 0;
string text = codeRTB.Text.Substring(0, selpos);
lastbn = text.LastIndexOf('\n');
text = text.Substring(lastbn+1);
Match match = Regex.Match(text, "[ ]*");
string spaces = match.Value;
codeRTB.SelectedText = "\n" + spaces;
e.SuppressKeyPress = true;
}
if (e.KeyCode == Keys.F9)
{
int lnNum = getLineNumberAt(codeRTB.Text, codeRTB.SelectionStart);
if (LineNumberer.BreakPoints.Contains(lnNum))
{
LineNumberer.BreakPoints.Remove(lnNum);
}
else
{
LineNumberer.BreakPoints.Add(lnNum);
}
LineNumberer.Refresh();
}
if (e.KeyCode == Keys.K && e.Modifiers == Keys.Control)
{
try
{
Parser parser = new Parser(new LangManager(""));
parser.updateTokens(new Lexer(new LangManager(""), codeRTB.Text).lex());
codeRTB.SelectionStart = 0;
codeRTB.SelectionLength = codeRTB.Text.Length;
codeRTB.SelectedText = ((StatementList)parser.parse()).ToString(false);
codeRTB.SelectionStart = 0;
codeRTB.SelectionLength = 0;
}
catch (LangException)
{
// Nothing to do here
/*
░░░░░▄▄▄▄▄▄░░░░░░░░░░
░░▄█▀░░░░░▄▀▄░░░░░░░░
░█░▀▀▀▀▀▀▀▀░░█▄░░░░░░
█▀░░░░░░░░░░░░█░░░░░░
█░░░░░░░░░░░░░█░░░░░░
▀█░░░░░░░░░░░░█░░░░░░
░▀▄░░░░░░░░░▄█░░░░░░░
░░░▀█▄▄▄▄▄▄██▄▄░░░░░░
░░▄▄█▀███▀██████░░░░░
░░▀██░░██▀█████▄▄░░░░
░░░░░░░░░░▄▄███▀█▄▀▄░
░░▄█▄▄▄██████▀▄▀▄▀█▄▀
░██░░░▀██▀░░░░░░▄▀▄█░
░█▄░░░░░█▄░░░░░░░▀░░░
░░▀█▄░░░░▀▀▀░░░░░░░░░
*/
}
}
if (e.KeyCode == Keys.F && e.Modifiers == Keys.Control)
{
Finder.Visible = !Finder.Visible;
if (Finder.Visible)
{
Finder.Focus();
}
}
}
private void LiveErrors_Tick(object sender, EventArgs e)
{
string fileName = "Untitled.lan";
if (path != null)
{
fileName = path.Substring(path.LastIndexOf('\\') + 1);
}
LangManager langManager = new LangManager(fileName);
langManager.INTERPRET = false;
langManager.updateCode(codeRTB.Text);
langManager.lastLiveException = "";
Thread thread = new Thread(new ThreadStart(langManager.run));
thread.Start();
thread.Join(1000);
ErrorsShower.Text = langManager.lastLiveException;
if (langManager.lastLiveException.Length > 0)
{
string lineNumber = langManager.lastLiveException;
int number = -1;
try
{
lineNumber = lineNumber.Substring(5);
lineNumber = lineNumber.Substring(0, lineNumber.IndexOf(':'));
number = Convert.ToInt32(lineNumber);
}
catch (Exception)
{
// Nothing to do here
}
LineNumberer.ErrorLineNumber = number;
}
else
{
LineNumberer.ErrorLineNumber = -1;
}
LineNumberer.Refresh();
langManager.INTERPRET = true;
}
}
}