Skip to content

Commit 5639800

Browse files
committed
Final improvements for the first release of the script language
1 parent cd56b41 commit 5639800

File tree

3 files changed

+138
-76
lines changed

3 files changed

+138
-76
lines changed

CustomShell/MainController.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,11 @@ public void RunCommand(string command, bool fromScript)
703703
AddCommandToConsole(tokens);
704704
if (wand == null)
705705
wand = new WandEditor();
706-
wand.LoadFile(tokens);
706+
707+
if (tokens.Length == 2)
708+
wand.LoadFile(tokens);
709+
else if (tokens.Length == 3 && tokens[1] == "append")
710+
wand.AppendText(tokens);
707711
break;
708712
case true when cmds[i].StartsWith("clear"):
709713
if (tokens.Length == 1)
@@ -747,19 +751,17 @@ public void RunCommand(string command, bool fromScript)
747751
case true when cmds[i].StartsWith("script"):
748752
if (tokens.Length == 2)
749753
{
750-
AddCommandToConsole(tokens);
751-
if (tokens.Length > 0)
752-
{
753-
if (script == null)
754-
script = new ScriptInterpreter(tokens[1]);
754+
string[] scriptInput = cmds;
755+
if (script == null)
756+
script = new ScriptInterpreter(tokens[1]);
755757

756-
script = null; //delete object when script has been run
757-
}
758-
else
759-
{
760-
AddTextToConsole("Incorrect ammount of parameters in command...");
761-
return;
762-
}
758+
script = null; //delete object when script has been run
759+
AddCommandToConsole(scriptInput);
760+
}
761+
else
762+
{
763+
AddTextToConsole("Incorrect ammount of parameters in command...");
764+
return;
763765
}
764766
break;
765767
case true when cmds[i].StartsWith("ftp"):

CustomShell/ScriptInterpreter.cs

Lines changed: 108 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
34
using System.IO;
45

56
namespace CustomShell
@@ -8,6 +9,8 @@ class ScriptInterpreter
89
{
910
MainController main = MainController.controller;
1011

12+
DataTable dt = new DataTable();
13+
1114
string[] lines;
1215
string[] file;
1316
List<NUM> nums = new List<NUM>();
@@ -45,14 +48,14 @@ private struct GOTO
4548
private struct IF
4649
{
4750
public string statement;
51+
public string name;
4852
public int line;
4953
public int? endLine;
50-
public int index;
5154
}
5255

5356
private struct ENDIF
5457
{
55-
public int index;
58+
public string name;
5659
public int line;
5760
public int? startLine;
5861
}
@@ -66,7 +69,7 @@ private void ReadScriptFile(string filePath)
6669
{
6770
filePath = main.GetPathType(filePath);
6871

69-
if (!filePath.EndsWith(".srp"))
72+
if (!filePath.EndsWith(".rls"))
7073
return;
7174

7275
lines = File.ReadAllLines(filePath);
@@ -79,8 +82,6 @@ private void CreateTokens()
7982
if (!lines[0].Equals("[SCRIPT]"))
8083
return;
8184

82-
int ifIndex = 0;
83-
int endifIndex = 0;
8485
for (int i = 0; i < lines.Length; ++i)
8586
{
8687
if (string.IsNullOrWhiteSpace(lines[i]))//Skip all empty lines
@@ -112,12 +113,12 @@ private void CreateTokens()
112113
{
113114
IF iF;
114115
iF.line = i;
115-
iF.statement = string.Concat(tokens[1], " ",tokens[2], " ", tokens[3]);
116-
iF.index = ifIndex;
116+
iF.name = tokens[1];
117+
iF.statement = string.Concat(tokens[3], " ",tokens[4], " ", tokens[5]);
117118
iF.endLine = null;
118119
for (int x = 0; x < endifs.Count; ++x)//Check if an if statement can be paired to an end line
119120
{
120-
if (iF.index == endifs[x].index)
121+
if (iF.name == endifs[x].name)
121122
iF.endLine = endifs[x].line;
122123
}
123124

@@ -127,20 +128,21 @@ private void CreateTokens()
127128
else if (lines[i].Contains("endif"))
128129
{
129130
ENDIF endif;
131+
endif.name = tokens[1];
130132
endif.line = i;
131-
endif.index = endifIndex;
132133
endif.startLine = null;
133134
for (int x = 0; x < ifs.Count; ++x)
134135
{
135-
if (endif.index == ifs[x].index)
136+
if (tokens[1] == ifs[x].name)
136137
{
137138
endif.startLine = ifs[x].line;
138-
ifs[x] = new IF { endLine = endif.line, index = ifs[x].index, line = ifs[x].line, statement = ifs[x].statement }; //Create a new copy of the if struct with a modified end line
139+
ifs[x] = new IF { endLine = endif.line, line = ifs[x].line, name = ifs[x].name, statement = ifs[x].statement }; //Create a new copy of the if struct with a modified end line
139140
}
140141
}
141142
endifs.Add(endif);
142143
}
143144
}
145+
HandleComments();
144146
InterpretTokens();
145147
}
146148

@@ -153,6 +155,7 @@ private void InterpretTokens()
153155

154156
lines[i] = lines[i].Trim();
155157
string[] tokens = lines[i].Split();
158+
HandleComments();
156159

157160
for (int j = 0; j < tokens.Length; ++j)
158161
{
@@ -168,7 +171,7 @@ private void InterpretTokens()
168171

169172
lines[i] = string.Join(" ", tokens); //Insert the modified string back into the main file array
170173

171-
if (lines[i].Contains("goto"))
174+
if (lines[i].StartsWith("goto"))
172175
{
173176
for (int x = 0; x < labels.Count; ++x)
174177
{
@@ -182,17 +185,23 @@ private void InterpretTokens()
182185
{
183186
for (int z = 0; z < ifs.Count; ++z)
184187
{
185-
if (ifs[z].line == i)
188+
if (ifs[z].line == i && ifs[z].name == tokens[1])
186189
{
187-
i = (int)ifs[z].endLine - 1;
190+
int l = GetEndifIndex(tokens[1]);
191+
i = endifs[l].line;
188192
continue;
189193
}
190194
}
191195
}
192196
}
193-
else if (lines[i].Contains("[END]"))
197+
else if(tokens[0] == "print")
198+
{
199+
string output = string.Join(" ", tokens, 1, tokens.Length - 1);
200+
main.AddTextToConsole(output);
201+
}
202+
else if (lines[i].StartsWith("[END]"))
194203
return;
195-
else if (!lines[i].Contains("NUM") && !lines[i].Contains("label") && !lines[i].Contains("[SCRIPT]") && !lines[i].Contains("endif") && LineContainsVariable(lines[i]) == false)//If line doesn't contain a keyword, run it as a command
204+
else if (!lines[i].StartsWith("NUM") && !lines[i].StartsWith("label") && !lines[i].StartsWith("[SCRIPT]") && !lines[i].Contains("endif") && !lines[i].StartsWith("print") && LineContainsVariable(lines[i]) == false)//If line doesn't contain a keyword, run it as a command
196205
main.RunCommand(lines[i], true);
197206

198207
CheckForVariableChange(tokens);
@@ -212,34 +221,53 @@ private bool LineContainsVariable(string line)
212221
return containsVar;
213222
}
214223

224+
private void HandleComments()
225+
{
226+
for (int i = 0; i < lines.Length; i++)
227+
{
228+
if (lines[i].Contains("#"))
229+
{
230+
int commentIndex = 0;
231+
commentIndex = lines[i].IndexOf("#");
232+
233+
if (commentIndex == 0)
234+
file[i] = "";
235+
else if(commentIndex > 0)
236+
file[i] = file[i].Substring(0, commentIndex);
237+
238+
file.CopyTo(lines, 0);
239+
}
240+
}
241+
}
242+
215243
private bool isValidStatement(string[] statement)
216244
{
217245
bool valid = false;
218246

219-
switch (statement[2])
247+
switch (statement[4])
220248
{
221249
case "==":
222-
if (statement[1] == statement[3])
250+
if (statement[3] == statement[5])
223251
valid = true;
224252
break;
225253
case "<":
226-
if (Convert.ToSingle(statement[1]) < Convert.ToSingle(statement[3]))
254+
if (Convert.ToSingle(statement[3]) < Convert.ToSingle(statement[5]))
227255
valid = true;
228256
break;
229257
case "<=":
230-
if (Convert.ToSingle(statement[1]) <= Convert.ToSingle(statement[3]))
258+
if (Convert.ToSingle(statement[3]) <= Convert.ToSingle(statement[5]))
231259
valid = true;
232260
break;
233261
case ">":
234-
if (Convert.ToSingle(statement[1]) > Convert.ToSingle(statement[3]))
262+
if (Convert.ToSingle(statement[3]) > Convert.ToSingle(statement[5]))
235263
valid = true;
236264
break;
237265
case ">=":
238-
if (Convert.ToSingle(statement[1]) >= Convert.ToSingle(statement[3]))
266+
if (Convert.ToSingle(statement[3]) >= Convert.ToSingle(statement[5]))
239267
valid = true;
240268
break;
241269
case "!=":
242-
if (statement[1] != statement[3])
270+
if (statement[3] != statement[5])
243271
valid = true;
244272
break;
245273
default:
@@ -251,51 +279,48 @@ private bool isValidStatement(string[] statement)
251279

252280
private void CheckForVariableChange(string[] tokens)
253281
{
254-
for (int l = 0; l < tokens.Length; ++l) //Look for lines where a NUM variable changes in value, only +, -, ++, --
282+
if (!IsNumVar(tokens[0]))
283+
return;
284+
285+
int numIndex = GetNumIndex(tokens[0]);
286+
287+
//Check for var++ and var--
288+
if (IsNumVar(tokens[0]) && tokens.Length == 1 && tokens[0].EndsWith("++"))
255289
{
256-
for (int j = 0; j < nums.Count; ++j)
257-
{
258-
if (tokens[j].StartsWith(nums[j].name))
259-
{
260-
if (tokens.Length >= 2)
261-
{
262-
if (tokens[j + 1] == "+")
263-
{
264-
ChangeNUMValue(nums[j], j, true, Convert.ToSingle(tokens[2]));
265-
}
266-
else if (tokens[j + 1] == "-")
267-
{
268-
ChangeNUMValue(nums[j], j, false, Convert.ToSingle(tokens[2]));
269-
}
270-
else if (tokens[j + 1] == "=")
271-
{
272-
ChangeNUMValue(nums[j], j, false, Convert.ToSingle(tokens[2]));
273-
}
274-
return;
275-
}
276-
else if (tokens.Length == 1)
277-
{
278-
if (tokens[j].EndsWith("++"))
279-
{
280-
ChangeNUMValue(nums[j], j, true, 1.0f);
281-
}
282-
else if (tokens[j].EndsWith("--"))
283-
{
284-
ChangeNUMValue(nums[j], j, false, 1.0f);
285-
}
286-
return;
287-
}
288-
}
289-
}
290+
ChangeNUMValue(nums[numIndex], numIndex, true, 1.0f);
291+
return;
292+
}
293+
else if (IsNumVar(tokens[0]) && tokens.Length == 1 && tokens[0].EndsWith("--"))
294+
{
295+
ChangeNUMValue(nums[numIndex], numIndex, false, 1.0f);
296+
return;
297+
}
298+
299+
for (int i = 0; i < tokens.Length; ++i) //Skip first two tokens since they are not part of the new value
300+
{
301+
if (IsNumVar(tokens[i]))
302+
tokens[i] = nums[GetNumIndex(tokens[i])].value.ToString();
290303
}
304+
305+
string equation = string.Empty;
306+
307+
if(tokens[1] == "=")
308+
equation = string.Join(" ", tokens, 2, tokens.Length - 2);
309+
else
310+
equation = string.Join(" ", tokens);
311+
312+
float answer = Convert.ToSingle(dt.Compute(equation, ""));
313+
nums[numIndex] = new NUM { name = nums[numIndex].name, value = answer };
314+
315+
file.CopyTo(lines, 0); //Reset the lines array to default
291316
}
292317

293318
private bool IsNumVar(string name)
294319
{
295320
bool isVar = false;
296321
for (int i = 0; i < nums.Count; ++i)
297322
{
298-
if (name == nums[i].name)
323+
if (name == nums[i].name || name == nums[i].name + "++" || name == nums[i].name + "--")
299324
isVar = true;
300325
}
301326
return isVar;
@@ -305,14 +330,35 @@ private float GetNumValue(string name)
305330
{
306331
for (int i = 0; i < nums.Count; ++i)
307332
{
308-
if (name == nums[i].name)
309-
{
333+
if (name == nums[i].name || name == nums[i].name + "++" || name == nums[i].name + "--")
310334
return nums[i].value;
311-
}
312335
}
313336
return 0.0f; //This should never be reached since a check for IsNumVar should always have happened before calling this
314337
}
315338

339+
private int GetNumIndex(string name)
340+
{
341+
int index = 0;
342+
for (int i = 0; i < nums.Count; ++i)
343+
{
344+
if (name == nums[i].name)
345+
index = i;
346+
}
347+
348+
return index;
349+
}
350+
351+
private int GetEndifIndex(string name)
352+
{
353+
int index = 0;
354+
for (int i = 0; i < endifs.Count; ++i)
355+
{
356+
if (endifs[i].name == name)
357+
index = i;
358+
}
359+
return index;
360+
}
361+
316362
private void ChangeNUMValue(NUM number, int index, bool add, float change)
317363
{
318364
float newValue = 0.0f;

CustomShell/WandEditor.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ public void LoadFile(string[] tokens)
8686
}
8787
}
8888

89+
public void AppendText(string[] tokens)
90+
{
91+
try
92+
{
93+
File.AppendAllText(tokens[1], "\n" + tokens[2]);
94+
}
95+
catch (Exception)
96+
{
97+
main.wandTextBox.Visible = false;
98+
main.outputBox.Visible = true;
99+
main.AddTextToConsole("File not valid...");
100+
}
101+
}
102+
89103
public void DuplicateLine()
90104
{
91105
int index = main.wandTextBox.GetLineFromCharIndex(main.wandTextBox.SelectionStart);
@@ -119,7 +133,7 @@ public void ApplySyntaxHighlight()
119133
syntaxHighlighted = true;
120134
string[] types = new string[] {"int ", "integer ", "float ", "single ", "double ", "decimal ", "bool ", "boolean ", "string "};
121135
string[] operators = new string[] {"!", "=", ">", "<", "|", "@", "%", "+", "-", "*", "/", "\\", "?", ";", ":"};
122-
string[] statements = new string[] {"if", "else if", "elif", "if else", "else", "true", "false", "try", "catch", "finally", "public", "private", "protected", "static", "using", "import", "include", "define", "void", "while", "for", "return", "continue", "break"};
136+
string[] statements = new string[] {"if", "else if", "elif", "elseif", "endif", "if else", "else", "true", "false", "try", "catch", "finally", "public", "private", "protected", "static", "using", "import", "include", "define", "void", "while", "for", "return", "continue", "break"};
123137
string[] misc = new string[] {"#", "$", "\"", "'", "region", "endregion"};
124138
string[] numbers = new string[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
125139

0 commit comments

Comments
 (0)