Further to Solution 1...
1. You are using an
ArrayList
to store "realtokens". ArrayLists are not strongly typed, i.e. you can have a mixture of types in there. Since .NET 2.n you should only use ArrayList if you absolutely have to (link to some old API for example).
Consider using
List<string></string>
instead e.g.
List<string> realtokens = new List<string>();
for (int i = 0; i < listBox6.Items.Count; i++)
{
realtokens.Add(listBox6.Items[i].ToString());
}
You might get the exception thrown earlier where it's easier to spot what's wrong.
2. It also means that you don't have to do all of that casting, for example ...
(((String)realtokens[j] == "dint"
becomes
((realtokens[j] == "dint"
and all of your code becomes easier to read - and more efficient.
3. Now let's look at the line
if (((String)realtokens[j] == "dint" || (String)realtokens[j] == "dig" || (String)realtokens[j] == "chart") && ident((String)realtokens[j + 1]) == true && (String)realtokens[j + 2] == ":")
which is apparently causing the problem... except it is going to be simpler now.
if ((realtokens[j] == "dint" || realtokens[j] == "dig" || realtokens[j] == "chart") && ident(realtokens[j + 1]) == true && realtokens[j + 2] == ":")
In fact because we have forced the list to be all
string
the error may have already gone away.
When you are debugging, don't just try to F10 over the line, dig into it with F11 - or use the Immediate window in Visual Studio to print the various values out. That way you can drill down to the point of failure ... e.g. type this into the immediate window
? ident(realtokens[j + 1])
and hit enter. If you don't get an error then pull out another part of the offending line.
4. A final gift.
That line
else if ((String)realtokens[h] == "(" && (ident((String)realtokens[h + 1]) == true) && ((String)realtokens[h + 2] == "++" || (String)realtokens[h + 2] == "--" || (String)realtokens[h + 2] == "**" || (String)realtokens[h + 2] == "^/" || (String)realtokens[h + 2] == "/>" || (String)realtokens[h + 2] == "/<" || (String)realtokens[h + 2] == "</=" || (String)realtokens[h + 2] == ">/=" || (String)realtokens[h + 2] == "/==" && (ident((String)realtokens[h + 3]) == true) && (String)realtokens[h + 4] == ")"))
is truly awful and impossible to read. We've just got rid of all of the casting, but also get rid of any brackets that aren't actually required (they just confuse matters).
Also consider using whitespace to make things clearer, and dropping
== true
(it's not required). The whole thing then looks like this...
else if (realtokens[h] == "("
&& ident(realtokens[h + 1])
&& (realtokens[h + 2] == "++" || realtokens[h + 2] == "--" || realtokens[h + 2] == "**" || realtokens[h + 2] == "^/" || realtokens[h + 2] == "/>" || realtokens[h + 2] == "/<" || realtokens[h + 2] == "</=" || realtokens[h + 2] == ">/=" || realtokens[h + 2] == "/=="
&& (ident(realtokens[h + 3]) == true)
&& realtokens[h + 4] == ")"))
Hm... a little better but still room for improvement.
Create a function something like this ...
private bool IsIn(string test, string[] against)
{
if (against.Contains(test))
return true;
else
return false;
}
then you can replace all those OR's (||) with a call to that function
&& IsIn(realtokens[h + 2], new string[]{"++","--","**","^/","/>","/<","</=",">/=","/=="})