Skip to content

Commit 398b078

Browse files
committed
Completed first version of the scripting language
1 parent 569e87e commit 398b078

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

CustomShell/MainController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,13 @@ public void RunCommand(string command, bool fromScript)
764764
case true when cmds[i].StartsWith("script"):
765765
if (tokens.Length == 2)
766766
{
767+
AddCommandToConsole(tokens);
767768
if (tokens.Length > 0)
768769
{
769-
AddCommandToConsole(tokens);
770770
if (script == null)
771771
script = new ScriptInterpreter(tokens[1]);
772+
773+
script = null; //delete object when script has been run
772774
}
773775
else
774776
{

CustomShell/ScriptInterpreter.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ private void InterpretTokens()
149149
{
150150
for (int i = 0; i < lines.Length; ++i)
151151
{
152-
lines = file; //Reset the file each iteration
153-
154152
if (string.IsNullOrWhiteSpace(lines[i]))//Skip all empty lines
155153
continue;
156154

@@ -161,7 +159,7 @@ private void InterpretTokens()
161159
{
162160
for (int l = 0; l < nums.Count; ++l)
163161
{
164-
if (tokens[j].Equals(nums[l].name) && tokens[j - 1] != "NUM")
162+
if (tokens[j].Equals(nums[l].name) && lines[i].Contains("if")) //If line contains both "variable name" and "if"
165163
{
166164
tokens[j] = nums[l].value.ToString(); //Replace variable names with the appropriate number
167165
break;
@@ -187,21 +185,34 @@ private void InterpretTokens()
187185
{
188186
if (ifs[z].line == i)
189187
{
190-
i = (int)ifs[z].endLine;
188+
i = (int)ifs[z].endLine - 1;
191189
continue;
192190
}
193191
}
194192
}
195193
}
196-
else if (!lines[i].Contains("NUM") && !lines[i].Contains("label") && !lines[i].Contains("[SCRIPT]"))//If line doesn't contain a keyword, run it as a command
197-
main.RunCommand(lines[i], true);
198194
else if (lines[i].Contains("[END]"))
199195
return;
196+
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
197+
main.RunCommand(lines[i], true);
200198

201199
CheckForVariableChange(tokens);
202200
}
203201
}
204202

203+
private bool LineContainsVariable(string line)
204+
{
205+
bool containsVar = false;
206+
for (int i = 0; i < nums.Count; ++i)
207+
{
208+
if (line.Contains(nums[i].name)) //Line contains a variable
209+
containsVar = true;
210+
else
211+
containsVar = false;
212+
}
213+
return containsVar;
214+
}
215+
205216
private bool isValidStatement(string[] statement)
206217
{
207218
bool valid = false;
@@ -250,16 +261,28 @@ private void CheckForVariableChange(string[] tokens)
250261
if (tokens.Length >= 2)
251262
{
252263
if (tokens[j + 1] == "+")
264+
{
253265
ChangeNUMValue(nums[j], j, true, Convert.ToSingle(tokens[2]));
266+
return;
267+
}
254268
else if (tokens[j + 1] == "-")
269+
{
255270
ChangeNUMValue(nums[j], j, false, Convert.ToSingle(tokens[2]));
271+
return;
272+
}
256273
}
257274
else
258275
{
259276
if (tokens[j].EndsWith("++"))
277+
{
260278
ChangeNUMValue(nums[j], j, true, 1.0f);
279+
return;
280+
}
261281
else if (tokens[j].EndsWith("--"))
282+
{
262283
ChangeNUMValue(nums[j], j, false, 1.0f);
284+
return;
285+
}
263286
}
264287
}
265288
}
@@ -275,6 +298,7 @@ private void ChangeNUMValue(NUM number, int index, bool add, float change)
275298
newValue = number.value - change;
276299

277300
nums[index] = new NUM { name = number.name, value = newValue };
301+
file.CopyTo(lines, 0); //Reset the file each iteration
278302
}
279303
}
280304
}

0 commit comments

Comments
 (0)