Skip to content

Commit

Permalink
Added #106
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Oct 17, 2015
1 parent e4ac11f commit 3c04f61
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
22 changes: 16 additions & 6 deletions xFunc.Maths/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,12 @@ public IEnumerable<IToken> Tokenize(string function)
if (letter == '0' && i + 1 < function.Length)
{
var nextLetter = function[i + 1];
if (nextLetter == 'x' && i + 2 < function.Length)
if (nextLetter == 'x' && i + 2 < function.Length) // hex
{
i += 2;

length = 1;
for (j = i + 1; j < function.Length && (char.IsDigit(function[j]) || (function[j] >= 97 && function[j] <= 102)); j++)
for (j = i + 1; j < function.Length && (char.IsDigit(function[j]) || (function[j] >= 97 && function[j] <= 102)); j++) // from 'a' to 'f'
length++;

strNumber = function.Substring(i, length);
Expand All @@ -383,7 +383,7 @@ public IEnumerable<IToken> Tokenize(string function)
i += length;
continue;
}
if (nextLetter == 'b' && i + 2 < function.Length)
if (nextLetter == 'b' && i + 2 < function.Length) // bin
{
i += 2;

Expand All @@ -398,7 +398,7 @@ public IEnumerable<IToken> Tokenize(string function)
i += length;
continue;
}
if (char.IsDigit(nextLetter))
if (char.IsDigit(nextLetter)) // oct
{
length = 1;
for (j = i + 1; j < function.Length && char.IsDigit(function[j]); j++)
Expand All @@ -413,6 +413,8 @@ public IEnumerable<IToken> Tokenize(string function)
}
}

// normal number (dec)

length = 1;
for (j = i + 1; j < function.Length && char.IsDigit(function[j]); j++)
length++;
Expand All @@ -424,6 +426,16 @@ public IEnumerable<IToken> Tokenize(string function)
length++;
}

if (CheckNextSymbol(function, i + length - 1, 'e')) // exp notation
{
length++;
if (CheckNextSymbol(function, i + length - 1, '-'))
length++;

for (j = i + length; j < function.Length && char.IsDigit(function[j]); j++)
length++;
}

strNumber = function.Substring(i, length);
number = double.Parse(strNumber, CultureInfo.InvariantCulture);
tokens.Add(new NumberToken(number));
Expand All @@ -432,9 +444,7 @@ public IEnumerable<IToken> Tokenize(string function)

var f = function.Substring(i);
if (i < function.Length && char.IsLetter(function[i]) && !notVar.Any(f.StartsWith))
{
tokens.Add(new OperationToken(Operations.Multiplication));
}

continue;
}
Expand Down
35 changes: 35 additions & 0 deletions xFunc.Tests/MathLexerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,41 @@ public void Brackets()
Assert.Equal(expected, tokens.ToList());
}

[Fact]
public void ExpNumber1()
{
var tokens = lexer.Tokenize("1.2345E-10");
var expected = new List<IToken>
{
new NumberToken(0.00000000012345)
};
Assert.Equal(expected, tokens.ToList());
}

[Fact]
public void ExpNumber2()
{
var tokens = lexer.Tokenize("1.2345E10");
var expected = new List<IToken>
{
new NumberToken(12345000000)
};
Assert.Equal(expected, tokens.ToList());
}

[Fact]
public void ExpNumber3()
{
var tokens = lexer.Tokenize("1.2e2 + 2.1e-3");
var expected = new List<IToken>
{
new NumberToken(120),
new OperationToken(Operations.Addition),
new NumberToken(0.0021)
};
Assert.Equal(expected, tokens.ToList());
}

[Fact]
public void Add()
{
Expand Down

0 comments on commit 3c04f61

Please sign in to comment.