Skip to content

Commit

Permalink
Removed unnecessary abstraction around evaluating exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Aug 20, 2016
1 parent 5beb220 commit 3ff6715
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 151 deletions.
2 changes: 1 addition & 1 deletion PInvoke.Core/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Exception CreateInvalidEnumValueException<T>(T value) where T : st
return new ContractException(message);
}

public static void Require(bool b)
public static void Requires(bool b)
{
ThrowIfFalse(b);
}
Expand Down
87 changes: 69 additions & 18 deletions PInvoke.Core/Parser/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

namespace PInvoke.Parser
{

/// <summary>
/// Used to evaluate basic expressions encounter by the parser.
/// Used to evaluate basic expressions encounter by the parser.
/// </summary>
/// <remarks></remarks>
public class ExpressionEvaluator
public sealed class ExpressionEvaluator
{
private ExpressionParser _parser = new ExpressionParser();
private readonly ExpressionParser _parser = new ExpressionParser();
private readonly Dictionary<string, Macro> _macroMap;
private readonly ScannerOptions _opts;

private ScannerOptions _opts;
public ExpressionEvaluator()
public ExpressionEvaluator(Dictionary<string, Macro> macroMap = null)
{
_macroMap = macroMap ?? new Dictionary<string, Macro>();
_opts = new ScannerOptions();
_opts.HideComments = true;
_opts.HideNewLines = true;
Expand Down Expand Up @@ -94,27 +94,40 @@ private bool TryEvaluateCore(ExpressionNode node)
}
}

protected virtual bool TryEvaluateCast(ExpressionNode node)
/// <summary>
/// For a cast just return the value of the left node
/// </summary>
private bool TryEvaluateCast(ExpressionNode node)
{
return false;
// CTODO: why left here? Shouldn't it be right?
node.Tag = node.LeftNode.Tag;
return true;
}

protected virtual bool TryEvaluateFunctionCall(ExpressionNode node)
private bool TryEvaluateFunctionCall(ExpressionNode node)
{
return false;
bool value =
node.Token.Value == "defined" &&
node.LeftNode != null &&
_macroMap.ContainsKey(node.LeftNode.Token.Value);

node.Tag = ExpressionValue.Create(value);
return true;
}

protected virtual bool TryEvaluateNegation(ExpressionNode node)
private bool TryEvaluateNegation(ExpressionNode node)
{
return false;
ExpressionValue value = (ExpressionValue)node.LeftNode.Tag;
node.Tag = ExpressionValue.Create(!value.ConvertToBool());
return true;
}

protected virtual bool TryEvaluateList(ExpressionNode node)
private bool TryEvaluateList(ExpressionNode node)
{
return true;
}

protected virtual bool TryEvaluateNegative(ExpressionNode node)
private bool TryEvaluateNegative(ExpressionNode node)
{
var exprValue = ((ExpressionValue)node.LeftNode.Tag);
if (exprValue.IsFloatingPoint)
Expand All @@ -131,7 +144,7 @@ protected virtual bool TryEvaluateNegative(ExpressionNode node)
return true;
}

protected virtual bool TryEvaluateLeaf(ExpressionNode node)
private bool TryEvaluateLeaf(ExpressionNode node)
{
Token token = node.Token;
if (token.IsNumber)
Expand All @@ -154,6 +167,11 @@ protected virtual bool TryEvaluateLeaf(ExpressionNode node)
node.Tag = ExpressionValue.Create(false);
return true;
}
else if (token.TokenType == TokenType.Word)
{
return TryEvaluateMacro(node);
}

else if (token.IsCharacter)
{
char cValue = '0';
Expand All @@ -174,13 +192,46 @@ protected virtual bool TryEvaluateLeaf(ExpressionNode node)
node.Tag = ExpressionValue.Create(sValue);
return true;
}
else if (TokenHelper.IsKeyword(node.Token.TokenType))
{
node.Tag = ExpressionValue.Create(1);
return true;
}
else
{
return false;
}
}

protected virtual bool TryEvaluateBinaryOperation(ExpressionNode node)
private bool TryEvaluateMacro(ExpressionNode node)
{
Contract.Requires(node.Kind == ExpressionKind.Leaf);
Contract.Requires(node.Token.TokenType == TokenType.Word);

ExpressionValue value = default(ExpressionValue);
Macro m = null;
if (_macroMap.TryGetValue(node.Token.Value, out m))
{
Number numValue;
if (TokenHelper.TryConvertToNumber(m.Value, out numValue))
{
value = ExpressionValue.Create(numValue);
}
else
{
value = ExpressionValue.Create(1);
}
}
else
{
value = ExpressionValue.Create(0);
}

node.Tag = value;
return true;
}

private bool TryEvaluateBinaryOperation(ExpressionNode node)
{
BinaryOperator op;
if (!TryConvertToBinaryOperator(node.Token.TokenType, out op))
Expand All @@ -196,7 +247,7 @@ protected virtual bool TryEvaluateBinaryOperation(ExpressionNode node)
return succeeded;
}

public static bool TryConvertToBinaryOperator(TokenType type,out BinaryOperator op)
public static bool TryConvertToBinaryOperator(TokenType type, out BinaryOperator op)
{
switch (type)
{
Expand Down
3 changes: 1 addition & 2 deletions PInvoke.Core/Parser/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public enum NumberKind
Double
}

// CTODO: Add in the equality tests
public struct Number : IEquatable<Number>
{
public static Number Empty => default(Number);
Expand Down Expand Up @@ -90,7 +89,7 @@ public double ConvertToDouble()

private T Get<T>(NumberKind kind)
{
Contract.Require(kind == Kind);
Contract.Requires(kind == Kind);
return (T)Value;
}

Expand Down
Loading

0 comments on commit 3ff6715

Please sign in to comment.