I have a string "(2+2)*3S1(2)+Sin(1+2/S1(2))" and list of operations like ["(", ",", ")","+","Sin"...] without S1.
Now I want to split this string on list of lexemes.
In case of S1 not in the available operations - exception or whatever
In case of all correct - array of lexems.
What I've already done:
const string input = "(2+2)*3S1(2)+Sin(1+2/S1(2))";;
var lexemes = new List<string>();
for (var i = 0; i < input.Length; i++)
{
var currentNumber = new StringBuilder();
while(i < input.Length && (Int32.TryParse(input[i].ToString(), out _) || input[i]=='.'))
{
currentNumber.Append(input[i]);
i++;
}
if (currentNumber.Length > 0)
{
lexemes.Add(currentNumber.ToString());
}
if (i >= input.Length)
{
break;
}
var currentOp = new StringBuilder();
while(i < input.Length)
{
if (operations.Count(x => x.Name.StartsWith(currentOp.ToString() + input[i])) > 0)
{
currentOp.Append(input[i]);
i++;
}
else
{
break;
}
}
i--;
var operation = operations.Single(x => x.Name == currentOp.ToString());
lexemes.Add(operation.Name);
}
My problem is:
- I don't know how to detect wrong lexem
- I don't understand why this code wrong and skip everythin in Sin arguments
- Well.. This code smells not good enough...
Hm.. It's alive!
And my list op operations smth like:
Yeah, 4 and '(' is hardcode and magicwtf, but this is better than previous version..