// 引き算 private Result CalcMinus([NotNull] calcParser.Expr_additiveContext context) { var(lSuc, ltype, lValue) = Visit(context.lhs); var(rSuc, rtype, rValue) = Visit(context.rhs); if (!(lSuc && rSuc)) { return(new Result(false, CalcRule.None, (int)ErrString.ErrID.ArgNG)); } 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)) { string rStr = ((string)rValue); string lStr = ((string)lValue); // 元のながさチェック var lLen = lStr.Length; var retStr = lStr.Replace(rStr, ""); if (lLen == retStr.Length) { // 文字列の引き算に失敗 return(new Result(false, CalcRule.None, (int)ErrString.ErrID.CantMinusString)); } else { // 文字列の引き算(空白Replace)に成功 return(new Result(true, CalcRule.CalcString, retStr)); } } 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 { return(new Result(false, CalcRule.None, (int)ErrString.ErrID.UnSupportCalcRule)); } } } catch (OverflowException) { return(new Result(false, CalcRule.None, (int)ErrString.ErrID.OverFlow)); } }
//+、ーの計算 public override Result VisitExpr_additive([NotNull] calcParser.Expr_additiveContext context) { switch (context.op.Type) { // 足し算の計算 case calcParser.PLUS: return(CalcAdd(context)); case calcParser.MINUS: return(CalcMinus(context)); default: Debug.Assert(false); return(DefaultResult); } }
// 足し算 private Result CalcAdd([NotNull] calcParser.Expr_additiveContext context) { var(lSuc, ltype, lValue) = Visit(context.lhs); var(rSuc, rtype, rValue) = Visit(context.rhs); if (!(lSuc && rSuc)) { return(new Result(false, CalcRule.None, (int)ErrString.ErrID.ArgNG)); } 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)) { return(new Result(true, CalcRule.CalcString, (string)lValue + (string)rValue)); } 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 { return(new Result(false, CalcRule.None, (int)ErrString.ErrID.UnSupportCalcRule)); } } } catch (OverflowException) { return(new Result(false, CalcRule.None, (int)ErrString.ErrID.OverFlow)); } }
/// <summary> /// Visit a parse tree produced by the <c>expr_additive</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_additive([NotNull] calcParser.Expr_additiveContext context) { return(VisitChildren(context)); }