Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unaryOperatorsDictionary to manage custom operators that are both unaries and binaries better #160

Merged
merged 1 commit into from
Sep 22, 2023

Conversation

Yazwh0
Copy link
Contributor

@Yazwh0 Yazwh0 commented Apr 15, 2023

Add unary operators so they can be added to.

Eg the code below adds three new ones, as described in #159 .

Sadly I can't get the unit tests working as I don't have framework installed.

/// <summary>
/// Adds 6502 ASM extensions to the expression evaluator.
/// </summary>
public class Asm6502ExpressionEvaluator : ExpressionEvaluator
{
    protected new static readonly IList<ExpressionOperator> rightOperandOnlyOperatorsEvaluationDictionary =
        BaseExpressionEvaluator.rightOperandOnlyOperatorsEvaluationDictionary
            .ToList()
            .FluidAdd(Asm6502Operator.LowByte)
            .FluidAdd(Asm6502Operator.HighByte)
            .FluidAdd(Asm6502Operator.TopByte);

    protected new static readonly IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> operatorsEvaluations =
        BaseExpressionEvaluator.operatorsEvaluations
            .Copy()
            .AddOperatorEvaluationAtNewLevelAfter(Asm6502Operator.LowByte, (left, right) => right & 0xff, ExpressionOperator.UnaryPlus)
            .AddOperatorEvaluationAtLevelOf(Asm6502Operator.HighByte, (left, right) => (right & 0xff00) >> 8, Asm6502Operator.LowByte)
            .AddOperatorEvaluationAtLevelOf(Asm6502Operator.TopByte, (left, right) => (right & 0xff0000) >> 16, Asm6502Operator.LowByte);

    protected override IList<ExpressionOperator> RightOperandOnlyOperatorsEvaluationDictionary => rightOperandOnlyOperatorsEvaluationDictionary;
 
    protected override IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> OperatorsEvaluations => operatorsEvaluations;

    protected override void Init()
    {
        unaryOperatorsDictionary.Add(">", Asm6502Operator.LowByte);
        unaryOperatorsDictionary.Add("<", Asm6502Operator.HighByte);
        unaryOperatorsDictionary.Add("^", Asm6502Operator.TopByte);
    }
}

internal class Asm6502Operator : ExpressionOperator
{
    public static readonly ExpressionOperator LowByte = new Asm6502Operator();
    public static readonly ExpressionOperator HighByte = new Asm6502Operator();
    public static readonly ExpressionOperator TopByte = new Asm6502Operator();
}

Add unary operators so they can be added to
@codingseb codingseb changed the title Update ExpressionEvaluator.cs Add unaryOperatorsDictionary to manage custom operators that are both unaries and binaries better Sep 22, 2023
@codingseb codingseb changed the base branch from master to TesUnaryOperators September 22, 2023 07:03
@codingseb codingseb merged commit 8221b7c into codingseb:TesUnaryOperators Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants