string MassageInput(string s, out bool wasAssignment)
{
// process the words in reverse order when looking for assignments!
MatchCollection words = wordPattern.Matches(s);
Match[] wordArray = new Match[words.Count];
words.CopyTo(wordArray, 0);
Array.Reverse(wordArray);
wasAssignment = false;
bool varDeclaration = false;
for (int i = 0; i < wordArray.Length; i++)
{
Match m = wordArray[i];
// exclude matches found inside strings
if (m.Index > 0 && (quoteRegex.Matches(s.Substring(0, m.Index)).Count % 2) != 0 && (quoteRegex.Matches(s.Substring(m.Index)).Count % 2) != 0)
continue;
string sym = m.Value;
if (!mustDeclare) // strip the '$'
sym = sym.EndsWith("}") ? sym.Substring(2, sym.Length - 3) : sym.Substring(1);
else
{ // either it's a declaration, or the var was previously declared.
if (sym == "var")
continue;
// are we preceded by 'var'? If so, this is a declaration
if (i + 1 < wordArray.Length && wordArray[i + 1].Value == "var")
varDeclaration = true;
else if (varTable[sym] == null)
continue;
}
string symRef = "V[\"" + sym + "\"]"; // will index our hashtable
// are we followed by an assignment operator?
Match lhs = assignment.Match(s, m.Index);
wasAssignment = lhs.Success && lhs.Index == m.Index;
object symVal = varTable[sym];
string symTypeName = (symVal == null) ? null : GetPublicRuntimeTypeName(symVal.GetType(), false);
// unless we're on the LHS, try to strongly type this variable reference.
if ((!string.IsNullOrEmpty(symTypeName)) && (!wasAssignment))
{
symRef = "((" + symTypeName + ")" + symRef + ")";
}
s = wordPattern.Replace(s, symRef, 1, m.Index);
}
if (varDeclaration)
s = s.Replace("var ", "");
return s;
}