so i am writing a little toy compiler sort of thing in C#, and what i am trying to do is in the print command, for every substring that begins with the '$' character (eg - $foo), replace it with the appropriate variable. (basically, the '$' character indicates a variable name).
what i have done so far is to use regex to find all the substrings containing a '$' character, however i am having trouble with the replace method. How variables are stored is through the FLEE evaluator variable storage class. (FLEE being fast lightweight expression evaluator), they act as a map where the key is the variable name, and the value is the variable value.
my code is as follows:
public void print(string exp)
{
this.expression = exp;
MatchEvaluator eval = new MatchEvaluator(this.matchEval);
MatchCollection coll = Regex.Matches(exp, @"(?<!\w)\$\w+");
this.split = new string[coll.Count];
int index = 0;
foreach (Match match in coll)
{
this.split[index] = match.ToString();
index++;
}
this.i = 0;
Regex.Replace(exp, @"(?<!\w)\$\w+", eval);
Console.WriteLine(exp);
Console.ReadKey();
}
private string matchEval(Match m)
{
this.split[this.i] = this.split[this.i].TrimStart('$');
if(i != split.Length-1)
this.i++;
return this.split[this.i];
}
it doesn't return the variables yet, as it still returns the regex matches including the '$' character.
any help would be much appreciated, thanks.