private void funcFirst(Stack <RpnItemOperand> stack, int argCount) { RpnItemOperand result = null; while (argCount-- > 0) { var test = stack.Pop(); if (!test.IsNull) { result = test; } } stack.Push(result); }
private void funcMax(Stack <RpnItemOperand> stack, int argCount) { RpnItemOperand result = null; while (argCount-- > 0) { var test = stack.Pop(); if (!(test is RpnItemOperandNumeric)) { continue; } if (result == null || test.Numeric > result.Numeric) { result = test; } } stack.Push(result); }
private void typeEval() { var stack = new Stack <RpnItemOperand>(); for (var i = 0; i < _original.Count; i++) { var item = _original[i]; var operand = item as RpnItemOperand; if (operand != null) { stack.Push(operand); continue; } var variable = item as RpnIndexedVariable; if (variable != null) { stack.Push(variable.IsNumeric ? (RpnItemOperand) new RpnItemOperandNumeric(0) : new RpnItemOperandString("")); continue; } var itemOperator = item as RpnItemOperator; if (itemOperator != null) { switch (itemOperator.Operator) { case Operator.Addition: { var op2 = stack.Pop(); var op1 = stack.Pop(); if (op1 is RpnItemOperandString || op2 is RpnItemOperandString) { _original[i] = new RpnItemOperator(Operator.Concat); push(stack, ""); } else { push(stack, 0); } break; } case Operator.Negation: case Operator.Not: stack.Pop(); stack.Push(new RpnItemOperandNumeric(0)); break; case Operator.NullCoalescing: case Operator.Question: { var op2 = stack.Pop(); stack.Pop(); stack.Push(op2); break; } default: stack.Pop(); stack.Pop(); push(stack, 0); break; } continue; } var itemFunction = item as RpnItemFunction; if (itemFunction != null) { for (var j = 0; j < itemFunction.ArgumentCount; j++) { stack.Pop(); } if (_functions[itemFunction.Name].Type == typeof(string)) { push(stack, ""); } else { push(stack, 0); } continue; } throw new Exception("What the hell happened here?"); } ResultingType = stack.Single(); }