Skip to content

Commit

Permalink
Close #311 - Remove nullable tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Sep 14, 2020
1 parent 14c6ee0 commit ee2f5f0
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 183 deletions.
38 changes: 19 additions & 19 deletions xFunc.Maths/Parser.ExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace xFunc.Maths
/// </summary>
public partial class Parser
{
private IExpression CreateFunction(Token token, IList<IExpression> arguments)
private IExpression CreateFunction(in Token token, IList<IExpression> arguments)
{
Debug.Assert(token.IsId(), "Token should be Id.");
Debug.Assert(!string.IsNullOrWhiteSpace(token.StringValue), "Id is empty.");
Expand Down Expand Up @@ -175,7 +175,7 @@ private IExpression CreateAssign(IExpression first, IExpression second)
=> new Define(first, second);

private IExpression CreateBinaryAssign(
Token token,
in Token token,
Variable first,
IExpression second)
{
Expand All @@ -196,7 +196,7 @@ private IExpression CreateAssign(IExpression first, IExpression second)
return new RightShiftAssign(first, second);
}

private IExpression CreateUnaryAssign(Token token, Variable first)
private IExpression CreateUnaryAssign(in Token token, Variable first)
{
if (token.Is(IncrementOperator))
return new Inc(first);
Expand All @@ -208,7 +208,7 @@ private IExpression CreateUnaryAssign(Token token, Variable first)
}

private IExpression CreateConditionalOperator(
Token token,
in Token token,
IExpression first,
IExpression second)
{
Expand All @@ -222,7 +222,7 @@ private IExpression CreateUnaryAssign(Token token, Variable first)
}

private IExpression CreateBitwiseOperator(
Token token,
in Token token,
IExpression first,
IExpression second)
{
Expand All @@ -247,7 +247,7 @@ private IExpression CreateUnaryAssign(Token token, Variable first)
}

private IExpression CreateEqualityOperator(
Token token,
in Token token,
IExpression first,
IExpression second)
{
Expand All @@ -268,7 +268,7 @@ private IExpression CreateUnaryAssign(Token token, Variable first)
return new GreaterOrEqual(first, second);
}

private IExpression CreateShift(Token token, IExpression first, IExpression second)
private IExpression CreateShift(in Token token, IExpression first, IExpression second)
{
if (token.Is(LeftShiftOperator))
return new LeftShift(first, second);
Expand All @@ -279,7 +279,7 @@ private IExpression CreateShift(Token token, IExpression first, IExpression seco
return new RightShift(first, second);
}

private IExpression CreateAddSub(Token token, IExpression first, IExpression second)
private IExpression CreateAddSub(in Token token, IExpression first, IExpression second)
{
if (token.Is(PlusOperator))
return new Add(first, second);
Expand All @@ -290,7 +290,7 @@ private IExpression CreateAddSub(Token token, IExpression first, IExpression sec
return new Sub(first, second);
}

private IExpression CreateMulDivMod(Token token, IExpression first, IExpression second)
private IExpression CreateMulDivMod(in Token token, IExpression first, IExpression second)
{
if (token.Is(MultiplicationOperator))
return new Mul(first, second);
Expand All @@ -315,7 +315,7 @@ private IExpression CreateExponentiation(IExpression first, IExpression second)
private IExpression CreateFactorial(IExpression first)
=> new Fact(first);

private IExpression CreateBoolean(Token token)
private IExpression CreateBoolean(in Token token)
{
if (token.Is(TrueKeyword))
return Bool.True;
Expand All @@ -328,20 +328,20 @@ private IExpression CreateBoolean(Token token)

// TODO: positional cache
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private IExpression CreateNumber(Token numberToken)
private IExpression CreateNumber(in Token numberToken)
=> new Number(numberToken.NumberValue);

private IExpression CreateAngleNumber(Token numberToken, AngleUnit unit)
private IExpression CreateAngleNumber(in Token numberToken, AngleUnit unit)
=> new AngleValue(numberToken.NumberValue, unit).AsExpression();

private IExpression CreateComplexNumber(
Token? magnitudeSign,
Token magnitude,
Token? phaseSign,
Token phase)
in Token magnitudeSign,
in Token magnitude,
in Token phaseSign,
in Token phase)
{
static int GetSign(Token? token)
=> token != null && token.Value.Is(MinusOperator) ? -1 : 1;
static int GetSign(Token token)
=> token.Is(MinusOperator) ? -1 : 1;

var magnitudeNumber = magnitude.NumberValue * GetSign(magnitudeSign);
var phaseNumber = phase.NumberValue * GetSign(phaseSign);
Expand All @@ -351,7 +351,7 @@ static int GetSign(Token? token)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Variable CreateVariable(Token token)
private Variable CreateVariable(in Token token)
{
Debug.Assert(!string.IsNullOrWhiteSpace(token.StringValue), "Id is null.");

Expand Down
26 changes: 13 additions & 13 deletions xFunc.Maths/Parser.TokenReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public partial class Parser
// points to last write position (item != null)
private int writeIndex;

private Token?[] buffer;
private Token[] buffer;

private int scopeCount;

Expand All @@ -50,27 +50,27 @@ public TokenReader(ref Lexer lexer)
enumerableEnded = false;
readIndex = -1;
writeIndex = -1;
buffer = ArrayPool<Token?>.Shared.Rent(BufferSize);
buffer = ArrayPool<Token>.Shared.Rent(BufferSize);
scopeCount = 0;
}

public void Dispose()
{
ArrayPool<Token?>.Shared.Return(buffer);
ArrayPool<Token>.Shared.Return(buffer);
}

private void EnsureEnoughSpace()
{
if (writeIndex < buffer.Length)
return;

var newBuffer = ArrayPool<Token?>.Shared.Rent(buffer.Length * 2);
var newBuffer = ArrayPool<Token>.Shared.Rent(buffer.Length * 2);
Array.Copy(buffer, newBuffer, buffer.Length);
ArrayPool<Token?>.Shared.Return(buffer);
ArrayPool<Token>.Shared.Return(buffer);
buffer = newBuffer;
}

private Token? Read()
private ref readonly Token Read()
{
// readIndex > writeIndex
Debug.Assert(readIndex <= writeIndex, "The read index should be less than or equal to write index.");
Expand All @@ -96,7 +96,7 @@ private void EnsureEnoughSpace()
Debug.Assert(writeIndex >= 0, "The write index should be greater than or equal to 0.");

// read from buffer
return buffer[readIndex + 1];
return ref buffer[readIndex + 1];
}

private void Rollback(int index)
Expand Down Expand Up @@ -142,26 +142,26 @@ public void Commit()
Flush();
}

public Token? GetCurrent() => Read();
public Token GetCurrent() => Read();

public Token? GetCurrent(TokenKind kind)
public Token GetCurrent(TokenKind kind)
{
var token = Read();
if (token != null && token.Value.Is(kind))
if (token.Is(kind))
{
AdvanceToNextPosition();

return token;
}

return null;
return Token.Empty;
}

public bool Check(TokenKind kind) => GetCurrent(kind) != null;
public bool Check(TokenKind kind) => !GetCurrent(kind).IsEmpty();

public bool IsEnd => enumerableEnded;

public readonly struct Scope
public readonly ref struct Scope
{
private readonly int position;

Expand Down

0 comments on commit ee2f5f0

Please sign in to comment.