using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;
namespace Basic_Debugger
{
public class Debugger
{
public Debugger(BasicProject project)
{
_project = project;
}
BasicProject _project;
Regex r;
public bool Check(out int[] errorLines)
{
List<int> errors = new List<int>();
string[] lines = _project.Code.MainCode.Split("\n".ToCharArray());
for (int I = 0; I < lines.Length; I++)
{
string[] words = lines[I].Split(" ".ToCharArray());
if (words.Length > 0)
{
if (!CheckLine(lines[I], words[0]))
errors.Add(I);
}
}
errorLines = new int[errors.Count];
for (int I = 0; I < errors.Count; I++)
errorLines[I] = errors[I];
if (errors.Count > 0)
return false;
return true;
}
private bool CheckLine(string line, string command)
{
switch (command.ToUpper())
{
case "PRINT":
r = new Regex("^PRINT\\s*" +
"(?:(?<text>(?:\\-?(?:[a-zA-Z0-9]+[$%]|\\d)(?:\\s*[\\+\\-\\*\\/]\\s*(?:[a-zA-Z0-9]+[$%]|\\d+))*))" +
"|\\\"(?<text>[^\"]*?)\\\")?" +
"(?:\\s*[,;]\\s*" +
"(?:(?<text>(?:\\-?(?:[a-zA-Z0-9]+[$%]|\\d)(?:\\s*[\\+\\-\\*\\/]\\s*(?:[a-zA-Z0-9]+[$%]|\\d+))*))" +
"|\\\"(?<text>[^\"]*?)\\\"))*\\s*(?:'.*)?$");
return r.IsMatch(line);
case "INPUT":
r = new Regex("^INPUT\\s\".*\"\\s*[,;]\\s*[a-zA-Z0-9]*[$%](\\s*'.*)?$");
return r.IsMatch(line);
case "SLEEP":
r = new Regex("^SLEEP(\\s*'.*)?$");
return r.IsMatch(line);
case "REM":
return true;
case "'":
return true;
default:
r = new Regex("^[a-zA-Z0-9]+[%]\\s*\\=\\s*" + "(?<math>\\-?[a-zA-Z0-9]+[%]|\\d)(?<math>\\s*[\\+\\-\\*\\/]\\s*(?:[a-zA-Z0-9]+[%]|\\d+))*");
if (r.IsMatch(line))
return true;
r = new Regex( "^[a-zA-Z0-9]+[$]\\s*\\=\\s*" +
"(?:(?<text>(?:\\-?(?:[a-zA-Z0-9]+[$%]|\\d)(?:\\s*[\\+\\-\\*\\/]\\s*(?:[a-zA-Z0-9]+[$%]|\\d+))*))" +
"|\\\"(?<text>.*?)\\\")?" +
"(?:\\s*[\\+]\\s*" +
"(?:(?<text>(?:\\-?(?:[a-zA-Z0-9]+[$%]|\\d)(?:\\\\s*[\\+\\-\\\\*\\/]\\s*(?:[a-zA-Z0-9]+[$%]|\\d+))*))" +
"|\\\"(?<text>.*?)\\\"))*\\s*(?:'.*)?$(\\\\s*'.*)?");
if (r.IsMatch(line))
return true;
return false;
}
}
public bool Debug(out int errorline, DoWorkEventArgs e)
{
return RunQBasic.RunCode(_project, out errorline, e);
}
}
}