コード例 #1
0
        private void NewOperation(RpnOperation operation)
        {
            while (expressionStack.TryPeek(out var stackOp) &&
                   !(stackOp is null) &&
                   !(stackOp.HasLessPriorityThan(operation)))
            {
                AddRpn(expressionStack.Pop());
            }

            expressionStack.Push(operation);
        }
コード例 #2
0
        /// <summary>
        /// Adds a new unary operation.
        /// </summary>
        public void UnaryOperation(Token token)
        {
            RpnOperation rpn = token.Value switch
            {
                "!" => new RpnNot(token),
                "-" => new RpnNegate(token),
                "$" => new RpnGetValue(token, variables),
                var op => throw new RpnCreationException("Unknown unary operation: " + op)
            };

            NewOperation(rpn);
        }
コード例 #3
0
        /// <summary>
        /// Adds a new binary operation.
        /// </summary>
        public void BinaryOperation(Token token)
        {
            RpnOperation rpn = token.Value switch
            {
                "=" => new RpnAssign(token, variables),
                "+" => new RpnAdd(token),
                "-" => new RpnSubtract(token),
                "*" => new RpnMultiply(token),
                "/" => new RpnDivide(token),
                "%" => new RpnMod(token),
                "|" => new RpnOr(token),
                "&" => new RpnAnd(token),
                "~" => new RpnEqual(token),
                ">" => new RpnGreater(token),
                "<" => new RpnLess(token),
                ":" => new RpnCast(token),
                "?" => new RpnCheckCast(token),
                "->" => new RpnRightAssign(token, variables),
                var op => throw new RpnCreationException("Unknown binary operation: " + op)
            };

            NewOperation(rpn);
        }