Commit 995ac668 authored by Alexander Dietsch's avatar Alexander Dietsch

LWSimulator Console:

-First working version of console based simulator

LWSimulatorGUI:
-Added Undo/Redo

MainSimulator:
-Minor Bugfix on exceptions if syntax errors were to occur in line 1
parent e4c99fdc
This diff is collapsed.
This diff is collapsed.
......@@ -535,8 +535,8 @@ namespace LWSimulatorGUI
private void highlightText()
{
if (onUnix)
return;
/*if (onUnix)
return;*/
if (InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
......@@ -1196,6 +1196,19 @@ namespace LWSimulatorGUI
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
if(fCTBProgram.CanUndo)
fCTBProgram.Undo();
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
if(fCTBProgram.CanRedo)
fCTBProgram.Redo();
}
#endregion
private void importToolStripMenuItem_Click(object sender, EventArgs e)
......@@ -1271,6 +1284,10 @@ namespace LWSimulatorGUI
else
return;
}
/*
* Test functions to print a string how it looks in the program
*/
public string EscapeForCSharp(string str)
{
StringBuilder sb = new StringBuilder();
......
......@@ -377,6 +377,7 @@ namespace MainLWSimulator
if(!String.IsNullOrEmpty(path) && Directory.Exists(path))
{
string compare = AppDomain.CurrentDomain.BaseDirectory;
// Make sure that OS specific char for directories is at the very end
if (path[path.Length-1]!=compare[compare.Length-1])
{
path = path + compare[compare.Length - 1];
......@@ -1049,12 +1050,13 @@ namespace MainLWSimulator
foreach (ParseError p in tPGtree.Errors)
{
int errorLine = 0;
int errorPosition;
while (p.Position > lineLengths[errorLine] && errorLine < lineLengths.Count)
int errorPosition = p.Position;
while (errorPosition > lineLengths[errorLine] && errorLine < lineLengths.Count)
errorLine++;
errorLine--;
errorPosition = p.Position - lineLengths[errorLine];
if (errorLine != -1)
errorPosition = errorPosition - lineLengths[errorLine];
logMessage(string.Format("Syntax error in line {0}, position {1}: {2}", errorLine + 1, errorPosition, p.Message));
}
......
......@@ -934,7 +934,7 @@ namespace MainLWSimulator
/// <param name="amIn">Amount of input registers provided</param>
/// <param name="amAux">Amount of auxiliary registers provided</param>
/// <param name="callNode">Node calling the macro</param>
/// <returns></returns>
/// <returns>True on correct number; false otherwise</returns>
private bool checkRightAmountReg(ParseNode macro, int amOut, int amIn, int amAux,ParseNode callNode )
{
int res = (macro.Nodes[2].Nodes.Count - 1) / 2;
......@@ -1082,225 +1082,6 @@ namespace MainLWSimulator
return line;
}
/*
/// <summary>
/// Interpretes given command; for more information see syntax of loop and while programs
/// </summary>
/// <param name="command">A complete line of the code containing</param>
/// <returns>False if an error occured (e.g. invalid command); true otherwise.</returns>
private bool interpreteCommand(string command)
{
string modifiedCommand = TrimAllWithInplaceCharArray(command);
if(modifiedCommand.StartsWith("x"))
{
if (!interpreteCommandRegisterAction(modifiedCommand))
return false;
}
else
{
parent.writeLogMessage(string.Format("Invalid command in line {0}.", currentNode.Token.StartPos));
return false;
}
parent.writeLogMessage(string.Format("Line {0}: {1}", tree.getCurrentLine(), command));
return true;
}
/// <summary>
/// Method that is called when command is determined as action that involves changing a register.
/// </summary>
/// <param name="command">Command line</param>
/// <returns></returns>
private bool interpreteCommandRegisterAction(string command)
{
int[] regs = { -1, -1, -1 };
long[] vals = { -1, -1, -1 };
char operation;
int index;
readRegNumb(command, ref regs[0], 1, out index);
if (command[index] != ':' && command[index + 1] != '=')
{
parent.writeLogMessage(string.Format("Invalid operation in line {0}.", tree.getCurrentLine()));
return false;
}
index = index + 2;
if (command[index] == 'x')
{
if(!readRegNumb(command, ref regs[1], index + 1, out index))
return false;
vals[1] = registers.getRegister(regs[1]);
if (vals[1] == -1)
{
parent.writeLogMessage(string.Format("Invalid register in line {0}.", tree.getCurrentLine()));
return false;
}
}
else
{
long numRes;
if (!readNumb(command, index, out numRes, out index))
{
parent.writeLogMessage(string.Format("Invalid number in line {0}.", tree.getCurrentLine()));
return false;
}
vals[1] = numRes;
}
if (vals[1] > registers.getMaxVal())
{
parent.writeLogMessage(string.Format("Number in line {0} exceeds max allowed value.", tree.getCurrentLine()));
return false;
}
// check for end of command
if (index < command.Length - 1)
{
operation = command[index];
if (command[index + 1] == 'x')
{
if (!readRegNumb(command, ref regs[2], index + 2, out index))
return false;
vals[2] = registers.getRegister(regs[2]);
if (vals[2] == -1)
{
parent.writeLogMessage(string.Format("Invalid register in line {0}.", tree.getCurrentLine()));
return false;
}
}
else
{
long numRes;
if (!readNumb(command, index + 1, out numRes, out index))
{
parent.writeLogMessage(string.Format("Invalid number in line {0}.", tree.getCurrentLine()));
return false;
}
vals[2] = numRes;
}
if (vals[2] > registers.getMaxVal())
{
parent.writeLogMessage(string.Format("Number in line {0} exceeds max allowed value.", tree.getCurrentLine()));
return false;
}
// End of command must be here
if (index < command.Length - 1)
{
if(command[index] != ';')
{
parent.writeLogMessage(string.Format("Unknown symbol after last register/number in line {0}.", tree.getCurrentLine()));
return false;
}
}
switch (operation)
{
case ('+'):
registers.editRegister(regs[0], vals[1] + vals[2], true);
break;
case ('-'):
registers.editRegister(regs[0], vals[1] - vals[2], true);
break;
case ('*'):
if ((registers.getMaxVal() / vals[1]) < vals[2] + 1)
{
parent.writeLogMessage(string.Format("WARNING: When executing line {0} the result exceeded the max value for a register.", tree.getCurrentLine()));
registers.editRegister(regs[0], registers.getMaxVal(), true);
}
else
registers.editRegister(regs[0], vals[1] * vals[2], true);
break;
case ('/'):
registers.editRegister(regs[0], (long)(vals[1] / vals[2]), true);
break;
default:
parent.writeLogMessage(string.Format("Invalid operation in line {0}.", tree.getCurrentLine()));
return false;
}
}
else
{
registers.editRegister(regs[0], vals[1], true);
}
return true;
}
/// <summary>
/// Reads in the next number and stores it as index of the register.
/// </summary>
/// <param name="str">Command that is currently interpreted</param>
/// <param name="register">Register that is currently being determined</param>
/// <param name="startPos">Starting position in command string</param>
/// <param name="endPos">End position in command string</param>
/// <returns></returns>
private bool readRegNumb(string str, ref int register, int startPos, out int endPos)
{
endPos = -1;
long outNumb;
if (!readNumb(str, startPos, out outNumb, out endPos))
{
parent.writeLogMessage(string.Format("Invalid register in line {0}.", tree.getCurrentLine()));
return false;
}
if (outNumb >= registers.getSize())
{
parent.writeLogMessage(string.Format("Invalid register in line {0}.", tree.getCurrentLine()));
return false;
}
register = (int)outNumb;
return true;
}
/// <summary>
/// Reads the next number in the string.
/// </summary>
/// <param name="str">String to read number from</param>
/// <param name="startPos">Starting position in string</param>
/// <param name="number">Number that is found</param>
/// <param name="endPos">End position which doesn't contain any more digit.</param>
/// <returns>True if valid number was found; false otherwise (also false if number exceeds long boundries)</returns>
private bool readNumb(string str,int startPos,out long number, out int endPos)
{
number = -1;
endPos = -1;
if (str == null)
return false;
if (startPos < 0 || startPos > str.Length)
return false;
string result = "";
for (int i = startPos; i < str.Length; i++)
{
if (char.IsDigit(str[i]))
result += str[i];
else
{
endPos = i;
break;
}
}
if (endPos == -1)
endPos = str.Length;
if (!long.TryParse(result, out number))
{
return false;
}
if (number < 0)
return false;
return true;
}*/
/// <summary>
/// Apparently fastest hand-optimized method with managed code to remove all whitespace characters of a string.
/// </summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment