Commit adcd5bfe authored by Alexander Dietsch's avatar Alexander Dietsch

- small bugfixes

- new Pre-execution command #IMPORT: import all macros from another piece
  of code to use in your own code
- Log window should flicker less now
parent 0ba079d8
......@@ -11,6 +11,13 @@ namespace LWSimulatorGUI
{
class CustomRichTextBox : RichTextBox
{
private bool onUnix = false;
internal void setOnUnix(bool onUnix)
{
this.onUnix = onUnix;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab)
......@@ -20,6 +27,19 @@ namespace LWSimulatorGUI
return true;
}
if (keyData == Keys.Enter && !onUnix)
{
SelectionLength = 0;
int countSpaces = 0;
string line = "";
if(Lines.Length > 0)
line = Lines[GetLineFromCharIndex(SelectionStart)];
while (countSpaces < line.Length && line[countSpaces] == ' ')
countSpaces++;
SelectedText = "\n" + new string(' ', countSpaces);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
......
......@@ -10,24 +10,63 @@ namespace LWSimulatorGUI
{
public class DoubleBufferedListBox : ListBox
{
/*
* Code by http://yacsharpblog.blogspot.de/2008/07/listbox-flicker.html
*/
public DoubleBufferedListBox()
{
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
{
this.SetStyle(
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint,
true);
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DoubleBuffered = true;
}
/*
protected override void OnPaint(PaintEventArgs pe)
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (this.Items.Count > 0)
{
e.DrawBackground();
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(this.ForeColor), new PointF(e.Bounds.X, e.Bounds.Y));
}
base.OnDrawItem(e);
}
protected override void OnPaint(PaintEventArgs e)
{
// we draw the progressbar normally with
// the flags sets to our settings
base.OnPaintBackground(pe);
base.OnPaint(pe);
Region iRegion = new Region(e.ClipRectangle);
e.Graphics.FillRegion(new SolidBrush(this.BackColor), iRegion);
if (this.Items.Count > 0)
{
for (int i = 0; i < this.Items.Count; ++i)
{
System.Drawing.Rectangle irect = this.GetItemRectangle(i);
if (e.ClipRectangle.IntersectsWith(irect))
{
if ((this.SelectionMode == SelectionMode.One && this.SelectedIndex == i)
|| (this.SelectionMode == SelectionMode.MultiSimple && this.SelectedIndices.Contains(i))
|| (this.SelectionMode == SelectionMode.MultiExtended && this.SelectedIndices.Contains(i)))
{
OnDrawItem(new DrawItemEventArgs(e.Graphics, this.Font,
irect, i,
DrawItemState.Selected, this.ForeColor,
this.BackColor));
}
else
{
OnDrawItem(new DrawItemEventArgs(e.Graphics, this.Font,
irect, i,
DrawItemState.Default, this.ForeColor,
this.BackColor));
}
iRegion.Complement(irect);
}
}
}
base.OnPaint(e);
}
*/
}
}
namespace LWSimulatorGUI
using System.Windows.Forms;
namespace LWSimulatorGUI
{
partial class LogWindow
{
......@@ -34,6 +36,7 @@
this.exportToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.clearToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.btLogOk = new System.Windows.Forms.Button();
this.lBLog = new LWSimulatorGUI.DoubleBufferedListBox();
this.mSLogMenu.SuspendLayout();
this.SuspendLayout();
//
......@@ -59,14 +62,14 @@
// exportToolStripMenuItem
//
this.exportToolStripMenuItem.Name = "exportToolStripMenuItem";
this.exportToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.exportToolStripMenuItem.Size = new System.Drawing.Size(107, 22);
this.exportToolStripMenuItem.Text = "Export";
this.exportToolStripMenuItem.Click += new System.EventHandler(this.exportToolStripMenuItem_Click);
//
// clearToolStripMenuItem
//
this.clearToolStripMenuItem.Name = "clearToolStripMenuItem";
this.clearToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.clearToolStripMenuItem.Size = new System.Drawing.Size(107, 22);
this.clearToolStripMenuItem.Text = "Clear";
this.clearToolStripMenuItem.Click += new System.EventHandler(this.clearToolStripMenuItem_Click);
//
......@@ -81,6 +84,21 @@
this.btLogOk.UseVisualStyleBackColor = true;
this.btLogOk.Click += new System.EventHandler(this.btLogOk_Click);
//
// lBLog
//
this.lBLog.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lBLog.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.lBLog.FormattingEnabled = true;
this.lBLog.HorizontalScrollbar = true;
this.lBLog.Location = new System.Drawing.Point(13, 28);
this.lBLog.Name = "lBLog";
this.lBLog.SelectionMode = System.Windows.Forms.SelectionMode.None;
this.lBLog.Size = new System.Drawing.Size(343, 238);
this.lBLog.TabIndex = 1;
this.lBLog.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.lBlog_DrawItem);
//
// LogWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
......@@ -88,10 +106,10 @@
this.ClientSize = new System.Drawing.Size(368, 313);
this.Controls.Add(this.btLogOk);
this.Controls.Add(this.mSLogMenu);
this.Controls.Add(this.lBLog);
this.DoubleBuffered = true;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MainMenuStrip = this.mSLogMenu;
this.MaximizeBox = false;
this.Name = "LogWindow";
this.Text = "Log";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.LogWindow_FormClosing);
......@@ -107,7 +125,7 @@
private System.Windows.Forms.MenuStrip mSLogMenu;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exportToolStripMenuItem;
private System.Windows.Forms.ListBox lBLog;
private LWSimulatorGUI.DoubleBufferedListBox lBLog;
private System.Windows.Forms.Button btLogOk;
private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem;
}
......
......@@ -21,24 +21,7 @@ namespace LWSimulatorGUI
public LogWindow(MainGUI parent, LWCoordinator simul)
{
InitializeComponent();
//
// lBLog
//
this.lBLog = new LWSimulatorGUI.DoubleBufferedListBox();
this.lBLog.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lBLog.FormattingEnabled = true;
this.lBLog.Location = new System.Drawing.Point(13, 28);
this.lBLog.Name = "lBLog";
this.lBLog.SelectionMode = System.Windows.Forms.SelectionMode.None;
this.lBLog.Size = new System.Drawing.Size(343, 238);
this.lBLog.TabIndex = 1;
this.lBLog.DrawMode = DrawMode.OwnerDrawFixed;
this.lBLog.DrawItem += lBlog_DrawItem;
this.Controls.Add(this.lBLog);
this.PerformLayout();
//this.lBLog.DrawMode = DrawMode.OwnerDrawFixed;
this.parent = parent;
simulator = simul;
simulator.setLogFuncs(addLine_Notify, newRun, saveLog);
......@@ -84,7 +67,6 @@ namespace LWSimulatorGUI
else
textBrush = lBlogTextNormalBrush;
g.DrawString(text, e.Font, textBrush, lBLog.GetItemRectangle(index).Location);
}
e.DrawFocusRectangle();
......
......@@ -52,7 +52,6 @@
this.projectToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.runProgramToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.enableBreakpointsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.showTransitionFunctionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.displayLogToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.registersToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.insertRegistersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
......@@ -246,7 +245,6 @@
this.projectToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.runProgramToolStripMenuItem,
this.enableBreakpointsToolStripMenuItem,
this.showTransitionFunctionToolStripMenuItem,
this.displayLogToolStripMenuItem});
this.projectToolStripMenuItem.Name = "projectToolStripMenuItem";
this.projectToolStripMenuItem.Size = new System.Drawing.Size(56, 20);
......@@ -256,7 +254,7 @@
//
this.runProgramToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("runProgramToolStripMenuItem.Image")));
this.runProgramToolStripMenuItem.Name = "runProgramToolStripMenuItem";
this.runProgramToolStripMenuItem.Size = new System.Drawing.Size(209, 22);
this.runProgramToolStripMenuItem.Size = new System.Drawing.Size(174, 22);
this.runProgramToolStripMenuItem.Text = "Run Program";
this.runProgramToolStripMenuItem.Click += new System.EventHandler(this.tSBRun_Click);
//
......@@ -267,20 +265,13 @@
this.enableBreakpointsToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
this.enableBreakpointsToolStripMenuItem.Enabled = false;
this.enableBreakpointsToolStripMenuItem.Name = "enableBreakpointsToolStripMenuItem";
this.enableBreakpointsToolStripMenuItem.Size = new System.Drawing.Size(209, 22);
this.enableBreakpointsToolStripMenuItem.Size = new System.Drawing.Size(174, 22);
this.enableBreakpointsToolStripMenuItem.Text = "Enable Breakpoints";
//
// showTransitionFunctionToolStripMenuItem
//
this.showTransitionFunctionToolStripMenuItem.Enabled = false;
this.showTransitionFunctionToolStripMenuItem.Name = "showTransitionFunctionToolStripMenuItem";
this.showTransitionFunctionToolStripMenuItem.Size = new System.Drawing.Size(209, 22);
this.showTransitionFunctionToolStripMenuItem.Text = "Show Transition Function";
//
// displayLogToolStripMenuItem
//
this.displayLogToolStripMenuItem.Name = "displayLogToolStripMenuItem";
this.displayLogToolStripMenuItem.Size = new System.Drawing.Size(209, 22);
this.displayLogToolStripMenuItem.Size = new System.Drawing.Size(174, 22);
this.displayLogToolStripMenuItem.Text = "Display Log";
this.displayLogToolStripMenuItem.Click += new System.EventHandler(this.displayLogToolStripMenuItem_Click);
//
......@@ -632,7 +623,6 @@
private System.Windows.Forms.ToolStripMenuItem projectToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem runProgramToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem enableBreakpointsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem showTransitionFunctionToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem displayLogToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem registersToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem insertRegistersToolStripMenuItem;
......
......@@ -43,8 +43,8 @@ namespace LWSimulatorGUI
@"((a|i|o|x)(0|[1-9]([0-9])*))", //registers
@"(?<=(\s)|^)(def|enddef|do|loop|while|as)(?=(\s|$))", //commands ending with whitespace
@"(?<=(\s)|^)(in|out|aux)(?=(\s|:))", //commands ending with whitespace or colon
@"(?<=(\s)|^)end(?=(\s|;))", //commands ending with whitespace or semicolon
@"((?<=(\s|\())|^)(pred|succ)(?=(\s|\())", //commands ending with whitespace or bracket
@"(?<=(\s)|^)end(?=(\s|;|$))", //commands ending with whitespace or semicolon
@"((?<=(\s|\())|^)(pred|succ)(?=(\s|\())", //commands ending with whitespace or bracket
@"#[A-Z]+", //Preprocessor commands
};
......@@ -77,6 +77,7 @@ namespace LWSimulatorGUI
if (platform == 4 || platform == 6 || platform == 128)
onUnix = true;
InitializeComponent();
fCTBProgram.setOnUnix(onUnix);
// Remove ugly selection colors (usually cell has blue background => cell is selected)
/*this.dGVRegisters.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
this.dGVRegisters.DefaultCellStyle.SelectionForeColor = SystemColors.HighlightText;*/
......@@ -370,7 +371,7 @@ namespace LWSimulatorGUI
if (res == DialogResult.Yes)
{
saveFile();
saveFile(currentFilePath);
return true;
}
return false;
......@@ -858,7 +859,7 @@ namespace LWSimulatorGUI
dGVRegisters.CurrentCell = null;
dGVRegisters.Columns[1].ReadOnly = true; // Prevent deadlocks by calling this here.
}
simulator.runProgram(fCTBProgram.Text);
simulator.runProgram(fCTBProgram.Text, currentFilePath);
failSafeTimer = new System.Timers.Timer(100);
failSafeTimer.Elapsed += FailSafeTimer_Elapsed;
failSafeTimer.Enabled = true;
......@@ -896,7 +897,7 @@ namespace LWSimulatorGUI
dGVRegisters.CurrentCell = null; // Workaround to validate cell even if run is pressed
dGVRegisters.Columns[1].ReadOnly = true; // Prevent deadlocks by calling this here.
}
simulator.singleStepProgram(fCTBProgram.Text);
simulator.singleStepProgram(fCTBProgram.Text, currentFilePath);
failSafeTimer = new System.Timers.Timer(100);
failSafeTimer.Elapsed += FailSafeTimer_Elapsed;
failSafeTimer.Enabled = true;
......
......@@ -85,7 +85,7 @@
this.pPreferences.Controls.Add(this.gBRegisters);
this.pPreferences.Location = new System.Drawing.Point(13, 13);
this.pPreferences.Name = "pPreferences";
this.pPreferences.Size = new System.Drawing.Size(436, 385);
this.pPreferences.Size = new System.Drawing.Size(439, 395);
this.pPreferences.TabIndex = 0;
//
// gBEditor
......@@ -93,7 +93,7 @@
this.gBEditor.Controls.Add(this.chBHighlight);
this.gBEditor.Location = new System.Drawing.Point(4, 331);
this.gBEditor.Name = "gBEditor";
this.gBEditor.Size = new System.Drawing.Size(427, 47);
this.gBEditor.Size = new System.Drawing.Size(427, 59);
this.gBEditor.TabIndex = 3;
this.gBEditor.TabStop = false;
this.gBEditor.Text = "Texteditor";
......@@ -103,10 +103,10 @@
this.chBHighlight.AutoSize = true;
this.chBHighlight.Location = new System.Drawing.Point(10, 19);
this.chBHighlight.Name = "chBHighlight";
this.chBHighlight.Size = new System.Drawing.Size(421, 17);
this.chBHighlight.Size = new System.Drawing.Size(473, 17);
this.chBHighlight.TabIndex = 8;
this.chBHighlight.Text = "Highlight code after 2 seconds of no input (currently not supported on Unix Syste" +
"ms)";
this.chBHighlight.Text = "Highlight code after 2 seconds of no input \n(may cause unexpected behaviour on Uni" +
"x Systems)";
this.chBHighlight.UseVisualStyleBackColor = true;
//
// gBSimulation
......@@ -408,7 +408,7 @@
// bPrefCancel
//
this.bPrefCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.bPrefCancel.Location = new System.Drawing.Point(374, 404);
this.bPrefCancel.Location = new System.Drawing.Point(377, 414);
this.bPrefCancel.Name = "bPrefCancel";
this.bPrefCancel.Size = new System.Drawing.Size(75, 23);
this.bPrefCancel.TabIndex = 2;
......@@ -419,7 +419,7 @@
// bPrefOK
//
this.bPrefOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.bPrefOK.Location = new System.Drawing.Point(293, 404);
this.bPrefOK.Location = new System.Drawing.Point(296, 414);
this.bPrefOK.Name = "bPrefOK";
this.bPrefOK.Size = new System.Drawing.Size(75, 23);
this.bPrefOK.TabIndex = 3;
......@@ -435,7 +435,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(461, 439);
this.ClientSize = new System.Drawing.Size(464, 449);
this.Controls.Add(this.bPrefOK);
this.Controls.Add(this.bPrefCancel);
this.Controls.Add(this.pPreferences);
......
......@@ -17,7 +17,7 @@ namespace MainLWSimulator
internal bool onUnix;
// Hard limit for options
private Dictionary<string, bool> ALLOWRUNNINGCHANGE = new Dictionary<string, bool>(); // allow changing the value during run
private Dictionary<string, byte> ALLOWRUNNINGCHANGE = new Dictionary<string, byte>(); // allow changing the value during run; 0 - no changing, 1 - only when paused, 2 - always
private Dictionary<string, long> HARDLIMITVALSHIGH = new Dictionary<string, long>();
private Dictionary<string, long> HARDLIMITVALSLOW = new Dictionary<string, long>();
......@@ -82,6 +82,7 @@ namespace MainLWSimulator
// Programcode variables
private string program;
private List<int> lineLengths;
private string programDir;
// Dummy Function for any kind of Action with no input
private void dummyAction()
......@@ -158,15 +159,15 @@ namespace MainLWSimulator
HARDLIMITVALSLOW.Add("alwaysSaveLog", long.MinValue);
HARDLIMITVALSLOW.Add("doHighlight", long.MinValue);
ALLOWRUNNINGCHANGE.Add("maxRegAm", false);
ALLOWRUNNINGCHANGE.Add("maxStepAm", false);
ALLOWRUNNINGCHANGE.Add("defRegVal", false);
ALLOWRUNNINGCHANGE.Add("maxLogLines", true);
ALLOWRUNNINGCHANGE.Add("clearLog", true);
ALLOWRUNNINGCHANGE.Add("timePerStep", true);
ALLOWRUNNINGCHANGE.Add("advancedLog", true);
ALLOWRUNNINGCHANGE.Add("alwaysSaveLog", false);
ALLOWRUNNINGCHANGE.Add("doHighlight", true);
ALLOWRUNNINGCHANGE.Add("maxRegAm", 0);
ALLOWRUNNINGCHANGE.Add("maxStepAm", 1);
ALLOWRUNNINGCHANGE.Add("defRegVal", 0);
ALLOWRUNNINGCHANGE.Add("maxLogLines", 2);
ALLOWRUNNINGCHANGE.Add("clearLog", 2);
ALLOWRUNNINGCHANGE.Add("timePerStep", 2);
ALLOWRUNNINGCHANGE.Add("advancedLog", 2);
ALLOWRUNNINGCHANGE.Add("alwaysSaveLog", 0);
ALLOWRUNNINGCHANGE.Add("doHighlight", 2);
}
/// <summary>
......@@ -202,7 +203,10 @@ namespace MainLWSimulator
optionVals.Add("timePerStep", 100);
optionVals.Add("advancedLog", 0);
optionVals.Add("alwaysSaveLog", 0);
optionVals.Add("doHighlight", 1);
if(onUnix)
optionVals.Add("doHighlight", 0);
else
optionVals.Add("doHighlight", 1);
saveOptions();
}
}
......@@ -339,7 +343,7 @@ namespace MainLWSimulator
return false;
lock (runningLock)
{
if (running != 0 && !ALLOWRUNNINGCHANGE[name])
if ((running != 0 && ALLOWRUNNINGCHANGE[name] == 0) || (running != 0 && running != 2 && ALLOWRUNNINGCHANGE[name] == 1))
return false;
}
optionVals[name] = val;
......@@ -380,11 +384,10 @@ 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])
if (path[path.Length -1] != Path.DirectorySeparatorChar)
{
path = path + compare[compare.Length - 1];
path = path + Path.DirectorySeparatorChar;
}
lock (optionVals)
{
......@@ -591,9 +594,9 @@ namespace MainLWSimulator
/// Alternatively continues the simulation if stopped.
/// </summary>
/// <param name="code">Program code; optional parameter if already <see cref="setProgram(string)"/> was used.</param>
public void runProgram(string code = null)
public void runProgram(string code = null, string path = null)
{
setProgram(code);
setProgram(code, path);
lock (runningLock)
{
if (running != 0 && running != 2)
......@@ -648,7 +651,7 @@ namespace MainLWSimulator
/// <summary>
/// Starts simulation in SingleStep mode or continues a step.
/// </summary>
public void singleStepProgram(string code = null)
public void singleStepProgram(string code = null, string path = null)
{
lock (runningLock)
{
......@@ -660,7 +663,7 @@ namespace MainLWSimulator
if (running == 0)
{
setProgram(code);
setProgram(code, path);
if (!createDefaultEnvironment())
return;
ThreadPool.QueueUserWorkItem(unused =>
......@@ -1011,25 +1014,7 @@ namespace MainLWSimulator
}
#endregion
/*
/// <summary>
/// Sets path to the File containing the program.
/// </summary>
/// <param name="path">Path to the File containing the code.</param>
/// <returns>True on success; false if File was not found or tried to set while running simulation.</returns>
public bool setProgramFile(string path)
{
lock (runningLock)
{
if (running != 0)
{
logMessage("ERROR: Cannot load program file while running simulation.");
return false;
}
return lwPars.setFilePath(path);
}
}
*/
/// <summary>
/// Checks the syntax of a given code whether it is correct according to the specified grammar.
/// </summary>
......@@ -1037,34 +1022,7 @@ namespace MainLWSimulator
/// <returns>True if syntax is correct; false else</returns>
private bool checkSyntax(string program)
{
lineLengths = new List<int>();
lineLengths.Add(0);
for (int i = 0; i < program.Length; i++)
{
if (program[i] == '\n')
lineLengths.Add(i + 1);
}
lineLengths.Add(program.Length);
tPGtree = tPGparser.Parse(program);
if (tPGtree.Errors.Count == 0)
return true;
//Create a list with accumulative line lenghts, so no further calculations need to be done.
foreach (ParseError p in tPGtree.Errors)
{
int errorLine = 0;
int errorPosition = p.Position;
while (errorPosition > lineLengths[errorLine] && errorLine < lineLengths.Count)
errorLine++;
errorLine--;
if (errorLine != -1)
errorPosition = errorPosition - lineLengths[errorLine];
logMessage(string.Format("Syntax error in line {0}, position {1}: {2}", errorLine + 1, errorPosition, p.Message));
}
return false;
return parseProgram(out tPGtree, out lineLengths, program);
}
......@@ -1082,13 +1040,119 @@ namespace MainLWSimulator
/// Sets the programcode that will be executed on new run.
/// </summary>
/// <param name="program">Code of the program as string.</param>
public void setProgram(string program)
public void setProgram(string program, string path)
{
lock(codeLock)
{
if(!string.IsNullOrEmpty(program))
this.program = program;
if (!string.IsNullOrEmpty(path))
{
programDir = Path.GetDirectoryName(path);
if (programDir[programDir.Length - 1] != Path.DirectorySeparatorChar)
programDir = programDir + Path.DirectorySeparatorChar;
}
}
}
/// <summary>
/// Tries to import a program given in a specific path
/// </summary>
/// <param name="tree">The resulting ParseTree</param>
/// <param name="lineLength">The resulting length of lines</param>
/// <param name="path">Path to the program</param>
/// <returns>Error code; 0 - no error; 1 - no such file exists; 2 - file exists, but error on opening/reading; 3 - ParseError</returns>
internal byte importProgram(out ParseTree tree, out List<int> lineLength, out string absPath, string path)
{
string loadedProgram = null;
tree = null;
lineLength = null;
byte errNo = 1;
absPath = string.Empty;
// File without ending
if(Path.GetExtension(path) == string.Empty)
{
byte loadAttempt = importProgram(out tree, out lineLength, out absPath, path + ".txt");
// only if file does not exist, try loading it another way
if (loadAttempt != 1)
return loadAttempt;
}
if(File.Exists(programDir + path))
{
try
{
loadedProgram = File.ReadAllText(programDir + path);
absPath = Path.GetFullPath(programDir + path);
}
catch (Exception ex)
{
loadedProgram = null;
errNo = 2;
}
}
if (loadedProgram == null && File.Exists(path))
{
try
{
loadedProgram = File.ReadAllText(path);
absPath = Path.GetFullPath(programDir + path);
}
catch (Exception ex)
{
loadedProgram = null;
errNo = 2;
}
}