Ejemplo n.º 1
0
        //*、/の計算
        public override Result VisitExpr_multipricative([NotNull] calcParser.Expr_multipricativeContext context)
        {
            switch (context.op.Type)
            {
            case calcParser.ASTERISK:
                return(CalcMulti(context));

            case calcParser.SLASH:
                return(CalcDiv(context));

            default: return(DefaultResult);
            }
        }
Ejemplo n.º 2
0
        ///の計算
        private Result CalcDiv([NotNull] calcParser.Expr_multipricativeContext context)
        {
            var(lSuc, ltype, lValue) = Visit(context.lhs);
            var(rSuc, rtype, rValue) = Visit(context.rhs);
            if (!(lSuc && rSuc))
            {
                return(DefaultResult);
            }

            try
            {
                checked
                {
                    if ((int)rValue == 0)
                    {
                        return(new Result(false, CalcRule.None, (int)ErrString.ErrID.ZeroDiv));
                    }
                    else
                    {
                        if ((ltype == CalcRule.CalcInt) && (rtype == CalcRule.CalcInt))
                        {
                            if (((int)lValue % (int)rValue) == 0)
                            {
                                return(new Result(true, CalcRule.CalcInt, GetValInt(ltype, lValue) / GetValInt(rtype, rValue)));
                            }
                            else
                            {
                                return(new Result(true, CalcRule.CalcReal, GetValFloat(ltype, lValue) / GetValFloat(rtype, rValue)));
                            }
                        }
                        else
                        {
                            return(new Result(true, CalcRule.CalcReal, GetValFloat(ltype, lValue) / GetValFloat(rtype, rValue)));
                        }
                    }
                }
            }
            catch (OverflowException)
            {
                return(new Result(false, CalcRule.None, (int)ErrString.ErrID.OverFlow));
            }
        }
Ejemplo n.º 3
0
        //*の計算
        private Result CalcMulti([NotNull] calcParser.Expr_multipricativeContext context)
        {
            var(lSuc, ltype, lValue) = Visit(context.lhs);
            var(rSuc, rtype, rValue) = Visit(context.rhs);
            if (!(lSuc && rSuc))
            {
                return(DefaultResult);
            }

            try
            {
                checked
                {
                    if ((ltype == CalcRule.CalcInt) && (rtype == CalcRule.CalcInt))
                    {
                        return(new Result(true, CalcRule.CalcInt, GetValInt(ltype, lValue) * GetValInt(rtype, rValue)));
                    }
                    else if ((ltype == CalcRule.CalcString) && (rtype == CalcRule.CalcString))
                    {
                        Debug.Assert(false);
                        return(DefaultResult);
                    }
                    else if ((ltype == CalcRule.CalcReal) && (rtype == CalcRule.CalcReal))
                    {
                        return(new Result(true, CalcRule.CalcReal, GetValFloat(ltype, lValue) * GetValFloat(rtype, rValue)));
                    }
                    else if (((ltype == CalcRule.CalcInt) && (rtype == CalcRule.CalcReal)) ||
                             ((ltype == CalcRule.CalcReal) && (rtype == CalcRule.CalcInt)))
                    {
                        return(new Result(true, CalcRule.CalcReal, GetValFloat(ltype, lValue) * GetValFloat(rtype, rValue)));
                    }
                    else if (((ltype == CalcRule.CalcInt) && (rtype == CalcRule.CalcString)) ||
                             ((ltype == CalcRule.CalcString) && (rtype == CalcRule.CalcInt)))
                    {
                        var    iNum = 0;
                        string str;
                        string strReturn = "";
                        if (ltype == CalcRule.CalcInt)
                        {
                            iNum = GetValInt(ltype, lValue);
                            str  = (string)rValue;
                        }
                        else
                        {
                            iNum = GetValInt(rtype, rValue);
                            str  = (string)lValue;
                        }
                        for (int i = 0; i < iNum; i++)
                        {
                            strReturn += str;
                        }

                        return(new Result(true, CalcRule.CalcString, strReturn));
                    }
                    else
                    {
                        Debug.Assert(false);
                        return(DefaultResult);
                    }
                }
            }
            catch (OverflowException)
            {
                return(new Result(false, CalcRule.None, (int)ErrString.ErrID.OverFlow));
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Visit a parse tree produced by the <c>expr_multipricative</c>
 /// labeled alternative in <see cref="calcParser.expr"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitExpr_multipricative([NotNull] calcParser.Expr_multipricativeContext context)
 {
     return(VisitChildren(context));
 }