Skip to content

Commit

Permalink
#513 - Time Units (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 authored Mar 27, 2022
1 parent f0c4acf commit a6f8b4a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
29 changes: 28 additions & 1 deletion xFunc.Maths/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,8 @@ private ImmutableArray<IExpression> ParseParameterList(ref TokenReader tokenRead
ParseAngleUnit(ref tokenReader) ??
ParsePowerUnit(ref tokenReader) ??
ParseMassUnit(ref tokenReader) ??
ParseLengthUnit(ref tokenReader);
ParseLengthUnit(ref tokenReader) ??
ParseTimeUnit(ref tokenReader);
if (unit is not null)
return unit;

Expand Down Expand Up @@ -866,6 +867,32 @@ private ImmutableArray<IExpression> ParseParameterList(ref TokenReader tokenRead
};
});

private IExpression? ParseTimeUnit(ref TokenReader tokenReader)
=> tokenReader.Scoped(this, static (Parser _, ref TokenReader reader) =>
{
var number = reader.GetCurrent(TokenKind.Number);
if (number.IsEmpty())
return null;

var id = reader.GetCurrent(Id);
if (id.IsEmpty())
return null;

return id.StringValue switch
{
"s" => TimeValue.Second(number.NumberValue).AsExpression(),
"ns" => TimeValue.Nanosecond(number.NumberValue).AsExpression(),
"μs" => TimeValue.Microsecond(number.NumberValue).AsExpression(),
"ms" => TimeValue.Millisecond(number.NumberValue).AsExpression(),
"min" => TimeValue.Minute(number.NumberValue).AsExpression(),
"h" => TimeValue.Hour(number.NumberValue).AsExpression(),
"day" => TimeValue.Day(number.NumberValue).AsExpression(),
"week" => TimeValue.Week(number.NumberValue).AsExpression(),
"year" => TimeValue.Year(number.NumberValue).AsExpression(),
_ => null,
};
});

private IExpression? ParsePolarComplexNumber(ref TokenReader tokenReader)
=> tokenReader.Scoped(this, static (Parser _, ref TokenReader reader) =>
{
Expand Down
40 changes: 40 additions & 0 deletions xFunc.Tests/ParserTests/TimeUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace xFunc.Tests.ParserTests;

public class TimeUnitTests : BaseParserTests
{
[Fact]
public void ParseSecond()
=> ParseTest("1 s", TimeValue.Second(1).AsExpression());

[Fact]
public void ParseNanosecond()
=> ParseTest("1 ns", TimeValue.Nanosecond(1).AsExpression());

[Fact]
public void ParseMicrosecond()
=> ParseTest("1 μs", TimeValue.Microsecond(1).AsExpression());

[Fact]
public void ParseMillisecond()
=> ParseTest("1 ms", TimeValue.Millisecond(1).AsExpression());

[Fact]
public void ParseMinute()
=> ParseTest("1 min", TimeValue.Minute(1).AsExpression());

[Fact]
public void ParseHour()
=> ParseTest("1 h", TimeValue.Hour(1).AsExpression());

[Fact]
public void ParseDay()
=> ParseTest("1 day", TimeValue.Day(1).AsExpression());

[Fact]
public void ParseWeek()
=> ParseTest("1 week", TimeValue.Week(1).AsExpression());

[Fact]
public void ParseYear()
=> ParseTest("1 year", TimeValue.Year(1).AsExpression());
}

0 comments on commit a6f8b4a

Please sign in to comment.