Ejemplo n.º 1
0
        private void Button1_Click(object sender, EventArgs e)
        {
            bool  flag;
            float Operand1;
            float Operand2;
            float answer;

            flag = float.TryParse(txtOperand1.Text, out Operand1);
            if (flag == false)
            {
                MessageBox.Show("Enter a whole number", "Input Error");
                txtOperand1.Focus();
                return;
            }


            flag = float.TryParse(txtOperand2.Text, out Operand2);
            if (flag == false)
            {
                MessageBox.Show("Enter a whole number", "Input Error");
                txtOperand2.Focus();
                return;
            }
            answer            = Operand1 / Operand2;
            txtResult.Text    = Operand1.ToString() + "devided by" + Operand2.ToString() + "equal" + answer.ToString();
            txtResult.Visible = true;
        }
Ejemplo n.º 2
0
        public override string ToString()
        {
            string s = Opcode.ToString();

            if (Size != 0)
            {
                s = s + "/" + Size.ToString();
            }

            if (Operand1 != null)
            {
                s = s + " " + Operand1.ToString();
            }
            if (Operand2 != null)
            {
                s = s + ", " + Operand2.ToString();
            }
            if (Operand3 != null)
            {
                s = s + ", " + Operand3.ToString();
            }
            if (Operand4 != null)
            {
                s = s + ", " + Operand4.ToString();
            }

            return(s);
        }
Ejemplo n.º 3
0
        public override string ToString()
        {
            string Operand1String = Operand1.ToString();
            string Operand2String = Operand2.ToString();

            return($"({Operand1String}) {Type} ({Operand2String})");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the string representation of the expression.
        /// </summary>
        /// <returns>Returns the string representation of the expression.</returns>
        public override string ToString()
        {
            var sb = new StringBuilder();

            if (Operand1.IsAtomOrPrimitive())
            {
                sb.Append(Operand1);
            }
            else
            {
                sb.Append("(").Append(Operand1.ToString()).Append(")");
            }
            sb.Append(" ").Append(Operator1).Append(" ");
            if (Operand2.IsAtomOrPrimitive())
            {
                sb.Append(Operand2);
            }
            else
            {
                sb.Append("(").Append(Operand2.ToString()).Append(")");
            }
            sb.Append(" ").Append(Operator2).Append(" ");
            if (Operand3.IsAtomOrPrimitive())
            {
                sb.Append(Operand3);
            }
            else
            {
                sb.Append("(").Append(Operand3.ToString()).Append(")");
            }
            return(sb.ToString());
        }
Ejemplo n.º 5
0
        private void Calc()
        {
            bool hasError = false;

            if (Operand1 == null || Operand2 == null || Operation == "")
            {
//				_logger.error("input data not valid");
                return;
            }
            switch (Operation)
            {
            case "+":
                _result = Operand1 + Operand2;
                break;

            case "-":
                _result = Operand1 - Operand2;
                break;

            case "*":
                _result = Operand1 * Operand2;
                break;

            case "^2":
                _result = Operand1 * Operand1;
                break;

            case "/":
                if (0 == Operand2)
                {
                    _logger.Error("divide by 0");
                    hasError = true;
                    break;
                }
                _result = Operand1 / Operand2;
                break;

            default:
                _logger.Error($"invalid operation \"{Operation}\"");
                hasError = true;
                break;
            }
            if (hasError)
            {
                return;
            }
            _logger.Log(
                Operand1.ToString(),
                Operation != "^2" ? Operand2.ToString() : "",
                Operation,
                _result ?? default(double)
                );
            Operand1  = _result;
            Operation = "";
            Operand2  = null;
        }
Ejemplo n.º 6
0
        public string ToString(bool includeValue, bool reduceParentheses)
        {
            if (Operator == Operator.Create)
            {
                return(Value.ToString());
            }

            StringBuilder stringBuilder = new StringBuilder();

            if (includeValue)
            {
                stringBuilder.Append(Value);
                stringBuilder.Append("=");
            }

            if (Operand1.Operator == Operator.Create ||
                !includeValue && reduceParentheses && (Operator == Operator.Add || Operator == Operator.Subtract || (Operator == Operator.Multiply || Operator == Operator.Divide) && (Operand1.Operator == Operator.Multiply || Operand1.Operator == Operator.Divide)))
            {
                stringBuilder.Append(Operand1.ToString(includeValue, reduceParentheses));
            }
            else
            {
                stringBuilder.Append(String.Format("({0})", Operand1.ToString(includeValue, reduceParentheses)));
            }

            stringBuilder.Append(GetOperatorString(Operator));

            if (Operand2.Operator == Operator.Create ||
                !includeValue && reduceParentheses && (Operator == Operator.Add || (Operator == Operator.Subtract || Operator == Operator.Multiply || Operator == Operator.Divide) && (Operand2.Operator == Operator.Multiply || Operand2.Operator == Operator.Divide)))
            {
                stringBuilder.Append(Operand2.ToString(includeValue, reduceParentheses));
            }
            else
            {
                stringBuilder.Append(String.Format("({0})", Operand2.ToString(includeValue, reduceParentheses)));
            }

            return(stringBuilder.ToString());
        }
Ejemplo n.º 7
0
        public override string ToString()
        {
            var symbol = SymbolToken.OperatorStringOf(Type);
            var left   = Operand1.ToString();

            if (Operand1.Type.Order() < Type.Order())
            {
                left = $"({left})";
            }

            var right = Operand2.ToString();

            if (Operand2.Type.Order() < Type.Order())
            {
                right = $"({right})";
            }

            // we reverse here since we want the more complex stuff on the left.
            // our transforms tend to push them to the right, and we can do this
            // since multiplication is commutative.
            return($"{right} {symbol} {left}");
        }
Ejemplo n.º 8
0
        public override string ToString()
        {
            // In an ideal world we would convert this into subtraction when
            // applicable. This is the lazy way without doing re-tokenization.
            var symbol = SymbolToken.OperatorStringOf(Type);
            var left   = Operand1.ToString();

            if (Operand1.Type.Order() < Type.Order())
            {
                left = $"({left})";
            }

            var right = Operand2.ToString();

            if (Operand2.Type.Order() < Type.Order())
            {
                right = $"({right})";
            }

            // we reverse here since we want the more complex stuff on the left.
            // our transforms tend to push them to the right, and we can do this
            // since multiplication is commutative.
            return($"{right} {symbol} {left}");
        }
Ejemplo n.º 9
0
 public override string ToString()
 {
     return((Operand1 == null ? "<null>" : Operand1.ToString())
            + ' ' + OutputVisitor.OperatorString(OperatorToken) + ' '
            + (Operand2 == null ? "<null>" : Operand2.ToString()));
 }
Ejemplo n.º 10
0
 public override string ToString()
 {
     return((Operand1 == null ? "<null>" : Operand1.ToString())
            + ' ' + OperatorToken.ToString() + ' '
            + (Operand2 == null ? "<null>" : Operand2.ToString()));
 }
Ejemplo n.º 11
0
        static private string InterpretExpression(string expression, out MemorySlot result)
        {
            List <ExpressionMember> postfix_form;
            var postfix_result = GetPostfixForm(expression, out postfix_form);

            if (postfix_result != null)
            {
                result = new MemorySlot("Unknown", null);
                return(postfix_result);
            }

            var OperandStack = new Stack <MemorySlot>();

            try
            {
                MemorySlot Operand1_;
                MemorySlot Operand2_;
                dynamic    Operand1;
                dynamic    Operand2;
                for (var postfix_item = 0; postfix_item < postfix_form.Count; postfix_item++)
                {
                    if (postfix_form[postfix_item].data == null)
                    {
                        if (//postfix_form[postfix_item].operation == OperatorType.Decrement || postfix_form[postfix_item].operation == OperatorType.Increment ||
                            postfix_form[postfix_item].operation == OperatorType.Not || postfix_form[postfix_item].operation == OperatorType.UnaryMinus ||
                            postfix_form[postfix_item].operation == OperatorType.UnaryPlus)
                        {
                            Operand1_ = OperandStack.Pop();
                            if (Operand1_.GetRealTypeVariable(out Operand1))
                            {
                                switch (postfix_form[postfix_item].operation)
                                {
                                //case OperatorType.Decrement:
                                //    Operand1--;
                                //    OperandStack.Push(new MemorySlot(Operand1_.DataType, Operand1.ToString()));
                                //    break;
                                //case OperatorType.Increment:
                                //    Operand1++;
                                //    OperandStack.Push(new MemorySlot(Operand1_.DataType, Operand1.ToString()));
                                //    break;
                                case OperatorType.UnaryMinus:
                                    Operand1 = -Operand1;
                                    OperandStack.Push(new MemorySlot(Operand1_.DataType, Operand1.ToString()));
                                    break;

                                case OperatorType.UnaryPlus:
                                    Operand1 = +Operand1;
                                    OperandStack.Push(new MemorySlot(Operand1_.DataType, Operand1.ToString()));
                                    break;

                                case OperatorType.Not:
                                    Operand1 = !Operand1;
                                    OperandStack.Push(new MemorySlot(Operand1_.DataType, Operand1.ToString()));
                                    break;
                                }
                            }
                            else
                            {
                                result = new MemorySlot("Unknown", null);
                                return("BAD_CAST");
                            }
                        }
                        else
                        {
                            Operand2_ = OperandStack.Pop();
                            Operand1_ = OperandStack.Pop();
                            if (Operand1_.GetRealTypeVariable(out Operand1) | Operand2_.GetRealTypeVariable(out Operand2))
                            {
                                switch (postfix_form[postfix_item].operation)
                                {
                                case OperatorType.Add:
                                    Operand1 = Operand1 + Operand2;
                                    string var_type;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.And:
                                    Operand1 = Operand1 & Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.Divide:
                                    Operand1 = Operand1 / Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.Equal:
                                    Operand1 = Operand1 == Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.LeftShift:
                                    Operand1 = Operand1 << Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.Less:
                                    Operand1 = Operand1 < Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.LessOrEqual:
                                    Operand1 = Operand1 <= Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.ModuleDivide:
                                    Operand1 = Operand1 % Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.More:
                                    Operand1 = Operand1 > Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.MoreOrEqual:
                                    Operand1 = Operand1 >= Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.Multiply:
                                    Operand1 = Operand1 * Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.NotEqual:
                                    Operand1 = Operand1 != Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.Or:
                                    Operand1 = Operand1 | Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.RightShift:
                                    Operand1 = Operand1 >> Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.Substract:
                                    Operand1 = Operand1 - Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                case OperatorType.Xor:
                                    Operand1 = Operand1 ^ Operand2;
                                    if (!GetVariableTypeFromCSharp(Operand1, out var_type))
                                    {
                                        result = new MemorySlot("Unknown", null);
                                        return("BAD_TYPE");
                                    }
                                    OperandStack.Push(new MemorySlot(var_type, Operand1.ToString()));
                                    break;

                                default:
                                    result = new MemorySlot("Unknown", null);
                                    return("BAD_OPERATOR");
                                }
                            }
                            else
                            {
                                result = new MemorySlot("Unknown", null);
                                return("BAD_CAST");
                            }
                        }
                    }
                    else
                    {
                        if (postfix_form[postfix_item].data == null)
                        {
                            result = new MemorySlot("Unknown", null);
                            return("BAD_VARIABLE");
                        }
                        OperandStack.Push(postfix_form[postfix_item].data.Value);
                    }
                }
            }
            catch (InvalidCastException)
            {
                result = new MemorySlot("Unknown", null);
                return("BAD_TYPE");
            }
            catch
            {
                result = new MemorySlot("Unknown", null);
                return("BAD_EXPRESSION");
            }
            if (OperandStack.Count != 1)
            {
                result = new MemorySlot("Unknown", null);
                return("BAD_EXPRESSION");
            }
            result = OperandStack.Pop();
            return(null);
        }
Ejemplo n.º 12
0
 public override string ToString() => "( " + Operand1.ToString() + " * " + Operand2.ToString() + " )";